0

I am currently working at the blur filter. While the other task were pretty easy, this one ist very much difficult. I have spend hours looking for the mistake - but without success, I tried to do it other ways but this was the only one were I didnt get a memory error. I am thankful for any advices!!!

This is my code:

void blur(int height, int width, RGBTRIPLE image[height][width])
{
    //making temp array to store changed pixels
    RGBTRIPLE temp[height][width];

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            //making temps that can store
            //the rgbt values
            int red = 0;
            int green = 0;
            int blue = 0;

            //making a loop to take the the last, next and current values
            for (int k = 0; k < 3; k++)
            {
                //if j = 0 or width, there is no value at k == 0 or == 2
                if (j == 0 || k == 0)
                    continue;
                if (j == width || k == 2)
                    continue;
                //if thats not the case, the base values can be added
                red += image[i][k].rgbtRed;
                blue += image[i][k].rgbtBlue;
                green += image[i][k].rgbtGreen;

                //if we are at the top/bottom, there is nothing higher or deeper, s[enter image description here][1]o dont take those values into account
                if (i == 0)
                {
                        red += image[i++][k].rgbtRed;
                        blue += image[i++][k].rgbtBlue;
                        green += image[i++][k].rgbtGreen;

                }
                else if (i == height)
                {
                        red += image[i--][k].rgbtRed;
                        blue += image[i--][k].rgbtBlue;
                        green += image[i--][k].rgbtGreen;
                }
                //usual case
                else
                {
                        red += image[i--][k].rgbtRed;
                        blue += image[i--][k].rgbtBlue;
                        green += image[i--][k].rgbtGreen;

                        red += image[i++][k].rgbtRed;
                        blue += image[i++][k].rgbtBlue;
                        green += image[i++][k].rgbtGreen;
                }
            }

            //after all values for 9 bytes have been calculated, they are stored in the temp array
            if (round(red / 9) > 255)
                temp[i][j].rgbtRed = 255;
            else
                temp[i][j].rgbtRed = red / 9;

            if (round(blue / 9) > 255)
                temp[i][j].rgbtBlue = 255;
            else
                temp[i][j].rgbtBlue = blue / 9;
            if (round(green / 9) > 255)
                temp[i][j].rgbtGreen = 255;
            else
                temp[i][j].rgbtGreen = green / 9;

        }
    }

    //assigning all values from temp array to og array
    for (int j = 0; j < height; j++)
    {
        for (int i = 0; i < width; i++)
        {
            image[j][i].rgbtRed = temp[j][i].rgbtRed;
            image[j][i].rgbtGreen = temp[j][i].rgbtGreen;
            image[j][i].rgbtBlue = temp[j][i].rgbtBlue;
        }
    }
}

The input picture:

This is the input

This is the output I am getting:

This is the ouput

the check message from check50

:( blur correctly filters middle pixel
    expected "127 140 149\n", not "29 37 24\n"
:( blur correctly filters pixel on edge
    expected "80 95 105\n", not "0 0 255\n"
:( blur correctly filters pixel in corner
    expected "70 85 95\n", not "0 0 0\n"
:( blur correctly filters 3x3 image
    expected "70 85 95\n80 9...", not "0 0 0\n0 0 255..."
:( blur correctly filters 4x4 image
    expected "70 85 95\n80 9...", not "0 0 0\n127 255..."

Here is the link for the complete report: https://submit.cs50.io/check50/988107f8dc294808a995efc11a3824f24bd053e9

1

A couple of issues:

  1. You say "if j = 0 or width, there is no value at k == 0 or == 2", but include k == 0 and k == 2 as conditions to skip an iteration with OR logic. So the loop will skip whenever k is 0 or 2 regardless of the value of j anyway. In effect k will only ever be 1.
  2. You're treating k as an offset to j, but you use k directly as an indice. Combined with issue 1, every lookup will be to a pixel at [?][1].
  3. The increment/decrement operator is used on i when looking up neighbouring pixels. This will actually change the value of i for the next lookup and future loop iterations, an issue that will keep compounding itself.
1
  • Thanks, improved the output by a lot!!! Still having a couple mistakes but I think i can track them down – CodingForFun Nov 19 '20 at 11:07

You must log in to answer this question.

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