OpenCV 中的 Alpha 通道

发布于 2024-08-05 10:23:13 字数 177 浏览 4 评论 0原文

OpenCV 支持 alpha 通道吗?或者有什么方法可以使用透明png?我需要合并两个图像,其中第一个是背景,第二个是由 cvWarpAffine 旋转的图像。我可以通过一一合并像素并省略具有某个值的像素来实现此目的,这些值是我在 cvWarpAffine 的 cvScalar 中设置的。但是,我不认为这是预期的解决方案。 感谢您的建议

does OpenCV support alpha-channel? Or is there any way to work with transparent png? I need to merge two images, where the first one is background and the second one is image which was rotated by cvWarpAffine. I can do this by merging pixels one by one and omit pixels with some value, which I set in cvScalar in cvWarpAffine. However, I don't think that this is intended solution.
Thanks for suggestions

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

涫野音 2024-08-12 10:23:13

更新答案:使用CV_LOAD_IMAGE_UNCHANGED标志从图像加载所有四个通道(包括Alpha)。然后,使用 mixChannels() 和/或 split() 将 Alpha 通道与其他通道分开,并对其设置阈值,如下所述。

非常旧的答案:

OpenCV 不支持 Alpha 通道,仅支持遮罩。如果你想用 alpha 读取 PNG,请首先使用 imagemagick 提取 alpha 通道:

convert input.png -channel Alpha -negate -separate input-mask.png

然后在 OpenCV 中你可以这样做:

Mat_<float> mask = imread("input-mask.png", 0);
threshold(mask, mask, 254., 1., THRESH_BINARY);

... 得到一个真正的掩模(可在 OpenCV 操作中用作掩模矩阵)。或者您可以在自己的操作中使用它,而无需阈值处理。要应用掩模,将其扩展到三个通道也可能是一个好主意:

std::vector<Mat> marr(3, mask);
Mat_<Vec<float, 3> > maskRGB;
merge(marr, maskRGB);

之后您可以像这样测试它:

imshow("Target", target);
imshow("Mask", mask*255);
imshow("Applied", target.mul(maskRGB));
waitKey();

注意:这是 OpenCV 2.0 代码。

Updated answer: Use CV_LOAD_IMAGE_UNCHANGED flag to load all four channels (including Alpha) from the image. Then, use mixChannels() and/or split() to separate the alpha channel from others, and threshold on it, as explained below.

Very old answer:

OpenCV does not support alpha channel, only masking. If you want to read in PNG with alpha, use imagemagick first to extract alpha channel:

convert input.png -channel Alpha -negate -separate input-mask.png

Then in OpenCV you can do sth like this:

Mat_<float> mask = imread("input-mask.png", 0);
threshold(mask, mask, 254., 1., THRESH_BINARY);

... to get a real mask (usable as mask matrix in OpenCV operations). Or you can use it in your own operations without the thresholding. To apply the mask it may also be a good idea to extend it to three channels:

std::vector<Mat> marr(3, mask);
Mat_<Vec<float, 3> > maskRGB;
merge(marr, maskRGB);

After that you can test it like this:

imshow("Target", target);
imshow("Mask", mask*255);
imshow("Applied", target.mul(maskRGB));
waitKey();

Note: This is OpenCV 2.0 code.

清风无影 2024-08-12 10:23:13

这是我编写的一个 bash 脚本,它将对目录中的所有 png 文件执行 ypnos 给出的 ImageMagick 转换。您可以通过用 find 命令替换第三行中的 * 来使其递归。

#!/bin/bash

for file in *
do
    if [[ $file =~ (.+)-mask\.png ]]; then
        echo "Ignoring mask $file"
    elif [[ $file =~ (.+)\.png ]]; then
        echo "Generating mask for $file"
        basefn=${BASH_REMATCH[1]}
        convert "$basefn.png" -channel Alpha -negate -separate "$basefn-mask.png"
    fi
done

Here is a bash script that I threw together that will perform the ImageMagick conversion given by ypnos on all of the png files in a directory. You can make it recursive by replacing the * in the third line with a find command.

#!/bin/bash

for file in *
do
    if [[ $file =~ (.+)-mask\.png ]]; then
        echo "Ignoring mask $file"
    elif [[ $file =~ (.+)\.png ]]; then
        echo "Generating mask for $file"
        basefn=${BASH_REMATCH[1]}
        convert "$basefn.png" -channel Alpha -negate -separate "$basefn-mask.png"
    fi
done
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文