C++;像素比较和着色 我是一个计算机科学的学生,我被困在C++课程中。我似乎找不到一种方法来比较两幅图像的像素,然后合并成一幅。这是我目前一直坚持的任务:
第1部分: 加载图像文件夹中提供的两个图像对: (第1对:渲染_顶部_1和渲染_顶部_2,第2对:渲染_底部_1和渲染_底部_2) 注意:您可以使用讲座和教程中给出的示例加载函数加载图像。对于每一对,比较它们的像素,如果像素相同,则将其变为黑色,如果不相同,则将其变为白色。将结果图像保存为顶部对的“stage1_top.png”,底部对的“stage1_bottom.png”。然后,您需要将“stage1_top”和“stage1_bottom”两个图像组合成一个图像。组合应该是相等的分割,从每个图像中提取50%的像素值。将生成的组合图像保存为“stage1_combined.png” 到目前为止,我设法加载了图像并获得了它们的宽度和高度,现在我想我必须使用双for循环来比较top_1和top_2,boto_1和bottom_2 这是我加载图片的方式:C++;像素比较和着色 我是一个计算机科学的学生,我被困在C++课程中。我似乎找不到一种方法来比较两幅图像的像素,然后合并成一幅。这是我目前一直坚持的任务:,c++,freeimage,C++,Freeimage,第1部分: 加载图像文件夹中提供的两个图像对: (第1对:渲染_顶部_1和渲染_顶部_2,第2对:渲染_底部_1和渲染_底部_2) 注意:您可以使用讲座和教程中给出的示例加载函数加载图像。对于每一对,比较它们的像素,如果像素相同,则将其变为黑色,如果不相同,则将其变为白色。将结果图像保存为顶部对的“stage1_top.png”,底部对的“stage1_bottom.png”。然后,您需要将“stage1_top”和“stage1_bottom”两个图像组合成一个图像。组合应该是相等的分割,从每
//Pair 1 Loading images
fipImage inputImage_bot1;
inputImage_bot1.load("Images\render_bottom_1.png");
inputImage_bot1.convertToFloat();
fipImage inputImage_top1;
inputImage_top1.load("Images\render_top_1.png");
inputImage_top1.convertToFloat();
fipImage inputImage_bot2;
inputImage_bot2.load("Images\render_bottom_2.png");
inputImage_bot2.convertToFloat();
fipImage inputImage_top2;
inputImage_top2.load("Images\render_top_2.png");
inputImage_top2.convertToFloat();
auto width_bot1 = inputImage_bot1.getWidth();
auto height_bot1 = inputImage_bot1.getHeight();
const float* const inputBuffer_bot1 = (float*)inputImage_bot1.accessPixels();
auto width_bot2 = inputImage_bot2.getWidth();
auto height_bot2 = inputImage_bot2.getHeight();
const float* const inputBuffer_bot2 = (float*)inputImage_bot2.accessPixels();
auto width_top1 = inputImage_top1.getWidth();
auto height_top1 = inputImage_top1.getHeight();
const float* const inputBuffer_top1 = (float*)inputImage_top1.accessPixels();
auto width_top2 = inputImage_top2.getWidth();
auto height_top2 = inputImage_top2.getHeight();
const float* const inputBuffer_top2 = (float*)inputImage_top2.accessPixels();
我尝试使用for循环来比较像素:
for (int i = 0; i < width_bot1; i++)
{
for (int j = 0; j < width_bot2; j++)
{
if (inputImage_bot1[i][j] == inputImage_bot2[i][j])
{
//comparison code here
}
}
}
我只能使用FreeImagePlus库,我完全被它迷住了。如何比较两张图片的像素,然后将每幅图片的50%保存在不同的文件中 通过查看fipImage
类的属性,该类没有定义的运算符[]
重载。因此,不能以这种方式访问像素
如果我们更加关注文档,我们可以找到方法和(顺便说一句,还有它们的集合变体),它们似乎满足了您的需求
用法示例:
// We assume:
// - We have a fipImage instance called my_image.
// - We want to get the pixel color at position (x,y)
RGBQUAD color;
if(my_image.getPixelColor(x, y, &color))
{
// color now contains the color at position (x,y).
}
可以找到有关
RGBQUAD
结构的详细信息。您的嵌套循环有点滑稽。我建议使用循环变量名,如x
和y
,因为您似乎指的是x和y坐标。然后您应该认为,如果y
循环应该具有与高度而不是宽度相关的结束条件。
// We assume:
// - We have a fipImage instance called my_image.
// - We want to get the pixel color at position (x,y)
RGBQUAD color;
if(my_image.getPixelColor(x, y, &color))
{
// color now contains the color at position (x,y).
}