本文整理匯總了Python中cv2.GC_INIT_WITH_RECT屬性的典型用法代碼示例。如果您正苦於以下問題:Python cv2.GC_INIT_WITH_RECT屬性的具體用法?Python cv2.GC_INIT_WITH_RECT怎麽用?Python cv2.GC_INIT_WITH_RECT使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類cv2
的用法示例。
在下文中一共展示了cv2.GC_INIT_WITH_RECT屬性的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: run_grabcut
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import GC_INIT_WITH_RECT [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)
示例2: image_matting
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import GC_INIT_WITH_RECT [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