【发布时间】:2020-07-09 02:03:26
【问题描述】:
我正在处理 pset4 中的过滤器(更少),并且我已经完全检查了所有其他功能,但是当我对此运行 check50 时,我得到以下模糊响应:
:( blur correctly filters middle pixel
expected "127 140 149\n", not "120 140 150\n"
:( blur correctly filters pixel on edge
expected "80 95 105\n", not "40 50 60\n"
:( blur correctly filters pixel in corner
expected "70 85 95\n", not "10 20 30\n"
:( blur correctly filters 3x3 image
expected "70 85 95\n80 9...", not "10 20 30\n40 5..."
:( blur correctly filters 4x4 image
expected "70 85 95\n80 9...", not "10 20 30\n40 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/
【问题讨论】: