Exorcismus
Exorcismus

Reputation: 2482

How to remove inside contour in OpenCV

I want to set a contour ROI and remove it along with the area inside it. I am using the below code to detect and remove the contour, but how can I remove the area inside too?

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)

Upvotes: 0

Views: 321

Answers (1)

Ha Bom
Ha Bom

Reputation: 2917

you can use cv.fillPoly to fill the contour with a color. docs.opencv.org/3.0-beta/modules/imgproc/doc/

Upvotes: 1

Related Questions