首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >4通道图像叠加

问4通道图像叠加
EN

Stack Overflow用户
提问于 2014-09-26 07:42:24
回答 1查看 647关注 0票数 0

我有一个有4个频道的图像,我需要把它覆盖在一堆图片上。在有3个通道的图片上,叠加效果很好,但是在有alpha通道的图片上,图片的背景会变成黑色。

原图:http://img.blog.csdn.net/20130610074054484

叠加图片:http://imgur.com/mlVAN0A

这是进行叠加的代码:

代码语言:javascript
复制
void overlayImage(const cv::Mat &background, const cv::Mat &foreground, 
              cv::Mat &output, cv::Point2i location)
{
    background.copyTo(output);

    for(int y = std::max(location.y , 0); y < background.rows; ++y)
    {
        int fY = y - location.y;
        if(fY >= foreground.rows)
            break;

        for(int x = std::max(location.x, 0); x < background.cols; ++x)
        {
            int fX = x - location.x; 
            if(fX >= foreground.cols)
                break;

            double opacity = ((double)foreground.data[fY * foreground.step + fX * foreground.channels() + 3]) / 255.;

            for(int c = 0; opacity > 0 && c < output.channels(); ++c)
            {
                unsigned char foregroundPx = foreground.data[fY * foreground.step + fX * foreground.channels() + c];
                unsigned char backgroundPx = background.data[y * background.step + x * background.channels() + c];
                output.data[y*output.step + output.channels()*x + c] =
                backgroundPx * (1.-opacity) + foregroundPx * opacity;
            }
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-09-26 07:54:50

这是因为你使用1-不透明度作为背景图像.如果基础图像的不透明度为0,则背景像素的不透明度将为1,而不是以前的0。

您必须计算结果的不透明度,fpr,这两个图像,可以是0,两者也。

克劳斯

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26054497

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档