Python cv2.watershed() Examples
The following are 16
code examples of cv2.watershed().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
cv2
, or try the search function
.
Example #1
Source File: watershed.py From OpenCV-Python-Tutorial with MIT License | 6 votes |
def run(self): while cv2.getWindowProperty('img', 0) != -1 or cv2.getWindowProperty('watershed', 0) != -1: ch = cv2.waitKey(50) if ch == 27: break if ch >= ord('1') and ch <= ord('7'): self.cur_marker = ch - ord('0') print('marker: ', self.cur_marker) if ch == ord(' ') or (self.sketch.dirty and self.auto_update): self.watershed() self.sketch.dirty = False if ch in [ord('a'), ord('A')]: self.auto_update = not self.auto_update print('auto_update if', ['off', 'on'][self.auto_update]) if ch in [ord('r'), ord('R')]: self.markers[:] = 0 self.markers_vis[:] = self.img self.sketch.show() cv2.destroyAllWindows()
Example #2
Source File: Tshirt.py From virtual-dressing-room with Apache License 2.0 | 6 votes |
def detect_shirt(self): #self.dst=cv2.inRange(self.norm_rgb,np.array([self.lb,self.lg,self.lr],np.uint8),np.array([self.b,self.g,self.r],np.uint8)) self.dst=cv2.inRange(self.norm_rgb,np.array([20,20,20],np.uint8),np.array([255,110,80],np.uint8)) cv2.threshold(self.dst,0,255,cv2.THRESH_OTSU+cv2.THRESH_BINARY) fg=cv2.erode(self.dst,None,iterations=2) #cv2.imshow("fore",fg) bg=cv2.dilate(self.dst,None,iterations=3) _,bg=cv2.threshold(bg, 1,128,1) #cv2.imshow("back",bg) mark=cv2.add(fg,bg) mark32=np.int32(mark) cv2.watershed(self.norm_rgb,mark32) self.m=cv2.convertScaleAbs(mark32) _,self.m=cv2.threshold(self.m,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) #cv2.imshow("final_tshirt",self.m) cntr,h=cv2.findContours(self.m,cv2.cv.CV_RETR_EXTERNAL,cv2.cv.CV_CHAIN_APPROX_SIMPLE) return self.m,cntr
Example #3
Source File: mask_morphology.py From NucleiDetectron with Apache License 2.0 | 6 votes |
def skimage_watershed_segmentation(mask, kernel=k_3x3, k=1): # mask = cv.dilate(mask, kernel, iterations=k) distance = ndimage.distance_transform_edt(mask) local_maxi = peak_local_max(distance, indices=False, footprint=kernel, labels=mask) markers = measure.label(local_maxi) labels_ws = watershed(-distance, markers, mask=mask) if labels_ws.max() < 2: return [mask], labels_ws res_masks = [] for idx in range(1, labels_ws.max() + 1): m = labels_ws == idx if m.sum() > 20: res_masks.append(m.astype(np.uint8)) return res_masks, labels_ws
Example #4
Source File: watershed.py From PyCV-time with MIT License | 6 votes |
def run(self): while True: ch = 0xFF & cv2.waitKey(50) if ch == 27: break if ch >= ord('1') and ch <= ord('7'): self.cur_marker = ch - ord('0') print 'marker: ', self.cur_marker if ch == ord(' ') or (self.sketch.dirty and self.auto_update): self.watershed() self.sketch.dirty = False if ch in [ord('a'), ord('A')]: self.auto_update = not self.auto_update print 'auto_update if', ['off', 'on'][self.auto_update] if ch in [ord('r'), ord('R')]: self.markers[:] = 0 self.markers_vis[:] = self.img self.sketch.show() cv2.destroyAllWindows()
Example #5
Source File: watershed.py From PyCV-time with MIT License | 6 votes |
def run(self): while True: ch = 0xFF & cv2.waitKey(50) if ch == 27: break if ch >= ord('1') and ch <= ord('7'): self.cur_marker = ch - ord('0') print 'marker: ', self.cur_marker if ch == ord(' ') or (self.sketch.dirty and self.auto_update): self.watershed() self.sketch.dirty = False if ch in [ord('a'), ord('A')]: self.auto_update = not self.auto_update print 'auto_update if', ['off', 'on'][self.auto_update] if ch in [ord('r'), ord('R')]: self.markers[:] = 0 self.markers_vis[:] = self.img self.sketch.show() cv2.destroyAllWindows()
Example #6
Source File: watershed.py From OpenCV-Python-Tutorial with MIT License | 5 votes |
def watershed(self): m = self.markers.copy() cv2.watershed(self.img, m) overlay = self.colors[np.maximum(m, 0)] vis = cv2.addWeighted(self.img, 0.5, overlay, 0.5, 0.0, dtype=cv2.CV_8UC3) cv2.imshow('watershed', vis)
Example #7
Source File: Tshirt.py From virtual-dressing-room with Apache License 2.0 | 5 votes |
def detect_shirt2(self): self.hsv=cv2.cvtColor(self.norm_rgb,cv.CV_BGR2HSV) self.hue,s,_=cv2.split(self.hsv) _,self.dst=cv2.threshold(self.hue,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) self.fg=cv2.erode(self.dst,None,iterations=3) self.bg=cv2.dilate(self.dst,None,iterations=1) _,self.bg=cv2.threshold(self.bg,1,128,1) mark=cv2.add(self.fg,self.bg) mark32=np.int32(mark) cv2.watershed(self.norm_rgb,mark32) m=cv2.convertScaleAbs(mark32) _,m=cv2.threshold(m,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) cntr,h=cv2.findContours(m,cv.CV_RETR_EXTERNAL,cv.CV_CHAIN_APPROX_SIMPLE) print len(cntr) #print cntr[0].shape #cntr[1].dtype=np.float32 #ret=cv2.contourArea(np.array(cntr[1])) #print ret #cntr[0].dtype=np.uint8 cv2.drawContours(m,cntr,-1,(255,255,255),3) cv2.imshow("mask_fg",self.fg) cv2.imshow("mask_bg",self.bg) cv2.imshow("mark",m)
Example #8
Source File: Back_sub.py From virtual-dressing-room with Apache License 2.0 | 5 votes |
def subtract_back(self,frm): #dst=self.__back__-self.__foreground__ temp=np.zeros((config.height,config.width),np.uint8) self.__foreground__=cv2.blur(self.__foreground__,(3,3)) dst=cv2.absdiff(self.__back__,self.__foreground__) #dst=cv2.adaptiveThreshold(dst,255,cv.CV_THRESH_BINARY,cv.CV_ADAPTIVE_THRESH_GAUSSIAN_C,5,10) val,dst=cv2.threshold(dst,0,255,cv.CV_THRESH_BINARY+cv.CV_THRESH_OTSU) fg=cv2.erode(dst,None,iterations=1) bg=cv2.dilate(dst,None,iterations=4) _,bg=cv2.threshold(bg,1,128,1) mark=cv2.add(fg,bg) mark32=np.int32(mark) #dst.copy(temp) #seq=cv.FindContours(cv.fromarray(dst),self.mem,cv.CV_RETR_EXTERNAL,cv.CV_CHAIN_APPROX_SIMPLE) #cntr,h=cv2.findContours(dst,cv.CV_RETR_EXTERNAL,cv.CV_CHAIN_APPROX_SIMPLE) #print cntr,h #cv.DrawContours(cv.fromarray(temp),seq,(255,255,255),(255,255,255),1,cv.CV_FILLED) cv2.watershed(frm, mark32) self.final_mask=cv2.convertScaleAbs(mark32) #print temp #--outputs--- #cv2.imshow("subtraction",fg) #cv2.imshow("thres",dst) #cv2.imshow("thres1",bg) #cv2.imshow("mark",mark) #cv2.imshow("final",self.final_mask)
Example #9
Source File: fake_util.py From CRAFT_keras with Apache License 2.0 | 5 votes |
def watershed(src): """ Performs a marker-based image segmentation using the watershed algorithm. :param src: 8-bit 1-channel image. :return: 32-bit single-channel image (map) of markers. """ # cv2.imwrite('{}.png'.format(np.random.randint(1000)), src) gray = src.copy() img = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR) # h, w = gray.shape[:2] # block_size = (min(h, w) // 4 + 1) * 2 + 1 # thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, block_size, 0) _ret, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) # noise removal kernel = np.ones((3, 3), np.uint8) opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=2) # sure background area sure_bg = cv2.dilate(opening, kernel, iterations=3) # Finding sure foreground area dist_transform = cv2.distanceTransform(opening, cv2.DIST_L2, 5) # dist_transform = opening & gray # cv2.imshow('dist_transform', dist_transform) # _ret, sure_bg = cv2.threshold(dist_transform, 0.2 * dist_transform.max(), 255, cv2.THRESH_BINARY_INV) _ret, sure_fg = cv2.threshold(dist_transform, 0.2 * dist_transform.max(), 255, cv2.THRESH_BINARY) # Finding unknown region # sure_bg = np.uint8(sure_bg) sure_fg = np.uint8(sure_fg) # cv2.imshow('sure_fg', sure_fg) unknown = cv2.subtract(sure_bg, sure_fg) # Marker label lingret, marker_map = cv2.connectedComponents(sure_fg) # Add one to all labels so that sure background is not 0, but 1 marker_map = marker_map + 1 # Now, mark the region of unknown with zero marker_map[unknown == 255] = 0 marker_map = cv2.watershed(img, marker_map) return marker_map
Example #10
Source File: mask_morphology.py From NucleiDetectron with Apache License 2.0 | 5 votes |
def opencv_segmentation(mask, kernel=k_3x3, k=3): # noise removal opening = cv.morphologyEx(mask, cv.MORPH_OPEN, kernel, iterations=k) # sure background area sure_bg = cv.dilate(opening, kernel, iterations=k) # Finding sure foreground area dist_transform = cv.distanceTransform(opening,cv.DIST_L2, 5) ret, sure_fg = cv.threshold(dist_transform, 0.7*dist_transform.max(), 255, 0) # Finding unknown region sure_fg = np.uint8(sure_fg) unknown = cv.subtract(sure_bg, sure_fg) # Marker labelling ret, markers = cv.connectedComponents(sure_fg) # Add one to all labels so that sure background is not 0, but 1 markers = markers + 1 # Now, mark the region of unknown with zero markers[unknown > 0] = 0 labels_ws = cv.watershed(cv.cvtColor(mask, cv.COLOR_GRAY2RGB), markers) if labels_ws.max() - 1 < 2: return [mask], labels_ws res_masks = [] for idx in range(2, labels_ws.max() + 1): m = labels_ws == idx if m.sum() > 5: m = cv.dilate(m.astype(np.uint8), kernel, iterations=1) res_masks.append(m) return res_masks, labels_ws
Example #11
Source File: watershed.py From PyCV-time with MIT License | 5 votes |
def watershed(self): m = self.markers.copy() cv2.watershed(self.img, m) overlay = self.colors[np.maximum(m, 0)] vis = cv2.addWeighted(self.img, 0.5, overlay, 0.5, 0.0, dtype=cv2.CV_8UC3) cv2.imshow('watershed', vis)
Example #12
Source File: watershed.py From PyCV-time with MIT License | 5 votes |
def watershed(self): m = self.markers.copy() cv2.watershed(self.img, m) overlay = self.colors[np.maximum(m, 0)] vis = cv2.addWeighted(self.img, 0.5, overlay, 0.5, 0.0, dtype=cv2.CV_8UC3) cv2.imshow('watershed', vis)
Example #13
Source File: plant_features.py From bonnet with GNU General Public License v3.0 | 4 votes |
def watershed(rgb, idx, mask): ''' Get watershed transform from image ''' # kernel definition kernel = np.ones((3, 3), np.uint8) # sure background area sure_bg = cv2.dilate(mask, kernel) sure_bg = np.uint8(sure_bg) # util.im_gray_plt(sure_bg,"sure back") # Finding sure foreground area dist_transform = cv2.distanceTransform(np.uint8(mask), cv2.DIST_L2, 3) # util.im_gray_plt(dist_transform,"dist transform") ret, sure_fg = cv2.threshold( dist_transform, 0.5 * dist_transform.max(), 255, 0) # Finding unknown region sure_fg = np.uint8(sure_fg) # util.im_gray_plt(sure_fg,"sure fore") unknown = cv2.subtract(sure_bg, sure_fg) # util.im_gray_plt(unknown,"unknown") # marker labelling ret, markers = cv2.connectedComponents(sure_fg) # add one to all labels so that sure background is not 0, but 1 markers = markers + 1 # mark the region of unknown with zero markers[unknown == 255] = 0 # util.im_gray_plt(np.uint8(markers),"markers") # apply watershed markers = cv2.watershed(rgb, markers) # create limit mask mask = np.zeros(mask.shape, np.uint8) mask[markers == -1] = 255 return mask
Example #14
Source File: watershed.py From python-image-processing with MIT License | 4 votes |
def watershed(src): # Change color to gray scale gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY) # Use the Otsu's binarization thresh,bin_img = cv2.threshold(gray,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU) # print(thresh) # print threshold # Noise removal kernel = np.ones((3,3), np.uint8) opening = cv2.morphologyEx(bin_img,cv2.MORPH_OPEN,kernel,iterations = 2) # Sure background area sure_bg = cv2.dilate(opening,kernel,iterations=3) # Finding sure foreground area dist_transform = cv2.distanceTransform(opening,cv2.DIST_L2,5) ret, sure_fg = cv2.threshold(dist_transform,0.7*dist_transform.max(),255,0) # Finding unknown region sure_fg = np.uint8(sure_fg) unknown = cv2.subtract(sure_bg,sure_fg) # Marker labelling ret, markers = cv2.connectedComponents(sure_fg) # Add one to all labels so that sure background is not 0, but 1 markers = markers+1 # Now, mark the region of unknown with zero markers[unknown==255] = 0 # Apply watershed markers = cv2.watershed(src,markers) src[markers == -1] = [255,0,0] # Check marker (If check markers, please import matplotlib) # plt.imshow(markers) # plt.show() # Check markers data # print(np.unique(markers,return_counts=True)) return markers, src
Example #15
Source File: normcut.py From sima with GNU General Public License v2.0 | 4 votes |
def split(self): """Split the region according to the normalized cut criterion. Returns ------- list of CutRegion The regions created by splitting. float The normalized cut cost. """ if not cv2_available: raise ImportError('OpenCV >= 2.4.8 required') tmp_im = np.zeros(self.shape[0] * self.shape[1]) tmp_im[self.indices] = 1 labeled_array, num_features = ndimage.label(tmp_im.reshape(self.shape)) if num_features > 1: labeled_array = labeled_array.reshape(-1)[self.indices] segments = [] for i in range(1, num_features + 1): idx = np.nonzero(labeled_array == i)[0] segments.append(CutRegion(self.affinity_matrix[idx, :][:, idx], self.indices[idx], self.shape)) return segments, 0.0 C = normcut_vectors(self.affinity_matrix, 1) im = C[:, -2] im -= im.min() im /= im.max() markers = -np.ones(self.shape[0] * self.shape[1]).astype('uint16') markers[self.indices] = 0 markers[self.indices[im < 0.02]] = 1 markers[self.indices[im > 0.98]] = 2 markers = markers.reshape(self.shape) vis2 = 0.5 * np.ones(self.shape[0] * self.shape[1]) vis2[self.indices] = im vis2 *= (2 ** 8 - 1) vis2 = cv2.cvtColor(np.uint8(vis2.reshape(self.shape)), cv2.COLOR_GRAY2BGR) markers = np.int32(markers) cv2.watershed(vis2, markers) cut = ndimage.morphology.binary_dilation(markers == 2).reshape(-1)[ self.indices] cut[im > 0.98] = True cost = self._normalized_cut_cost(cut) for thresh in np.linspace(0, 1, 20)[1:-1]: tmp_cut = im > thresh tmp_cost = self._normalized_cut_cost(tmp_cut) if tmp_cost < cost: cost = tmp_cost cut = tmp_cut a = cut.nonzero()[0] a = cut.nonzero()[0] b = np.logical_not(cut).nonzero()[0] return ( [CutRegion(self.affinity_matrix[x, :][:, x], self.indices[x], self.shape) for x in [a, b] if len(x)], self._normalized_cut_cost(cut) )
Example #16
Source File: image_processing.py From kaggle-dsb2018 with Apache License 2.0 | 3 votes |
def post_process_image(image, mask, contour): """ Watershed on the markers generated on the sure foreground to find all disconnected objects The (mask - contour) is the true foreground. We set the contour to be unknown area. Index of contour = -1 Index of unkown area = 0 Index of background = 1 -> set back to 0 after watershed Index of found objects > 1 """ kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5)) new_contour = (contour*255).astype(np.uint8) new_mask = (mask*255).astype(np.uint8) new_mask = cv2.morphologyEx(new_mask, cv2.MORPH_OPEN, kernel, iterations=1) _, thresh_mask = cv2.threshold(new_mask,0,255, cv2.THRESH_BINARY+cv2.THRESH_OTSU) _, thresh_contour = cv2.threshold(new_contour,0,255, cv2.THRESH_BINARY+cv2.THRESH_OTSU) sure_background = cv2.dilate(thresh_mask,kernel,iterations=3) sure_foreground = cv2.subtract(thresh_mask, thresh_contour) mask_plus_contour = cv2.add(thresh_mask, thresh_contour) mask_plus_contour = cv2.cvtColor(mask_plus_contour, cv2.COLOR_GRAY2RGB) unknown = cv2.subtract(sure_background, sure_foreground) # Marker labelling output = cv2.connectedComponentsWithStats(sure_foreground) labels = output[1] stats = output[2] # Add one to all labels so that sure background is not 0, 0 is considered unknown by watershed # this way, watershed can distinguish unknown from the background labels = labels + 1 labels[unknown==255] = 0 try: # random walker on thresh_mask leads a lot higher mean IoU but lower LB #labels = random_walker(thresh_mask, labels) # random walker on thresh_mask leads lower mean IoU but higher LB labels = random_walker(mask_plus_contour, labels, multichannel=True) except: labels = cv2.watershed(mask_plus_contour, labels) labels[labels==-1] = 0 labels[labels==1] = 0 labels = labels -1 labels[labels==-1] = 0 # discard nuclei which are too big or too small mean = np.mean(stats[1:,cv2.CC_STAT_AREA]) for i in range(1, labels.max()): if stats[i, cv2.CC_STAT_AREA] > mean*10 or stats[i, cv2.CC_STAT_AREA] < mean/10: labels[labels==i] = 0 labels = renumber_labels(labels) return labels