如何将一个图像叠加在另一个图像上?c++
How to overlay one image on top of another? C++
我有两张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一样整洁
相关文章:
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- OpenCV EqualizeHist()从彩色图像创建黑白图像
- 将"打开的CV图像"中的"颜色"转换为整数格式
- 平均图像时图像损坏
- 在C++中使用GDAL可以将图像的像素坐标转换为lat,long吗
- 如何将图像传输到c++(dll)中的缓冲区,然后在c#的缓冲区中读/写
- Vulkan验证层不断在VkQueuePresentKHR()上抛出图像布局错误
- 使用FFMPEG将RGB图像序列保存到.mp4时出现问题
- 将RGB图像保存为PPM格式
- 将图像添加到资源文件夹UWP C++
- 彩色图像的卤化物处理平均值
- C++射线示踪剂ppm表示没有足够的数据来显示图像
- 重新定位图像时如何前进到下一个内存块
- 视差图和校正图像之间的叠加
- 在 Dlib 中,如何保存带有叠加层的图像
- 使用 QGraphicsView 或 QGraphicsScene 在较大图像中叠加图像
- 叠加在4通道图像上
- 在黑色图像上叠加精明的边缘
- 如何将一个图像叠加在另一个图像上?c++