0

I am having trouble with the blur function. I am not given any error messages when I run the program, and it creates an output image as if the blurring is working. However, when I open the output image, it is identical to the input image. What am I doing wrong?(The check50 does show it all red, so clearly it's incorrect.)

Thank you!

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE image2[height][width];
    for (int i = 0; i < height && i > 0; i++)
    {
        for (int j = 0; j < width && j > 0; j++)
        {
            int blurRed = (image[i-1][j-1].rgbtRed + image[i-1][j].rgbtRed + image[i-1][j+1].rgbtRed + image[i][j-1].rgbtRed + image[i][j].rgbtRed + image[i][j+1].rgbtRed + image[i+1][j-1].rgbtRed + image[i+1][j].rgbtRed + image[i+1][j+1].rgbtRed / (i + j));
            int blurGreen = (image[i-1][j-1].rgbtGreen + image[i-1][j].rgbtGreen + image[i-1][j+1].rgbtGreen + image[i][j-1].rgbtGreen + image[i][j].rgbtGreen + image[i][j+1].rgbtGreen + image[i+1][j-1].rgbtGreen + image[i+1][j].rgbtGreen + image[i+1][j+1].rgbtGreen / (i + j));
            int blurBlue = (image[i-1][j-1].rgbtBlue + image[i-1][j].rgbtBlue + image[i-1][j+1].rgbtBlue + image[i][j-1].rgbtBlue + image[i][j].rgbtBlue + image[i][j+1].rgbtBlue + image[i+1][j-1].rgbtBlue + image[i+1][j].rgbtBlue + image[i+1][j+1].rgbtBlue /  (i + j));
            image[i][j].rgbtRed = blurRed;
            image[i][j].rgbtGreen = blurGreen;
            image[i][j].rgbtBlue = blurBlue;
        }
    }
    return;
}
1

For starters, the loops never run. That's why the output looks like the original image. Look at the test conditions in the for loop:

for (int i = 0; i < height && i > 0; i++)
{
    for (int j = 0; j < width && j > 0; j++)

If i is initialized as 0 and the test condition is that i>0, then the loop never starts.

Other programming notes: Why create image2 if it's not going to be used? I'm surprised it didn't generate an unused var warning. It isn't used in the code, but it should be used.

The next problem is that the code will alter each cell in the original image as it goes. That means that every subsequent cell will be blurred using the contents of previously blurred cells and not the original image.

That should get you started.

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

2
  • Thanks! That was definitely helpful! I have removed the >0 in the for loops so that now the loop will start. However, I am back to my old issue (which is why I put the >0 in...) of having a runtime error because it is trying to use pixels that don't exist from beyond the edges of the image. Is there a way to easily avoid this? I saw that others used a bunch of different loops (for the corners etc) but I would like to avoid having to do that if there is a better way? – user29490 Aug 17 '20 at 16:28
  • Can you please help point me in the next step? – user29490 Aug 18 '20 at 4:49

You must log in to answer this question.

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