0

I can't seen to find what is wrong with blur in my code. My code seems fine and it compiles too but the image doesn't get as blurred as it should. I've surfed through almost all the blur related questions here but my error is still not solved.

Please help me find the error in my code. Down below are the error messages that check50 is giving me:

:( blur correctly filters middle pixel expected "127 140 149\n", not "105 120 130\n"

:( blur correctly filters pixel on edge expected "80 95 105\n", not "76 91 101\n"

:) blur correctly filters pixel in corner

:( blur correctly filters 3x3 image expected "70 85 95\n80 9...", not "70 85 95\n76 9..."

:( blur correctly filters 4x4 image expected "70 85 95\n80 9...", not "70 85 95\n76 9..."

Here is my code so far:

// Blur image

void blur(int height, int width, RGBTRIPLE image[height][width]) { int avgred = 0, avggreen = 0, avgblue = 0; // initialzing the values float counter = 0.00; RGBTRIPLE temp[height][width];

for (int i = 0; i < height; i++)  // takes care of height
{
    for (int j = 0; j < width; j++)  // takes care of width
    {
       for (int m = -1; m < 2; m++)  // takes care of the columns of the 3x3 matrix
       {
           for (int n = -1; n < 2; n++)  // takes care of the rows of the 3x3 matrix
           {
                if (i + m < 0 || i + m > height)  // checking to see if the height matches the criteria in question
                {
                    continue;
                }
                if(j + n < 0 || j + n > width)  // checking for width instead of height
                {
                    continue;
                }

                // blurring process begins
                avgred+= image[i + m][j + n].rgbtRed;
                avggreen+= image[i + m][j + n].rgbtGreen;
                avgblue+= image[i + m][j + n].rgbtBlue;
                counter++;

                temp[i][j].rgbtRed = round(avgred / counter);
                temp[i][j].rgbtGreen = round(avggreen / counter);
                temp[i][j].rgbtBlue = round(avgblue / counter);
            }
        }
    }
}

 // final output
 for (int i = 0; i < height; i++)
{
    for (int j = 0; j < width; j++)
    {
        image[i][j].rgbtRed = temp[i][j].rgbtRed;
        image[i][j].rgbtGreen = temp[i][j].rgbtGreen;
        image[i][j].rgbtBlue = temp[i][j].rgbtBlue;
    }
}
return;

}

1 Answer 1

1

You are averaging the values in the wrong place. You have to calculate the average after you have added all the values around the current pixel.

Don't forget to reset the variables for the following iterations.

5
  • so this basically means that I just had to take the averaging lines out of my for loop and put them after the loop ends, right? Thank you so much for helping my out with this!
    – Heta
    Commented Jun 14, 2020 at 1:35
  • UPDATE: I did exactly what you said and now my code is blurring the image too :) but I'm still getting a runtime error message that says "index 600 out of bounds for RGBTTRIPLE [width]"
    – Heta
    Commented Jun 14, 2020 at 2:00
  • UPDATE2: I fixed the runtime error too by changing the conditions in my if statements to height/width - 1
    – Heta
    Commented Jun 14, 2020 at 3:13
  • what were changes you did in your if statements ?? facing the same problem here. Commented Sep 20, 2020 at 12:01
  • your fix in the if statements doesn't remove the runtime error Commented Sep 20, 2020 at 12:09

You must log in to answer this question.

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