0

I finished the other filters and just the blur filter is giving errors. The errors are the following:

:) blur correctly filters pixel on edge
:( blur correctly filters pixel in corner
    expected "70 85 95\n", not "47 64 71\n"
:( blur correctly filters 3x3 image
    expected "70 85 95\n80 9...", not "44 73 71\n80 9..."
:( blur correctly filters 4x4 image
    expected "70 85 95\n80 9...", not "46 60 79\n80 9..."  "

The code is the following:

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
    //new image for making the changes and keep the original image intact
    RGBTRIPLE(*blurimage)[width] = calloc(height, width * sizeof(RGBTRIPLE));
    //creating floats to store the average of which color
    float blurRed, blurGreen, blurBlue;
    //iterate over the pixels
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            // condition for the left up edge
            if (i == 0 && j == 0)
            {
                blurRed = round((image[i][j].rgbtRed + image[i][j + 1].rgbtRed + image[i + 1][j].rgbtRed + image[i + 1][j + 1].rgbtRed) / 4.0);
                blurGreen = round((image[i][j].rgbtGreen + image[i][j + 1].rgbtGreen + image[i + 1][j].rgbtGreen + image[i + 1][j + 1].rgbtGreen) / 4.0);
                blurBlue = round((image[i][j].rgbtBlue + image[i][j + 1].rgbtBlue + image[i + 1][j].rgbtBlue + image[i + 1][j + 1].rgbtBlue) / 4.0);
                blurimage[i][j].rgbtRed = blurRed;
                blurimage[i][j].rgbtGreen = blurGreen;
                blurimage[i][j].rgbtBlue = blurBlue;
            }
            // condition for the right up edge
            else if ((i == 0) && (j == (width -1)))
            {
                blurRed = round((image[i][j].rgbtRed + image[i][j - 1].rgbtRed + image[i + 1][j].rgbtRed + image[i + 1][j - 1].rgbtRed) / 4.0);
                blurGreen = round((image[i][j].rgbtGreen + image[i][j - 1].rgbtGreen + image[i + 1][j].rgbtGreen + image[i + 1][j - 1].rgbtGreen) / 4.0);
                blurBlue = round((image[i][j].rgbtBlue + image[i][j - 1].rgbtBlue + image[i + 1][j].rgbtBlue + image[i + 1][j - 1].rgbtBlue) / 4.0);
                blurimage[i][j].rgbtRed = blurRed;
                blurimage[i][j].rgbtGreen = blurGreen;
                blurimage[i][j].rgbtBlue = blurBlue;
            }
            // condition for the left down edge
            else if ((i == (height -1)) && (j == 0))
            {
                blurRed = round((image[i][j].rgbtRed + image[i][j + 1].rgbtRed + image[i - 1][j].rgbtRed + image[i - 1][j + 1].rgbtRed) / 4.0);
                blurGreen = round((image[i][j].rgbtGreen + image[i][j + 1].rgbtGreen + image[i - 1][j].rgbtGreen + image[i - 1][j + 1].rgbtGreen) / 4.0);
                blurBlue = round((image[i][j].rgbtBlue + image[i][j + 1].rgbtBlue + image[i - 1][j].rgbtBlue + image[i - 1][j + 1].rgbtBlue) / 4.0);
                blurimage[i][j].rgbtRed = blurRed;
                blurimage[i][j].rgbtGreen = blurGreen;
                blurimage[i][j].rgbtBlue = blurBlue;
            }
            // condition for the right downedge
            else if ((i == (height -1)) && (j == (width -1)))
            {
                blurRed = round((image[i][j].rgbtRed + image[i][j - 1].rgbtRed + image[i - 1][j].rgbtRed + image[i - 1][j - 1].rgbtRed) / 4.0);
                blurGreen = round((image[i][j].rgbtGreen + image[i][j - 1].rgbtGreen + image[i - 1][j].rgbtGreen + image[i - 1][j - 1].rgbtGreen) / 4.0);
                blurBlue = round((image[i][j].rgbtBlue + image[i][j - 1].rgbtBlue + image[i - 1][j].rgbtBlue + image[i - 1][j - 1].rgbtBlue) / 4.0);
                blurimage[i][j].rgbtRed = blurRed;
                blurimage[i][j].rgbtGreen = blurGreen;
                blurimage[i][j].rgbtBlue = blurBlue;
            }
            // condition for the upper edge
            if ((i == 0) && (j != 0) && (j != (width -1)))
            {
                blurRed = round((image[i][j].rgbtRed + image[i][j + 1].rgbtRed + image[i + 1][j].rgbtRed + image[i + 1][j + 1].rgbtRed + image[i][j - 1].rgbtRed +
                image[i + 1][j - 1].rgbtRed) / 6.0);
                blurGreen = round((image[i][j].rgbtGreen + image[i][j + 1].rgbtGreen + image[i + 1][j].rgbtGreen + image[i + 1][j + 1].rgbtGreen +
                image[i][j - 1].rgbtGreen + image[i + 1][j - 1].rgbtGreen) / 6.0);
                blurBlue = round((image[i][j].rgbtBlue + image[i][j + 1].rgbtBlue + image[i + 1][j].rgbtBlue + image[i + 1][j + 1].rgbtBlue + image[i][j - 1].rgbtBlue
                + image[i + 1][j - 1].rgbtBlue) / 6.0);
                blurimage[i][j].rgbtRed = blurRed;
                blurimage[i][j].rgbtGreen = blurGreen;
                blurimage[i][j].rgbtBlue = blurBlue;
            }
            // condition for the down edge
            else if ((i == (height -1)) && (j != 0) && (j != (width -1)))
            {
                blurRed = round((image[i][j].rgbtRed + image[i][j + 1].rgbtRed + image[i - 1][j].rgbtRed + image[i - 1][j + 1].rgbtRed + image[i - 1][j].rgbtRed +
                image[i -1][j - 1].rgbtRed) / 6.0);
                blurGreen = round((image[i][j].rgbtGreen + image[i][j + 1].rgbtGreen + image[i - 1][j].rgbtGreen + image[i - 1][j + 1].rgbtGreen +
                image[i - 1][j].rgbtGreen + image[i - 1][j - 1].rgbtGreen) / 6.0);
                blurBlue = round((image[i][j].rgbtBlue + image[i][j + 1].rgbtBlue + image[i - 1][j].rgbtBlue + image[i - 1][j + 1].rgbtBlue +
                image[i - 1][j].rgbtBlue + image[i - 1][j - 1].rgbtBlue) / 6.0);
                blurimage[i][j].rgbtRed = blurRed;
                blurimage[i][j].rgbtGreen = blurGreen;
                blurimage[i][j].rgbtBlue = blurBlue;
            }
            // condition for the left edge
            else if ((j == 0) && (i != 0) && (i != (height -1)))
            {
                blurRed = round((image[i][j].rgbtRed + image[i][j + 1].rgbtRed + image[i + 1][j].rgbtRed + image[i + 1][j + 1].rgbtRed + image[i - 1][j + 1].rgbtRed +
                image[i - 1][j - 1].rgbtRed) / 6.0);
                blurGreen = round((image[i][j].rgbtGreen + image[i][j + 1].rgbtGreen + image[i + 1][j].rgbtGreen + image[i + 1][j + 1].rgbtGreen +
                image[i - 1 ][j + 1].rgbtGreen + image[i - 1][j - 1].rgbtGreen) / 6.0);
                blurBlue = round((image[i][j].rgbtBlue + image[i][j + 1].rgbtBlue + image[i + 1][j].rgbtBlue + image[i + 1][j + 1].rgbtBlue +
                image[i - 1][j + 1].rgbtBlue + image[i - 1][j - 1].rgbtBlue) / 6.0);
                blurimage[i][j].rgbtRed = blurRed;
                blurimage[i][j].rgbtGreen = blurGreen;
                blurimage[i][j].rgbtBlue = blurBlue;
            }
            // condition for the right edge
            else if ((j == (width -1)) && (i != 0) && (i != (height -1)))
            {
                blurRed = round((image[i][j].rgbtRed + image[i][j - 1].rgbtRed + image[i - 1][j].rgbtRed + image[i - 1][j - 1].rgbtRed + image[i + 1][j].rgbtRed +
                image[i + 1][j - 1].rgbtRed) / 6.0);
                blurGreen = round((image[i][j].rgbtGreen + image[i][j - 1].rgbtGreen + image[i - 1][j].rgbtGreen + image[i - 1][j - 1].rgbtGreen +
                image[i + 1][j].rgbtGreen + image[i + 1][j - 1].rgbtGreen) / 6.0);
                blurBlue = round((image[i][j].rgbtBlue + image[i][j - 1].rgbtBlue + image[i - 1][j].rgbtBlue + image[i - 1][j - 1].rgbtBlue +
                image[i + 1][j].rgbtBlue + image[i + 1][j - 1].rgbtBlue) / 6.0);
                blurimage[i][j].rgbtRed = blurRed;
                blurimage[i][j].rgbtGreen = blurGreen;
                blurimage[i][j].rgbtBlue = blurBlue;
            }
            // if not corner and not edge
            else
            {
                blurRed = round((image[i][j].rgbtRed + image[i][j - 1].rgbtRed + image[i - 1][j].rgbtRed + image[i - 1][j - 1].rgbtRed + image[i + 1][j].rgbtRed +
                image[i + 1][j - 1].rgbtRed + image[i][j + 1].rgbtRed + image[i - 1][j + 1].rgbtRed + image[i + 1][j + 1].rgbtRed) / 9.0);
                blurGreen = round((image[i][j].rgbtGreen + image[i][j - 1].rgbtGreen + image[i - 1][j].rgbtGreen + image[i - 1][j - 1].rgbtGreen +
                image[i + 1][j].rgbtGreen + image[i + 1][j - 1].rgbtGreen + image[i][j + 1].rgbtGreen + image[i - 1][j + 1].rgbtGreen + image[i + 1][j + 1].rgbtGreen) / 9.0);
                blurBlue = round((image[i][j].rgbtBlue + image[i][j - 1].rgbtBlue + image[i - 1][j].rgbtBlue + image[i - 1][j - 1].rgbtBlue + image[i + 1][j].rgbtBlue +
                image[i + 1][j - 1].rgbtBlue + image[i][j + 1].rgbtBlue + image[i - 1][j + 1].rgbtBlue + image[i + 1][j + 1].rgbtBlue) / 9.0);
                blurimage[i][j].rgbtRed = blurRed;
                blurimage[i][j].rgbtGreen = blurGreen;
                blurimage[i][j].rgbtBlue = blurBlue;
            }
        }
    }
    // coping the for the out
    for (int l = 0; l < height; l++)
    {
        for (int j = 0; j < width; j++)
        {
            image[l][j].rgbtRed = blurimage[l][j].rgbtRed;
            image[l][j].rgbtGreen = blurimage[l][j].rgbtGreen;
            image[l][j].rgbtBlue = blurimage[l][j].rgbtBlue;
        }
    }
    free(blurimage);
    return;
}

I verified if some of the positions of the pixels are wrong, but apparently they are not. The conditions seem to be correct too.

If someone could indicate what is wrong or what would make the code work, I would really appreciate.

1

Your code is close. All 4 corners have a problem, but not with the algorithm used to calculate them.
Look at the 'if' topology. After working out each of the corners, there's a plain 'if', eventually followed by an 'else' with no qualifiers. This means that when i and j are both 0, the code following the condition at line 14 is evaluated, and then the code after line 106 is evaluated, overwriting the previous (correct) values. Focus on line 54 to fix this problem.
I beleive the left and bottom edges also have a problem, although the check claims "correctly filters pixel on edge." The top and right edges match what I expect. On the left edge, where j is 0, there's a 'j - 1' used. In the calculation for the bottom edge, I see 'image[i - 1][j].rgbtRed' included twice.

1
  • Really thanks man, you are a life saver. :) blur correctly filters middle pixel :) blur correctly filters pixel on edge :) blur correctly filters pixel in corner :) blur correctly filters 3x3 image :) blur correctly filters 4x4 image – João Victor Schiavon Mar 3 at 18:49

You must log in to answer this question.

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