How To Blackout The Background Of An Image After Doing Contour Detection Using Open In Python?
I've detected contours for an image using opencv python,now I should blackout the image outside the contour.Could anyone help me to do this?
Solution 1:
Given your found contours, use drawContours
to create a binary mask, in which your contours are filled. Dependent on how you do that (black image, white contours vs. white image, black contours), you set all pixels in your input image to 0 expect for the masked (or unmasked) ones. See the following code snippet for a visualization:
import cv2
import numpy as np
# Artificial inputinput = np.uint8(128 * np.ones((200, 100, 3)))
cv2.rectangle(input, (10, 10), (40, 60), (255, 240, 172), cv2.FILLED)
cv2.circle(input, (70, 100), 20, (172, 172, 255), cv2.FILLED)
# Input to grayscale
gray = cv2.cvtColor(input, cv2.COLOR_RGB2GRAY)
# Simple binary threshold
_, gray = cv2.threshold(gray, 128, 255, cv2.THRESH_BINARY)
# Find contours
cnts, _ = cv2.findContours(gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# Generate mask
mask = np.ones(gray.shape)
mask = cv2.drawContours(mask, cnts, -1, 0, cv2.FILLED)
# Generate output
output = input.copy()
output[mask.astype(np.bool), :] = 0
cv2.imwrite("images/input.png", input)
cv2.imwrite("images/mask.png", np.uint8(255 * mask))
cv2.imwrite("images/output.png", output)
The artificial input image:
The mask generated during processing:
The final output:
Post a Comment for "How To Blackout The Background Of An Image After Doing Contour Detection Using Open In Python?"