我们从Python开源项目中,提取了以下7个代码示例,用于说明如何使用cv2.watershed()。
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() return self.returnVar
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
def run(self, ips, snap, img, para = None): a, msk = cv2.connectedComponents(ips.get_msk().astype(np.uint8)) msk = cv2.watershed(img, msk)==-1 img //= 2 img[msk] = 255
def watershed(self): m = self.markers.copy() cv2.watershed(self.img, m) self.returnVar = m.copy() overlay = self.colors[np.maximum(m, 0)] vis = cv2.addWeighted(self.img, 0.5, overlay, 0.5, 0.0, dtype=cv2.CV_8UC3) cv2.namedWindow('watershed', cv2.WINDOW_NORMAL) cv2.moveWindow('watershed',780,200) cv2.imshow('watershed', vis)
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)
def subtract_back(self,frm): #dst=self.__back__-self.__foreground__ temp=np.zeros((600,800),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)
def remove_pectoral(self, img, breast_mask, high_int_threshold=.8, morph_kn_size=3, n_morph_op=7, sm_kn_size=25): '''Remove the pectoral muscle region from an input image Args: img (2D array): input image as a numpy 2D array. breast_mask (2D array): high_int_threshold ([int]): a global threshold for high intensity regions such as the pectoral muscle. Default is 200. morph_kn_size ([int]): kernel size for morphological operations such as erosions and dilations. Default is 3. n_morph_op ([int]): number of morphological operations. Default is 7. sm_kn_size ([int]): kernel size for final smoothing (i.e. opening). Default is 25. Returns: an output image with pectoral muscle region removed as a numpy 2D array. Notes: this has not been tested on .dcm files yet. It may not work!!! ''' # Enhance contrast and then thresholding. img_equ = cv2.equalizeHist(img) if high_int_threshold < 1.: high_th = int(img.max()*high_int_threshold) else: high_th = int(high_int_threshold) maxval = self.max_pix_val(img.dtype) _, img_bin = cv2.threshold(img_equ, high_th, maxval=maxval, type=cv2.THRESH_BINARY) pect_marker_img = np.zeros(img_bin.shape, dtype=np.int32) # Sure foreground (shall be pectoral). pect_mask_init = self.select_largest_obj(img_bin, lab_val=maxval, fill_holes=True, smooth_boundary=False) kernel_ = np.ones((morph_kn_size, morph_kn_size), dtype=np.uint8) pect_mask_eroded = cv2.erode(pect_mask_init, kernel_, iterations=n_morph_op) pect_marker_img[pect_mask_eroded > 0] = 255 # Sure background - breast. pect_mask_dilated = cv2.dilate(pect_mask_init, kernel_, iterations=n_morph_op) pect_marker_img[pect_mask_dilated == 0] = 128 # Sure background - pure background. pect_marker_img[breast_mask == 0] = 64 # Watershed segmentation. img_equ_3c = cv2.cvtColor(img_equ, cv2.COLOR_GRAY2BGR) cv2.watershed(img_equ_3c, pect_marker_img) img_equ_3c[pect_marker_img == -1] = (0, 0, 255) # Extract only the breast and smooth. breast_only_mask = pect_marker_img.copy() breast_only_mask[breast_only_mask == -1] = 0 breast_only_mask = breast_only_mask.astype(np.uint8) breast_only_mask[breast_only_mask != 128] = 0 breast_only_mask[breast_only_mask == 128] = 255 kernel_ = np.ones((sm_kn_size, sm_kn_size), dtype=np.uint8) breast_only_mask = cv2.morphologyEx(breast_only_mask, cv2.MORPH_OPEN, kernel_) img_breast_only = cv2.bitwise_and(img_equ, breast_only_mask) return (img_breast_only, img_equ_3c)