Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV & Python: Cover a colored mask over a image

I want to cover a image with a transparent solid color overlay in the shape of a black-white mask

Currently I'm using the following java code to implement this.

redImg = new Mat(image.size(), image.type(), new Scalar(255, 0, 0));
redImg.copyTo(image, mask);

I'm not familiar with the python api.

So I want to know if there any alternative api in python. Is there any better implementation?

image:

src img

mask:

mask

what i want:

what i want

like image 462
Will Li Avatar asked Jun 14 '17 03:06

Will Li


1 Answers

Now after I deal with all this Python, OpenCV, Numpy thing for a while, I find out it's quite simple to implement this with code:

image[mask] = (0, 0, 255)

-------------- the original answer --------------

I solved this by the following code:

redImg = np.zeros(image.shape, image.dtype)
redImg[:,:] = (0, 0, 255)
redMask = cv2.bitwise_and(redImg, redImg, mask=mask)
cv2.addWeighted(redMask, 1, image, 1, 0, image)
like image 67
Will Li Avatar answered Sep 22 '22 03:09

Will Li