【问题标题】:CS50 - pset4 filter's blur function is just counting upwardsCS50 - pset4 滤镜的模糊功能只是向上计数
【发布时间】: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/

【问题讨论】:

    标签: c filter blur cs50


    【解决方案1】:

    所以我实际上遇到了同样的问题,假设我没看错。本质上,问题在于 RGBTRIPLE 结构不是存储三个整数,而是存储三个字节,一种用于三种颜色中的每一种。因此,当您添加数字以获得大于 255 的总数时,它会“重置”为 0。我不会使用 RGBTRIPLE 作为临时值,而是使用三个整数。

    例如:

    int red;
    int green;
    int blue;
    

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

    【讨论】:

    • 不幸的是,这根本不会改变输出。但是当我检查时,我注意到模糊功能似乎根本没有改变输入图片。
    • 啊,好吧。我会告诉你,如果你使用 RGBTRIPLE 作为你的 temp,你会看到其他问题。
    • 这是一个非常好的观点......我有一些问题不记得在早期的问题中 BYTE 不仅仅是整数,但是当涉及到这个问题时,我对此有所了解。实际上,我最终放弃了这个版本,并试图重写整个函数以提高可读性;感谢您帮助我解决这个问题!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多