如何在OpenCV中删除内部轮廓

时间:2019-04-08 22:37:50

标签: opencv cv2

我要设置轮廓ROI,并将其连同其中的区域一起删除。一世 我正在使用下面的代码检测并删除轮廓,但是如何也可以删除内部区域?

def get_skin_area(self):

    # Get pointer to video frames from primary device
    sourceImage = cv2.imread(self.img)

    # Constants for finding range of skin color in YCrCb
    min_YCrCb = np.array([0, 133, 77], np.uint8)
    max_YCrCb = np.array([255, 173, 127], np.uint8)

    # Convert image to YCrCb
    imageYCrCb = cv2.cvtColor(sourceImage, cv2.COLOR_BGR2YCR_CB)

    # Find region with skin tone in YCrCb image
    skinRegion = cv2.inRange(imageYCrCb, min_YCrCb, max_YCrCb)

    # Do contour detection on skin region
    _, contours, hierarchy = cv2.findContours(skinRegion, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    mask = np.ones(sourceImage.shape[:2], dtype="uint8") * 255

    # Draw the contour on the source image
    for i, c in enumerate(contours):
        area = cv2.contourArea(c)
        if area > 1000:
            # cv2.drawContours(sourceImage, contours, i, (0, 255, 0), 3)
            cv2.drawContours(mask, contours, i, (0, 255, 0), 3)

    image = cv2.bitwise_and(sourceImage, sourceImage, mask=mask)

    cv2.imshow("Mask", mask)
    cv2.imshow("After", image)

1 个答案:

答案 0 :(得分:1)

您可以使用cv.fillPoly用颜色填充轮廓。 docs.opencv.org/3.0-beta/modules/imgproc/doc /