本文整理汇总了Python中cv2.grabCut方法的典型用法代码示例。如果您正苦于以下问题:Python cv2.grabCut方法的具体用法?Python cv2.grabCut怎么用?Python cv2.grabCut使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cv2
的用法示例。
在下文中一共展示了cv2.grabCut方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: SMGetSalientRegion
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import grabCut [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: run_grabcut
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import grabCut [as 别名]
def run_grabcut(img_orig, rect_final):
# Initialize the mask
mask = np.zeros(img_orig.shape[:2],np.uint8)
# Extract the rectangle and set the region of
# interest in the above mask
x,y,w,h = rect_final
mask[y:y+h, x:x+w] = 1
# Initialize background and foreground models
bgdModel = np.zeros((1,65), np.float64)
fgdModel = np.zeros((1,65), np.float64)
# Run Grabcut algorithm
cv2.grabCut(img_orig, mask, rect_final, bgdModel, fgdModel, 5, cv2.GC_INIT_WITH_RECT)
# Extract new mask
mask2 = np.where((mask==2)|(mask==0),0,1).astype('uint8')
# Apply the above mask to the image
img_orig = img_orig*mask2[:,:,np.newaxis]
# Display the image
cv2.imshow('Output', img_orig)
示例3: _process
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import grabCut [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)
示例4: grab_cut_mask
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import grabCut [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
示例5: image_matting
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import grabCut [as 别名]
def image_matting(self, image_file, shape, iteration=10):
points = shape['points']
xmin, ymin, xmax, ymax = Grab_cut.convertPoints2BndBox(points)
self.width = xmax - xmin
self.height = ymax - ymin
src_img = cv2.imread(image_file)
mask = np.zeros(src_img.shape[:2], np.uint8)
bgdModel = np.zeros((1, 65), np.float64)
fgdModel = np.zeros((1, 65), np.float64)
rect = (xmin, ymin, self.width, self.height)
# Grabcut
cv2.grabCut(src_img, mask, rect, bgdModel, fgdModel,
iteration, cv2.GC_INIT_WITH_RECT)
r_channel, g_channel, b_channel = cv2.split(src_img)
a_channel = np.where((mask == 2) | (mask == 0), 0, 255).astype('uint8')
# crop image space
for row in range(ymin, ymax):
if sum(r_channel[row, xmin:xmax + 1]) > 0:
out_ymin = row
break
for row in range(ymin, ymax)[::-1]:
if sum(r_channel[row, xmin:xmax + 1]) > 0:
out_ymax = row + 1
break
for col in range(xmin, xmax):
if sum(a_channel[ymin:ymax + 1, col]) > 0:
out_xmin = col
break
for col in range(xmin, xmax)[::-1]:
if sum(a_channel[ymin:ymax + 1, col]) > 0:
out_xmax = col + 1
break
# output image
img_RGBA = cv2.merge((r_channel[out_ymin:out_ymax, out_xmin:out_xmax],
g_channel[out_ymin:out_ymax, out_xmin:out_xmax],
b_channel[out_ymin:out_ymax, out_xmin:out_xmax],
a_channel[out_ymin:out_ymax, out_xmin:out_xmax]))
return img_RGBA