如何在 OpenCV 中去除内部轮廓

How to remove inside contour in OpenCV

我想设置轮廓 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)

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