TheStrangeQuark
TheStrangeQuark

Reputation: 2405

OpenCV - Apply color mask to color image

I'm trying to apply a color mask to a color image. The color mask is an outline that I want to apply to the color image. The mask is all black except for the outline which is pink ( BGR = [180, 105,255]). Oddly, I am able to apply an outline that is cyan [227,230,49] using the following method:

Let the color image be imgColor and the cyan outline be maskCyan. Again, this mask is all black [0,0,0] except for the pixels that are part of the outline which are [227,230,49]. Then I can apply this over the image by just doing imgColor_with_cyan_outline = cv2.bitwise_or(imgColor, maskCyan). When I do this same this with maskPink which has pink pixels instead of cyan using imgColor_with_pink_outline = cv2.bitwise_or(imgColor, maskPink) then I am returned the original image without any mask or outline applied to it. I think I'm just misunderstanding how cv2.bitwise_or() works, but I'm not sure.

Is there any other way to apply a color mask to a color image?

Upvotes: 2

Views: 14925

Answers (2)

zindarod
zindarod

Reputation: 6468

I think you misunderstood the properties of bitwise OR operation. The cv2.bitwise_or takes two source images plus an optional mask.

cv2.bitwise_or(src1, src2, dst, mask)

So if src1 has a pixel with value 1 and src2 has a pixel with value 2, then src1 | src2 is:

0001 = src1
0010 = src2
0011 = src1 | src2

which makes the resultant pixel value 3. For 8-bit unsigned char images, the maximum resultant pixel value will be 255 (135 | 235 is 255).

Upvotes: 3

onur aslan
onur aslan

Reputation: 292

You can use cv2.inrange function for masking if you have a baundary for filtering. Modify following code as your values. Check for syntax according to your opencv version

Pink=cv2.inrange(pink baundary)
Black=cv2.inrange(black baundary)
Mask= Pink+Black

For more information you can read

https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_imgproc/py_colorspaces/py_colorspaces.html

Upvotes: 0

Related Questions