0

I can't catch the error in my code, I think that I 'm using the right Algorithm and approach.

    // creating a copy to apply blur on it
RGBTRIPLE copy[height][width];

// initializing avaerages' variables
int avgRed, avgGreen, avgBlue;

// looping for height and width
for (int i = 0; i < height; i++)
{
    for (int p = 0; p < width; p++)
    {
        // declaring pixels used in average
        RGBTRIPLE pixel = image[i][p];
        RGBTRIPLE top = image[i-1][p];
        RGBTRIPLE bottom = image[i+1][p];
        RGBTRIPLE right = image[i][p+1];
        RGBTRIPLE left = image[i][p-1];
        RGBTRIPLE tRight = image[i-1][p+1];
        RGBTRIPLE tLeft = image[i-1][p-1];
        RGBTRIPLE bRight = image[i+1][p+1];
        RGBTRIPLE bLeft = image[i+1][p-1];

        //middle pixel
        if (i != 0 && p != 0 && i < height - 1 && p < width - 1 )
        {
            // averaging
            avgRed = round(((float) pixel.rgbtRed +(float) bottom.rgbtRed
            +(float) top.rgbtRed +(float) left.rgbtRed +(float) right.rgbtRed +(float) tLeft.rgbtRed
            +(float) tRight.rgbtRed +(float) bRight.rgbtRed +(float) bLeft.rgbtRed) / 9);

            avgGreen = round(((float) pixel.rgbtGreen +(float) bottom.rgbtGreen
            +(float) top.rgbtGreen +(float) left.rgbtGreen + (float) right.rgbtGreen+(float) tLeft.rgbtGreen
            +(float) tRight.rgbtGreen +(float) bRight.rgbtGreen +(float) bLeft.rgbtGreen) / 9);

            avgBlue = round(((float) pixel.rgbtBlue +(float) bottom.rgbtBlue +(float) top.rgbtBlue
            +(float) left.rgbtBlue +(float) right.rgbtBlue +(float) tLeft.rgbtBlue
            +(float) tRight.rgbtBlue +(float) bRight.rgbtBlue +(float) bLeft.rgbtBlue) / 9);
        }

        // top-left corner pixel
        if (i == 0 && p ==0)
        {
            // averaging
            avgRed = round(((float) pixel.rgbtRed + (float) bottom.rgbtRed +(float) bRight.rgbtRed
            +(float) right.rgbtRed) / 4);

            avgGreen = round(((float) pixel.rgbtGreen + (float) bottom.rgbtGreen +(float) bRight.rgbtGreen
            +(float) right.rgbtGreen) / 4);

            avgBlue = round(((float) pixel.rgbtBlue + (float) bottom.rgbtBlue +(float) bRight.rgbtBlue
            +(float) right.rgbtBlue) / 4);
        }

        // top-right corner pixel
        if (i == 0 && p == width-1)
        {
            // averaging
            avgRed = round(((float) pixel.rgbtRed + (float) bLeft.rgbtRed +(float) bottom.rgbtRed
            +(float) left.rgbtRed) / 4);

            avgGreen = round(((float) pixel.rgbtGreen + (float) bLeft.rgbtGreen +(float) bottom.rgbtGreen
            +(float) left.rgbtGreen) / 4);

            avgBlue = round(((float) pixel.rgbtBlue + (float) bLeft.rgbtBlue +(float) bottom.rgbtBlue
            +(float) left.rgbtBlue) / 4);
        }

        // bottom-left corner pixel
        if (i == height - 1 && p ==0)
        {
            // averaging
            avgRed = round(((float) pixel.rgbtRed + (float) right.rgbtRed +(float) tRight.rgbtRed
            +(float) top.rgbtRed) / 4);

            avgGreen = round(((float) pixel.rgbtGreen + (float) right.rgbtGreen +(float) tRight.rgbtGreen
            +(float) top.rgbtGreen) / 4);

            avgBlue = round(((float) pixel.rgbtBlue + (float) right.rgbtBlue +(float) tRight.rgbtBlue
            +(float) top.rgbtBlue) / 4);
        }

        // bottom-right corner pixel
        if (i == height - 1 && p == width - 1)
        {
            // averaging
            avgRed = round(((float) pixel.rgbtRed + (float) left.rgbtRed +(float) tLeft.rgbtRed
            +(float) top.rgbtRed) / 4);

            avgGreen = round(((float) pixel.rgbtGreen + (float) left.rgbtGreen +(float) tLeft.rgbtGreen
            +(float) top.rgbtGreen) / 4);

            avgBlue = round(((float) pixel.rgbtBlue + (float) left.rgbtBlue +(float) tLeft.rgbtBlue
            +(float) top.rgbtBlue) / 4);
        }

        // top row
        if (i == 0 && p != 0 && p != width - 1)
        {
            // averaging
            avgRed = round(((float) pixel.rgbtRed + (float) left.rgbtRed +(float) right.rgbtRed
            +(float) bLeft.rgbtRed +(float) bRight.rgbtRed +(float) bottom.rgbtRed) / 6);

            avgGreen = round(((float) pixel.rgbtGreen + (float) left.rgbtGreen +(float) right.rgbtGreen
            +(float) bLeft.rgbtGreen +(float) bRight.rgbtRed +(float) bottom.rgbtRed) / 6);

            avgBlue = round(((float) pixel.rgbtBlue + (float) left.rgbtBlue +(float) right.rgbtBlue
            +(float) bLeft.rgbtBlue +(float) bRight.rgbtRed +(float) bottom.rgbtRed) / 6);
        }

        // bottom row
        if (i == height - 1 && p != 0 && p != width - 1)
        {
            // averaging
            avgRed = round(((float) pixel.rgbtRed + (float) right.rgbtRed +(float) left.rgbtRed
            +(float) top.rgbtRed +(float) tLeft.rgbtRed +(float) tRight.rgbtRed) / 6);

            avgGreen = round(((float) pixel.rgbtGreen + (float) right.rgbtGreen +(float) left.rgbtGreen
            +(float) top.rgbtGreen +(float) tLeft.rgbtGreen +(float) tRight.rgbtGreen) / 6);

            avgBlue = round(((float) pixel.rgbtBlue + (float) right.rgbtBlue +(float) left.rgbtBlue
            +(float) top.rgbtBlue +(float) tLeft.rgbtRed +(float) tRight.rgbtRed) / 6);
        }

        // right row
        if (i != 0 && i != height - 1 && p == width - 1)
        {
            // averaging
            avgRed = round(((float) pixel.rgbtRed + (float) bottom.rgbtRed +(float) top.rgbtRed
            +(float) tLeft.rgbtRed +(float) bLeft.rgbtRed +(float) left.rgbtRed) / 6);

            avgGreen = round(((float) pixel.rgbtGreen + (float) bottom.rgbtGreen +(float) top.rgbtGreen
            +(float) tLeft.rgbtGreen +(float) bLeft.rgbtGreen +(float) left.rgbtGreen) / 6);

            avgBlue = round(((float) pixel.rgbtBlue + (float) bottom.rgbtBlue +(float) top.rgbtBlue
            +(float) tLeft.rgbtBlue +(float) bLeft.rgbtRed +(float) left.rgbtRed) / 6);
        }

        // left row
        if (i != 0 && i != height - 1 && p == 0)
        {
            // averaging
            avgRed = round(((float) pixel.rgbtRed + (float) bottom.rgbtRed +(float) top.rgbtRed
            +(float) right.rgbtRed +(float) tRight.rgbtRed +(float) bRight.rgbtRed) / 6);

            avgGreen = round(((float) pixel.rgbtGreen + (float) bottom.rgbtGreen +(float) top.rgbtGreen
            +(float) right.rgbtGreen +(float) tRight.rgbtGreen +(float) bRight.rgbtGreen) / 6);

            avgBlue = round(((float) pixel.rgbtBlue + (float) bottom.rgbtBlue +(float) top.rgbtBlue
            +(float) right.rgbtBlue +(float) tRight.rgbtRed +(float) bRight.rgbtRed) / 6);
        }

        // assigning averages to copy image
        copy[i][p].rgbtRed = avgRed;
        copy[i][p].rgbtGreen = avgGreen;
        copy[i][p].rgbtBlue = avgBlue;
    }
}

// setting the original image as the blured copy image
for (int i = 0; i < height; i++)
{
    for (int p = 0; p < width; p++)
    {
        image[i][p] = copy[i][p];
    }
}
return;

Check50 output

:) blur correctly filters middle pixel
:( blur correctly filters pixel on edge
expected "80 95 105\n", not "80 88 95\n"
:) blur correctly filters pixel in corner
:( blur correctly filters 3x3 image
expected "70 85 95\n80 9...", not "70 85 95\n80 8..."
:( blur correctly filters 4x4 image
expected "70 85 95\n80 9...", not "70 85 95\n80 8..."

from check50, the error should be in right, left, top or bottom edges. But I can't catch any.

0

Recall that in the class, David and Brian used an example of two glasses with liquids. To change the liquids without changing the place of the glasses it was necessary a third glass, otherwise, the liquids would be mixed.

Looks like you're using the original image to do the math and changing the value of each pixel in the process. If you don't use a copy of the image, you'll end up doing the math using the new value of a pixel instead of the original value.

In conclusion, it's best if you use a copy of the image to calculate the average of a 3x3 grid, and then change the value of that pixel in the original image.

3
  • Actually I 've been working on an updated approach for the same algorithm, I 've edited the post to contain the new code Oct 13 at 16:34
  • I totally understand what you are talking about, I will work on getting more simple approach and also will try doing what you say as you are absolutely right. Oct 13 at 16:37
  • I 've edited the question with the code's latest version. Oct 13 at 22:59
0

It was a small typo in the code. Thanks.

You must log in to answer this question.

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