Commit 7f308b9b authored by Josef Brandt's avatar Josef Brandt
Browse files

Seedpoints are now stored with size as third parameter (besides x and y)

parent 0e2b4d8a
Loading
Loading
Loading
Loading
+96 −34
Original line number Diff line number Diff line
@@ -32,7 +32,10 @@ class HistWidget(QtWidgets.QWidget):
        super().__init__(parent)
        
        self.updateCallbacks(histcallback, curvecallback)
        self.points = np.array([[50,0],[100,150],[200,255]])
        if parent.dataset is None:
            self.points = np.array([[50,0],[100,220],[200,255]])
        else:
            self.points = parent.dataset.detectParams['points']
        
        self.fig = plt.Figure()
        self.canvas = FigureCanvasQTAgg(self.fig)
@@ -151,12 +154,12 @@ class ImageView(QtWidgets.QLabel):
        p0 = np.array([p0])
        if len(self.seedpoints)>0:
            arr = np.array(self.seedpoints)
            d = np.linalg.norm(arr-p0, axis=1)
            d = np.linalg.norm(arr[:, :2]-p0, axis=1)
            ind = d>self.seedradius
            self.seedpoints = arr[ind,:].tolist()
        if len(self.seeddeletepoints)>0:
            arr = np.array(self.seeddeletepoints)
            d = np.linalg.norm(arr-p0, axis=1)
            d = np.linalg.norm(arr[:, :2]-p0, axis=1)
            ind = d>self.seedradius
            self.seeddeletepoints = arr[ind,:].tolist()
        
@@ -170,9 +173,9 @@ class ImageView(QtWidgets.QLabel):
                self.drag = "add"
            p0 = event.pos()
            if self.drag=="add":
                self.seedpoints.append([p0.x(),p0.y()])
                self.seedpoints.append([p0.x(),p0.y(),self.seedradius])
            elif self.drag=="delete":
                self.seeddeletepoints.append([p0.x(),p0.y()])
                self.seeddeletepoints.append([p0.x(),p0.y(),self.seedradius])
            else:
                self.removeSeeds([p0.x(),p0.y()])
                
@@ -183,9 +186,9 @@ class ImageView(QtWidgets.QLabel):
        if self.drag:
            p0 = event.pos()
            if self.drag=="add":
                self.seedpoints.append([p0.x(),p0.y()])
                self.seedpoints.append([p0.x(),p0.y(),self.seedradius])
            elif self.drag=="delete":
                self.seeddeletepoints.append([p0.x(),p0.y()])
                self.seeddeletepoints.append([p0.x(),p0.y(),self.seedradius])
            else:
                self.removeSeeds([p0.x(),p0.y()])
            self.update()
@@ -205,6 +208,10 @@ class ImageView(QtWidgets.QLabel):
        self.overlay = None
        
    def updateSeedPoints(self, seedpoints=[], seeddeletepoints=[]):
#        if len(seedpoints) > 0 and len(self.seedpoints) > 0:
#            print(seedpoints[0, :], self.seedpoints[0, :])
#        else:
#            print('else...', len(seedpoints), len(self.seedpoints))
        self.seedpoints = seedpoints
        self.seeddeletepoints = seeddeletepoints
        
@@ -231,6 +238,7 @@ class ImageView(QtWidgets.QLabel):
        self.overlay = pix
        
    def paintEvent(self, event):
        
        painter = QtGui.QPainter(self)
        painter.drawPixmap(0, 0, self.pixmap())
        painter.setOpacity(self.alpha)
@@ -247,16 +255,16 @@ class ImageView(QtWidgets.QLabel):
                painter.drawEllipse(p[0]-2, p[1]-2, 5, 5)

        if self.showseedpoints:
            r = self.seedradius
            painter.setPen(QtCore.Qt.magenta)
            painter.setBrush(QtCore.Qt.magenta)
            for p in self.seeddeletepoints:
                painter.drawEllipse(p[0]-r, p[1]-r, 2*r, 2*r)
                
            painter.setPen(QtCore.Qt.white)
            painter.setBrush(QtCore.Qt.white)
            for p in self.seedpoints:
                painter.drawEllipse(p[0]-r, p[1]-r, 2*r, 2*r)
                painter.drawEllipse(p[0]-p[2], p[1]-p[2], 2*p[2], 2*p[2])
            
            painter.setPen(QtCore.Qt.magenta)           #I think it is more useful when the deletpoints override the seedpoints
            painter.setBrush(QtCore.Qt.magenta)
            
            for p in self.seeddeletepoints:
                painter.drawEllipse(p[0]-p[2], p[1]-p[2], 2*p[2], 2*p[2])
            
class ParticleDetectionView(QtWidgets.QWidget):
    imageUpdate = QtCore.pyqtSignal(name='imageUpdate')
@@ -264,10 +272,10 @@ class ParticleDetectionView(QtWidgets.QWidget):
    
    def __init__(self, img, dataset, parent=None):
        super().__init__(parent, QtCore.Qt.Window)
        self.dataset = dataset
        self.dataset = self.verifySeedpoints(dataset)
        self.img = img
        self.imgclip = 0,0,0,0
        self.seg = Segmentation()
        self.seg = Segmentation(self.dataset)
        self.thread = None
        
        vbox = QtWidgets.QVBoxLayout()
@@ -294,6 +302,7 @@ class ParticleDetectionView(QtWidgets.QWidget):
        group = QtWidgets.QGroupBox("Detection settings", self)
        grid = QtWidgets.QGridLayout()
        self.parameters = []
        #create editable parameters:
        for i, p in enumerate(self.seg.parlist):
            label, colstretch = None, 1
            if p.name == "points":
@@ -371,6 +380,37 @@ class ParticleDetectionView(QtWidgets.QWidget):
        self.setLayout(hbox)
        self.setWindowTitle("Particle Detection")
    
    def saveDetectParams(self, ds=None):
        if ds is not None:
            for param in self.parameters:
                if param[1] == 'points':
                    print(param[0].value())
                try:                #is it a spinbox or the histWidget? Read out the value
                    ds.detectParams[param[1]] = param[0].value()
                except:             #otherwise checkbox -> take its state
                    ds.detectParams[param[1]] = param[0].isChecked()
            ds.detectParams['seedRad'] = self.seedradiusedit.value()
            ds.save()

    def verifySeedpoints(self, dataset):
        seedpoints = dataset.seedpoints
        seeddeletepoints = dataset.seeddeletepoints
        
        if len(seedpoints) > 0:     #points are present
            if seedpoints.shape[1] == 2:    #old entries with only x,y coordinates
                radii = np.ones((seedpoints.shape[0], 1))*3
                dataset.seedpoints = np.hstack((seedpoints, radii))
        else:
            dataset.seedpoints = np.array([])
        
        if len(seeddeletepoints) > 0:     #points are present
            if seeddeletepoints.shape[1] == 2:    #old entries with only x,y coordinates
                radii = np.ones((seeddeletepoints.shape[0], 1))*3
                dataset.seeddeletepoints = np.hstack((seeddeletepoints, radii))
        else:
            dataset.seeddeletepoints = np.array([])
        return dataset
    
    @QtCore.pyqtSlot()
    def seedChanged(self):
        seedradius = self.seedradiusedit.value()
@@ -380,20 +420,21 @@ class ParticleDetectionView(QtWidgets.QWidget):
                ind &= (arr0[:,1]>n1-seedradius)&(arr0[:,1]<=n2+seedradius)
                arr0 = arr0[~ind,:]
            else:
                arr0 = arr0.reshape(0,2)
                arr0 = arr0.reshape(0,3)

            if arr1.shape[0]>0:
                arr1 += p0
                arr1[:,:2] += p0
            else:
                arr1 = arr1.reshape(0,2)
                arr1 = arr1.reshape(0,3)
            return np.concatenate((arr0, arr1), axis=0)
        
        if self.dataset is not None:
            n1,n2,m1,m2 = self.imgclip
            p0 = np.array([[m1,n1]], dtype=np.int32)
            
            self.dataset.seedpoints = clipdata(self.dataset.seedpoints,
                                               np.array(self.imglabel.seedpoints, dtype=np.int32),
                                               p0, n1, n2, m1, m2)
            
            self.dataset.seeddeletepoints = clipdata(self.dataset.seeddeletepoints,
                                                     np.array(self.imglabel.seeddeletepoints, dtype=np.int32),
                                                     p0, n1, n2, m1, m2)
@@ -402,23 +443,35 @@ class ParticleDetectionView(QtWidgets.QWidget):
    @QtCore.pyqtSlot()
    def updateImageSeeds(self):
        if self.dataset is not None:
            seedradius = self.seedradiusedit.value()
#            seedradius = self.seedradiusedit.value()
            n1,n2,m1,m2 = self.imgclip
            p0 = np.array([[m1,n1]], dtype=np.int32)
            seedpoints = []
            seeddeletepoints = []

            arr1 = self.dataset.seedpoints
            if arr1.shape[0]>0:
                ind = (arr1[:,0]>m1-seedradius)&(arr1[:,0]<=m2+seedradius) 
                ind &= (arr1[:,1]>n1-seedradius)&(arr1[:,1]<=n2+seedradius)
                if np.any(ind):
                    seedpoints = (arr1[ind,:]-p0).tolist()
            #what seeds are actually in image view?
            for point in arr1:                  #Josef says: I replaced the commented logic with the one right here below, as the old one somehow did not work.... The for-loop might become slow at some point??
                if point[0] > (m1-point[2]) and point[0] <= (m2+point[2]) and point[1] > (n1-point[2]) and point[1] <= (n2+point[2]):
                    seedpoints.append([point[0] - p0[0][0], point[1] - p0[0][1], point[2]])
#            if arr1.shape[0]>0:
#                ind = (arr1[:,0]>m1-seedradius)&(arr1[:,0]<=m2+seedradius) 
#                ind &= (arr1[:,1]>n1-seedradius)&(arr1[:,1]<=n2+seedradius)
#                if np.any(ind):
#                    arr1[ind, :2] -= p0
#                    seedpoints = arr1.tolist()
#             
            arr2 = self.dataset.seeddeletepoints
            if arr2.shape[0]>0:
                ind = (arr2[:,0]>m1-seedradius)&(arr2[:,0]<=m2+seedradius) 
                ind &= (arr2[:,1]>n1-seedradius)&(arr2[:,1]<=n2+seedradius)
                if np.any(ind):
                    seeddeletepoints = (arr2[ind,:]-p0).tolist()
            for point in arr2:                  #Josef says: I replaced the commented logic with the one right here below, as the old one somehow did not work.... The for-loop might become slow at some point??
                if point[0] > (m1-point[2]) and point[0] <= (m2+point[2]) and point[1] > (n1-point[2]) and point[1] <= (n2+point[2]):
                    seeddeletepoints.append([point[0] - p0[0][0], point[1] - p0[0][1], point[2]])
#            if arr2.shape[0]>0:
#                ind = (arr2[:,0]>m1-seedradius)&(arr2[:,0]<=m2+seedradius) 
#                ind &= (arr2[:,1]>n1-seedradius)&(arr2[:,1]<=n2+seedradius)
#                if np.any(ind):
#                    arr2[ind, :2] -= p0
#                    seeddeletepoints = arr2.tolist()

            self.imglabel.updateSeedPoints(seedpoints, seeddeletepoints)
        
    def closeEvent(self, event):
@@ -436,6 +489,12 @@ class ParticleDetectionView(QtWidgets.QWidget):
            self.lastmove = (self.lastcenter[0]+dp.x(),self.lastcenter[1]+dp.y())
            self.setImageCenter(self.lastmove)
            
    def wheelEvent(self, event):
        if event.angleDelta().y() > 0:
            self.seedradiusedit.setValue(self.seedradiusedit.value()+1)
        else:
            self.seedradiusedit.setValue(self.seedradiusedit.value()-1)
        
    def mouseReleaseEvent(self, event):
        if self.drag:
            self.lastcenter = self.lastmove
@@ -486,6 +545,7 @@ class ParticleDetectionView(QtWidgets.QWidget):
        self.updateImageSeeds()
        
    def detectShow(self, showname):
        self.saveDetectParams(self.dataset)
        img = self.subimg.copy()
        kwargs = {}
        for ui, name, valuefunc in self.parameters:
@@ -534,6 +594,7 @@ class ParticleDetectionView(QtWidgets.QWidget):
    
    @QtCore.pyqtSlot()
    def detectParticles(self):
        self.saveDetectParams(self.dataset)
        if self.thread is not None and self.thread.is_alive():
            self.cancelThread()
            return
@@ -596,7 +657,8 @@ if __name__ == "__main__":
    from dataset import DataSet
    from time import time
    
    fname = r"D:\Projekte\Mikroplastik\Waferreinigung_Kerzenfilter\WaferMilliQH118\fullimage.tif"
#    fname = r"D:\Projekte\Mikroplastik\Waferreinigung_Kerzenfilter\WaferMilliQH118\fullimage.tif"
    fname = r"C:\Users\brandt\Desktop\20180723DemoTZW\fullimage.tif"
    app = QtWidgets.QApplication(sys.argv)
    
    t2 = time()
@@ -605,8 +667,8 @@ if __name__ == "__main__":
    print("OpenCV read:", t3-t2)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    
    view = ParticleDetectionView(img, None)
    ds = DataSet("dummy")    
    view = ParticleDetectionView(img, ds, None)    
    view.setDataSet(ds)
    view.show()
    app.exec_()
 No newline at end of file