在 OpenCV 中使颜色完全透明
我有一个基本的 png 文件,其中有两种颜色:绿色和洋红色。我想要做的是获取所有洋红色像素并使它们透明,以便我可以将图像合并到另一个图像中。
一个例子是,如果我有一个洋红色背景上的 2D 角色的图像文件。我会删除背景中的所有洋红色,使其透明。从那里,我只需拍摄角色的图像并将其添加为另一个图像中的图层,这样看起来角色已被放置在环境中。
提前致谢。
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.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这就是我要使用的代码,
首先,加载您的图像:
然后使用这样的蒙版来选择颜色,您应该参考文档。在下面,我想选择蓝色(不要忘记在 OpenCV 中图像是 BGR 格式,因此 125,0,0 是蓝色(它对应于下限),255,127,127 是具有一定容差的蓝色,是上限。
我选择了带有公差的下限和上限,以获取图像的所有蓝色,但您可以选择任何您想要的...
现在我们已经选择了蒙版,让我们反转它(因为我们不想保留蒙版,但要删除它)
然后复制带有遮罩的图像,
希望它能有所帮助,
请参阅 OpenCVDDocumentation 了解更多信息
这里是
朱利安,
That's the code i would use,
First, load your image :
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...
Now we have selected the mask, let's inverse it (as we don't want to keep the mask, but to remove it)
And then copy your image with the mask,
Hope it could help,
Please refer to the OpenCVDocumentation for further information
Here it is
Julien,