我们从Python开源项目中,提取了以下6个代码示例,用于说明如何使用cv2.grabCut()。
def grabcutbb(im, bbv): mask = np.full(im.shape[:2],cv2.GC_PR_BGD,np.uint8) for bb in bbv: if bb[4]: cv2.rectangle(mask, (bb[0], bb[1]), (bb[2], bb[3]), int(cv2.GC_FGD), -1) else: cv2.rectangle(mask, (bb[0], bb[1]), (bb[2], bb[3]), int(cv2.GC_BGD), -1) bgdModel = np.zeros((1,65),np.float64) fgdModel = np.zeros((1,65),np.float64) rect = (0, im.shape[:2][0]/2, im.shape[:2][1], im.shape[:2][0]) cv2.grabCut(im, mask, rect, bgdModel, fgdModel, 5, cv2.GC_INIT_WITH_MASK) mask2 = np.where((mask==2)|(mask==0),0,1).astype('uint8') return mask2
def grab_cut_mask(img_col, mask, debug=False): assert isinstance(img_col, numpy.ndarray), 'image must be a numpy array' assert isinstance(mask, numpy.ndarray), 'mask must be a numpy array' assert img_col.ndim == 3, 'skin detection can only work on color images' assert mask.ndim == 2, 'mask must be 2D' kernel = numpy.ones((50, 50), numpy.float32) / (50 * 50) dst = cv2.filter2D(mask, -1, kernel) dst[dst != 0] = 255 free = numpy.array(cv2.bitwise_not(dst), dtype=numpy.uint8) if debug: scripts.display('not skin', free) scripts.display('grabcut input', mask) grab_mask = numpy.zeros(mask.shape, dtype=numpy.uint8) grab_mask[:, :] = 2 grab_mask[mask == 255] = 1 grab_mask[free == 255] = 0 if numpy.unique(grab_mask).tolist() == [0, 1]: logger.debug('conducting grabcut') bgdModel = numpy.zeros((1, 65), numpy.float64) fgdModel = numpy.zeros((1, 65), numpy.float64) if img_col.size != 0: mask, bgdModel, fgdModel = cv2.grabCut(img_col, grab_mask, None, bgdModel, fgdModel, 5, cv2.GC_INIT_WITH_MASK) mask = numpy.where((mask == 2) | (mask == 0), 0, 1).astype(numpy.uint8) else: logger.warning('img_col is empty') return mask
def grabcuthm(im, hm): size = hm.shape bright = np.amax(hm) ret,fgd = cv2.threshold(hm, FGD_BOUND * bright, 1 * bright, cv2.THRESH_BINARY) fgd[1:size[0]/2] = 0 fgd[1:size[0], 1:size[1]/4] = 0 fgd[1:size[0], size[1]*3/4:size[1]] = 0 ret,pr_fgd = cv2.threshold(hm, FGD_BGD_SEP * bright, 1 * bright, cv2.THRESH_BINARY) pr_fgd -= fgd ret, bgd = cv2.threshold(hm, BGD_BOUND * bright, 1 * bright, cv2.THRESH_BINARY_INV) bgd[size[0]/3:size[0]] = 0 ret,pr_bgd = cv2.threshold(hm, FGD_BGD_SEP * bright, 1 * bright, cv2.THRESH_BINARY_INV) pr_bgd -= bgd mask = cv2.GC_BGD * bgd + cv2.GC_FGD * fgd + cv2.GC_PR_BGD * pr_bgd + cv2.GC_PR_FGD * pr_fgd mask = mask.astype(np.uint8, copy=False) bgdModel = np.zeros((1,65),np.float64) fgdModel = np.zeros((1,65),np.float64) rect = (0, im.shape[:2][0]/2, im.shape[:2][1], im.shape[:2][0]) cv2.grabCut(im, mask, rect, bgdModel, fgdModel, 5, cv2.GC_INIT_WITH_MASK) mask2 = np.where((mask==2)|(mask==0),0,1).astype('uint8') return mask2
def run(self, ips, snap, img, para = None): msk = ips.mark.buildmsk(img.shape) bgdModel = np.zeros((1,65),np.float64) fgdModel = np.zeros((1,65),np.float64) msk, bgdModel, fgdModel = cv2.grabCut(snap, msk,None,bgdModel,fgdModel,5,cv2.GC_INIT_WITH_MASK) img[msk%2 == 0] //= 3
def remove_bkg(self, img): # quick sloppy background removal###### self.log.info("removing background") #use grabcutwith facerecthas foreground mask = np.zeros(img.shape[:2], np.uint8) bgdModel = np.zeros((1, 65), np.float64) fgdModel = np.zeros((1, 65), np.float64) mask2 = np.where((mask == 2) | (mask == 0), 0, 1).astype('uint8') cv2.grabCut(img, mask, self.rect, bgdModel, fgdModel, 5, cv2.GC_INIT_WITH_RECT) img = img * mask2[:, :, np.newaxis] return img # vision context
def image_segmentation(ip_convert): img = cv2.imdecode(np.squeeze(np.asarray(ip_convert[1])), 1) # cv2.imwrite("Skin_removed.jpg",img_skin) height, width, channels = img.shape # blurred = cv2.GaussianBlur(img, (5, 5), 0) # mask = np.zeros(img.shape[:2], np.uint8) bgdModel = np.zeros((1, 65), np.float64) fgdModel = np.zeros((1, 65), np.float64) rect = (5, 5, width - 5, height - 5) cv2.grabCut(img, mask, rect, bgdModel, fgdModel, 2, cv2.GC_INIT_WITH_RECT) mask2 = np.where((mask == 2) | (mask == 0), 0, 1).astype('uint8') img_mask = img* mask2[:, :, np.newaxis] cv2.imwrite("download(8)_grab.jpg",img_mask) # cv2.waitKey(0) # blurred = cv2.GaussianBlur(img_mask,(3,3),0) # img_skin = skin_detector.process(img_mask) # cv2.imwrite("download(10)_skin.jpg",img_skin) # cv2.waitKey(0) blurred = cv2.GaussianBlur(img_mask,(5,5),0) edgeImg = np.max( np.array([ edgedetect(blurred[:,:, 0]), edgedetect(blurred[:,:, 1]), edgedetect(blurred[:,:, 2]) ]), axis=0 ) mean = np.mean(edgeImg); # # Zero any value that is less than mean. This reduces a lot of noise. edgeImg[edgeImg < mean] = 0; edgeImg_8u = np.asarray(edgeImg, np.uint8) # # Find contours significant = findSignificantContours(img_mask, edgeImg_8u, edgeImg) cv2.imwrite("download(8)_contour.jpg",significant) significant = cv2.GaussianBlur(significant,(3,3),0) tmp = cv2.cvtColor(significant, cv2.COLOR_BGR2GRAY) _, alpha = cv2.threshold(tmp, 0, 1, cv2.THRESH_BINARY) b, g, r = cv2.split(significant) rgba = [b, g, r, alpha] dst = cv2.merge(rgba, 4) img_out = cv2.imencode('.png', dst) # cv2.imshow("Masking_Done.jpg",dst) # cv2.waitKey(0) return img_out