CS50 pset4模糊(如何解决)
每次都被困在模糊中最长时间(甚至比潮人还要多),我不知道我的代码出了什么问题。当我执行 Check50 时,它会返回相同的图像。甚至尝试 malloc 但不知道为什么它不起作用。 这是我的代码,
// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
int sumRed = 0;
int sumBlue = 0;
int sumGreen = 0;
RGBTRIPLE temp[height][width];
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
// discuss exceptions
//4 corners
if (i == 0 && j == 0)
{
sumRed = image[0][0].rgbtRed + image[0][1].rgbtRed + image[1][0].rgbtRed;
sumBlue = image[0][0].rgbtBlue + image[0][1].rgbtBlue + image[1][0].rgbtBlue;
sumGreen = image[0][0].rgbtGreen + image[0][1].rgbtGreen + image[1][0].rgbtGreen;
temp[0][0].rgbtRed = round(sumRed/3);
temp[0][0].rgbtBlue = round(sumBlue/3);
temp[0][0].rgbtGreen = round(sumGreen/3);
}
else if (i == height - 1 && j == 0)
{
sumRed = image[height - 1][0].rgbtRed + image[height - 2][0].rgbtRed + image[height - 1][1].rgbtRed;
sumBlue = image[height - 1][0].rgbtBlue + image[height - 2][0].rgbtBlue + image[height - 1][1].rgbtBlue;
sumGreen = image[height - 1][0].rgbtGreen + image[height - 2][0].rgbtGreen + image[height - 1][1].rgbtGreen;
temp[height - 1][0].rgbtRed = round(sumRed/3);
temp[height - 1][0].rgbtBlue = round(sumBlue/3);
temp[height - 1][0].rgbtGreen = round(sumGreen/3);
}
else if ( i == 0 && j == width - 1)
{
sumRed = image[0][width - 1].rgbtRed + image[0][width - 2].rgbtRed + image[1][width - 1].rgbtRed;
sumBlue = image[0][width - 1].rgbtBlue+ image[0][width - 2].rgbtBlue + image[1][width - 1].rgbtBlue;
sumGreen = image[0][width - 1].rgbtGreen + image[0][width - 2].rgbtGreen + image[1][width - 1].rgbtGreen;
temp[0][width - 1].rgbtRed = round(sumRed/3);
temp[0][width - 1].rgbtBlue = round(sumBlue/3);
temp[0][width - 1].rgbtGreen = round(sumGreen/3);
}
else if ( i == height - 1 && j == width - 1)
{
sumRed = image[height - 1][width - 1].rgbtRed + image[height - 1][width - 2].rgbtRed + image[height - 2][width - 1].rgbtRed;
sumBlue = image[height - 1][width - 1].rgbtBlue+ image[height - 1][width - 2].rgbtBlue + image[height - 2][width - 1].rgbtBlue;
sumGreen = image[height - 1][width - 1].rgbtGreen + image[height - 1][width - 2].rgbtGreen + image[height - 2][width - 1].rgbtGreen;
temp[height - 1][width - 1].rgbtRed = round(sumRed/3);
temp[height - 1][width - 1].rgbtBlue = round(sumBlue/3);
temp[height - 1][width - 1].rgbtGreen = round(sumGreen/3);
}
//4 sides
else if (i == 0 && (j != 0) && (j != width -1)) // (top row)
{
for (int a = i; a < i + 2 ; a++)
{
for (int b = j - 1; b < j + 2 ; b++)
{
sumRed += image[a][b].rgbtRed;
sumBlue += image[a][b].rgbtBlue;
sumGreen += image[a][b].rgbtGreen;
temp[i][j].rgbtRed = round(sumRed/6);
temp[i][j].rgbtBlue = round(sumBlue/6);
temp[i][j].rgbtGreen = round(sumGreen/6);
}
}
}
else if (i == height - 1 && (j != 0) && (j != width -1)) // (bottom row)
{
for (int a = i - 1; a < i + 1 ; a++)
{
for (int b = j - 1; b < j + 2 ; b++)
{
sumRed += image[a][b].rgbtRed;
sumBlue += image[a][b].rgbtBlue;
sumGreen += image[a][b].rgbtGreen;
temp[i][j].rgbtRed = round(sumRed/6);
temp[i][j].rgbtBlue = round(sumBlue/6);
temp[i][j].rgbtGreen = round(sumGreen/6);
}
}
}
else if (j == 0 && (i != 0) && (i != height -1)) // (left column)
{
for (int a = i - 1; a < i + 2 ; a++)
{
for (int b = j; b < j + 2 ; b++)
{
sumRed += image[a][b].rgbtRed;
sumBlue += image[a][b].rgbtBlue;
sumGreen += image[a][b].rgbtGreen;
temp[i][j].rgbtRed = round(sumRed/6);
temp[i][j].rgbtBlue = round(sumBlue/6);
temp[i][j].rgbtGreen = round(sumGreen/6);
}
}
}
else if (j == width - 1 && (i != 0) && (i != height - 1)) // (right column)
{
for (int a = i - 1; a < i + 2 ; a++)
{
for (int b = j - 1; b < j + 1 ; b++)
{
sumRed += image[a][b].rgbtRed;
sumBlue += image[a][b].rgbtBlue;
sumGreen += image[a][b].rgbtGreen;
temp[i][j].rgbtRed = round(sumRed/6);
temp[i][j].rgbtBlue = round(sumBlue/6);
temp[i][j].rgbtGreen = round(sumGreen/6);
}
}
}
else
{
// Add everything around a pixel (9)
for (int a = i - 1; a < i + 2 ; a++)
{
for (int b = j - 1; b < j + 2 ; b++)
{
sumRed += image[a][b].rgbtRed;
sumBlue += image[a][b].rgbtBlue;
sumGreen += image[a][b].rgbtGreen;
temp[i][j].rgbtRed = round(sumRed/9);
temp[i][j].rgbtBlue = round(sumBlue/9);
temp[i][j].rgbtGreen = round(sumGreen/9);
}
}
}
}
}
return;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
image[i][j].rgbtRed = temp[i][j].rgbtRed;
image[i][j].rgbtGreen = temp[i][j].rgbtGreen;
image[i][j].rgbtBlue = temp[i][j].rgbtBlue;
}
}
}
我知道它有点乏味+有一些方法可以改进它,但是考虑到我解决这个 pset 的逻辑,是否有任何错误?提前感谢大家!
been stuck at blur for the longest time every (even more than tideman) and i do not know what is wrong with my code. It is returning the same image back to me when i do Check50. Even tried to malloc but not sure why it is not working.
Here is my code
// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
int sumRed = 0;
int sumBlue = 0;
int sumGreen = 0;
RGBTRIPLE temp[height][width];
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
// discuss exceptions
//4 corners
if (i == 0 && j == 0)
{
sumRed = image[0][0].rgbtRed + image[0][1].rgbtRed + image[1][0].rgbtRed;
sumBlue = image[0][0].rgbtBlue + image[0][1].rgbtBlue + image[1][0].rgbtBlue;
sumGreen = image[0][0].rgbtGreen + image[0][1].rgbtGreen + image[1][0].rgbtGreen;
temp[0][0].rgbtRed = round(sumRed/3);
temp[0][0].rgbtBlue = round(sumBlue/3);
temp[0][0].rgbtGreen = round(sumGreen/3);
}
else if (i == height - 1 && j == 0)
{
sumRed = image[height - 1][0].rgbtRed + image[height - 2][0].rgbtRed + image[height - 1][1].rgbtRed;
sumBlue = image[height - 1][0].rgbtBlue + image[height - 2][0].rgbtBlue + image[height - 1][1].rgbtBlue;
sumGreen = image[height - 1][0].rgbtGreen + image[height - 2][0].rgbtGreen + image[height - 1][1].rgbtGreen;
temp[height - 1][0].rgbtRed = round(sumRed/3);
temp[height - 1][0].rgbtBlue = round(sumBlue/3);
temp[height - 1][0].rgbtGreen = round(sumGreen/3);
}
else if ( i == 0 && j == width - 1)
{
sumRed = image[0][width - 1].rgbtRed + image[0][width - 2].rgbtRed + image[1][width - 1].rgbtRed;
sumBlue = image[0][width - 1].rgbtBlue+ image[0][width - 2].rgbtBlue + image[1][width - 1].rgbtBlue;
sumGreen = image[0][width - 1].rgbtGreen + image[0][width - 2].rgbtGreen + image[1][width - 1].rgbtGreen;
temp[0][width - 1].rgbtRed = round(sumRed/3);
temp[0][width - 1].rgbtBlue = round(sumBlue/3);
temp[0][width - 1].rgbtGreen = round(sumGreen/3);
}
else if ( i == height - 1 && j == width - 1)
{
sumRed = image[height - 1][width - 1].rgbtRed + image[height - 1][width - 2].rgbtRed + image[height - 2][width - 1].rgbtRed;
sumBlue = image[height - 1][width - 1].rgbtBlue+ image[height - 1][width - 2].rgbtBlue + image[height - 2][width - 1].rgbtBlue;
sumGreen = image[height - 1][width - 1].rgbtGreen + image[height - 1][width - 2].rgbtGreen + image[height - 2][width - 1].rgbtGreen;
temp[height - 1][width - 1].rgbtRed = round(sumRed/3);
temp[height - 1][width - 1].rgbtBlue = round(sumBlue/3);
temp[height - 1][width - 1].rgbtGreen = round(sumGreen/3);
}
//4 sides
else if (i == 0 && (j != 0) && (j != width -1)) // (top row)
{
for (int a = i; a < i + 2 ; a++)
{
for (int b = j - 1; b < j + 2 ; b++)
{
sumRed += image[a][b].rgbtRed;
sumBlue += image[a][b].rgbtBlue;
sumGreen += image[a][b].rgbtGreen;
temp[i][j].rgbtRed = round(sumRed/6);
temp[i][j].rgbtBlue = round(sumBlue/6);
temp[i][j].rgbtGreen = round(sumGreen/6);
}
}
}
else if (i == height - 1 && (j != 0) && (j != width -1)) // (bottom row)
{
for (int a = i - 1; a < i + 1 ; a++)
{
for (int b = j - 1; b < j + 2 ; b++)
{
sumRed += image[a][b].rgbtRed;
sumBlue += image[a][b].rgbtBlue;
sumGreen += image[a][b].rgbtGreen;
temp[i][j].rgbtRed = round(sumRed/6);
temp[i][j].rgbtBlue = round(sumBlue/6);
temp[i][j].rgbtGreen = round(sumGreen/6);
}
}
}
else if (j == 0 && (i != 0) && (i != height -1)) // (left column)
{
for (int a = i - 1; a < i + 2 ; a++)
{
for (int b = j; b < j + 2 ; b++)
{
sumRed += image[a][b].rgbtRed;
sumBlue += image[a][b].rgbtBlue;
sumGreen += image[a][b].rgbtGreen;
temp[i][j].rgbtRed = round(sumRed/6);
temp[i][j].rgbtBlue = round(sumBlue/6);
temp[i][j].rgbtGreen = round(sumGreen/6);
}
}
}
else if (j == width - 1 && (i != 0) && (i != height - 1)) // (right column)
{
for (int a = i - 1; a < i + 2 ; a++)
{
for (int b = j - 1; b < j + 1 ; b++)
{
sumRed += image[a][b].rgbtRed;
sumBlue += image[a][b].rgbtBlue;
sumGreen += image[a][b].rgbtGreen;
temp[i][j].rgbtRed = round(sumRed/6);
temp[i][j].rgbtBlue = round(sumBlue/6);
temp[i][j].rgbtGreen = round(sumGreen/6);
}
}
}
else
{
// Add everything around a pixel (9)
for (int a = i - 1; a < i + 2 ; a++)
{
for (int b = j - 1; b < j + 2 ; b++)
{
sumRed += image[a][b].rgbtRed;
sumBlue += image[a][b].rgbtBlue;
sumGreen += image[a][b].rgbtGreen;
temp[i][j].rgbtRed = round(sumRed/9);
temp[i][j].rgbtBlue = round(sumBlue/9);
temp[i][j].rgbtGreen = round(sumGreen/9);
}
}
}
}
}
return;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
image[i][j].rgbtRed = temp[i][j].rgbtRed;
image[i][j].rgbtGreen = temp[i][j].rgbtGreen;
image[i][j].rgbtBlue = temp[i][j].rgbtBlue;
}
}
}
I understand that it is abit tedious + there are ways to improve it, but given my logic of solving this pset, are there any errors? Thanks everyone in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论