Making a color completely transparent in OpenCV

时间秒杀一切 提交于 2019-11-30 05:47:57

That's the code i would use,

First, load your image :

IplImage *myImage;
myImage = cvLoadImage("/path/of/your/image.jpg");

Then use a mask like this to select the color, you should refer to the documentation. In the following, I want to select a blue (don't forget that in OpenCV images are in BGR format, therefore 125,0,0 is a blue (it corresponds to the lower bound) and 255,127,127 is blue with a certain tolerance and is the upper bound. I chose lower and upper bound with a tolerance to take all the blue of your image, but you can select whatever you want...

cvInRangeS(image, 
           cvScalar(125.0, 0.0, 0.0), 
           cvScalar(255.0, 127.0, 127.0), 
           mask
           );

Now we have selected the mask, let's inverse it (as we don't want to keep the mask, but to remove it)

cvNot(mask, mask);

And then copy your image with the mask,

IplImage *myImageWithTransparency; //You may need to initialize it before
cvCopy(myImage,myImageWithTransparency,mask);

Hope it could help,

Please refer to the OpenCVDocumentation for further information

Here it is

Julien,

This is a whole topic in itself, with a number of solutions. I would read this first:

http://en.wikipedia.org/wiki/Transparency_(graphic)

then see how these methods can be applied in OpenCV.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!