def label_objects(I, min_object_size, threshold=220, connectivity=8, kernel=8, apply_watershed=False): # try: BW = (cv2.cvtColor(I, cv2.COLOR_BGR2GRAY) < threshold).astype(bool) # if apply_watershed: # BW = morph.binary_opening(BW, np.ones((connectivity,connectivity)).astype(int)) labels = scilabel(BW)[0] BW = fill_holes( morph.remove_small_objects(labels, min_size=min_object_size, connectivity=connectivity, in_place=True)) if apply_watershed: distance = distance_transform_edt(BW) local_maxi = peak_local_max(distance, indices=False, footprint=np.ones((kernel, kernel)), labels=BW) markers = scilabel(local_maxi)[0] labels = watershed(-distance, markers, mask=BW) else: labels = scilabel(BW)[0] return (BW != 0), labels
def label_objects(img, otsu=True, min_object_size=100000, threshold=240, connectivity=8, kernel=61, keep_holes=False, max_hole_size=0, gray_before_close=False, blur_size=0): I=cv2.cvtColor(img,cv2.COLOR_RGB2GRAY) gray_mask=filter_grays(img, output_type="bool") if otsu: threshold = threshold_otsu(I) BW = (I<threshold).astype(bool) if gray_before_close: BW=BW&gray_mask if kernel>0: BW = morph.binary_closing(BW, morph.disk(kernel))#square if not gray_before_close: BW=BW&gray_mask if blur_size: BW=(cv2.blur(BW.astype(np.uint8), (blur_size,blur_size))==1) labels = scilabel(BW)[0] labels=morph.remove_small_objects(labels, min_size=min_object_size, connectivity = connectivity, in_place=True) if not keep_holes and max_hole_size: BW=morph.remove_small_objects(labels==0, min_size=max_hole_size, connectivity = connectivity, in_place=True)==False#remove_small_holes(labels,area_threshold=max_hole_size, connectivity = connectivity, in_place=True)>0 elif keep_holes: BW=labels>0 else: BW=fill_holes(labels) labels = scilabel(BW)[0] return(BW!=0),labels
def label_objects(I, min_object_size, threshold=220, connected_components=False, connectivity=8, kernel=8, apply_watershed=False): #try: BW = (cv2.cvtColor(I, cv2.COLOR_RGB2GRAY) < threshold).astype(bool) # if apply_watershed: # BW = morph.binary_opening(BW, np.ones((connectivity,connectivity)).astype(int)) labels = scilabel(BW)[0] if connected_components: BW = fill_holes( morph.remove_small_objects(labels, min_size=min_object_size, connectivity=connectivity, in_place=True)) labels = scilabel(BW)[0] return (BW != 0), labels