def addPolygonArea(self, polygon: QPolygonF): # convert QImage into numpy array imgArr = ImageViewerWithLabel.__qimage2narray(self.Image) # convert image into gray mode if it's rgb mode. gray = color.rgb2gray(imgArr) # check if the polygon is inside the cropPolygon polygon = polygon.intersected(self.cropPolygon) # polygon to binary mask mask = ImageViewerWithLabel.__QPolygon2Mask(gray.shape, polygon) # __otsuWithMask2bw bw = ImageViewerWithLabel.__otsuWithMask2bw(gray, mask) # remove small objects # morphology.remove_small_objects(bw, min_size=self.remove_small_objects, connectivity=2, in_place=True) # morphology.remove_small_holes(bw, area_threshold=self.remove_small_holes, connectivity=2, in_place=True) # binary image to label image label = morphology.label(bw, connectivity=2) self.addLabelMask(label)