CS50模糊功能即使图像模糊也无法通过check50

时间:2020-06-05 07:09:12

标签: c pixel blur cs50

我正在研究CS50问题集,其中我需要对图像的每个像素执行box blur。尽管我的代码有点多余,但由于我为像素的特殊情况(如边和角)创建了8条if语句,但它会按预期使图像模糊,因此我不确定如何解决该问题。这是错误代码:

:( blur correctly filters middle pixel
    expected "127 140 149\n", not "145 160 169\n"
:( blur correctly filters pixel on edge
    expected "80 95 105\n", not "90 106 116\n"
:) blur correctly filters pixel in corner
:( blur correctly filters 3x3 image
    expected "70 85 95\n80 9...", not "70 85 95\n90 1..."
:( blur correctly filters 4x4 image
    expected "70 85 95\n80 9...", not "70 85 95\n90 1..."

还有更详细的错误代码here(仅查看“模糊”错误)

下面是我的代码:

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
    int blue;
    int green;
    int red;
    int counter = 0;

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            if (i == 0 && j == 0)
            {
                // top left corner
                blue  = (image[i][j].rgbtBlue  + image[i + 1][j].rgbtBlue  + image[i][j+1].rgbtBlue  + image[i + 1][j+1].rgbtBlue);
                green = (image[i][j].rgbtGreen + image[i + 1][j].rgbtGreen + image[i][j+1].rgbtGreen + image[i + 1][j+1].rgbtGreen);
                red   = (image[i][j].rgbtRed   + image[i + 1][j].rgbtRed   + image[i][j+1].rgbtRed   + image[i + 1][j+1].rgbtRed);
                counter = 4;
            }

            else if (i == 0 && j == (width - 1))
            {
                // top right corner
                blue  = (image[i][j].rgbtBlue  + image[i + 1][j].rgbtBlue  + image[i][j-1].rgbtBlue  + image[i + 1][j-1].rgbtBlue);
                green = (image[i][j].rgbtGreen + image[i + 1][j].rgbtGreen + image[i][j-1].rgbtGreen + image[i + 1][j-1].rgbtGreen);
                red   = (image[i][j].rgbtRed   + image[i + 1][j].rgbtRed   + image[i][j-1].rgbtRed   + image[i + 1][j-1].rgbtRed);
                counter = 4;
            }

            else if (i == 0 && (j != 0 || j != (width - 1)))
            {
                // top edge
                blue  = (image[i][j - 1].rgbtBlue  + image[i][j].rgbtBlue  + image[i][j + 1].rgbtBlue  + image[i + 1][j].rgbtBlue  + image[i+1][j - 1].rgbtBlue  + image[i + 1][j + 1].rgbtBlue);
                green = (image[i][j - 1].rgbtGreen + image[i][j].rgbtGreen + image[i][j + 1].rgbtGreen + image[i + 1][j].rgbtGreen + image[i+1][j - 1].rgbtGreen + image[i + 1][j + 1].rgbtGreen);
                red   = (image[i][j - 1].rgbtRed   + image[i][j].rgbtRed   + image[i][j + 1].rgbtRed   + image[i + 1][j].rgbtRed   + image[i+1][j - 1].rgbtRed   + image[i + 1][j + 1].rgbtRed);
                counter = 6;
            }

            else if (i == (height - 1) && j == 0)
            {
                // bottom left corner
                blue  = (image[i-1][j + 1].rgbtBlue  + image[i][j + 1].rgbtBlue  + image[i - 1][j].rgbtBlue  + image[i][j].rgbtBlue);
                green = (image[i-1][j + 1].rgbtGreen + image[i][j + 1].rgbtGreen + image[i - 1][j].rgbtGreen + image[i][j].rgbtGreen);
                red   = (image[i-1][j + 1].rgbtRed   + image[i][j + 1].rgbtRed   + image[i - 1][j].rgbtRed   + image[i][j].rgbtRed);
                counter = 4;
            }

            else if (i == (height - 1) && j == (width - 1))
            {
                // bottom right corner
                blue  = (image[i][j].rgbtBlue  + image[i - 1][j].rgbtBlue  + image[i][j-1].rgbtBlue  + image[i - 1][j-1].rgbtBlue);
                green = (image[i][j].rgbtGreen + image[i - 1][j].rgbtGreen + image[i][j-1].rgbtGreen + image[i - 1][j-1].rgbtGreen);
                red   = (image[i][j].rgbtRed   + image[i - 1][j].rgbtRed   + image[i][j-1].rgbtRed   + image[i - 1][j-1].rgbtRed);
                counter = 4;
            }

            else if (i == (height - 1) && (j != 0 || j != (width - 1)))
            {
                // bottom edge
                blue  = (image[i][j].rgbtBlue  + image[i][j - 1].rgbtBlue  + image[i][j + 1].rgbtBlue  + image[i - 1][j].rgbtBlue  + image[i-1][j - 1].rgbtBlue  + image[i - 1][j + 1].rgbtBlue);
                green = (image[i][j].rgbtGreen + image[i][j - 1].rgbtGreen + image[i][j + 1].rgbtGreen + image[i - 1][j].rgbtGreen + image[i-1][j - 1].rgbtGreen + image[i - 1][j + 1].rgbtGreen);
                red   = (image[i][j].rgbtRed   + image[i][j - 1].rgbtRed   + image[i][j + 1].rgbtRed   + image[i - 1][j].rgbtRed   + image[i-1][j - 1].rgbtRed   + image[i - 1][j + 1].rgbtRed);
                counter = 6;
            }

            else if (j == 0 && (i != 0 || i != (height - 1)))
            {
                // left edge
                blue  = (image[i][j].rgbtBlue  + image[i - 1][j].rgbtBlue  + image[i+1][j].rgbtBlue  + image[i][j + 1].rgbtBlue  + image[i-1][j + 1].rgbtBlue  + image[i + 1][j + 1].rgbtBlue);
                green = (image[i][j].rgbtGreen + image[i - 1][j].rgbtGreen + image[i+1][j].rgbtGreen + image[i][j + 1].rgbtGreen + image[i-1][j + 1].rgbtGreen + image[i + 1][j + 1].rgbtGreen);
                red   = (image[i][j].rgbtRed   + image[i - 1][j].rgbtRed   + image[i+1][j].rgbtRed   + image[i][j + 1].rgbtRed   + image[i-1][j + 1].rgbtRed   + image[i + 1][j + 1].rgbtRed);
                counter = 6;
            }

            else if (j == (width - 1) && (i != 0 || i != (height - 1)))
            {
                // right edge
                blue  = (image[i][j].rgbtBlue  + image[i-1][j].rgbtBlue  + image[i + 1][j].rgbtBlue  + image[i][j - 1].rgbtBlue  + image[i + 1][j-1].rgbtBlue  + image[i-1][j - 1].rgbtBlue);
                green = (image[i][j].rgbtGreen + image[i-1][j].rgbtGreen + image[i + 1][j].rgbtGreen + image[i][j - 1].rgbtGreen + image[i + 1][j-1].rgbtGreen + image[i-1][j - 1].rgbtGreen);
                red   = (image[i][j].rgbtRed   + image[i-1][j].rgbtRed   + image[i + 1][j].rgbtRed   + image[i][j - 1].rgbtRed   + image[i + 1][j-1].rgbtRed   + image[i-1][j - 1].rgbtRed);
                counter = 6;
            }

            else
            {
                blue  = (image[i][j].rgbtBlue  + image[i - 1][j].rgbtBlue  + image[i + 1][j].rgbtBlue  + image[i][j - 1].rgbtBlue  + image[i][j + 1].rgbtBlue  + image[i - 1][j + 1].rgbtBlue  + image[i-1][j - 1].rgbtBlue  + image[i + 1][j - 1].rgbtBlue  + image[i + 1][j + 1].rgbtBlue);
                green = (image[i][j].rgbtGreen + image[i - 1][j].rgbtGreen + image[i + 1][j].rgbtGreen + image[i][j - 1].rgbtGreen + image[i][j + 1].rgbtGreen + image[i - 1][j + 1].rgbtGreen + image[i-1][j - 1].rgbtGreen + image[i + 1][j - 1].rgbtGreen + image[i + 1][j + 1].rgbtGreen);
                red   = (image[i][j].rgbtRed   + image[i - 1][j].rgbtRed   + image[i + 1][j].rgbtRed   + image[i][j - 1].rgbtRed   + image[i][j + 1].rgbtRed   + image[i - 1][j + 1].rgbtRed   + image[i-1][j - 1].rgbtRed   + image[i + 1][j - 1].rgbtRed   + image[i + 1][j + 1].rgbtRed);
                counter = 9;
            }

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

看看其他人对这个问题的回答,我发现有些人做了第二个嵌套的for循环来存储像素的原始值。我最初尝试实现此功能,但最终导致问题,因此我认为没有必要。这是我的代码的问题吗?如果是,我该如何在我的代码中正确实现“原始值”像素?如果没有,有人知道这个问题吗?预先感谢。

1 个答案:

答案 0 :(得分:2)

在将模糊功能应用于像素时,您正在修改image。这意味着当您修改几个像素时,相邻像素的模糊值将使用“模糊的像素值”进行计算。错了所有计算必须在原始图像像素值内完成。为此,您应该在开头创建image的副本(例如temp),并在具有不变像素值的temp图像内进行所有计算。

将此添加到代码的开头;

RGBTRIPLE temp[height][width]; // create a temporary array to store a duplicate of image.

// save a new copy of image as temp per color.
for (int i = 0; i < height; i++) //Loop for height of image.
{
    for (int j = 0; j < width; j++) //Loop for width of image and save color values in temp.
    {
        temp[i][j] = image[i][j];
    }
}

在计算中将image替换为temp(末尾的赋值除外)。