【发布时间】:2020-06-05 07:57:22
【问题描述】:
我花了两天时间尝试纠正我的函数以模糊给定图像,但尽管进行了广泛的校对,但它现在仅适用于极端情况。对于其余部分,它会在 RGB 值中产生 2-20+ 的差异。
该任务是哈佛 CS50 课程的一部分(有关 pset4 https://cs50.harvard.edu/x/2020/psets/4/filter/less/ 的更多信息)。
我已经阅读了我可以在网上找到的所有内容并尝试使用这些技巧,例如将新的 RGB 值与浮点数相除、将结果直接复制回原始图像、调整 if 条件,但这并没有帮助,我仍然有不知道出了什么问题。非常感谢您的帮助,谢谢!
// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
float new_red, new_blue, new_green;
new_red = new_blue = new_green = 0;
int count = 0;
// Copy the image
RGBTRIPLE temp[height][width];
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
temp[i][j] = image[i][j];
}
}
// Loop through height
for (int i = 0; i < height; i++)
{
// Loop through width
for (int j = 0; j < width; j++)
{
// Loop through rows around a pixel
for (int k = -1; k <= 1; k++)
{
// Loop through columns around a pixel
for (int m = -1; m <= 1; m++)
{
if (i + k >= 0 && i + k < height && j + m >= 0 && j + m < width)
{
count++;
new_red += temp[i + k][j + m].rgbtRed;
new_blue += temp[i + k][j + m].rgbtBlue;
new_green += temp[i + k][j + m].rgbtGreen;
}
}
}
temp[i][j].rgbtBlue = round(new_blue / count);
temp[i][j].rgbtRed = round(new_red / count);
temp[i][j].rgbtGreen = round(new_green / count);
}
}
// Copy the blurred image to original file
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
image[i][j] = temp[i][j];
}
}
return;
}
【问题讨论】: