Commit 92f24fb4 authored by Josef Brandt's avatar Josef Brandt
Browse files

Particle Painting seems to be stable

parent 156ef9b8
Loading
Loading
Loading
Loading
+1 −3
Original line number Diff line number Diff line
@@ -178,8 +178,6 @@ 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])
        return coords
+53 −40
Original line number Diff line number Diff line
@@ -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):
    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,48 +98,64 @@ 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()))
        
        if self.painting:
            cv2.circle(self.img, center, self.radius, 255, -1)
        elif self.erasing:
            cv2.circle(self.img, center, self.radius, 0, -1)
            
#        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()
        self.setPixmap()
        self.update()

    def paint(self, painter, option, widget):   
        painter.setPen(QtCore.Qt.white)
+10 −6
Original line number Diff line number Diff line
@@ -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)
+5 −6
Original line number Diff line number Diff line
@@ -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()
+2 −9
Original line number Diff line number Diff line
@@ -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)
            
        tryDisconnectingSignal(self.particleEditor.particleAssignmentChanged)
        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:
Loading