Commit 33010f81 authored by Josef Brandt's avatar Josef Brandt
Browse files

Segmentation now reads seg-parameters from dataset.

Furthermore, seedpoints are drawn according their unique size. Also, SeedDeletePoints override and delete areas in sure_bg (that actually makes more sense and is useful, Julia said)
parent 96856846
Loading
Loading
Loading
Loading
+38 −29
Original line number Diff line number Diff line
@@ -41,22 +41,36 @@ class Parameter(object):
        self.show = show

class Segmentation(object):
    def __init__(self):
    def __init__(self, dataset=None):
        self.cancelcomputation = False
        if dataset is not None:
            self.detectParams = dataset.detectParams
        else:
            self.detectParams = {'points': np.array([[50,0],[100,200],[200,255]]),
                                 'contrastcurve': True,
                                'blurRadius': 9,
                                'threshold': 0.2,
                                'maxholebrightness': 0.5,
                                'erodeconvexdefects': 0,
                                'minparticlearea': 20,
                                'minparticledistance': 20,
                                'measurefrac': 1,
                                'compactness': 0.1,
                                'seedRad': 3}
        self.initialParameters()
        
    def initialParameters(self):
        parlist = [Parameter("points", np.ndarray, np.array([[20,0],[50,100],[200,255]]), helptext="Curve contrast"),
                   Parameter("contrastcurve", np.bool, True, helptext="Contrast curve", show=True),
                   Parameter("blurRadius", int, 9, 3, 99, 1, 2, helptext="Blur radius", show=True),
                   Parameter("threshold", float, .2, .01, .9, 2, .02, helptext="Basic threshold", show=True),
                   Parameter("maxholebrightness", float, 0.5, 0, 1, 2, 0.02, helptext="Close holes brighter than..", show = True),
                   Parameter("erodeconvexdefects", int, 0, 0, 20, helptext="Erode convex defects", show=True),
                   Parameter("minparticlearea", int, 20, 10, 1000, 0, 50, helptext="Min. particle pixel area", show=False),
                   Parameter("minparticledistance", int, 20, 10, 1000, 0, 5, helptext="Min. distance between particles", show=False),
                   Parameter("measurefrac", float, 1, 0, 1, 2, stepsize = 0.05, helptext="measure fraction of particles", show=False),
        parlist = [Parameter("points", np.ndarray, self.detectParams['points'], helptext="Curve contrast"),
                   Parameter("contrastcurve", np.bool, self.detectParams['contrastcurve'], helptext="Contrast curve", show=True),
                   Parameter("blurRadius", int, self.detectParams['blurRadius'], 3, 99, 1, 2, helptext="Blur radius", show=True),
                   Parameter("threshold", float, self.detectParams['threshold'], .01, .9, 2, .02, helptext="Basic threshold", show=True),
                   Parameter("maxholebrightness", float, self.detectParams['maxholebrightness'], 0, 1, 2, 0.02, helptext="Close holes brighter than..", show = True),
                   Parameter("erodeconvexdefects", int, self.detectParams['erodeconvexdefects'], 0, 20, helptext="Erode convex defects", show=True),
                   Parameter("minparticlearea", int, self.detectParams['minparticlearea'], 10, 1000, 0, 50, helptext="Min. particle pixel area", show=False),
                   Parameter("minparticledistance", int, self.detectParams['minparticledistance'], 10, 1000, 0, 5, helptext="Min. distance between particles", show=False),
                   Parameter("measurefrac", float, self.detectParams['measurefrac'], 0, 1, 2, stepsize = 0.05, helptext="measure fraction of particles", show=False),
                   Parameter("sure_fg", None, helptext="Show sure foreground", show=True),
                   Parameter("compactness", float, 0.1, 0, 1, 2, 0.05, helptext="watershed compactness", show=False),
                   Parameter("compactness", float, self.detectParams['compactness'], 0, 1, 2, 0.05, helptext="watershed compactness", show=False),
                   Parameter("watershed", None, helptext="Show watershed markers", show=True),
                   ]
        # make each parameter accessible via self.name
@@ -279,10 +293,6 @@ class Segmentation(object):
        if return_step=="contrastcurve": return gray, 0
        
        # image blur for noise-reduction
        if self.blurRadius%2 != 1:
            self.blurRadius += 1
            print('blur Radius was an even number, incremented blur Radius by 1')
            
        blur = cv2.medianBlur(gray, self.blurRadius)
        blur = np.uint8(blur*(255/blur.max()))
        if return_step=="blurRadius": return blur, 0
@@ -330,27 +340,26 @@ class Segmentation(object):
        
        sure_fg = self.getSureForeground(erthresh, self.minparticledistance, self.minparticlearea)
        
        # modify sure_fg with seedpoints and deletepoints
        sure_bg = cv2.dilate(erthresh, np.ones((5, 5)), iterations = 1)
        sure_bg = self.closeHoles(sure_bg)
        
        sure_bg = cv2.dilate(thresh, np.ones((5, 5)), iterations = 1)
        sure_bg = self.closeHoles(sure_bg)
        
        # modify sure_fg and sure_bg with seedpoints and deletepoints
        if len(deletepoints)>0:
            h, w = sure_fg.shape[:2]
            mask = np.zeros((h+2, w+2), np.uint8)
        for p in np.int32(deletepoints):
            if p[0] >= 0 and p[1] >= 0:
                cv2.floodFill(sure_fg, mask, tuple(p), 0)
            else:
                print('skipped del point at {}'.format(p))
        for p in np.int32(deletepoints):
            cv2.circle(sure_fg, tuple(p), int(seedradius), 0, -1)
            if p[0] > 0 and p[1] > 0:
                cv2.floodFill(sure_fg, mask, tuple([p[0], p[1]]), 0)
            
        for p in np.int32(seedpoints):
            cv2.circle(sure_fg, tuple(p), int(seedradius), 1, -1)
        
        sure_bg = cv2.dilate(erthresh, np.ones((5, 5)), iterations = 1)
        sure_bg = self.closeHoles(sure_bg)
        print("sure_fg, sure_bg")
            cv2.circle(sure_fg, tuple([p[0], p[1]]), int(p[2]), 1, -1)
        for p in np.int32(deletepoints):
            cv2.circle(sure_fg, tuple([p[0], p[1]]), int(p[2]), 0, -1)
            cv2.circle(sure_bg, tuple([p[0], p[1]]), int(p[2]), 0, -1)
        
        sure_bg = cv2.dilate(thresh, np.ones((5, 5)), iterations = 1)
        sure_bg = self.closeHoles(sure_bg)
        print("sure_fg, sure_bg")
        if self.cancelcomputation:
            return None, None, None