Given two input i. original image & ii. mask image, what's the best way to remove to the background from the original image.
Original Image
Mask Image
The final output would contain just the dog without the background and look transparent. I have seen the mask images are also created with OpenCV. Is there a way to just the existing mask image and generate the output image?
Update I tried this
import cv2
# opencv loads the image in BGR, convert it to RGB
img = cv2.imread("originalImage.png")
mask = cv2.imread("maskImage.png")
final = cv2.bitwise_and(img, mask)
cv2.imwrite("final.png", final)
Final Image
Is there a way to set the background to be transparent?
You can create a transparent image by creating a 4-channel BGRA image and copying the first 3 channels from the original image and setting the alpha channel using the mask image.
transparent = np.zeros((img.shape[0], img.shape[1], 4), dtype=np.uint8)
transparent[:,:,0:3] = img
transparent[:, :, 3] = mask
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With