0

I have been stuck on this problem for days and have tried so many ways to write this code, but I continuously get errors. Nothing works. The output file is just returned as a darker image of the input file. I'm not sure what to do, so any tips/suggestions/advice is greatly appreciated!

void blur(int height, int width, RGBTRIPLE image[height][width]) { RGBTRIPLE new_image[height][width];

for (int i = 0; i < height; i++)
{
    for (int j = 0; j < width; j++)
    {
        new_image[i][j] = image[i][j];

        int totalRed = 0;
        int totalGreen = 0;
        int totalBlue = 0;
        float counter = 0.00;

        for (int a = -1; a <= 1; a++)
        {
            for (int b = -1; b <= 1; b++)
            {
                if (i + a < 0 || i + a > height - 1 || j + b < 0 || j + b > width - 1)
                {
                    continue;
                }
                else
                {
                    totalRed += new_image[i + a][j + b].rgbtRed;
                    totalGreen += new_image[i + a][j + b].rgbtGreen;
                    totalBlue += new_image[i + a][j + b].rgbtBlue;

                    counter++;
                }
            }
        }
        new_image[i][j].rgbtRed = round(totalRed / counter);
        new_image[i][j].rgbtGreen = round(totalGreen / counter);
        new_image[i][j].rgbtBlue = round(totalBlue / counter);
    }
}
for (int i = 0; i < height; i++)
{
    for (int j = 0; j < width; j++)
    {
        image[i][j].rgbtRed = new_image[i][j].rgbtRed;
        image[i][j].rgbtGreen = new_image[i][j].rgbtGreen;
        image[i][j].rgbtBlue = new_image[i][j].rgbtBlue;
    }
}

}

1 Answer 1

0

The problem here is that you're getting the values for the average from new_image. As you're copying the image as you go, the surrounding pixels with positive sign i.e. 'i+1' or 'j+1' are not filled yet, so their values are not the right ones.

As I see it you can solve it in two ways:

  1. You copy the image first in a separate i,j loops. Then you get the values for the average from 'new_image' and then you copy totalRed, totalBlue and totalGreen to image. For this solution you wouldn't neeed the last loop, as 'image' is already filled.

  2. You get the values for the average from 'image'. You copy the results (totalRed etc) in 'new_image'. You keep the last loop to fill 'image' with the blurred values.

For both solutions you should get rid of this line new_image[i][j] = image[i][j]; at the begining of the first loops.

1
  • I can't believe I didn't notice that! Thank you! Your solution worked!
    – Leoness
    Jun 15, 2020 at 18:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .