如何将一个图像叠加在另一个图像上?c++

How to overlay one image on top of another? C++

本文关键字:图像 叠加 另一个 c++ 一个      更新时间:2023-10-16

我有两张RGB图像(ppm格式),我希望能够将顶部图像的非纯黑色的任何像素叠加到底部图像上。

我可以成功加载图像,保存图像,复制图像…但是我不能用上面描述的方式从这两个图像中创建一个图像。

我不打算包括我所有的代码,但实现这一点的重要部分是:

struct Pixel
{
    unsigned int r;
    unsigned int g;
    unsigned int b;
}

我重载了它的==操作符,以便于比较:

bool Pixel::operator==(const Pixel& other)
{
    if(r != other.r)
    {
        return true;
    }
    else if(g != other.g)
    {
        return true;
    }
    else if(b != other.b)
    {
        return true;
    }
    else
    {
        return false;
    }
}

在我的Pic类中,我有这个方法:

Pic Pic::overlay(const Pic& top, Pixel mask)
{    
    for(int h = 0; h < height; h++)
    {
        for(int w = 0; w < width; w++)
        {
            if(!(top.pixels[h][w] ==  mask))
            {
                pixels[h][w] = top.pixels[h][w];  // pixels[][] is a Pixel array
            }
        }
    }
    return *this;
}

我的主文件如下:

Pic top;
Pic bot;
Pic overlay;
Pixel mask:
mask.r = 0;
mask.g = 0;
mask.b = 0;
top.loadimage("top.ppm");  //loadimage() loads the image in and all the data
bot.loadimage("bot.ppm");  //samme thing
overlay = bot.overlay(bot, mask);
overlay.saveimage("overlay.ppm");

显然,对于Pic类,=操作符是重载的。

我遇到的问题是:

在叠加方法中,如果我保留上面描述的if语句,顶部图像将显示在保存的文件中。如果我没有!()部分,它将显示底部的图像。

如果我完全去掉If()语句,并简单地尝试改变单个像素,例如:

pixels[h][w].r = pixels[h][w].r - 50;

保存的图像会被改变,看起来很古怪,原因很明显。

但是... .b和。g对图像没有影响。

我没主意了…我已经玩了两天了,我不知道出了什么问题。在我的程序中,除了这个overlay方法之外,一切都按照需要工作。

EDIT:所以,我在我的代码中发现了一个问题,它回到了我如何用PPM P6格式加载图像。我没有将每个像素单独加载为1个字节,而是尝试将它们一起加载,这样就可以缓冲结构和压缩中的二进制读取……现在我可以把顶部的图像叠加到底部的图像上,但不是所有的颜色都显示出来。不过,还是比以前好。

下面是我修改overlay的嵌套for()循环的样子:
for(int h = 0; h < height; h++)
{
    for(int w = 0; w < width; w++)
    {
        if(top.pixels[h][w].r != mask.r &&
           top.pixels[h][w].g != mask.g &&
           top.pixels[h][w].b != mask.b   )
        {
            pixels[h][w].r = top.pixels[h][w].r;
            pixels[h][w].g = top.pixels[h][w].g;
            pixels[h][w].b = top.pixels[h][w].b;
        }
    }
}

这行看起来不对:

overlay = bot.overlay(bot, mask);

应该是:

overlay = bot.overlay(top, mask);

如果你想用更短的方式写出等式检验,那么你可以这样写:

bool Pixel::operator==(const Pixel& other)
{
    return (r==other.r && g==other.g && b==other.b);
}

最后,既然你有一个相等操作符,那么为什么不添加和赋值('=')来保持你的编码像poss一样整洁