0

i have been working on blur and it works just fine as i run it. but when i run check50 i get this :

:( blur correctly filters middle pixel expected "127 140 149\n", not "145 160 169\n"
:( blur correctly filters pixel on edge expected "80 95 105\n", not "90 106 116\n"
:) blur correctly filters pixel in corner
:( blur correctly filters 3x3 image expected "70 85 95\n80 9...", not "70 85 95\n90 1..."
:( blur correctly filters 4x4 image expected "70 85 95\n80 9...", not "70 85 95\n90 1..."

it seems that the rgb values differ, i am probably doing the arithmetic wrong but i really cant see it anymore. any kind of help will be appreciated because i am literally blind right now

here is my code:

float br, bg, bb;

for (int i = 0; i < height; i++)
{
    for (int j = 0; j < width; j++)
    {
        // top left corner
        if((i == 0) && (j == 0))
        {
            br = (image[i][j].rgbtRed + image[i][j + 1].rgbtRed + image[i + 1][j].rgbtRed + image[i + 1][j + 1].rgbtRed) / 4.0;
            bg = (image[i][j].rgbtGreen + image[i][j + 1].rgbtGreen + image[i + 1][j].rgbtGreen + image[i + 1][j + 1].rgbtGreen) / 4.0;
            bb = (image[i][j].rgbtBlue + image[i][j + 1].rgbtBlue + image[i + 1][j].rgbtBlue + image[i + 1][j + 1].rgbtBlue) / 4.0;

            image[i][j].rgbtRed = round(br);
            image[i][j].rgbtGreen = round(bg);
            image[i][j].rgbtBlue = round(bb);
        }
        // top right corner
        else if((i == 0) && (j == width - 1))
        {
            br = (image[i][j].rgbtRed + image[i][j - 1].rgbtRed + image[i + 1][j].rgbtRed + image[i + 1][j - 1].rgbtRed) / 4.0;
            bg = (image[i][j].rgbtGreen + image[i][j - 1].rgbtGreen + image[i + 1][j].rgbtGreen + image[i + 1][j - 1].rgbtGreen) / 4.0;
            bb = (image[i][j].rgbtBlue + image[i][j - 1].rgbtBlue + image[i + 1][j].rgbtBlue + image[i + 1][j - 1].rgbtBlue) / 4.0;

            image[i][j].rgbtRed = round(br);
            image[i][j].rgbtGreen = round(bg);
            image[i][j].rgbtBlue = round(bb);
        }
        //bottom left corner
        else if((i == height - 1) && (j == 0))
        {
            br = (image[i][j].rgbtRed + image[i][j + 1].rgbtRed + image[i - 1][j].rgbtRed + image[i - 1][j + 1].rgbtRed) / 4.0;
            bg = (image[i][j].rgbtGreen + image[i][j + 1].rgbtGreen + image[i - 1][j].rgbtGreen + image[i - 1][j + 1].rgbtGreen) / 4.0;
            bb = (image[i][j].rgbtBlue + image[i][j + 1].rgbtBlue + image[i - 1][j].rgbtBlue + image[i - 1][j + 1].rgbtBlue) / 4.0;

            image[i][j].rgbtRed = round(br);
            image[i][j].rgbtGreen = round(bg);
            image[i][j].rgbtBlue = round(bb);
        }
        //bottom right corner
        else if((i == height - 1) && (j == width - 1))
        {
            br = (image[i][j].rgbtRed + image[i][j - 1].rgbtRed + image[i - 1][j].rgbtRed + image[i - 1][j - 1].rgbtRed) / 4.0;
            bg = (image[i][j].rgbtGreen + image[i][j - 1].rgbtGreen + image[i - 1][j].rgbtGreen + image[i - 1][j - 1].rgbtGreen) / 4.0;
            bb = (image[i][j].rgbtBlue + image[i][j - 1].rgbtBlue + image[i - 1][j].rgbtBlue + image[i - 1][j - 1].rgbtBlue) / 4.0;

            image[i][j].rgbtRed = round(br);
            image[i][j].rgbtGreen = round(bg);
            image[i][j].rgbtBlue = round(bb);
        }
        // left side edges
        else if(j == 0)
        {
            br = (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;
            bg = (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;
            bb = (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;

            image[i][j].rgbtRed = round(br);
            image[i][j].rgbtGreen = round(bg);
            image[i][j].rgbtBlue = round(bb);
        }
        // right side edges
        else if (j == width - 1)
        {
            br = (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;
            bg = (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;
            bb = (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;

            image[i][j].rgbtRed = round(br);
            image[i][j].rgbtGreen = round(bg);
            image[i][j].rgbtBlue = round(bb);
        }
        // top side edges
        else if(i == 0)
        {
            br = (image[i][j].rgbtRed + image[i][j + 1].rgbtRed + image[i][j - 1].rgbtRed + image[i + 1][j].rgbtRed + image[i + 1][j + 1].rgbtRed + image[i + 1][j - 1].rgbtRed) / 6.0;
            bg = (image[i][j].rgbtGreen + image[i][j + 1].rgbtGreen + image[i][j - 1].rgbtGreen + image[i + 1][j].rgbtGreen + image[i + 1][j + 1].rgbtGreen + image[i + 1][j - 1].rgbtGreen) / 6.0;
            bb = (image[i][j].rgbtBlue + image[i][j + 1].rgbtBlue + image[i][j - 1].rgbtBlue + image[i + 1][j].rgbtBlue + image[i + 1][j + 1].rgbtBlue + image[i + 1][j - 1].rgbtBlue) / 6.0;

            image[i][j].rgbtRed = round(br);
            image[i][j].rgbtGreen = round(bg);
            image[i][j].rgbtBlue = round(bb);
        }
        // bottom side edges
        else if(i == height - 1)
        {
            br = (image[i][j].rgbtRed + image[i][j + 1].rgbtRed + image[i][j - 1].rgbtRed + image[i - 1][j].rgbtRed + image[i - 1][j + 1].rgbtRed + image[i - 1][j - 1].rgbtRed) / 6.0;
            bg = (image[i][j].rgbtGreen + image[i][j + 1].rgbtGreen + image[i][j - 1].rgbtGreen + image[i - 1][j].rgbtGreen + image[i - 1][j + 1].rgbtGreen + image[i - 1][j - 1].rgbtGreen) / 6.0;
            bb = (image[i][j].rgbtBlue + image[i][j + 1].rgbtBlue + image[i][j - 1].rgbtBlue + image[i - 1][j].rgbtBlue + image[i - 1][j + 1].rgbtBlue + image[i - 1][j - 1].rgbtBlue) / 6.0;

            image[i][j].rgbtRed = round(br);
            image[i][j].rgbtGreen = round(bg);
            image[i][j].rgbtBlue = round(bb);
        }
        // none of the above
        else
        {
            br = (image[i][j].rgbtRed + image[i][j + 1].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].rgbtRed + image[i + 1][j - 1].rgbtRed + image[i + 1][j + 1].rgbtRed) / 9.0;
            bg = (image[i][j].rgbtGreen + image[i][j + 1].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].rgbtGreen + image[i + 1][j - 1].rgbtGreen + image[i + 1][j + 1].rgbtGreen) / 9.0;
            bb = (image[i][j].rgbtBlue + image[i][j + 1].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].rgbtBlue + image[i + 1][j - 1].rgbtBlue + image[i + 1][j + 1].rgbtBlue) / 9.0;

            image[i][j].rgbtRed = round(br);
            image[i][j].rgbtGreen = round(bg);
            image[i][j].rgbtBlue = round(bb);
        }
    }
}
return;

i know it is hard to read and i am sorry for all the damage i made to your eyes

1 Answer 1

1

This is a great example of why it's critical to test raw data and not just depend on "it looks right". Yes, the image changed, but not correctly.

Can't see the forest for the trees? ;-) No, I don't think it's the math. TBH, I didn't check the math because I see a bigger, common problem with the logic. The code is corrupting the data used to generate the final image.

To calculate the blur for any pixel, the code needs to use the original values from all the surrounding pixels. This code is calculating blur and putting the calculated blur values back into the same image array. That means that for subsequent pixels, some of the surrounding pixels will be original while others will be blurred. The only pixel that will be sure to be correct is the upper left corner pixel.

The code needs to put the blurred pixels into a new array, not back into the original one, and then output the new array as the image.

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

1
  • thank you soo much ! Commented Jun 17, 2021 at 9:43

You must log in to answer this question.

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