CS50 pset4过滤器“模糊

时间:2020-07-10 10:59:01

标签: c filter blur cs50

所以我在pset4过滤器中有这个问题,不太舒服。我的模糊不起作用。我的问题是,它似乎并没有因高度而循环,因此在我的代码中唯一起作用的是第一行。

我的代码在第一行工作正常,但第二行没有变化,依此类推。就像我的代码中有一个breaks函数,这样它将在第一行之后停止。我以为这是我在if()中的条件,也许我对条件感到困惑,但是它没有解释为什么它不适用于盒子的左下角和右下角,条件是如此简单,所以没有混乱。我的意思是让我们假设我在像素的边缘和中间部分的情况是错误的,但它仍应计算该框最后一行的角。即使我的条件或公式有问题,它也应该更改RGB像素的值,但不会。

:((模糊可以正确过滤3x3图像

使用3x3样本图像进行测试

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)

预期输出:

70 85 95

80 95 105


90 105 115

117 130 140

127 140 149

137 150 159

163 178 188

170 185 194

178 193 201

实际输出:

70 85 95

80 95 105

90 105 115

110 130 140

120 140 150

130 150 160

200 210 220

220 230 240

240 250 255

这是代码

void blur(int height, int width, RGBTRIPLE image[height][width])
{
// Copying the height and width length
int heilen = height;
int widlen = width;


int avgRed;
int avgGreen;
int avgBlue;

// Creating temporary array
int tmpR[height][width];
int tmpG[height][width];
int tmpB[height][width];

//storing the values of the image[i][j] to my temporary array
for (int i = 0; i < heilen; i++)
{
    for (int j = 0; j < widlen; j++)
    {
        // storing the current images arrays to tmpRGB
        tmpR[i][j] = image[i][j].rgbtRed;
        tmpG[i][j] = image[i][j].rgbtGreen;
        tmpB[i][j] = image[i][j].rgbtBlue;
    }
}


for (height = 0; height < heilen; height++)
{
    for (width = 0; width < widlen; width++)
    {
        //=================== PIXELS IN CORNERS
        //upper left corner side of the box
        if (height == 0 && width == 0)
        {
            //calculating the total values of the curreent row and pixel and current row and pixel + 1
            float current_row_Red = tmpR[height][width] + tmpR[height][width + 1];

            //calculating the total values of the next row current pixel and pixel + 1
            float after_row_Red = tmpR[height + 1][width] + tmpR[height + 1][width + 1];

            //getting the average of the total current row and next row and dividing it to 4
            avgRed = round ((current_row_Red + after_row_Red) / 4);


            float current_row_Green = tmpG[height][width] + tmpG[height][width + 1];
            float after_row_Green = tmpG[height + 1][width] + tmpG[height + 1][width + 1];
            avgGreen = round ((current_row_Green + after_row_Green) / 4);

            float current_row_Blue = tmpB[height][width] + tmpB[height][width + 1];
            float after_row_Blue = tmpB[height + 1][width] + tmpB[height + 1][width + 1];
            avgBlue = round ((current_row_Blue + after_row_Blue) / 4);

        }

        // upper right corner side of Box
        else if (height == 0 && width == widlen - 1)
        {
            float current_row_Red = tmpR[height][width] + tmpR[height][width - 1];
            float after_row_Red = tmpR[height + 1][width] + tmpR[height + 1][width - 1];
            avgRed = round ((current_row_Red + after_row_Red) / 4);


            float current_row_Green = tmpG[height][width] + tmpG[height][width - 1];
            float after_row_Green = tmpG[height + 1][width] + tmpG[height  + 1][width - 1];
            avgGreen = round ((current_row_Green + after_row_Green) / 4);

            float current_row_Blue = tmpB[height][width] + tmpB[height][width - 1];
            float after_row_Blue = tmpB[height + 1][width] + tmpB[height + 1][width - 1];
            avgBlue = round ((current_row_Blue + after_row_Blue) / 4);

        }

        //lower left corner side of the box
        else if (height == heilen - 1 && width == 0)
        {
            float current_row_Red = tmpR[height][width] + tmpR[height][width + 1];
            float before_row_Red =  tmpR[height - 1][width] + tmpR[height - 1][width + 1];
            avgRed = round ((current_row_Red + before_row_Red) / 4);

            float current_row_Green = tmpG[height][width] + tmpG[height][width + 1];
            float before_row_Green =  tmpG[height - 1][width] + tmpG[height - 1][width + 1];
            avgGreen = round ((current_row_Green + before_row_Green) / 4);

            float current_row_Blue = tmpB[height][width] + tmpB[height][width + 1];
            float before_row_Blue =  tmpB[height - 1][width] + tmpB[height - 1][width + 1];
            avgBlue = round ((current_row_Blue + before_row_Blue) / 4);
        }

        //lower right corner side of the box
        else if (height == heilen - 1 && width == widlen - 1)
        {
            float current_row_Red = tmpR[height][width] + tmpR[height][width - 1];
            float before_row_Red =  tmpR[height - 1][width] + tmpR[height - 1][width - 1];
            avgRed = round ((current_row_Red + before_row_Red) / 4);

            float current_row_Green = tmpG[height][width] + tmpG[height][width - 1];
            float before_row_Green =  tmpG[height - 1][width] + tmpG[height - 1][width - 1];
            avgGreen = round ((current_row_Green + before_row_Green) / 4);

            float current_row_Blue = tmpB[height][width] + tmpB[height][width - 1];
            float before_row_Blue =  tmpB[height - 1][width] + tmpB[height - 1][width - 1];
            avgBlue = round ((current_row_Blue + before_row_Blue) / 4);
        }


        // ===================== PIXELS ON EDGES

        //upper side of the box
        else if ( (height == 0 && width >= 1) && (height == 0 &&width < widlen - 1) )
        {
            float current_row_Red = tmpR[height][width] + tmpR[height][width - 1] + tmpR[height][width + 1];
            float after_row_Red = tmpR[height + 1][width] + tmpR[height + 1][width - 1] + tmpR[height + 1][width + 1];
            avgRed = round ((current_row_Red + after_row_Red) / 6);

            float current_row_Green = tmpG[height][width] + tmpG[height][width - 1] + tmpG[height][width + 1];
            float after_row_Green = tmpG[height + 1][width] + tmpG[height + 1][width - 1] + tmpG[height + 1][width + 1];
            avgGreen = round ((current_row_Green + after_row_Green) / 6);

            float current_row_Blue = tmpB[height][width] + tmpB[height][width - 1] + tmpB[height][width + 1];
            float after_row_Blue = tmpB[height + 1][width] + tmpB[height + 1][width - 1] + tmpB[height + 1][width + 1];
            avgBlue = round ((current_row_Blue + after_row_Blue) / 6);
        }

        //lower side of the box
        else if (  ( height == heilen - 1 && width >= 1) && ( height == heilen - 1 && width < widlen - 1) )

        {
            float current_row_Red = tmpR[height][width] + tmpR[height][width - 1] + tmpR[height][width + 1];
            float before_row_Red =  tmpR[height - 1][width] + tmpR[height - 1][width - 1] + tmpR[height - 1][width + 1];
            avgRed = round ((current_row_Red + before_row_Red) / 6);

            float current_row_Green = tmpG[height][width] + tmpG[height][width - 1] + tmpG[height][width + 1];
            float before_row_Green =  tmpG[height - 1][width] + tmpG[height - 1][width - 1] + tmpG[height - 1][width + 1];
            avgGreen = round ((current_row_Green + before_row_Green) / 6);

            float current_row_Blue = tmpB[height][width] + tmpB[height][width - 1] + tmpB[height][width + 1];
            float before_row_Blue =  tmpB[height - 1][width] + tmpB[height - 1][width - 1] + tmpB[height - 1][width + 1];
            avgBlue = round ((current_row_Blue + before_row_Blue) / 6);
        }

        // left side of the box
        else if (  (height >= 1 && width == 0) && (height < heilen - 1 && width == 0) )
        {
            float current_row_Red = tmpR[height][width] + tmpR[height][width + 1];
            float before_row_Red =  tmpR[height - 1][width] + tmpR[height - 1][width + 1];
            float after_row_Red = tmpR[height + 1][width] + tmpR[height + 1][width + 1];
            avgRed = round ((current_row_Red + before_row_Red + after_row_Red) / 6);

            float current_row_Green = tmpG[height][width] + tmpG[height][width + 1];
            float before_row_Green =  tmpG[height - 1][width] + tmpG[height - 1][width + 1];
            float after_row_Green = tmpG[height + 1][width] + tmpG[height + 1][width + 1];
            avgGreen = round ((current_row_Green + before_row_Green + after_row_Green) / 6);

            float current_row_Blue = tmpB[height][width] + tmpB[height][width + 1];
            float before_row_Blue =  tmpB[height - 1][width] + tmpB[height - 1][width + 1];
            float after_row_Blue = tmpB[height + 1][width] + tmpB[height + 1][width + 1];
            avgBlue = round ((current_row_Blue + before_row_Blue + after_row_Blue) / 6);

        }

        //right side of the box
        else if ( ( height >= 1 && width == widlen- 1) && (height <= heilen -1 && width == widlen - 1) )
        {
            float current_row_Red = tmpR[height][width] + tmpR[height][width - 1];
            float before_row_Red =  tmpR[height - 1][width] + tmpR[height - 1][width - 1];
            float after_row_Red = tmpR[height + 1][width] + tmpR[height + 1][width - 1];
            avgRed = round ((current_row_Red + before_row_Red + after_row_Red) / 6);

            float current_row_Green = tmpG[height][width] + tmpG[height][width - 1];
            float before_row_Green =  tmpG[height - 1][width] + tmpG[height - 1][width - 1];
            float after_row_Green = tmpG[height + 1][width] + tmpG[height + 1][width - 1];
            avgGreen = round ((current_row_Green + before_row_Green + after_row_Green) / 6);

            float current_row_Blue = tmpB[height][width] + tmpB[height][width - 1];
            float before_row_Blue =  tmpB[height - 1][width] + tmpB[height - 1][width - 1];
            float after_row_Blue = tmpB[height + 1][width] + tmpB[height + 1][width - 1];
            avgBlue = round ((current_row_Blue + before_row_Blue + after_row_Blue) / 6);

        }


        //====================MIDDLE PIXELS

        //middle of the box
        else if ( (height >= 1 && width >= 1) && (height < heilen - 1 && width < widlen - 1) )
        {
            float current_row_Red = tmpR[height][width] + tmpR[height][width - 1] + tmpR[height][width + 1];
            float after_row_Red = tmpR[height + 1][width] + tmpR[height + 1][width - 1] + tmpR[height + 1][width + 1];
            float before_row_Red =  tmpR[height - 1][width] + tmpR[height - 1][width - 1] + tmpR[height - 1][width + 1];
            avgRed = round ((current_row_Red + before_row_Red + after_row_Red) / 9);

            float current_row_Green = tmpG[height][width] + tmpG[height][width - 1] + tmpG[height][width + 1];
            float before_row_Green =  tmpG[height - 1][width] + tmpG[height - 1][width - 1] + tmpG[height - 1][width + 1];
            float after_row_Green = tmpG[height + 1][width] + tmpG[height + 1][width - 1] + tmpG[height + 1][width + 1];
            avgGreen = round ((current_row_Green + before_row_Green + after_row_Green) / 9);

            float current_row_Blue = tmpB[height][width] + tmpB[height][width - 1] + tmpB[height][width + 1];
            float before_row_Blue =  tmpB[height - 1][width] + tmpB[height - 1][width - 1] + tmpB[height - 1][width + 1];
            float after_row_Blue = tmpB[height + 1][width] + tmpB[height + 1][width - 1] + tmpB[height + 1][width + 1];
            avgBlue = round ((current_row_Blue + before_row_Blue + after_row_Blue) / 9);

        }

        //Assigning the cuurent average of RGB to image[][]
        image[height][width].rgbtRed = avgRed;
        image[height][width].rgbtGreen = avgGreen;
        image[height][width].rgbtBlue = avgBlue;


    }
    return;
}

}

您会注意到,从第二行开始,实际输出值与给定值相同。

我仍然知道它不是最有效的方法,但我还是帮不上忙,刚开始编码超过1个月。请帮助我> _ << / p>

0 个答案:

没有答案