Bitmap Image Blur

43 Views Asked by At

I am trying to box blur a bmp image in a problem set in cs50, the code I wrote successfully identifies a middle pixel which is surrounded by 8 others. The goal is to take the average value of red, green and blue of every pixel including the one we're studying and change every one of the three values to the average value, it seems to me that the code is correct but the result isn't. Considering I am taking the CS50 course, please use hints only and no code because it would be a violation of the course's rules.

void blur(int height, int width, RGBTRIPLE image[height][width])
{
    int avg_r = 0;
    int avg_g = 0;
    int avg_b = 0;
    RGBTRIPLE temp[height][width];
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            float counter = 1.0;
            avg_r += image[i][j].rgbtRed;
            avg_g += image[i][j].rgbtGreen;
            avg_b += image[i][j].rgbtBlue;

            //first attempt

            // if (i != height - 1 || j != width - 1)
            // {
            //     avg_r += image[i + 1][j + 1].rgbtRed;
            //     avg_g += image[i + 1][j + 1].rgbtGreen;
            //     avg_b += image[i + 1][j + 1].rgbtBlue;
            //     counter += 1;
            // }
            // if (i != 0 || j != width - 1)
            // {
            //     avg_r += image[i - 1][j + 1].rgbtRed;
            //     avg_g += image[i - 1][j + 1].rgbtGreen;
            //     avg_b += image[i - 1][j + 1].rgbtBlue;
            //     counter += 1;
            // }
            // if (i != height - 1 || j != 0)
            // {
            //     avg_r += image[i + 1][j - 1].rgbtRed;
            //     avg_g += image[i + 1][j - 1].rgbtGreen;
            //     avg_b += image[i + 1][j - 1].rgbtBlue;
            //     counter += 1;
            // }
            // if (i != 0 || j != 0)
            // {
            //     avg_r += image[i - 1][j - 1].rgbtRed;
            //     avg_g += image[i - 1][j - 1].rgbtGreen;
            //     avg_b += image[i - 1][j - 1].rgbtBlue;
            //     counter += 1;
            // }
            // if (i != 0)
            // {
            //     avg_r += image[i - 1][j].rgbtRed;
            //     avg_g += image[i - 1][j].rgbtGreen;
            //     avg_b += image[i - 1][j].rgbtBlue;
            //     counter += 1;
            // }
            // if (j != 0)
            // {
            //     avg_r += image[i][j - 1].rgbtRed;
            //     avg_g += image[i][j - 1].rgbtGreen;
            //     avg_b += image[i][j - 1].rgbtBlue;
            //     counter += 1;
            // }
            // if (i != height - 1)
            // {
            //     avg_r += image[i + 1][j].rgbtRed;
            //     avg_g += image[i + 1][j].rgbtGreen;
            //     avg_b += image[i + 1][j].rgbtBlue;
            //     counter += 1;
            // }
            // if (j != width - 1)
            // {
            //     avg_r += image[i][j + 1].rgbtRed;
            //     avg_g += image[i][j + 1].rgbtGreen;
            //     avg_b += image[i][j + 1].rgbtBlue;
            //     counter += 1;
            // }
            //         avg_r = round(avg_r / counter);
            //         avg_g = round(avg_g / counter);
            //         avg_b = round(avg_b / counter);

            //         temp[i][j].rgbtGreen = avg_g;
            //         temp[i][j].rgbtBlue = avg_b;
            //         temp[i][j].rgbtRed = avg_r;

            //second attempt

                // if (i == 0)
                // {
                //     if (j == 0)
                //     {
                //         avg_r += image[i][j + 1].rgbtRed;
                //         avg_g += image[i][j + 1].rgbtGreen;
                //         avg_b += image[i][j + 1].rgbtBlue;

                //         counter += 1;

                //         avg_r += image[i + 1][j + 1].rgbtRed;
                //         avg_g += image[i + 1][j + 1].rgbtGreen;
                //         avg_b += image[i + 1][j + 1].rgbtBlue;

                //         counter += 1;

                //         avg_r += image[i + 1][j].rgbtRed;
                //         avg_g += image[i + 1][j].rgbtGreen;
                //         avg_b += image[i + 1][j].rgbtBlue;

                //         counter += 1;

                //         avg_r = round(avg_r / counter);
                //         avg_g = round(avg_g / counter);
                //         avg_b = round(avg_b / counter);

                //         temp[i][j].rgbtGreen = avg_g;
                //         temp[i][j].rgbtBlue = avg_b;
                //         temp[i][j].rgbtRed = avg_r;

                //         continue;
                //     }
                //     else if (j == width - 1)
                //     {
                //         avg_r += image[i][j + 1].rgbtRed;
                //         avg_g += image[i][j + 1].rgbtGreen;
                //         avg_b += image[i][j + 1].rgbtBlue;

                //         counter += 1;

                //         avg_r += image[i + 1][j].rgbtRed;
                //         avg_g += image[i + 1][j].rgbtGreen;
                //         avg_b += image[i + 1][j].rgbtBlue;

                //         counter += 1;

                //         avg_r += image[i + 1][j - 1].rgbtRed;
                //         avg_g += image[i + 1][j - 1].rgbtGreen;
                //         avg_b += image[i + 1][j - 1].rgbtBlue;

                //         counter += 1;

                //         avg_r = round(avg_r / counter);
                //         avg_g = round(avg_g / counter);
                //         avg_b = round(avg_b / counter);

                //         temp[i][j].rgbtGreen = avg_g;
                //         temp[i][j].rgbtBlue = avg_b;
                //         temp[i][j].rgbtRed = avg_r;

                //         continue;
                //     }
                //     else
                //     {
                //         avg_r += image[i][j + 1].rgbtRed;
                //         avg_g += image[i][j + 1].rgbtGreen;
                //         avg_b += image[i][j + 1].rgbtBlue;

                //         counter += 1;

                //         avg_r += image[i][j - 1].rgbtRed;
                //         avg_g += image[i][j - 1].rgbtGreen;
                //         avg_b += image[i][j - 1].rgbtBlue;

                //         counter += 1;

                //         avg_r += image[i + 1][j - 1].rgbtRed;
                //         avg_g += image[i + 1][j - 1].rgbtGreen;
                //         avg_b += image[i + 1][j - 1].rgbtBlue;

                //         counter += 1;

                //         avg_r += image[i + 1][j + 1].rgbtRed;
                //         avg_g += image[i + 1][j + 1].rgbtGreen;
                //         avg_b += image[i + 1][j + 1].rgbtBlue;

                //         counter += 1;

                //         avg_r += image[i + 1][j].rgbtRed;
                //         avg_g += image[i + 1][j].rgbtGreen;
                //         avg_b += image[i + 1][j].rgbtBlue;

                //         counter += 1;

                //         avg_r = round(avg_r / counter);
                //         avg_g = round(avg_g / counter);
                //         avg_b = round(avg_b / counter);

                //         temp[i][j].rgbtGreen = avg_g;
                //         temp[i][j].rgbtBlue = avg_b;
                //         temp[i][j].rgbtRed = avg_r;

                //         continue;
                //     }
                // }


            if (i != 0 && j != 0 && j != width - 1 && i != height - 1)
            {

                avg_r += image[i - 1][j - 1].rgbtRed;
                avg_b += image[i - 1][j - 1].rgbtBlue;
                avg_g += image[i - 1][j - 1].rgbtGreen;

                avg_r += image[i - 1][j].rgbtRed;
                avg_b += image[i - 1][j].rgbtBlue;
                avg_g += image[i - 1][j].rgbtGreen;

                avg_r += image[i - 1][j + 1].rgbtRed;
                avg_b += image[i - 1][j + 1].rgbtBlue;
                avg_g += image[i - 1][j + 1].rgbtGreen;

                avg_r += image[i][j - 1].rgbtRed;
                avg_b += image[i][j - 1].rgbtBlue;
                avg_g += image[i][j - 1].rgbtGreen;

                avg_r += image[i][j].rgbtRed;
                avg_b += image[i][j].rgbtBlue;
                avg_g += image[i][j].rgbtGreen;

                avg_r += image[i][j + 1].rgbtRed;
                avg_b += image[i][j + 1].rgbtBlue;
                avg_g += image[i][j + 1].rgbtGreen;

                avg_r += image[i + 1][j - 1].rgbtRed;
                avg_b += image[i + 1][j - 1].rgbtBlue;
                avg_g += image[i + 1][j - 1].rgbtGreen;

                avg_r += image[i + 1][j].rgbtRed;
                avg_b += image[i + 1][j].rgbtBlue;
                avg_g += image[i + 1][j].rgbtGreen;

                avg_r += image[i + 1][j + 1].rgbtRed;
                avg_b += image[i + 1][j + 1].rgbtBlue;
                avg_g += image[i + 1][j + 1].rgbtGreen;

                avg_r = round(avg_r / 9.0);
                avg_g = round(avg_g / 9.0);
                avg_b = round(avg_b / 9.0);

                temp[i][j].rgbtGreen = avg_g;
                temp[i][j].rgbtBlue = avg_b;
                temp[i][j].rgbtRed = avg_r;
            }


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

    return;
}

You can see that I tried three different methods, but none of them work, please focus on the last one first (the non commented one) which identifies just middle pixels.

0

There are 0 best solutions below