14
votes

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

1
Ok, Can you also show what have you tried so far ?ZdaR
Have you tried some permutations of blending the two images? :)Zeokav

1 Answers

18
votes

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)