Will Li
Will Li

Reputation: 615

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

Upvotes: 18

Views: 24623

Answers (3)

moien rangzan
moien rangzan

Reputation: 31

this is what worked for me:

red = np.ones(mask.shape)
red = red*255
img[:,:,0][mask>0] = red[mask>0]

so I made a 2d array with solid 255 values and replaced it with my image's red band in pixels where the mask is not zero. redmask

Upvotes: 0

nathancy
nathancy

Reputation: 46680

The idea is to convert the mask to a binary format where pixels are either 0 (black) or 255 (white). White pixels represent sections that are kept while black sections are thrown away. Then set all white pixels on the mask to your desired BGR color.

Input image and mask

Result

Code

import cv2

image = cv2.imread('1.jpg')
mask = cv2.imread('mask.jpg', 0)
mask = cv2.threshold(mask, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
image[mask==255] = (36,255,12)

cv2.imshow('image', image)
cv2.imshow('mask', mask)
cv2.waitKey()

Upvotes: 3

Will Li
Will Li

Reputation: 615

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)

Upvotes: 25

Related Questions