我有一个有4个频道的图像,我需要把它覆盖在一堆图片上。在有3个通道的图片上,叠加效果很好,但是在有alpha通道的图片上,图片的背景会变成黑色。
原图:http://img.blog.csdn.net/20130610074054484
这是进行叠加的代码:
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;
}
}
}
}发布于 2014-09-26 07:54:50
这是因为你使用1-不透明度作为背景图像.如果基础图像的不透明度为0,则背景像素的不透明度将为1,而不是以前的0。
您必须计算结果的不透明度,fpr,这两个图像,可以是0,两者也。
克劳斯
https://stackoverflow.com/questions/26054497
复制相似问题