简体   繁体   English

OpenCV-将彩色蒙版应用于彩色图像

[英]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] ). 蒙版为黑色,轮廓为粉红色(BGR = [180, 105,255] )。 Oddly, I am able to apply an outline that is cyan [227,230,49] using the following method: 奇怪的是,我可以使用以下方法应用青色的轮廓[227,230,49]

Let the color image be imgColor and the cyan outline be maskCyan . 令彩色图像为imgColor ,青色轮廓为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] . 再次,此遮罩全为黑色[0,0,0]只是轮廓的一部分像素为[227,230,49] Then I can apply this over the image by just doing imgColor_with_cyan_outline = cv2.bitwise_or(imgColor, maskCyan) . 然后我可以通过执行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. 当我做这个相同的这与maskPink其具有使用粉红色的像素,而不是青色imgColor_with_pink_outline = cv2.bitwise_or(imgColor, maskPink)然后我现在回到原始图像而没有施加到其上的任何掩模或轮廓。 I think I'm just misunderstanding how cv2.bitwise_or() works, but I'm not sure. 我想我只是误解了cv2.bitwise_or()工作原理,但是我不确定。

Is there any other way to apply a color mask to a color image? 还有其他方法可以将彩色蒙版应用于彩色图像吗?

I think you misunderstood the properties of bitwise OR operation. 我认为您误解了按位OR运算的属性。 The cv2.bitwise_or takes two source images plus an optional mask. cv2.bitwise_or拍摄两个源图像以及一个可选的蒙版。

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 因此,如果src1的像素值为1,而src2的像素值为2,则src1 | src2 src1 | src2 is: src1 | src2是:

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). 从而使结果像素值为3。对于8位无符号char图像,最大结果像素值为255(135 | 235为255)。

You can use cv2.inrange function for masking if you have a baundary for filtering. 如果有过滤的基础,则可以使用cv2.inrange函数进行屏蔽。 Modify following code as your values. 修改以下代码作为您的值。 Check for syntax according to your opencv version 根据您的opencv版本检查语法

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 https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_imgproc/py_colorspaces/py_colorspaces.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM