首页 > 解决方案 > 为什么C模糊滤镜不模糊角落?

问题描述

我在 cs50 ide 的 c 中开发了一个函数来框模糊位图图像,乍一看它似乎模糊了图像。根据check50,它没有正确模糊角落,但中间像素和边缘像素是正确的。除此之外,当我运行我的代码时,我会遇到多个运行时错误。

该函数应该循环遍历图像中的每个像素,并将当前像素及其周围像素的 RGB 值放入 3 个数组中,red[]、green[] 和 blue[]。如果周围像素不存在,则不会将 RGB 值放入颜色数组中。

然后,它分别对红色、绿色和蓝色值进行平均。它将这些值保存在一个名为 temp[][] 的临时二维数组中。在计算出每个像素的平均值后,图像会被 temp[] 数组中的颜色值覆盖。

我认为检查丢失像素的逻辑有缺陷,但我一直无法找到问题所在。另外,不明白为什么我会遇到运行时错误。我应该如何正确模糊角落像素并消除运行时错误?

这是我的代码:

void blur(int height, int width, RGBTRIPLE image[height][width])
{
    //Define variables
    RGBTRIPLE temp[height][width];
    int red[9];
    int green[9];
    int blue[9];
    int counter;
    int redavg;
    int blueavg;
    int greenavg;

    for (int i = 0; i < width; i++)
    {
        for (int j = 0; j < height; j++)
        {
            //This block of code is for the middle pixel of the 3 by 3 box
            counter = 0.00;
            red[counter] = image[j][i].rgbtRed;
            green[counter] = image[j][i].rgbtGreen;
            blue[counter] = image[j][i].rgbtBlue;
            counter++;

            //Check to see if the pixel is on the top edge
            if (i != 0)
            {
                red[counter] = image[j][i-1].rgbtRed;
                green[counter] = image[j][i-1].rgbtGreen;
                blue[counter] = image[j][i-1].rgbtBlue;
                counter++;

                //Checks to see if pixel is on the right edge
                if (j != width - 1)
                {
                    red[counter] = image[j+1][i-1].rgbtRed;
                    green[counter] = image[j+1][i-1].rgbtGreen;
                    blue[counter] = image[j+1][i-1].rgbtBlue;
                    counter++;

                    red[counter] = image[j+1][i].rgbtRed;
                    green[counter] = image[j+1][i].rgbtGreen;
                    blue[counter] = image[j+1][i].rgbtBlue;
                    counter++;

                }
                //Checks to see if pixel is on the right edge
                if (j != 0)
                {
                    red[counter] = image[j-1][i-1].rgbtRed;
                    green[counter] = image[j-1][i-1].rgbtGreen;
                    blue[counter] = image[j-1][i-1].rgbtBlue;
                    counter++;

                    red[counter] = image[j-1][i].rgbtRed;
                    green[counter] = image[j-1][i].rgbtGreen;
                    blue[counter] = image[j-1][i].rgbtBlue;
                    counter++;

                }
            }
            //Check to see if the pixel is on the bottom edge
            if (i != height - 1)
            {
                red[counter] = image[j][i+1].rgbtRed;
                green[counter] = image[j][i+1].rgbtGreen;
                blue[counter] = image[j][i+1].rgbtBlue;
                counter++;

                //Checks to see if pixel is on the right edge
                if (j != width - 1)
                {
                    red[counter] = image[j+1][i+1].rgbtRed;
                    green[counter] = image[j+1][i+1].rgbtGreen;
                    blue[counter] = image[j+1][i+1].rgbtBlue;
                    counter++;

                    if (i == 0)
                    {
                        red[counter] = image[j+1][i].rgbtRed;
                        green[counter] = image[j+1][i].rgbtGreen;
                        blue[counter] = image[j+1][i].rgbtBlue;
                        counter++;
                    }
                }
                //Checks to see if pixel is on the left edge
                if (j != 0)
                {
                    red[counter] = image[j-1][i+1].rgbtRed;
                    green[counter] = image[j-1][i+1].rgbtGreen;
                    blue[counter] = image[j-1][i+1].rgbtBlue;
                    counter++;

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

            //take red blue and green, average them, and put them into tmp array
            for (int k = 0; k < counter; k++)
            {
                redavg += red[k];
                blueavg += blue[k];
                greenavg += green[k];
            }
            redavg = round(redavg/(float)counter);
            greenavg = round(greenavg/(float)counter);
            blueavg = round(blueavg/counter);

            temp[j][i].rgbtRed = redavg;
            temp[j][i].rgbtGreen = greenavg;
            temp[j][i].rgbtBlue = blueavg;

            redavg = 0;
            blueavg = 0;
            greenavg = 0;
        }
    }
    for (int m = 0; m < width; m++)
    {
        for (int n = 0; n < height; n++)
        {
            image[n][m].rgbtRed = temp[n][m].rgbtRed;
            image[n][m].rgbtGreen = temp[n][m].rgbtGreen;
            image[n][m].rgbtBlue = temp[n][m].rgbtBlue;
        }
    }
    return;
}

我收到这些运行时错误:

helpers.c:299:32: runtime error: index 600 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:300:34: runtime error: index 600 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:301:33: runtime error: index 600 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:307:36: runtime error: index 600 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:308:38: runtime error: index 600 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:309:37: runtime error: index 600 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:323:36: runtime error: index 600 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:324:38: runtime error: index 600 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:325:37: runtime error: index 600 out of bounds for type 'RGBTRIPLE [width]'

check50 告诉我:

:) blur correctly filters middle pixel
:) blur correctly filters pixel on edge
:( blur correctly filters pixel in corner
    expected "70 85 95\n", not "70 136 55\n"
:( blur correctly filters 3x3 image
    expected "70 85 95\n80 9...", not "70 128 89\n80 ..."
:( blur correctly filters 4x4 image
    expected "70 85 95\n80 9...", not "70 135 71\n80 ..."

标签: ccs50

解决方案


推荐阅读