图像模糊的CS50 Pset4问题

时间:2020-02-18 16:16:33

标签: c cs50 bmp

已经在这个问题上停留了一段时间了,我对为什么我的代码没有按预期进行编译感到非常困惑。我应该将其周围像素的5号像素的颜色值进行平均,以使生成的图像看起来模糊。这是我的代码:

    // Blur image
    // 1 | 2 | 3
    // -   -   -
    // 4 | 5 | 6
    // -   -   -
    // 7 | 8 | 9
    //assuming i is 5
    void blur(int height, int width, RGBTRIPLE image[height][width])
    {
        RGBTRIPLE copy[height][width];
        for (int i = 0; i < height; i++)
        {
            for (int j = 0; j < width; j++)
            {
                copy[i][j] = image[i][j];
            }
        }
        for (int i = 0; i < height; i++)
        {
            for (int j = 0; j < width; j++)
            {
                int red = 0;
                int green = 0;
                int blue = 0;
                float counter = 0;

                // 5
                red += copy[i][j].rgbtRed;
                green += copy[i][j].rgbtGreen;
                blue += copy[i][j].rgbtBlue;
                counter++;

                //1
                if (i - 1 > -1 && j - 1 > -1)
                {
                    red += copy[i - 1][j].rgbtRed;
                    green += copy[i - 1][j].rgbtGreen;
                    blue += copy[i - 1][j].rgbtBlue;
                    counter++;
                }
                // 2
                if (i - 1 > -1)
                {
                    red += copy[i - 1][j].rgbtRed;
                    green += copy[i - 1][j].rgbtGreen;
                    blue += copy[i - 1][j].rgbtBlue;
                    counter++;
                }
                // 3
                if (i - 1 > -1 && j + 1 < height)
                {
                    red += copy[i - 1][j + 1].rgbtRed;
                    green += copy[i - 1][j + 1].rgbtGreen;
                    blue += copy[i - 1][j + 1].rgbtBlue;
                    counter++;
                }
                // 4
                if (j - 1 > -1)
                {
                    red += copy[i][j - 1].rgbtRed;
                    green += copy[i][j - 1].rgbtGreen;
                    blue += copy[i][j - 1].rgbtBlue;
                    counter++;
                }
                // 6
                if (j + 1 < width)
                {
                    red += copy[i][j + 1].rgbtRed;
                    green += copy[i][j + 1].rgbtGreen;
                    blue += copy[i][j + 1].rgbtBlue;
                    counter++;
                }
                // 7
                if (i + 1 < height && j - 1 > -1)
                {
                    red += copy[i + 1][j - 1].rgbtRed;
                    green += copy[i + 1][j - 1].rgbtGreen;
                    blue += copy[i + 1][j - 1].rgbtBlue;
                    counter++;
                }
                // 8
                if (i + 1 < height)
                {
                    red += copy[i + 1][j].rgbtRed;
                    green += copy[i + 1][j].rgbtGreen;
                    blue += copy[i + 1][j].rgbtBlue;
                    counter++;
                }
                // 9
                if (i + 1 < height && j + 1 < width)
                {
                    red += copy[i + 1][j + 1].rgbtRed;
                    green += copy[i + 1][j + 1].rgbtGreen;
                    blue += copy[i + 1][j + 1].rgbtBlue;
                    counter++;
                }

                image[i][j].rgbtRed = round(red/counter);
                image[i][j].rgbtGreen = round(green/counter);
                image[i][j].rgbtBlue = round(blue/counter);
            }
        }
        return;
    }

我检查从中心像素开始的位置,并检查周围像素是否存在,并相应地使用其周围像素的值来平均中心像素的值。我的代码可以正常编译,并且在我运行它时似乎可以正常工作。当我尝试运行debug50时;当我进行一些手动测试时,所有的值加起来。但是,当我尝试实现Check50时,会显示此错误消息

expected "127 140 149\n", not "130 143 153\n"
Log
testing with sample 3x3 image
first row: (10, 20, 30), (40, 50, 60), (70, 80, 90)
second row: (110, 130, 140), (120, 140, 150), (130, 150, 160)
third row: (200, 210, 220), (220, 230, 240), (240, 250, 255)
running ./testing 3 0...
checking for output "127 140 149\n"...

Expected Output:
127 140 149
Actual Output:
130 143 153

不太确定我的代码出了什么问题。任何建议将不胜感激!

1 个答案:

答案 0 :(得分:0)

                //1
                if (i - 1 > -1 && j - 1 > -1)
                {
                    red += copy[i - 1][j].rgbtRed;
                    green += copy[i - 1][j].rgbtGreen;
                    blue += copy[i - 1][j].rgbtBlue;
                    counter++;
                }

您使用的是“ j”,而应该使用的是“ j-1”。应该是这样:

                //1
                if (i - 1 > -1 && j - 1 > -1)
                {
                    red += copy[i - 1][j - 1].rgbtRed;
                    green += copy[i - 1][j - 1].rgbtGreen;
                    blue += copy[i - 1][j - 1].rgbtBlue;
                    counter++;
                }