c语言 - CS50 - pset4 滤镜的模糊功能只是向上计数



我正在pset4中处理filter(less(,我已经完全检查了其他所有函数,但当我对此运行check50时,我得到了以下模糊响应:

:( blur correctly filters middle pixel
expected "127 140 149n", not "120 140 150n"
:( blur correctly filters pixel on edge
expected "80 95 105n", not "40 50 60n"
:( blur correctly filters pixel in corner
expected "70 85 95n", not "10 20 30n"
:( blur correctly filters 3x3 image
expected "70 85 95n80 9...", not "10 20 30n40 5..."
:( blur correctly filters 4x4 image
expected "70 85 95n80 9...", not "10 20 30n40 5..."

第一个让我看起来像是在做某种舍入错误(尽管这是一个非常不一致的错误,先把第一个数字舍入,然后把最后一个数字舍入(,但随着图像越来越大,这些错误对我来说越来越陌生,直到我生成的最后一个数组:

10 20 30
40 50 60
70 80 90
100 110 120
110 130 140
120 140 150
130 150 160
140 160 170
195 204 213
205 214 223
225 234 243
245 254 253
50 28 90
0 0 0
255 255 255
85 85 85

我不知道我是怎么开始这么做的,但我一点也不喜欢。有人能告诉我我在这里做错了什么吗?非常感谢您抽出时间。代码如下:

void blur(int height, int width, RGBTRIPLE image[height][width])
{
// copy the image
// figure out blur data from the original picture and apply to the copy
RGBTRIPLE tempArray[height][width];
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
int validPixels = 0;
// cycle through area around a pixel
for (int blurRadiusX = -1; blurRadiusX > 1; blurRadiusX++)
{
for (int blurRadiusY = -1; blurRadiusY > 1; blurRadiusY++)
{
// check area around pixels to see if it's at a valid location
if ((i + blurRadiusX > 0) && (i + blurRadiusX < (height - 1)) && (j + blurRadiusY > 0) && (j + blurRadiusY < (width - 1)))
{
// an array adding all the values up, to be averaged out later
tempArray[i][j].rgbtRed = tempArray[i][j].rgbtRed + image[i + blurRadiusX][j + blurRadiusY].rgbtRed;
tempArray[i][j].rgbtGreen = tempArray[i][j].rgbtGreen + image[i + blurRadiusX][j + blurRadiusY].rgbtGreen;
tempArray[i][j].rgbtBlue = tempArray[i][j].rgbtBlue + image[i + blurRadiusX][j + blurRadiusY].rgbtBlue;
validPixels++;
}
}
// if there are enough bytes to add these correctly, add and then divide by valid pixels
// this should be applied to the image after tempArray is fully populated
image[i][j].rgbtRed = round(tempArray[i][j].rgbtRed / validPixels * 1.00);
image[i][j].rgbtGreen = round(tempArray[i][j].rgbtGreen / validPixels * 1.00);
image[i][j].rgbtBlue = round(tempArray[i][j].rgbtBlue / validPixels * 1.00);
}
}
}
return;
}

下面是CS50关于问题集的页面:https://cs50.harvard.edu/x/2020/psets/4/filter/less/

所以我实际上也遇到了同样的问题,假设我读对了。本质上,问题是RGBTRIPLE结构不是存储三个int,而是存储三个字节,三种颜色各一个。因此,当你加上数字得到一个大于255的总数时,它会"重置"为0。我会使用三个整数,而不是使用RGBTRIPLE作为临时值。

例如:

int red;
int green;
int blue;

然后只需增加这些数字并对其进行运算。如果这还不能解决问题,请告诉我,我会深入研究,但看起来这就是问题所在。

最新更新