0

Greatings fellow Humans

So I am about at my wit's end with this one. I have set it all out and have the maths (I beleive) correct on the averages. BUT... check 50 says that some of the averages are coming out incorrect. I have been looking at the code for hours now, and cannot see the fault.

Help me ObiWan, you are my only hope.

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
    // | /  |J-1 | J  |J+1 |
    // |_/__|____|____|____|
    // |I-1 |I-1 |I-1 |I-1 |
    // |____|J-1_|J___|J+1_|
    // | I  |I   | IJ |I   |
    // |____|J-1_|____|J+1_|
    // |I+1 |I+1 |I+1 |I+1 |
    // |____|J-1_|J___|J+1_|
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            // Local variables to calculate average colour levels
            float pix_count = 0.0;
            float tot_red = 0.0;
            float tot_green = 0.0;
            float tot_blue = 0.0;
            
            // These used for ease of syntax in qualifying statemtns
            int h = height - 1;
            int w = width - 1; 
            
            // Top left pixel
            // Checking edge cases
            // Correct
            if (i - 1 >= 0 && j - 1 >= 0)
            {
                tot_red = tot_red + image[i - 1][j - 1].rgbtRed;
                tot_green = tot_green + image[i - 1][j - 1].rgbtGreen;
                tot_blue = tot_blue + image[i - 1][j - 1].rgbtBlue;
                pix_count = pix_count + 1;
            }
            
            // Top middle pixel
            // Correct
            if (i - 1 >= 0)
            {
                tot_red = tot_red + image[i - 1][j].rgbtRed;
                tot_green = tot_green + image[i - 1][j].rgbtGreen;
                tot_blue = tot_blue + image[i - 1][j].rgbtBlue;
                pix_count = pix_count + 1;
            }
            
            // Top right pixel
            // Correct
            if (i - 1 >= 0 && j + 1 <= w)
            {
                tot_red = tot_red + image[i - 1][j + 1].rgbtRed;
                tot_green = tot_green + image[i - 1][j + 1].rgbtGreen;
                tot_blue = tot_blue + image[i - 1][j + 1].rgbtBlue;
                pix_count = pix_count + 1;
            }
            
            // Middle left pixel
            // Correct
            if (j - 1 >= 0)
            {
                tot_red = tot_red + image[i][j - 1].rgbtRed;
                tot_green = tot_green + image[i][j - 1].rgbtGreen;
                tot_blue = tot_blue + image[i][j - 1].rgbtBlue;
                pix_count = pix_count + 1;
            }
            
            // Middle pixel
            // Correct
            tot_red = tot_red + image[i][j].rgbtRed;
            tot_green = tot_green + image[i][j].rgbtGreen;
            tot_blue = tot_blue + image[i][j].rgbtBlue;
            pix_count = pix_count + 1;
            
            // Middle right pixel
            // Correct
            if (j + 1 <= w)
            {
                tot_red = tot_red + image[i][j + 1].rgbtRed;
                tot_green = tot_green + image[i][j + 1].rgbtGreen;
                tot_blue = tot_blue + image[i][j + 1].rgbtBlue;
                pix_count = pix_count + 1;
            }
            
            // Bottom right pixel
            // Correct
            if (i + 1 <= h && j - 1 >= 0)
            {
                tot_red = tot_red + image[i + 1][j - 1].rgbtRed;
                tot_green = tot_green + image[i + 1][j - 1].rgbtGreen;
                tot_blue = tot_blue + image[i + 1][j - 1].rgbtBlue;
                pix_count = pix_count + 1;
            }
            
            // Bottom middle pixel
            // Correct
            if (i + 1 <= h)
            {
                tot_red = tot_red + image[i + 1][j].rgbtRed;
                tot_green = tot_green + image[i + 1][j].rgbtGreen;
                tot_blue = tot_blue + image[i + 1][j].rgbtBlue;
                pix_count = pix_count + 1;
            }
            
            // Bottom right pixel
            // 
            if (i + 1 <= h && j + 1 <= w)
            {
                tot_red = tot_red + image[i + 1][j + 1].rgbtRed;
                tot_green = tot_green + image[i + 1][j + 1].rgbtGreen;
                tot_blue = tot_blue + image[i + 1][j + 1].rgbtBlue;
                pix_count = pix_count + 1;
            }
            // Averaging the colour levels
            float av_red = round(tot_red / pix_count);
            float av_green = round(tot_green / pix_count);
            float av_blue = round(tot_blue / pix_count);
            
            // Assigning back to central pixel
            image[i][j].rgbtRed = av_red;
            image[i][j].rgbtGreen = av_green;
            image[i][j].rgbtBlue = av_blue;
        }
    }
    
    return;
}

Can someone please point out to a blind old fart where he went wrong?

Ed

2

From one blind old fart to another, it's like the guy that lost his glasses in the basement but was looking in the back yard because the light was better. You're looking in the wrong place. ;-)

The problem is subtle. (I did the same thing the first time I did this, so don't feel bad.) The code is calculating the blur values (correctly, I assume), but then it is storing the result back in the original image array immediately. That means that the next pixel's calculation is based on the previous cell's blurred values, and not on the original values.

EVERY pixel's value needs all the calculations to be based on the original values, so the code can't change anything in the original image until all the values have been blurred. That means that a second array is needed to store the blurred image. Once all the pixels have been blurred, the values can all be copied back to the original image.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • Cliff B. Perfect and thank you ver much young man. I have corrected the code. Hopefully it passes now. – EdCase Sep 1 '20 at 7:20

You must log in to answer this question.

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