本文整理汇总了Python中cv2.GC_INIT_WITH_MASK属性的典型用法代码示例。如果您正苦于以下问题:Python cv2.GC_INIT_WITH_MASK属性的具体用法?Python cv2.GC_INIT_WITH_MASK怎么用?Python cv2.GC_INIT_WITH_MASK使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类cv2
的用法示例。
在下文中一共展示了cv2.GC_INIT_WITH_MASK属性的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: SMGetSalientRegion
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import GC_INIT_WITH_MASK [as 别名]
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
示例2: _process
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import GC_INIT_WITH_MASK [as 别名]
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)
示例3: grab_cut_mask
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import GC_INIT_WITH_MASK [as 别名]
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