我正在研究cs50 pset4运动过滤器,我完成了灰度过滤器和紫色过滤器,现在我使用的是反射过滤器。我应该水平地反射这个图像:
正规

但我所得到的只是:
反射

我不知道怎么回事。我试着像在录像里那样做。在我的代码中,我尝试将右侧像素放入一个临时变量中,然后将左侧像素放置在它们的位置上,然后将右侧像素取出并放到左侧像素点中。下面是我的代码(仅反映部分):
// Reflect image horizontally
void reflect(int height, int width, RGBTRIPLE image[height][width])
{
for (int j = 0; j < height;j++)
{
for (int i = 0; i < width/2;i++)
{
RGBTRIPLE temp = image[j][i];
image[j][width - i] = image[i][j];
temp = image[j][width - i];
}
}
return;
}请帮我理解一下。当我搜索它的时候,我得到的要么是不同的东西,要么是整个练习的答案,那就是从谷歌复制粘贴。
非常感谢,代码丢失了:)
发布于 2020-08-15 10:07:57
RGBTRIPLE temp = image[j][i];
image[j][width - i] = image[i][j];
temp = image[j][width - i];是错误的,因为
没有保存的
image[i][j]而不是image[j][i]的width - i - 1。例如,当out-of-range.i = 0时,width - i将是width,并且是width - i重写了temp两次,而不是将新值分配给两个像素。它应该是:
RGBTRIPLE temp = image[j][i];
image[j][i] = image[j][width - i - 1];
image[j][width - i - 1] = temp;发布于 2020-08-15 10:05:29
在保存图像之前,您可以覆盖它。
你有一箱交换过的i,j。
我建议交换保存、覆盖和恢复的顺序。
void reflect(int height, int width, RGBTRIPLE image[height][width])
{
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width/2; x++)
{
RGBTRIPLE temp = image[y][width -1 - x]; //save what gets overwritten
// then overwrite
image[y][width -1 - x] = image[y][x]; // note the wrong i,j which was here before
// then overwrite the other with what was saved
image[y][x] = temp;
}
}
return;
}问题是我是你的。应该是j,用名字x,y这样的问题更容易被发现。
谢谢你,MikeCat,你指出了我犯的错误。
https://stackoverflow.com/questions/63424729
复制相似问题