CS50 pset4-滤镜(较不舒适),模糊功能

时间:2020-04-15 13:34:44

标签: c cs50

我为滤镜pset的模糊功能编写了代码。但是,每当我尝试编译代码时,都会出现错误。有人可以看到我的错误所在吗?

代码:


// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
    int count = 0;

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            int red = 0;
            int green = 0;
            int blue = 0;

            if (j - 1 >= 0 && j - 1  <= 255)
            {
                red += image[i][j - 1].rgbtRed;
                green += image[i][j - 1].rgbtGreen;
                blue += image[i][j - 1].rgbtBlue;
                count++;
            }

            if (j + 1 >= 0 && j + 1  <= 255)
            {
                red += image[i][j + 1].rgbtRed;
                green += image[i][j + 1].rgbtGreen;
                blue += image[i][j + 1].rgbtBlue;
                count++;
            }

            if (j - 1 >= 0 && j - 1  <= 255 && i - 1 >= 0 && i - 1 <= 255)
            {
                red += image[i][j - 1].rgbtRed;
                green += image[i][j - 1].rgbtGreen;
                blue += image[i][j - 1].rgbtBlue;
                count++;
            }

            if (i - 1 >= 0 && i - 1 <= 255)
            {
                red += image[i][j - 1].rgbtRed;
                green += image[i][j - 1].rgbtGreen;
                blue += image[i][j - 1].rgbtBlue;
                count++;
            }

            if (j + 1 >= 0 && j + 1  <= 255 && i - 1 >= 0 && i - 1 <= 255)
            {
                red += image[i][j - 1].rgbtRed;
                green += image[i][j - 1].rgbtGreen;
                blue += image[i][j - 1].rgbtBlue;
                count++;
            }

            if (j - 1 >= 0 && j - 1  <= 255 && i + 1 >= 0 && i + 1 <= 255)
            {
                red += image[i][j - 1].rgbtRed;
                green += image[i][j - 1].rgbtGreen;
                blue += image[i][j - 1].rgbtBlue;
                count++;
            }

            if (i + 1 >= 0 && i + 1 <= 255)
            {
                red += image[i][j - 1].rgbtRed;
                green += image[i][j - 1].rgbtGreen;
                blue += image[i][j - 1].rgbtBlue;
                count++;
            }

            if (j + 1 >= 0 && j + 1  <= 255 && i + 1 >= 0 && i + 1 <= 255)
            {
                red += image[i][j - 1].rgbtRed;
                green += image[i][j - 1].rgbtGreen;
                blue += image[i][j - 1].rgbtBlue;
                count++;
            }

            image[i][j].rgbtRed = (red / count);
            image[i][j].rgbtGreen = (green / count);
            image[i][j].rgbtBlue = (blue / count);

        }

    }
    return;
}

错误消息:


helpers.c:170:24: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:171:26: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:172:25: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:178:24: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:179:26: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:180:25: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:146:24: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:147:26: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:148:25: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:154:24: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:155:26: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:156:25: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'

我的输入是:

./filter -b stadium.bmp outfile.bmp

如果有人需要有关pset的更多信息,请随时转到CS50自己的页面:

https://cs50.harvard.edu/x/2020/psets/4/filter/less/

更新的代码:

void blur(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE copy[height][width];
    int count = 0;

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            int red = 0;
            int green = 0;
            int blue = 0;


            if (j - 1 >= 0 & j - 1  <= 255)
            {
                red += image[i][j - 1].rgbtRed;
                green += image[i][j - 1].rgbtGreen;
                blue += image[i][j - 1].rgbtBlue;
                count++;
            }

            if (j + 1 >= 0 && j + 1  <= 255)
            {
                red += image[i][j + 1].rgbtRed;
                green += image[i][j + 1].rgbtGreen;
                blue += image[i][j + 1].rgbtBlue;
                count++;
            }

            if (j - 1 >= 0 && j - 1  <= 255 && i - 1 >= 0 && i - 1 <= 255)
            {
                red += image[i - 1][j - 1].rgbtRed;
                green += image[i - 1][j - 1].rgbtGreen;
                blue += image[i - 1][j - 1].rgbtBlue;
                count++;
            }

            if (i - 1 >= 0 && i - 1 <= 255)
            {
                red += image[i - 1][j].rgbtRed;
                green += image[i - 1][j].rgbtGreen;
                blue += image[i - 1][j].rgbtBlue;
                count++;
            }

            if (j + 1 >= 0 && j + 1  <= 255 && i - 1 >= 0 && i - 1 <= 255)
            {
                red += image[i - 1][j + 1].rgbtRed;
                green += image[i- 1][j + 1].rgbtGreen;
                blue += image[i- 1][j + 1].rgbtBlue;
                count++;
            }

            if (j - 1 >= 0 && j - 1  <= 255 && i + 1 >= 0 && i + 1 <= 255)
            {
                red += image[i + 1][j - 1].rgbtRed;
                green += image[i + 1] [j - 1].rgbtGreen;
                blue += image[i + 1][j - 1].rgbtBlue;
                count++;
            }

            if (i + 1 >= 0 && i + 1 <= 255)
            {
                red += image[i+ 1][j ].rgbtRed;
                green += image[i+ 1][j].rgbtGreen;
                blue += image[i+ 1][j].rgbtBlue;
                count++;
            }

            if (j + 1 >= 0 && j + 1  <= 255 && i + 1 >= 0 && i + 1 <= 255)
            {
                red += image[i + 1][j + 1].rgbtRed;
                green += image[i+ 1][j + 1].rgbtGreen;
                blue += image[i+ 1][j + 1].rgbtBlue;
                count++;
            }

            copy[i][j].rgbtRed = round(red / count);
            copy[i][j].rgbtGreen = round(green / count);
            copy[i][j].rgbtBlue = round(blue / count);

        }

    }

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            image[i][j].rgbtRed = copy[i][j].rgbtRed;
            image[i][j].rgbtGreen = copy[i][j].rgbtGreen;
            image[i][j].rgbtGreen = copy[i][j].rgbtGreen;
        }
    }
    return;
}

更新2:


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++)
        {
            float red = 0;
            float green = 0;
            float blue = 0;

            int count = 0;

            if (j >= 0 )
            {
                red += image[i][j].rgbtRed;
                green += image[i][j].rgbtGreen;
                blue += image[i][j].rgbtBlue;
                count++;
            }

            if (j - 1 >= 0 && j - 1  < width- 1)
            {
                red += image[i][j - 1].rgbtRed;
                green += image[i][j - 1].rgbtGreen;
                blue += image[i][j - 1].rgbtBlue;
                count++;
            }

            if (j + 1 >= 0 && j + 1  < width - 1)
            {
                red += image[i][j + 1].rgbtRed;
                green += image[i][j + 1].rgbtGreen;
                blue += image[i][j + 1].rgbtBlue;
                count++;
            }

            if (j - 1 >= 0 && j - 1  < width - 1 && i - 1 >= 0 && i - 1 < height - 1)
            {
                red += image[i - 1][j - 1].rgbtRed;
                green += image[i - 1][j - 1].rgbtGreen;
                blue += image[i - 1][j - 1].rgbtBlue;
                count++;
            }

            if (i - 1 >= 0 && i - 1 < height - 1)
            {
                red += image[i - 1][j].rgbtRed;
                green += image[i - 1][j].rgbtGreen;
                blue += image[i - 1][j].rgbtBlue;
                count++;
            }

            if (j + 1 >= 0 && j + 1  < width - 1 && i - 1 >= 0 && i - 1 < height - 1)
            {
                red += image[i - 1][j + 1].rgbtRed;
                green += image[i- 1][j + 1].rgbtGreen;
                blue += image[i- 1][j + 1].rgbtBlue;
                count++;
            }

            if (j - 1 >= 0 && j - 1  < width - 1 && i + 1 >= 0 && i + 1 < height - 1)
            {
                red += image[i + 1][j - 1].rgbtRed;
                green += image[i + 1] [j - 1].rgbtGreen;
                blue += image[i + 1][j - 1].rgbtBlue;
                count++;
            }

            if (i + 1 >= 0 && i + 1 < height - 1)
            {
                red += image[i+ 1][j ].rgbtRed;
                green += image[i+ 1][j].rgbtGreen;
                blue += image[i+ 1][j].rgbtBlue;
                count++;
            }

            if (j + 1 >= 0 && j + 1  < width- 1 && i + 1 >= 0 && i + 1 < height - 1)
            {
                red += image[i + 1][j + 1].rgbtRed;
                green += image[i+ 1][j + 1].rgbtGreen;
                blue += image[i+ 1][j + 1].rgbtBlue;
                count++;
            }

            copy[i][j].rgbtRed = round(red / count);
            copy[i][j].rgbtGreen = round(green / count);
            copy[i][j].rgbtBlue = round(blue / count);


        }

    }

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            image[i][j].rgbtRed = copy[i][j].rgbtRed;
            image[i][j].rgbtGreen = copy[i][j].rgbtGreen;
            image[i][j].rgbtBlue = copy[i][j].rgbtBlue;
        }
    }
    return;
}

运行测试代码后的错误消息:


:( blur correctly filters middle pixel
    expected "127 140 149\n", not "70 85 95\n"
:( blur correctly filters pixel on edge
    expected "80 95 105\n", not "81 101 65\n"
:( blur correctly filters pixel in corner
    expected "70 85 95\n", not "45 72 71\n"
:( blur correctly filters 3x3 image
    expected "70 85 95\n80 9...", not "49 57 71\n61 6..."
:( blur correctly filters 4x4 image
    expected "70 85 95\n80 9...", not "46 60 79\n85 8..."

2 个答案:

答案 0 :(得分:0)

您的代码中存在各种错误。

  1. 无效的索引值是由错误的像素访问条件引起的:

        if (j + 1 >= 0 && j + 1  <= 255 && i - 1 >= 0 && i - 1 <= 255)
        {
            red += image[i][j - 1].rgbtRed;
            green += image[i][j - 1].rgbtGreen;
            blue += image[i][j - 1].rgbtBlue;
            count++;
        }
    

    该条件对于j==0为真,这将导致访问image[i][-1],如错误消息中所示。

  2. 您永远不会重置count。 变量count旨在跟踪在计算平均值时包括了多少个相邻像素。应该为每个像素重置此值,而不是无限地挖槽。

  3. 您使用整数进行计算。 问题要求您对最近的颜色应用适当的舍入。这意味着使用浮点变量而不是整数,因为整数除法总是四舍五入。

  4. 您应该使用所有相邻像素。 这意味着您需要在行内和列之间使用偏移量-1,0,+ 1。换句话说,您必须使用索引值[i-1][...][i][...][i+1][...]以及[...][j-1][...][j][...][j+1]。总的来说,所有非边缘像素都应考虑9个像素。您当前的代码使用[i][j - 1]的方式比其他任何像素都要频繁,因此其权重比其他像素高得多。某些邻居从未使用过。

  5. 条件对位图的宽度和高度采用固定的幻数255。您应该改用变量heightwidth

  6. 条件太复杂了。 在每个条件块中,您访问的像素在任何方向上均偏移1。如果使用j+1,则无需检查它是否小于0。同样,如果使用i-1,则无需与上限进行比较。而且,如果您不使用偏移量([i],则根本不需要检查i的范围,因为外部循环已经对此有所考虑。

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            double red   = image[i][j].rgbtRed;
            double green = image[i][j].rgbtGreen;
            double blue  = image[i][j].rgbtBlue;
            int count = 1;

            if (j > 0)
            {
                red += image[i][j-1].rgbtRed;
                green += image[i][j-1].rgbtGreen;
                blue += image[i][j-1].rgbtBlue;
                count++;
            }
            ...
            if (i < height-1 && j > 0)
            {
                red += image[i+1][j-1].rgbtRed;
                green += image[i+1][j-1].rgbtGreen;
                blue += image[i+1][j-1].rgbtBlue;
                count++;
            }
  1. 您在计算过程中已经使用了模糊像素。 如果使用新值更新像素image[i][j],然后在下一步骤中通过image[i][j-1]再次使用它,则会得到错误的结果。使用图像的副本代替存储结果像素。

答案 1 :(得分:0)

RGBTRIPLE arr[height][width];
for (int i = 0; i < height; i++)
{
    for (int j = 0; j < width; j++)
    {
        arr[i][j] = image[i][j];
    }
}
float r, b, g;
float counter = 0;

for (int i = 0; i < height; i++)
{
    for (int j = 0; j < width; j++)
    {
        for (int k = -1; k < 2; k++)
        {
            for (int l = -1; l < 2; l++)
            {
                if (i + k >= 0 && j + l >= 0 && i + k <= height - 1 && j + l <= width - 1)
                {
                    r += arr[i + k][j + l].rgbtRed;
                    b += arr[i + k][j + l].rgbtBlue;
                    g += arr[i + k][j + l].rgbtGreen;
                    counter++;
                }

            }
        }
        r = r / counter;
        b = b / counter;
        g = g / counter;
        if (r > 255)
        {
            r = 255;
        }
        if (r < 0)
        {
            r = 0;
        }
        if (g > 255)
        {
            g = 255;
        }
        if (g < 0)
        {
            g = 0;
        }
        if (b > 255)
        {
            b = 255;
        }
        if (b < 0)
        {
            b = 0;
        }
        image[i][j].rgbtRed = round(r);
        image[i][j].rgbtBlue = round(b);
        image[i][j].rgbtGreen = round(g);
        counter = 0;
        r = 0;
        g = 0;
        b = 0;
    }
}
相关问题