CS50 pset4模糊功能-无法弄清楚为什么测试在3x3和4x4图像上失败

时间:2020-04-21 11:27:02

标签: c cs50

我一直在研究CS50 pset4模糊功能的代码。 该函数假定要计算所有相邻像素的平均RGB值,然后将其应用于“中间”像素(因为该像素可以位于拐角,边缘或中间的任何位置)

在编译或运行代码时没有错误。 输出图像都显得模糊,但是当我针对check50运行代码时,它在两次测试中均失败:

:( blur correctly filters 3x3 image
    expected "70 85 95\n80 9...", not "70 85 95\n80 9..."
:( blur correctly filters 4x4 image
    expected "70 85 95\n80 9...", not "70 85 95\n80 9..."

其他测试通过了(测试边缘,拐角和中间的像素),所以我很难理解自己在做什么。

这是我的代码:

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
    // creating a blank copy
    RGBTRIPLE blured[height][width];

    // upper left corner (anchored)
    for (int i = 0; i < 1; i++)
    {
        for (int j = 0; j < 1; j++)
        {
            float trackerRed = 0, trackerGreen = 0, trackerBlue = 0;
            for (int k = i; k <= i + 1; k++)
            {
                for (int l = j; l <= j + 1; l++)
                {
                    trackerRed += (float)image[k][l].rgbtRed;
                    trackerGreen += (float)image[k][l].rgbtGreen;
                    trackerBlue += (float)image[k][l].rgbtBlue;
                }
            }
            blured[i][j].rgbtRed = round(trackerRed / 4);
            blured[i][j].rgbtGreen = round(trackerGreen / 4);
            blured[i][j].rgbtBlue = round(trackerBlue / 4);
        }
    }
    // top middle row
    for (int i = 0; i < 1; i++)
    {
        for (int j = 1; j < width - 1; j++)
        {
            float trackerRed = 0, trackerGreen = 0, trackerBlue = 0;
            for (int k = i; k <= i + 1; k++)
            {
                for (int l = j - 1; l <= j + 1; l++)
                {
                    trackerRed += (float)image[k][l].rgbtRed;
                    trackerGreen += (float)image[k][l].rgbtGreen;
                    trackerBlue += (float)image[k][l].rgbtBlue;
                }
            }
            blured[i][j].rgbtRed = round(trackerRed / 6);
            blured[i][j].rgbtGreen = round(trackerGreen / 6);
            blured[i][j].rgbtBlue = round(trackerBlue / 6);
        }
    }
    // upper right corner (anchored)
    for (int i = 0; i < 1; i++)
    {
        for (int j = width - 1; j < width; j++)
        {
            float trackerRed = 0, trackerGreen = 0, trackerBlue = 0;
            for (int k = i; k <= i + 1; k++)
            {
                for (int l = j - 1; l <= j; l++)
                {
                    trackerRed += (float)image[k][l].rgbtRed;
                    trackerGreen += (float)image[k][l].rgbtGreen;
                    trackerBlue += (float)image[k][l].rgbtBlue;
                }
            }
            blured[i][j].rgbtRed = round(trackerRed / 4);
            blured[i][j].rgbtGreen = round(trackerGreen / 4);
            blured[i][j].rgbtBlue = round(trackerBlue / 4);
        }
    }
    // left middle column
    for (int i = 1; i < height - 1; i++)
    {
        for (int j = 0; j < 1; j++)
        {
            float trackerRed = 0, trackerGreen = 0, trackerBlue = 0;
            for (int k = i - 1; k <= i + 1; k++)
            {
                for (int l = j; l <= j + 1; l++)
                {
                    trackerRed += (float)image[k][l].rgbtRed;
                    trackerGreen += (float)image[k][l].rgbtGreen;
                    trackerBlue += (float)image[k][l].rgbtBlue;
                }
            }
            blured[i][j].rgbtRed = round(trackerRed / 6);
            blured[i][j].rgbtGreen = round(trackerGreen / 6);
            blured[i][j].rgbtBlue = round(trackerBlue / 6);
        }
    }
    // center
    for (int i = 1; i < height - 1; i++)
    {
        for (int j = 1; j < width - 1; j++)
        {
            float trackerRed = 0, trackerGreen = 0, trackerBlue = 0;
            for (int k = i - 1; k <= i + 1; k++)
            {
                for (int l = j - 1; l <= j + 1; l++)
                {
                    trackerRed += (float)image[k][l].rgbtRed;
                    trackerGreen += (float)image[k][l].rgbtGreen;
                    trackerBlue += (float)image[k][l].rgbtBlue;
                }
            }
            blured[i][j].rgbtRed = round(trackerRed / 9);
            blured[i][j].rgbtGreen = round(trackerGreen / 9);
            blured[i][j].rgbtBlue = round(trackerBlue / 9);
        }
    }
    // right middle column
    for (int i = 1; i < height - 1; i++)
    {
        for (int j = width - 1; j < width; j++)
        {
            float trackerRed = 0, trackerGreen = 0, trackerBlue = 0;
            for (int k = i - 1; k <= i + 1; k++)
            {
                for (int l = j; l <= j; l++)
                {
                    trackerRed += (float)image[k][l].rgbtRed;
                    trackerGreen += (float)image[k][l].rgbtGreen;
                    trackerBlue += (float)image[k][l].rgbtBlue;
                }
            }
            blured[i][j].rgbtRed = round(trackerRed / 6);
            blured[i][j].rgbtGreen = round(trackerGreen / 6);
            blured[i][j].rgbtBlue = round(trackerBlue / 6);
        }
    }
    // lower left corner (anchored)
    for (int i = height - 1; i < height; i++)
    {
        for (int j = 0; j < height; j++)
        {
            float trackerRed = 0, trackerGreen = 0, trackerBlue = 0;
            for (int k = i - 1; k <= i; k++)
            {
                for (int l = j; l <= j + 1; l++)
                {
                    trackerRed += (float)image[k][l].rgbtRed;
                    trackerGreen += (float)image[k][l].rgbtGreen;
                    trackerBlue += (float)image[k][l].rgbtBlue;
                }
            }
            blured[i][j].rgbtRed = round(trackerRed / 4);
            blured[i][j].rgbtGreen = round(trackerGreen / 4);
            blured[i][j].rgbtBlue = round(trackerBlue / 4);
        }
    }
    // bottom middle row
    for (int i = height - 1; i < height; i++)
    {
        for (int j = 1; j < width - 1; j++)
        {
            float trackerRed = 0, trackerGreen = 0, trackerBlue = 0;
            for (int k = i - 1; k <= i; k++)
            {
                for (int l = j - 1; l <= j + 1; l++)
                {
                    trackerRed += (float)image[k][l].rgbtRed;
                    trackerGreen += (float)image[k][l].rgbtGreen;
                    trackerBlue += (float)image[k][l].rgbtBlue;
                }
            }
            blured[i][j].rgbtRed = round(trackerRed / 6);
            blured[i][j].rgbtGreen = round(trackerGreen / 6);
            blured[i][j].rgbtBlue = round(trackerBlue / 6);
        }
    }
    // lower right corner (anchored)
    for (int i = height - 1; i < height; i++)
    {
        for (int j = width - 1; j < width; j++)
        {
            float trackerRed = 0, trackerGreen = 0, trackerBlue = 0;
            for (int k = i - 1; k <= i; k++)
            {
                for (int l = j - 1; l <= j; l++)
                {
                    trackerRed += (float)image[k][l].rgbtRed;
                    trackerGreen += (float)image[k][l].rgbtGreen;
                    trackerBlue += (float)image[k][l].rgbtBlue;
                }
            }
            blured[i][j].rgbtRed = round(trackerRed / 4);
            blured[i][j].rgbtGreen = round(trackerGreen / 4);
            blured[i][j].rgbtBlue = round(trackerBlue / 4);
        }
    }

    // copy blured image data back to the original image
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            image[i][j].rgbtRed = blured[i][j].rgbtRed;
            image[i][j].rgbtGreen = blured[i][j].rgbtGreen;
            image[i][j].rgbtBlue = blured[i][j].rgbtBlue;
        }
    }
    return;
}

非常感谢您的帮助

1 个答案:

答案 0 :(得分:0)

我也在上课,所以这可能是完全错误的,但这就是我所看到的:

// right middle column
for (int l = j; l <= j; l++)

您对int l的for循环似乎不正确。不应该有2列吗?

// lower left corner (anchored)
for (int j = 0; j < height; j++)

j应该是宽度,但由于它是基于您以前的代码的一个角,因此我认为应该为1