diff --git a/segmentation.py b/segmentation.py index 26f70ec452036f359fc2a5ea73ae0bebb2e53062..8d1ca44277d8b48ddababffd5090dc4d3544c207 100644 --- a/segmentation.py +++ b/segmentation.py @@ -52,6 +52,7 @@ class Segmentation(object): 'lowThresh': 0.2, 'activateUpThresh': False, 'upThresh': 0.5, + 'invertThresh': False, 'maxholebrightness': 0.5, # 'erodeconvexdefects': 0, 'minparticlearea': 20, @@ -73,9 +74,10 @@ class Segmentation(object): parlist = [Parameter("contrastCurve", np.ndarray, self.detectParams['contrastCurve'], helptext="Curve contrast"), Parameter("activateContrastCurve", np.bool, self.detectParams['activateContrastCurve'], helptext="activate Contrast curve", show=True, linkedParameter='contrastCurve'), Parameter("blurRadius", int, self.detectParams['blurRadius'], 3, 99, 1, 2, helptext="Blur radius", show=True), - Parameter("activateLowThresh", np.bool, self.detectParams['activateThresh2'], helptext="activate lower threshold", show=False, linkedParameter='lowThresh'), + Parameter("invertThresh", np.bool, self.detectParams['invertThresh'], helptext="Invert the current threshold", show=False), + Parameter("activateLowThresh", np.bool, self.detectParams['activateLowThresh'], helptext="activate lower threshold", show=False, linkedParameter='lowThresh'), Parameter("lowThresh", float, self.detectParams['lowThresh'], .01, .9, 2, .02, helptext="Lower threshold", show=True), - Parameter("activateUpThresh", np.bool, self.detectParams['activateThresh2'], helptext="activate upper threshold", show=False, linkedParameter='upThresh'), + Parameter("activateUpThresh", np.bool, self.detectParams['activateUpThresh'], helptext="activate upper threshold", show=False, linkedParameter='upThresh'), Parameter("upThresh", float, self.detectParams['upThresh'], .01, 1.0, 2, .02, helptext="Upper 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), #TODO: Consider removing it entirely. It is usually not used... @@ -320,6 +322,8 @@ class Segmentation(object): # thresholding if self.activateLowThresh and not self.activateUpThresh: thresh = cv2.threshold(blur, int(255*self.lowThresh), 255, cv2.THRESH_BINARY)[1] + if self.invertThresh: + thresh = 255-thresh if return_step=="lowThresh": return thresh, 0 print("lower threshold") if self.cancelcomputation: @@ -329,7 +333,8 @@ class Segmentation(object): lowerLimit, upperLimit = np.round(self.lowThresh*255), np.round(self.upThresh*255) thresh = np.zeros_like(blur) thresh[np.where(np.logical_and(blur >= lowerLimit, blur <= upperLimit))] = 255 - + if self.invertThresh: + thresh = 255-thresh if return_step=="lowThresh" or return_step=="upThresh": return thresh, 0 print("between threshold") if self.cancelcomputation: @@ -338,6 +343,8 @@ class Segmentation(object): elif not self.activateLowThresh and self.activateUpThresh: thresh = np.zeros_like(blur) thresh[np.where(blur <= np.round(self.upThresh*255))] = 255 + if self.invertThresh: + thresh = 255-thresh if return_step=="upThresh": return thresh, 0 print("upper threshold") if self.cancelcomputation: @@ -346,8 +353,7 @@ class Segmentation(object): if self.parent is not None: self.parent.raiseWarning('No thresholding method selected!\nAborted detection..') print('NO THRESHOLDING SELECTED!') - return None, None, None - + return blur, 0 #close holes darkter than self.max_brightness self.closeBrightHoles(thresh, blur, self.maxholebrightness)