[英]CS50 pset4 Filter (less comfortable) - blur function not working
I am working on the Filter (less comfortable) problem in CS50 (PSET4) and am stuck on the blur function.我正在研究 CS50 (PSET4) 中的过滤器(不太舒服)问题,并且被困在模糊功能上。 I am getting an error on check50 for all checkpoints, however, I am unable to figure out where I am going wrong.
我在所有检查点的 check50 上都收到错误消息,但是,我无法弄清楚我哪里出错了。 Will appreciate if someone can help me on this.
如果有人可以帮助我,我将不胜感激。 Thanks.
谢谢。 This is what I've written:
这是我写的:
// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE sum;
RGBTRIPLE image_copy[height][width];
//creating copy of image
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
image_copy[i][j].rgbtBlue = image[i][j].rgbtBlue;
image_copy[i][j].rgbtGreen = image[i][j].rgbtGreen;
image_copy[i][j].rgbtRed = image[i][j].rgbtRed;
}
}
//iterating over each pixel
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
sum.rgbtBlue = 0;
sum.rgbtGreen = 0;
sum.rgbtRed = 0;
int count = 0;
//setting loops for 3*3 grid
for (int k = i - 1; k <= i + 1; k++)
{
if(k >= 0 && k < height)
{
for (int l = j - 1; l <= j + 1; l++)
{
if (l >= 0 && l < width)
{
sum.rgbtBlue = sum.rgbtBlue + image_copy[k][l].rgbtBlue;
sum.rgbtGreen = sum.rgbtGreen + image_copy[k][l].rgbtGreen;
sum.rgbtRed = sum.rgbtRed + image_copy[k][l].rgbtRed;
count++;
}
}
}
}
// calculating average and updating original image
image[i][j].rgbtBlue = round (sum.rgbtBlue / count);
image[i][j].rgbtGreen = round (sum.rgbtGreen / count);
image[i][j].rgbtRed = round (sum.rgbtRed /count);
}
}
return;
}
sum.rgbtBlue / count
is integer division , so it has already been rounded down (truncated) before passing to round()
. sum.rgbtBlue / count
是整数除法,因此在传递给round()
之前已经四舍五入(截断round()
。
Not only that but RGBTRIPLE sum
cannot values of 765
(3 * 255).不仅如此,
RGBTRIPLE sum
也不能是765
(3 * 255) 的值。
Sum the pixels into three int
or float
variables, for example例如,将像素求和为三个
int
或float
变量
int blue = 0; // etc.
//...
image[i][j].rgbtBlue = round ((float)blue / count); // cast before division
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.