我有透明图像[如下所示],我试图用aishack.in cvOverlayImage()函数覆盖它以将其覆盖在相机源上
cvOverlayImage()
void cvOverlayImage(IplImage* src, IplImage* overlay, CvPoint location, CvScalar S, CvScalar D)
{
int x,y,i;
for(x=0;x < overlay->width -10;x++)
{
if(x+location.x>=src->width) continue;
for(y=0;y < overlay->height -10;y++)
{
if(y+location.y>=src->height) continue;
CvScalar source = cvGet2D(src, y+location.y, x+location.x);
CvScalar over = cvGet2D(overlay, y, x);
CvScalar merged;
for(i=0;i<4;i++)
merged.val[i] = (S.val[i]*source.val[i]+D.val[i]*over.val[i]);
cvSet2D(src, y+location.y, x+location.x, merged);
}
}
}
调用cvOverlayImage()
cvOverlayImage(image_n, neg_img, cvPoint(0, 0), cvScalar(1.0,1.0,1.0,1.0), cvScalar(0.1,0.1,0.1,0.1));
输入cvOverlayImage()
来自cvOverlayImage()的输出
你可以看到我得不到我需要的东西。请帮助我。
答案 0 :(得分:2)
我使用的一个解决方案是简单地检测白色存在的位置,在这种情况下,只需使用源图像中的像素。否则使用叠加图片的像素。对于类似的情况,我工作得很好。此外,如果您加载的图片具有Alpha通道并且可以用作蒙版,那就更好了。
void cvOverlayImage(IplImage* src, IplImage* overlay, CvPoint location,
CvScalar S, CvScalar D)
{
int x,y,i;
for(x=0;x < overlay->width;x++)
{
if(x+location.x>=src->width) continue;
for(y=0;y < overlay->height;y++)
{
if(y+location.y>=src->height) continue;
CvScalar source = cvGet2D(src, y+location.y, x+location.x);
CvScalar over = cvGet2D(overlay, y, x);
CvScalar merged;
if(over.val[0] == 255 && over.val[1] == 255 && over.val[2] == 255 && over.val[3] == 255)
{
// White pixel so don't overlay
for(i=0;i<4;i++)
merged.val[i] = (source.val[i]);
}
else
{
for(i=0;i<4;i++)
merged.val[i] = (over.val[i]);
}
cvSet2D(src, y+location.y, x+location.x, merged);
}
}
}
答案 1 :(得分:1)
未经测试,但S [i] + D [i] = 1不能保持总强度吗?
答案 2 :(得分:0)
我认为你想要实现的不是加法而是乘法:
int multiplicator = over.val[i] / 255 // 0 for black, 1 for white
merged.val[i] = source.val[i] * multiplicator;
这样,像素值将是白色叠加像素的原始值,黑色叠加像素的黑色。