0

I am having difficulty understanding what the next step should be for the blur function. I have rewritten the code to try to make it at least more understandable to me after a few trials, however, it still only correctly filters edge and corner pixels.

Any help would be much appreciated!

Thank you!

void blur(int height, int width, RGBTRIPLE image[height][width])
{
 // Make copy to be used for reading values
    RGBTRIPLE copy[height][width];
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            copy[i][j] = image[i][j];
        }
    }

    // Loop through pixels and locate neighboring pixels
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
        // Create variables to hold values and a counter value to count each pixel, set the counter variable to a float
            int totalR = 0, totalG = 0, totalB = 0;
            float n = 0;

            // Check whether pixel is in bounds, allow program to continue if not
            //Based on position relative to current pixel
            for (int x = -1; x < 2; x++)
            {
                for (int y = -1; y < 2; y++)
                {
                    //Check top edge
                    if ((i + x) < 0)
                    {
                        continue;
                    }
                    //Check bottom edge
                    else if ((i + x) > (height - 1))
                    {
                        continue;
                    }

                    // Check left edge
                    else if ((j + y) < 0)
                    {
                        continue;
                    }

                    // Check right edge
                    if ((j + y) > (width - 1))
                    {
                        continue;
                    }


                    // Add valid pixels
                    totalR += image[i + x][j + y].rgbtRed;
                    totalG += image[i + x][j + y].rgbtGreen;
                    totalB += image[i + x][j + y].rgbtBlue;

                    // Counter variable for all valid pixels
                    n++;

                   // Calculate image value with float, place in copy
                copy[i][j].rgbtRed = round(totalR / n);
                copy[i][j].rgbtGreen = round(totalG / n);
                copy[i][j].rgbtBlue = round(totalB / n);
            }
        }
    }

    // Return copy to original
    for (  i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            image[i][j].rgbtRed = copy[i][j].rgbtRed;
            image[i][j].rgbtGreen = copy[i][j].rgbtGreen;
            image[i][j].rgbtBlue = copy[i][j].rgbtBlue;
        }
    }
    return;
    }
}

1 Answer 1

0

Actually, it's only filtering the first row correctly. It took me a bit to find the root cause.

First of all, it would have been a better practice to initialize the copy array to all zeros to start. Knowing that the entire array would be updated, if you zero it out to start, it makes it much easier to spot certain issues, like this one. Simply declaring it without the copy loop would accomplish that. If you then ran the program, you'd see that the first row would have the correct values, but every row that follows is all zero values. That means that something is amiss in the code, causing the remaining rows not to be processed. Instinct will tell you to look at loops.

Also, if you had run it with debug50, a sudden and unexpected excursion into the code at the end would have signaled a problem. Learn to use debug. You'll wish you had later!!

As it turns out, it's all because of a misplaced curly bracket! ;-)

Go back through your code and check exactly where the pairs are. The easiest way is to print out the code and take a pencil and match them up, innermost pairs first.

Fixing this will produce another problem at compile that is easily recognized and fixed.

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

1
  • Thank you, as always, for your help! I look forward to going through it!
    – Jeff
    Commented Dec 29, 2023 at 19:31

You must log in to answer this question.

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