制作颜色OpenCV的完全透明 [英] Making a color completely transparent in OpenCV

查看:1168
本文介绍了制作颜色OpenCV的完全透明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有它的两种颜色,绿色,洋红色一个基本的PNG文件。什么我希望做的是采取一切品红像素,使其透明,这样我可以将图像合并到另一个形象。

I have a basic png file with two colors in it, green and magenta. What I'm looking to do is to take all the magenta pixels and make them transparent so that I can merge the image into another image.

一个例子是,如果我有一个紫红色的背景二维人物的图像文件。因此,它是透明的我会删除所有洋红色的背景。从那里,我只想把人物的形象,所以它看起来像角色已被放置在一个环境中它另一个图像添加为一个图层。

An example would be if I have an image file of a 2D character on a magenta background. I would remove all the magenta in the background so that it's transparent. From there I would just take the image of the character and add it as a layer in another image so it looks like the character has been placed in an environment.

先谢谢了。

推荐答案

这就是code我会用,

That's the code i would use,

首先,载入图片:

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

然后用面膜这样选择的颜色,你应该参考的文档。下面,我想选择一个蓝色(不要忘记,在OpenCV的图像是BGR格式,因此125,0,0是蓝色的(它对应的下限),并255127127是蓝色具有一定的宽容和是上限。
我选择了下限和上限的公差带图像的所有蓝色的,但你可以选择任何你想要的......

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,

请参阅OpenCVDocumentation以获得更多信息。

Please refer to the OpenCVDocumentation for further information

朱利安,

这篇关于制作颜色OpenCV的完全透明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆