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

时间:2020-07-09 02:03:26

标签: c filter blur cs50

我正在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/

1 个答案:

答案 0 :(得分:0)

因此,假设我正确阅读了这篇文章,我实际上遇到了同样的问题。从本质上讲,问题在于RGBTRIPLE结构没有存储三个整数,而是存储了三个字节,每种三种颜色各一个。因此,当您添加数字以获得大于255的总数时,它会“重置”为0。我将使用三个整数,而不是使用RGBTRIPLE作为临时文件。

例如:

int red;
int green;
int blue;

然后仅增加这些数字并对其进行操作。让我知道这是否不能解决问题,我将进行更深入的研究,但这似乎是问题所在。