CS50 Pset4 过滤器(不太舒服)模糊功能算法问题
Posted
技术标签:
【中文标题】CS50 Pset4 过滤器(不太舒服)模糊功能算法问题【英文标题】:CS50 Pset4 Filter (less comfortable) blur function Algorithmic Issue 【发布时间】:2020-07-27 00:41:30 【问题描述】:info about this task
当我尝试实现模糊功能时,它在图片上对我来说效果很好,但 check50(cs50 测试程序)对我的输出发出警告。
这是我的代码
void blur(int height, int width, RGBTRIPLE image[height][width])
float average_red = 0;
float average_green = 0;
float average_blue = 0;
float count = 0;
for (int i = 0; i < height; i++)
for (int j = 0; j < width; j++)
// reset values
average_red = 0;
average_blue = 0;
average_green = 0;
count = 0;
// look for around a pixel 3x3 box
// column
for (int k = i - 1; k < i + 2; k++)
// row
for (int l = j - 1; l < j + 2; l++)
// if pixel on the top
if (k == -1)
// it skips a column because it is out of the border
break;
// if pixel is on the left side
if (l == -1)
// skips a row otherwise it is out of the border
continue;
// if pixel passes the bottom
if (k >= height)
break;
// if pixels passes the right side
if (l >= width)
continue;
// everything else
else
average_red += image[k][l].rgbtRed;
average_green += image[k][l].rgbtGreen;
average_blue += image[k][l].rgbtBlue;
count++;
average_red /= count;
average_green /= count;
average_blue /= count;
image[i][j].rgbtRed = round(average_red);
image[i][j].rgbtGreen = round(average_green);
image[i][j].rgbtBlue = round(average_blue);
return;
expected output vs my output is here(模糊函数在下面输出)
它在拐角处工作正常,但其他像素值非常接近正确的输出,但不一样。 任何帮助表示赞赏
【问题讨论】:
“关于此任务的信息”没有提到像素图的每一行必须包含字节数,使得总字节数是 4 的倍数 OT:关于如下语句:float average_red = 0;
这是试图将int
文字分配给float
变量。当您在启用警告的情况下编译时,这将导致编译器警告语句。建议:float average_red = 0.0f;
注意.
和尾随的f
,这使得文字成为float
贴出的代码无法编译!除其他问题外,它正在调用函数:round()
,但缺少所需的头文件:math.h
Makefile
缺少库引用 -lm
【参考方案1】:
代码需要考虑几个因素。建议:
如果结果行号是 (height-1) 则不计算该像素
如果生成的列号为 (width-1),则不计算该像素
因此,对于周围 8 个像素中的每一个,应用上述两个标准。
【讨论】:
【参考方案2】:我自己的问题解决了,还是谢谢你的帮助
问题在于我的总和计算。它将结果保留在 image[i][j] 中,然后再次使用该值。 我将图像值复制到一个新值并用于此。这是我的代码
void blur(int height, int width, RGBTRIPLE image[height][width])
// copy original value to a new value to keep changing values
RGBTRIPLE copy[height][width];
for (int i = 0; i < height; i++)
for (int j = 0; j < width; j++)
copy[i][j] = image[i][j];
float average_red = 0.0f;
float average_green = 0.0f;
float average_blue = 0.0f;
float count = 0;
for (int i = 0; i < height; i++)
for (int j = 0; j < width; j++)
// reset values
average_red = 0.0f;
average_blue = 0.0f;
average_green = 0.0f;
count = 0;
// look for around a pixel 3x3 box
// column
for (int k = i - 1; k < i + 2; k++)
// row
for (int l = j - 1; l < j + 2; l++)
// if pixel on the top
if (k == -1)
// it skips a column because it is out of the border
break;
// if pixel is on the left side
else if (l == -1)
// skips a row otherwise it is out of the border
continue;
// if pixel passes the bottom
else if (k >= height)
break;
// if pixels passes the right side
else if (l >= width)
continue;
// everything else
else
average_red += copy[k][l].rgbtRed;
average_green += copy[k][l].rgbtGreen;
average_blue += copy[k][l].rgbtBlue;
count++;
average_red /= count;
average_green /= count;
average_blue /= count;
image[i][j].rgbtRed = round(average_red);
image[i][j].rgbtGreen = round(average_green);
image[i][j].rgbtBlue = round(average_blue);
return;
【讨论】:
以上是关于CS50 Pset4 过滤器(不太舒服)模糊功能算法问题的主要内容,如果未能解决你的问题,请参考以下文章