openCV:为 IplImage 添加透明度

发布于 2024-12-09 04:41:23 字数 436 浏览 0 评论 0原文

我有一个 3 通道 IplImage。我想创建一个 4 通道图像并将其 Alpha 通道设置为小于 1.0 的值以使其半透明。

首先,我将 Alpha 通道(第 4 个通道)设置为 0.5:

cvSet(Image_c4, cvScalar(0,0,0,0.5);

这是我用来将 3 通道图像复制到 4 通道图像的命令。

cvCvtColor(Image_c3, Image_c4, CV_RGB2RGBA);

问题:Image_c3 是彩色的。 Image_c4 成为 Image_c3 的灰度副本(并且没有透明度)。

更新: 事实证明,上面的代码实际上是正确的并且有效,并且实际上比下面答案中建议的解决方案更简洁。我在其他地方遇到了一个不相关的错误。

I have a 3-channel IplImage. I would like to create a 4-channel image and set the alpha channel for it to a value less than 1.0 to make it semi-transparent.

First I set the alpha channel (the 4-th channel) to 0.5:

cvSet(Image_c4, cvScalar(0,0,0,0.5);

Here is the command that I used to copy the 3-channel image into a 4-channel image.

cvCvtColor(Image_c3, Image_c4, CV_RGB2RGBA);

The problem: Image_c3 is in color. Image_c4 becomes a gray scale copy of Image_c3 (and with no transparency).

Update:
It turned out that the code above is actually correct and works and is actually more concise than the suggested solutions in the answers below. I had an unrelated bug somewhere else.

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

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

发布评论

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

评论(2

影子是时光的心 2024-12-16 04:41:23

也许还有另一种方法,但我添加了这样的透明度:

// BGR split
cvSplit(im1_bgr, im1_b, im1_g, im1_r, NULL);

// Alpha channel creation (transparency)
IplImage *im1_a = cvCreateImage(cvGetSize(im1_bgr), 8, 1);
// Set the alpha value
cvSet(im1_a, cvScalar(128), NULL);

// Merge the 4 channel to an BGRA image
IplImage *im1_bgra = cvCreateImage(cvGetSize(im1_bgr), 8, 4);
cvMerge(im1_b, im1_g, im1_r, im1_a, im1_bgra);

Maybe there is another way but I add transparency like this:

// BGR split
cvSplit(im1_bgr, im1_b, im1_g, im1_r, NULL);

// Alpha channel creation (transparency)
IplImage *im1_a = cvCreateImage(cvGetSize(im1_bgr), 8, 1);
// Set the alpha value
cvSet(im1_a, cvScalar(128), NULL);

// Merge the 4 channel to an BGRA image
IplImage *im1_bgra = cvCreateImage(cvGetSize(im1_bgr), 8, 4);
cvMerge(im1_b, im1_g, im1_r, im1_a, im1_bgra);
倾其所爱 2024-12-16 04:41:23
//This code help to make a transparency image But it take src image as one //single color background see![Removing background and added black background color  ][1]
Mat dst;//(src.rows,src.cols,CV_8UC4);
Mat tmp,alpha;

cvtColor(src,tmp,CV_BGR2GRAY);
threshold(tmp,alpha,0,255,THRESH_BINARY);

Mat rgb[3];
split(src,rgb);

Mat rgba[4]={rgb[0],rgb[1],rgb[2],alpha};
merge(rgba,4,dst);
imwrite("dst.png",dst);
//dst is transparency image see here![output image as transparency image][2]


  [1]: https://i.sstatic.net/9THqs.png
  [2]: https://i.sstatic.net/mpmgy.png
//This code help to make a transparency image But it take src image as one //single color background see![Removing background and added black background color  ][1]
Mat dst;//(src.rows,src.cols,CV_8UC4);
Mat tmp,alpha;

cvtColor(src,tmp,CV_BGR2GRAY);
threshold(tmp,alpha,0,255,THRESH_BINARY);

Mat rgb[3];
split(src,rgb);

Mat rgba[4]={rgb[0],rgb[1],rgb[2],alpha};
merge(rgba,4,dst);
imwrite("dst.png",dst);
//dst is transparency image see here![output image as transparency image][2]


  [1]: https://i.sstatic.net/9THqs.png
  [2]: https://i.sstatic.net/mpmgy.png
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文