向所有做CS50的人问好,
目前,我正在做pset4过滤器,反映和挣扎我写的代码。它编译得很好,但是输出图片看起来像我附加的那个。有什么线索可以帮我解决吗?(如果可能的话,没有解决方案;提示是完全可以的:)
#include "helpers.h"
#include <stdio.h>
#include <math.h>
// Convert image to grayscale
void grayscale(int height, int width, RGBTRIPLE image[height][width])
{
for (int i = 0; i < (height - 1); i++)
{
for (int j = 0; j < (width - 1); j++)
{
//get the average
float average = 0;
average = (image[i][j].rgbtRed + image[i][j].rgbtGreen + image[i][j].rgbtBlue) / 3;
//set color channels with the average value
image[i][j].rgbtRed = round(average);
image[i][j].rgbtGreen = round(average);
image[i][j].rgbtBlue = round(average);
}
}
return;
}
// Convert image to sepia
void sepia(int height, int width, RGBTRIPLE image[height][width])
{
for (int i = 0; i < (height - 1); i++)
{
for (int j = 0; j < (width - 1); j++)
{
//calculate the new values
float red = 0;
float green = 0;
float blue = 0;
red = .393 * image[i][j].rgbtRed + .769 * image[i][j].rgbtGreen + .189 * image[i][j].rgbtBlue;
if (round(red) > 255)
{
image[i][j].rgbtRed = 255;
}
else if (round(red <= 255))
{
image[i][j].rgbtRed = red;
}
green = .349 * image[i][j].rgbtRed + .686 * image[i][j].rgbtGreen + .168 * image[i][j].rgbtBlue;
if (round(green) > 255)
{
image[i][j].rgbtGreen = 255;
}
else if (round(green) <= 255)
{
image[i][j].rgbtGreen = green;
}
blue = .272 * image[i][j].rgbtRed + .534 * image[i][j].rgbtGreen + .131 * image[i][j].rgbtBlue;
if (round(blue) > 255)
{
image[i][j].rgbtBlue = 255;
}
else if (round(blue) <= 255)
{
image[i][j].rgbtBlue = blue;
}
}
}
return;
}
// Reflect image horizontally
void reflect(int height, int width, RGBTRIPLE image[height][width])
{
for (int i = 0; i < (height - 1); i++)
{
//RGBTRIPLE
int a[width - 1 /2];
int b[width - 1 /2];
int c[width - 1 /2];
for (int j = 0; j < ((width/2) - 1); j++)
{
a[j] = image[i][(width - 1) - j].rgbtRed;
b[j] = image[i][(width - 1) - j].rgbtGreen;
c[j] = image[i][(width - 1) - j].rgbtBlue;
}
// GERADE / UNGERADE ZAHLEN VERARBEITEN
for (int n = 0; (n + (width - 1) /2) < (width - 1); n++)
{
image[i][((width - 1)/2) - n].rgbtRed = image[i][((width - 1)/2) - n].rgbtRed;
image[i][((width - 1)/2) - n].rgbtGreen = image[i][((width - 1)/2) - n].rgbtGreen;
image[i][((width - 1)/2) - n].rgbtBlue = image[i][((width - 1)/2) - n].rgbtBlue;
}
for (int m = 0; m < ((width/2) - 1); m++)
{
image[i][m].rgbtRed = a[m];
image[i][m].rgbtRed = b[m];
image[i][m].rgbtRed = c[m];
}
}
return;
}
// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
return;
}输入:
./filter -r stadium.bmp outfile.bmp (stadium.bmp只是我选择的一个文件。您可以选择任何文件作为输入)
输出:编译成功,请参阅附图在这里输入图像描述
发布于 2020-04-12 22:25:11
你的代码太复杂了。您不需要将图像划分为RGB值。您需要从左到右交换图像值,反之亦然,以实现反射。
为此,您可能需要创建与“图像”相同的临时数据结构。
RGBTRIPLE temp[width];使用temp,您需要存储来自image的值,并交换它们。以下是您可以使用的设计模式。
//Pseudo code
1. //Loop though the row of an image (start from row = 0). We need to iterate every row.
2. // Loop through the height of an image to save current row in an image to the temp.
3. // Swap the values in image by using the temp. 如果你想过滤这个一维图像下面。
val1, val2, valn, val3, val4反光滤光器之后,会是这样的。
val4, val3, valn, val2, val1https://stackoverflow.com/questions/61154305
复制相似问题