Python cv2.GC_INIT_WITH_MASK Examples
The following are 3
code examples of cv2.GC_INIT_WITH_MASK().
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: pySaliencyMap.py From pliers with BSD 3-Clause "New" or "Revised" License | 8 votes |
def SMGetSalientRegion(self, src): # get a binarized saliency map binarized_SM = self.SMGetBinarizedSM(src) # GrabCut img = src.copy() mask = np.where( (binarized_SM != 0), cv2.GC_PR_FGD, cv2.GC_PR_BGD).astype('uint8') bgdmodel = np.zeros((1, 65), np.float64) fgdmodel = np.zeros((1, 65), np.float64) rect = (0, 0, 1, 1) # dummy iterCount = 1 cv2.grabCut(img, mask=mask, rect=rect, bgdModel=bgdmodel, fgdModel=fgdmodel, iterCount=iterCount, mode=cv2.GC_INIT_WITH_MASK) # post-processing mask_out = np.where( (mask == cv2.GC_FGD) + (mask == cv2.GC_PR_FGD), 255, 0).astype('uint8') output = cv2.bitwise_and(img, img, mask=mask_out) return output
Example #2
Source File: grabcut.py From EarthSim with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _process(self, element, key=None): try: import cv2 as cv except: # HACK: Avoids error loading OpenCV the first time # ImportError dlopen: cannot load any more object with static TLS try: import cv2 as cv except ImportError: raise ImportError('GrabCut algorithm requires openCV') if isinstance(self.p.foreground, hv.Polygons): rasterize_op = rasterize_polygon else: rasterize_op = rasterize.instance(aggregator=ds.any()) kwargs = {'dynamic': False, 'target': element} fg_mask = rasterize_op(self.p.foreground, **kwargs) bg_mask = rasterize_op(self.p.background, **kwargs) fg_mask = fg_mask.dimension_values(2, flat=False) bg_mask = bg_mask.dimension_values(2, flat=False) if fg_mask[np.isfinite(fg_mask)].sum() == 0 or bg_mask[np.isfinite(bg_mask)].sum() == 0: return element.clone([], vdims=['Foreground'], new_type=gv.Image, crs=element.crs) mask = np.where(fg_mask, 1, 2) mask = np.where(bg_mask, 0, mask).copy() bgdModel = np.zeros((1,65), np.float64) fgdModel = np.zeros((1,65), np.float64) if isinstance(element, hv.RGB): img = np.dstack([element.dimension_values(d, flat=False) for d in element.vdims]) else: img = element.dimension_values(2, flat=False) mask, _, _ = cv.grabCut(img, mask.astype('uint8'), None, bgdModel, fgdModel, self.p.iterations, cv.GC_INIT_WITH_MASK) fg_mask = np.where((mask==2)|(mask==0),0,1).astype('bool') xs, ys = (element.dimension_values(d, expanded=False) for d in element.kdims) return element.clone((xs, ys, fg_mask), vdims=['Foreground'], new_type=gv.Image, crs=element.crs)
Example #3
Source File: skin_detector.py From SkinDetector with MIT License | 5 votes |
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