From 92f24fb4c1230e3195154388f109544f186a3132 Mon Sep 17 00:00:00 2001 From: Josef Brandt Date: Fri, 19 Jul 2019 17:25:20 +0200 Subject: [PATCH] Particle Painting seems to be stable --- analysis/particleContainer.py | 4 +- analysis/particlePainter.py | 93 ++++++++++++++++++++--------------- analysis/particleeditor.py | 16 +++--- gepard.py | 11 ++--- sampleview.py | 11 +---- viewitems.py | 19 ------- 6 files changed, 71 insertions(+), 83 deletions(-) diff --git a/analysis/particleContainer.py b/analysis/particleContainer.py index 6bcf199..ffa6747 100644 --- a/analysis/particleContainer.py +++ b/analysis/particleContainer.py @@ -178,10 +178,8 @@ class ParticleContainer(object): def getMeasurementPixelCoords(self): coords = [] -# for particle in self.particles: -# for measurement in particle.getMeasurements(): for meas in self.measurements: - coords.append([meas.pixelcoord_x, meas.pixelcoord_y]) + coords.append([meas.pixelcoord_x, meas.pixelcoord_y]) return coords def getNumberOfParticlesOfAssignment(self, assignment): diff --git a/analysis/particlePainter.py b/analysis/particlePainter.py index d718397..8570d46 100644 --- a/analysis/particlePainter.py +++ b/analysis/particlePainter.py @@ -23,7 +23,6 @@ from PyQt5 import QtWidgets, QtCore, QtGui import numpy as np import cv2 - class ParticlePainter(QtWidgets.QGraphicsItem): def __init__(self, editorParent, img, topLeft): super(ParticlePainter, self).__init__() @@ -38,22 +37,20 @@ class ParticlePainter(QtWidgets.QGraphicsItem): self.painting = False self.erasing = False - self.minRadius = 10 + self.minRadius = 5 self.maxRadius = 500 self.radius = 30 self.brect = QtCore.QRectF(0,0,1,1) - self.imgToPixmap() + self.setPixmap() self.setBrect() - def imgToPixmap(self): - img = self.img.repeat(3).reshape(self.img.shape[0], self.img.shape[1],3) + def setPixmap(self): + img = self.img.repeat(3).reshape(self.img.shape[0], self.img.shape[1], 3) height, width, channel = img.shape - assert channel==3 bytesPerLine = 3 * width - pix = QtGui.QPixmap() - pix.convertFromImage(QtGui.QImage(img.data, width, height, bytesPerLine, QtGui.QImage.Format_RGB888)) - self.pixmap = pix + self.pixmap = QtGui.QPixmap() + self.pixmap.convertFromImage(QtGui.QImage(img.data, width, height, bytesPerLine, QtGui.QImage.Format_RGB888)) def setBrect(self): x0 = self.topLeft[1] @@ -101,49 +98,65 @@ class ParticlePainter(QtWidgets.QGraphicsItem): elif event.key() == QtCore.Qt.Key_Return: self.editorParent.acceptPaintedResult() - def drawParticle(self, pos): - pixelPos = round(pos.y()-self.topLeft[0]), round(pos.x()-self.topLeft[1]) - x_min, x_max = round(pixelPos[1]-self.radius/2), round(pixelPos[1]+self.radius/2) - y_min, y_max = round(pixelPos[0]-self.radius/2), round(pixelPos[0]+self.radius/2) + def drawParticle(self, pixelPos): + x_min, x_max = round(pixelPos.x()-self.radius), round(pixelPos.x()+self.radius) + y_min, y_max = round(pixelPos.y()-self.radius), round(pixelPos.y()+self.radius) - x_shift = y_shift = 0 + x_shift = y_shift = int(0) if x_min < 0: - x_shift = x_min - elif x_min >= self.img.shape[1]: - x_shift = x_min + 1 - self.img.shape[1] + x_shift = int(x_min) + self.topLeft[1] -= abs(x_min) + elif x_max >= self.img.shape[1]: + x_shift = int(x_max + 1 - self.img.shape[1]) if y_min < 0: - y_shift = y_min - elif y_min >= self.img.shape[0]: - y_shift = y_min + 1 - self.img.shape[0] + self.topLeft[0] -= abs(y_min) + y_shift = int(y_min) + elif y_max >= self.img.shape[0]: + y_shift = int(y_max + 1 - self.img.shape[0]) if x_shift != 0 or y_shift != 0: - x_range = self.img.shape[1] + abs(x_shift) - y_range = self.img.shape[0] + abs(y_shift) - + x_range = int(self.img.shape[1] + abs(x_shift)) + y_range = int(self.img.shape[0] + abs(y_shift)) newImg = np.zeros((y_range, x_range)) if x_shift < 0: - if y_shift == 0: - newImg[abs(x_shift):, :] = self.img - elif y_shift < 0: + if y_shift < 0: + newImg[abs(y_shift):, abs(x_shift):] = self.img + elif y_shift == 0: + newImg[:, abs(x_shift):] = self.img + elif y_shift > 0: + newImg[:self.img.shape[0], abs(x_shift):] = self.img + + elif x_shift == 0: + if y_shift < 0: + newImg[abs(y_shift):, :] = self.img + elif y_shift == 0: + newImg[:, :] = self.img + elif y_shift > 0: + newImg[:self.img.shape[0], :] = self.img + elif x_shift > 0: + if y_shift < 0: + newImg[abs(y_shift):, :self.img.shape[1]] = self.img + elif y_shift == 0: + newImg[:, :self.img.shape[1]] = self.img + elif y_shift > 0: + newImg[:y_shift, :self.img.shape[1]] = self.img + + self.img = np.uint8(newImg) + self.setBrect() + center = (int(pixelPos.x()), int(pixelPos.y())) - - -# img, xmin, ymin, padding = self.contoursToImg(self.contours) -# center = (int(pos.x()+self.radius), int(pos.y()+self.radius)) -# if self.painting: -# cv2.circle(img, center, self.radius, 255, -1) -# elif self.erasing: -# cv2.circle(img, center, self.radius, 0, -1) -# -# img = np.uint8(img) -# self.contours = self.imgToCnt(img, xmin, ymin, padding) -# self.getBrectAndPolygon() -# self.update() - + if self.painting: + cv2.circle(self.img, center, self.radius, 255, -1) + elif self.erasing: + cv2.circle(self.img, center, self.radius, 0, -1) + + self.setPixmap() + self.update() + def paint(self, painter, option, widget): painter.setPen(QtCore.Qt.white) painter.drawRect(self.brect) diff --git a/analysis/particleeditor.py b/analysis/particleeditor.py index 18e82e4..cffc389 100644 --- a/analysis/particleeditor.py +++ b/analysis/particleeditor.py @@ -189,9 +189,10 @@ class ParticleEditor(QtCore.QObject): self.storedAssignmend = newAssignment contours = self.particleContainer.getParticleContoursByIndex(contourIndices) - topLeft = self.getTopLeft(contours) - img, self.xmin, self.ymin, self.padding = self.contoursToImg(contours) - +# topLeft = self.getTopLeft(contours) +# img, self.xmin, self.ymin, self.padding = self.contoursToImg(contours, padding=0) + img, xmin, ymin, self.padding = self.contoursToImg(contours, padding=0) + topLeft = [ymin, xmin] self.particlePainter = ParticlePainter(self, img, topLeft) self.viewparent.normalSize() @@ -201,7 +202,10 @@ class ParticleEditor(QtCore.QObject): def acceptPaintedResult(self): try: - newContour = self.mergeContours(self.particlePainter.contours) + img = self.particlePainter.img + xmin = self.particlePainter.topLeft[1] + ymin = self.particlePainter.topLeft[0] + newContour = self.imgToCnt(img, xmin, ymin, 0) except NotConnectedContoursError: self.storedIndices = [] self.storedAssignmend = None @@ -258,7 +262,7 @@ class ParticleEditor(QtCore.QObject): if len(contours)>1: QtWidgets.QMessageBox.critical(self.viewparent, 'ERROR!', - 'Particle contours are not connected and cannot be combined!') + 'Particle contours are not connected or have holes.\nThat is currently not supported!') raise NotConnectedContoursError newContour = contours[0] @@ -273,7 +277,7 @@ class ParticleEditor(QtCore.QObject): #draw contours xmin = cnt[:,0,:][:, 0].min() ymin= cnt[:,0,:][:, 1].min() - return ymin, xmin + return [ymin, xmin] def mergeParticlesInParticleContainerAndSampleView(self, indices, newContour, stats, assignment): self.viewparent.addParticleContourToIndex(newContour, len(self.viewparent.contourItems)-1) diff --git a/gepard.py b/gepard.py index 68df137..5e09e86 100644 --- a/gepard.py +++ b/gepard.py @@ -326,12 +326,11 @@ if __name__ == '__main__': if not os.path.exists(logpath): os.mkdir(logpath) logname = os.path.join(logpath, 'logfile.txt') -# -# fp = open(logname, "a") -# sys.stderr = fp -# sys.stdout = fp - print("starting GEPARD at: " + strftime("%d %b %Y %H:%M:%S", localtime()), - flush=True) + + fp = open(logname, "a") + sys.stderr = fp + sys.stdout = fp + print("starting GEPARD at: " + strftime("%d %b %Y %H:%M:%S", localtime()), flush=True) gepard = GEPARDMainWindow(logpath) gepard.showMaximized() diff --git a/sampleview.py b/sampleview.py index 2807751..d5eb765 100644 --- a/sampleview.py +++ b/sampleview.py @@ -268,14 +268,9 @@ class SampleView(QtWidgets.QGraphicsView): if self.particleEditor is None: self.particleEditor = ParticleEditor(self, self.dataset.particleContainer) - #try disconnecting the signals. If they are connected multiple times, the functions will run accordingly... -# tryDisconnectingSignal(self.particleEditor.particleContoursChanged) + tryDisconnectingSignal(self.particleEditor.particleAssignmentChanged) - -# self.particleEditor.particleContoursChanged.connect(self.resetParticleContours) - if self.analysiswidget is not None: -# self.particleEditor.particleContoursChanged.connect(self.analysiswidget.updateHistogramsAndContours) self.particleEditor.particleAssignmentChanged.connect(self.analysiswidget.updateHistogramsAndContours) def setMicroscopeMode(self): @@ -381,9 +376,7 @@ class SampleView(QtWidgets.QGraphicsView): self.dataset.readin = False else: return -# x, y, z = self.dataset.mapToLengthRaman([p0.x(), p0.y()], -# microscopeMode=self.microscopeMode, -# noz=(False if self.mode=="RamanScan" else True)) + noz = (self.mode in ['OpticalScan', 'RamanScan']) x, y, z = self.dataset.mapToLengthRaman([pos.x(), pos.y()], microscopeMode=self.microscopeMode, noz=noz) if z is not None: diff --git a/viewitems.py b/viewitems.py index 2b7766d..7b19712 100644 --- a/viewitems.py +++ b/viewitems.py @@ -29,7 +29,6 @@ class SegmentationContour(QtWidgets.QGraphicsItem): self.setZValue(1) self.setPos(pos[0], pos[1]) self.setFlag(QtWidgets.QGraphicsItem.ItemIsSelectable) -# self.setAcceptedMouseButtons(QtCore.Qt.AllButtons) self.brect = QtCore.QRectF(0,0,1,1) self.contourData = contourData @@ -55,10 +54,7 @@ class SegmentationContour(QtWidgets.QGraphicsItem): def boundingRect(self): return self.brect - -# def createRamanScanIndices(self): - def setIndex(self, index): self.particleIndex = index @@ -83,13 +79,6 @@ class SegmentationContour(QtWidgets.QGraphicsItem): painter.drawPolygon(self.polygon) -# def mousePressEvent(self, event): -# print('press in contour') -# -# def mouseMoveEvent(self, event): -# print('move in contour') -# - def contextMenuEvent(self, event): if self.isSelected: self.contextMenu = ParticleContextMenu(self.viewparent) @@ -169,14 +158,6 @@ class RamanScanIndicator(QtWidgets.QGraphicsItem): font.setPointSize(10) painter.setFont(font) painter.drawText(rect, QtCore.Qt.AlignCenter, str(self.number)) - -# def mousePressEvent(self, event): -# p = event.pos() -# x, y = p.x(), p.y() -# r = np.sqrt(x**2+y**2) -# if r