1

This is the output and input image that my code generates: https://imgur.com/a/mT7YqCD

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

    RGBTRIPLE newImage[height][width];

    for (int h = 0; h < height; h++)
    {
        for (int w = 0; w < width; w++)
        {
            BYTE redAvg = 0;
            BYTE greenAvg = 0;
            BYTE blueAvg = 0;
            double count = 0.0;
        
            for (int y = -1; y <= 1; y++)
            {
                for (int x = -1; x <= 1; x++)
                {
                    int h_1 = h + y;
                    int w_1 = w + x;

                    if (h_1 >= 0 && h_1 < height && w_1 >= 0 && w_1 < width)
                    {
                        RGBTRIPLE img = image[h_1][w_1];
                        count++;
                        redAvg += img.rgbtRed;
                        greenAvg += img.rgbtGreen;
                        blueAvg += img.rgbtBlue;
                    }
                }
            }
        
            newImage[h][w].rgbtRed = round(redAvg/count);
            newImage[h][w].rgbtGreen = round(greenAvg/count);
            newImage[h][w].rgbtBlue = round(blueAvg/count);
        }
    }

    for (int x = 0; x < height; x++)
    {
        for (int y = 0; y < width; y++)
        {
            image[x][y] = newImage[x][y];
        }
    }

    return;
}
3

Got the solution. Using the BYTE type to store the color values resulted in overflow. Solving by using int or double for redAvg, greenAvg, and blueAvg.

0

why did you store the averages for each colour in a new image, why not just store it in image[h][w]?

1
  • blur looks at pixels surrounding each pixel. If you modify a pixel and then analyze an adjacent pixel you will be looking at incorrect values. – Michael Weiser Apr 16 '20 at 4:39

You must log in to answer this question.

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