OpenCV & Python: Cover a colored mask over a image

Will Li picture Will Li · Jun 14, 2017 · Viewed 10k times · Source

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

Answer

Will Li picture Will Li · Jun 14, 2017

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)