當前位置: 首頁>>代碼示例>>Python>>正文


Python cv2.GC_INIT_WITH_RECT屬性代碼示例

本文整理匯總了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) 
開發者ID:PacktPublishing,項目名稱:OpenCV-3-x-with-Python-By-Example,代碼行數:26,代碼來源:image_segmentation.py

示例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 
開發者ID:zihuaweng,項目名稱:Interactive-image-segmentation-opencv-qt,代碼行數:47,代碼來源:grab_cut.py


注:本文中的cv2.GC_INIT_WITH_RECT屬性示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。