0

I am trying to solve the final part of problem set 4 doing CS50, which is to blur a picture using the neighbouring pixels. I don't understand why my code is not working.

I am supposed to use the neighbouring pixels, and take into account that the pixels on the edges shouldn't go outside the boundaries.

So I have tried to make different loops for the top left, top right, bottom left, and bottom right pixels.

I have also made separate loops for the top row, bottom row, left column, and right column.

Then I made a loop for all the pixels in between.

void blur(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE Bluenew;
    RGBTRIPLE Rednew;
    RGBTRIPLE Greennew;

    for (int row = 0; row <= height -1; row ++)
    {
        for (int column = 0; column <= width - 1; width ++)
        {

            if (row == 0 && column == 0)
            {
                image[row][column].Bluenew = (1/4) * (image[row][column].rgbtBlue + image[row][column+1].rgbtBlue + image[row+1][column].rgbtBlue + image[row+1][column+1].rgbtBlue);
                image[row][column].Rednew = (1/4) * (image[row][column].rgbtRed + image[row][column+1].rgbtRed + image[row+1][column].rgbtRed + image[row+1][column+1].rgbtRed);
                image[row][column].Greennew = (1/4) * (image[row][column].rgbtGreen + image[row][column+1].rgbtGreen + image[row+1][column].rgbtGreen + image[row+1][column+1].rgbtGreen);
            }

            else if (row == 0 && column == width - 1)
            {
                image[row][column].Bluenew = (1/4) * (image[row][column-1].rgbtBlue + image[row][column].rgbtBlue + image[row+1][column-1].rgbtBlue + image[row+1][column].rgbtBlue);
                image[row][column].Rednew = (1/4) * (image[row][column-1].rgbtRed + image[row][column].rgbtRed + image[row+1][column-1].rgbtRed + image[row+1][column].rgbtRed);
                image[row][column].Greennew = (1/4) * (image[row][column-1].rgbtGreen + image[row][column].rgbtGreen + image[row+1][column-1].rgbtGreen + image[row+1][column].rgbtGreen);
            }


            else if (row == height - 1 && column == 0)
            {
                image[row][column].Bluenew = (1/4) * (image[row-1][column].rgbtBlue + image[row-1][column+1].rgbtBlue + image[row][column].rgbtBlue + image[row][column+1].rgbtBlue);
                image[row][column].Rednew = (1/4) * (image[row-1][column].rgbtRed + image[row-1][column+1].rgbtRed + image[row][column].rgbtRed + image[row][column+1].rgbtRed);
                image[row][column].Greennew = (1/4) * (image[row-1][column].rgbtGreen + image[row-1][column+1].rgbtGreen + image[row][column].rgbtGreen + image[row][column+1].rgbtGreen);
            }

            else if (row == height - 1 && column == width - 1)
            {
                image[row][column].Bluenew = (1/4) * (image[row-1][column-1].rgbtBlue + image[row-1][column].rgbtBlue + image[row][column-1].rgbtBlue + image[row][column].rgbtBlue);
                image[row][column].Rednew = (1/4) * (image[row-1][column-1].rgbtRed + image[row-1][column].rgbtRed + image[row][column-1].rgbtRed + image[row][column].rgbtRed);
                image[row][column].Greennew = (1/4) * (image[row-1][column-1].rgbtGreen + image[row-1][column].rgbtGreen + image[row][column-1].rgbtGreen + image[row][column].rgbtGreen);
            }
        }
    }



for (int row = 1; row <= height -2; row ++)
    {
        for (int column = 1; column <= width - 2; width ++)
        {

            image[row][column].Bluenew = (1/9) * (image[row-1][column-1].rgbtBlue + image[row-1][column].rgbtBlue + image[row-1][column+1].rgbtBlue +
                                        image[row][column-1].rgbtBlue + image[row][column].rgbtBlue + image[row][column+1].rgbtBlue +
                                        image[row+1][column-1].rgbtBlue + image[row+1][column].rgbtBlue + image[row+1][column+1].rgbtBlue);

             image[row][column].Rednew = (1/9) * (image[row-1][column-1].rgbtRed + image[row-1][column].rgbtRed + image[row-1][column+1].rgbtRed +
                                        image[row][column-1].rgbtRed + image[row][column].rgbtRed + image[row][column+1].rgbtRed +
                                        image[row+1][column-1].rgbtRed + image[row+1][column].rgbtRed + image[row+1][column+1].rgbtRed);

             image[row][column].Greennew = (1/9) * (image[row-1][column-1].rgbtGreen + image[row-1][column].rgbtGreen + image[row-1][column+1].rgbtGreen +
                                        image[row][column-1].rgbtGreen + image[row][column].rgbtGreen + image[row][column+1].rgbtGreen +
                                        image[row+1][column-1].rgbtGreen + image[row+1][column].rgbtGreen + image[row+1][column+1].rgbtGreen);
        }
    }

         for (int row = 1; row <= height -2; row ++)
        {
            image[row][0].Bluenew = (1/6) * (image[row-1][0].rgbtBlue + image[row-1][1].rgbtBlue + image[row][0].rgbtBlue +
                                       image[row+1][0].rgbtBlue + image[row][1].rgbtBlue + image[row+1][1].rgbtBlue);

            image[row][0].Rednew = (1/6) * (image[row-1][0].rgbtRed + image[row-1][1].rgbtRed + image[row][0].rgbtRed +
                                       image[row+1][0].rgbtRed + image[row][1].rgbtRed + image[row+1][1].rgbtRed);

            image[row][0].Greennew = (1/6) * (image[row-1][0].rgbtGreen + image[row-1][1].rgbtGreen + image[row][0].rgbtGreen +
                                       image[row+1][0].rgbtGreen + image[row][1].rgbtGreen + image[row+1][1].rgbtGreen);

        }

        for (int row = 1; row <= height -2; row ++)
        {
            image[row][width-1].Bluenew = (1/6) * (image[row-1][width-2].rgbtBlue + image[row-1][width-1].rgbtBlue + image[row][width-2].rgbtBlue +
                                       image[row][width-1].rgbtBlue + image[row+1][width-2].rgbtBlue + image[row+1][width-1].rgbtBlue);

            image[row][width-1].Rednew = (1/6) * (image[row-1][width-2].rgbtRed + image[row-1][width-1].rgbtRed + image[row][width-2].rgbtRed +
                                       image[row][width-1].rgbtRed + image[row+1][width-2].rgbtRed + image[row+1][width-1].rgbtRed);

            image[row][width-1].Greennew = (1/6) * (image[row-1][width-2].rgbtGreen + image[row-1][width-1].rgbtGreen + image[row][width-2].rgbtGreen +
                                       image[row][width-1].rgbtGreen + image[row+1][width-2].rgbtGreen + image[row+1][width-1].rgbtGreen);

        }
        for (int column = 1; column <= width -2; column ++)
        {
             image[0][column].Bluenew= (1/6) * (image[0][column-1].rgbtBlue + image[0][column].rgbtBlue + image[0][column+1].rgbtBlue +
                                            image[1][column-1].rgbtBlue + image[1][column].rgbtBlue + image[1][column+1].rgbtBlue);

             image[0][column].Rednew = (1/6) * (image[0][column-1].rgbtRed + image[0][column].rgbtRed + image[0][column+1].rgbtRed +
                                            image[1][column-1].rgbtRed + image[1][column].rgbtRed + image[1][column+1].rgbtRed);

             image[0][column].Greennew = (1/6) * (image[0][column-1].rgbtGreen + image[0][column].rgbtGreen + image[0][column+1].rgbtGreen +
                                            image[1][column-1].rgbtGreen + image[1][column].rgbtGreen + image[1][column+1].rgbtGreen);
        }

          for (int column = 1; column <= width -2; column ++)
        {
             image[height-1][column].Bluenew = (1/6) * (image[height-2][column-1].rgbtBlue + image[height-2][column].rgbtBlue + image[height-2][column+1].rgbtBlue +
                                            image[height-1][column-1].rgbtBlue + image[height-1][column].rgbtBlue + image[height-1][column+1].rgbtBlue);

             image[height-1][column].Rednew = (1/6) * (image[height-2][column-1].rgbtRed + image[height-2][column].rgbtRed + image[height-2][column+1].rgbtRed +
                                            image[height-1][column-1].rgbtRed + image[height-1][column].rgbtRed + image[height-1][column+1].rgbtRed);

             image[height-1][column].Greennew = (1/6) * (image[height-2][column-1].rgbtGreen + image[height-2][column].rgbtGreen + image[height-2][column+1].rgbtGreen +
                                            image[height-1][column-1].rgbtGreen + image[height-1][column].rgbtGreen + image[height-1][column+1].rgbtGreen);
        }

    return;
}

// RGBTRIPLE is defined like this:

typedef struct
{
    BYTE  rgbtBlue;
    BYTE  rgbtGreen;
    BYTE  rgbtRed;
    BYTE  Bluenew;
    BYTE  Greennew;
    BYTE  Rednew;
} __attribute__((__packed__))
RGBTRIPLE;
David
  • 159
  • 8
  • 4
    `(1/9)` This is always 0. This calculation is done using integer division which chops any fractions. Use `(1.0/9)` to make is a `double` division – Gerhardh Sep 10 '22 at 13:31
  • Where do the new members `Bluenew` etc in your struct come from? Did you add them? – Gerhardh Sep 10 '22 at 13:33
  • It does not make much sense to use 2 separate loops. Instead just add corresponding `if()` parts into the first loop. – Gerhardh Sep 10 '22 at 13:41
  • 1
    If you don't want to mess with floating point then divide in last operation: `(a+b+c)/4`. And if you want to get e.g. 3/4 then multiply first: `3*a/4` – dimich Sep 10 '22 at 13:41
  • 1
    You might also need to use rounding after dividing the sum of your pixel values. – Gerhardh Sep 10 '22 at 13:43
  • @dimich that suggestion will make rounding harder. – Gerhardh Sep 10 '22 at 13:46
  • Thank you. I have now used rounding, and included (1.0/9) as well as for the other divisions. I get a segmentation fault (core dumped) as a message when I run it. – David Sep 10 '22 at 13:55
  • The struct was already given to me (some of it was used in an earlier part of the problem set), but I added Bluenew, Rednew and Greennew to it. – David Sep 10 '22 at 13:59
  • @Gerhardh In what case integer multiplication and division will make inaccuracy more than 1 compared to floating point division and rounding? – dimich Sep 10 '22 at 14:01
  • 2
    You seem to miss a very important point: The structure matches exactly how the pixel values are stored in the BMP file. You mustn't mess with that struct by adding some new members. Instead you must create a copy of that array and use that during calculation – Gerhardh Sep 10 '22 at 14:04
  • @dimich I don't think it will be more than 1 but as the CS50 course checker will check the exact pixel values, "off by one" is still off. You might avoid that by adding 3,5 or 8 depending on the pixel location before you divide. – Gerhardh Sep 10 '22 at 14:06
  • Does this answer your question? [What is the behavior of integer division?](https://stackoverflow.com/questions/3602827/what-is-the-behavior-of-integer-division) – Gerhardh Sep 10 '22 at 14:17

0 Answers0