0

I have been trying to figure out the error for hours long and still no success, it'll mean a lot to me if someone can tell me what is the error in this code in order to apply a blur filter. I approached the blur by listing out all the cases and the changes required for each case. Thank you so much in advance.

The following is my code for the blur filter:

    void blur(int height, int width, RGBTRIPLE image[height][width])
    {
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
           if (i == 0 && j == 0 )
           {
              float blurred_red = round((image[i][j].rgbtRed + image[i][j + 1].rgbtRed + image[i + 1][j].rgbtRed + image[i + 1][j + 1].rgbtRed) / 4);
              float blurred_green = round((image[i][j].rgbtGreen + image[i][j + 1].rgbtGreen + image[i + 1][j].rgbtGreen + image[i + 1][j + 1].rgbtGreen) / 4);
              float blurred_blue = round((image[i][j].rgbtBlue + image[i][j + 1].rgbtBlue + image[i + 1][j].rgbtBlue + image[i + 1][j + 1].rgbtBlue) / 4);

              image[i][j].rgbtRed = blurred_red;
              image[i][j].rgbtGreen = blurred_green;
              image[i][j].rgbtBlue = blurred_blue;
           }
           else if (i == 0 && j == width - 1)
           {
              float blurred_red = round((image[i][j].rgbtRed + image[i][j - 1].rgbtRed + image[i + 1][j].rgbtRed + image[i + 1][j - 1].rgbtRed) / 4);
              float blurred_green = round((image[i][j].rgbtGreen + image[i][j - 1].rgbtGreen + image[i + 1][j].rgbtGreen + image[i + 1][j - 1].rgbtGreen) / 4);
              float blurred_blue = round((image[i][j].rgbtBlue + image[i][j - 1].rgbtBlue + image[i + 1][j].rgbtBlue + image[i + 1][j - 1].rgbtBlue) / 4);

              image[i][j].rgbtRed = blurred_red;
              image[i][j].rgbtGreen = blurred_green;
              image[i][j].rgbtBlue = blurred_blue;
           }
           else if (i == height - 1 && j == 0)
           {
              float blurred_red = round((image[i][j].rgbtRed + image[i][j + 1].rgbtRed + image[i - 1][j].rgbtRed + image[i - 1][j + 1].rgbtRed) / 4);
              float blurred_green = round((image[i][j].rgbtGreen + image[i][j + 1].rgbtGreen + image[i - 1][j].rgbtGreen + image[i - 1][j + 1].rgbtGreen) / 4);
              float blurred_blue = round((image[i][j].rgbtBlue + image[i][j + 1].rgbtBlue + image[i - 1][j].rgbtBlue + image[i - 1][j + 1].rgbtBlue) / 4);

              image[i][j].rgbtRed = blurred_red;
              image[i][j].rgbtGreen = blurred_green;
              image[i][j].rgbtBlue = blurred_blue;
           }
           else if (i == height - 1 && j == width - 1)
           {
              float blurred_red = round((image[i][j].rgbtRed + image[i][j - 1].rgbtRed + image[i - 1][j].rgbtRed + image[i - 1][j - 1].rgbtRed) / 4);
              float blurred_green = round((image[i][j].rgbtGreen + image[i][j - 1].rgbtGreen + image[i - 1][j].rgbtGreen + image[i - 1][j - 1].rgbtGreen) / 4);
              float blurred_blue = round((image[i][j].rgbtBlue + image[i][j - 1].rgbtBlue + image[i - 1][j].rgbtBlue + image[i - 1][j - 1].rgbtBlue) / 4);

              image[i][j].rgbtRed = blurred_red;
              image[i][j].rgbtGreen = blurred_green;
              image[i][j].rgbtBlue = blurred_blue;
           }

           else if (i == 0 && j != width - 1 && j != 0)
           {
              float blurred_red = round((image[i][j].rgbtRed + image[i][j - 1].rgbtRed + image[i][j + 1].rgbtRed + image[i + 1][j - 1].rgbtRed + image[i + 1][j].rgbtRed + image[i + 1][j + 1].rgbtRed) / 6);
              float blurred_green = round((image[i][j].rgbtGreen + image[i][j - 1].rgbtGreen + image[i][j + 1].rgbtGreen + image[i + 1][j - 1].rgbtGreen + image[i + 1][j].rgbtGreen + image[i + 1][j + 1].rgbtGreen) / 6);
              float blurred_blue = round((image[i][j].rgbtBlue + image[i][j - 1].rgbtBlue + image[i][j + 1].rgbtBlue + image[i + 1][j - 1].rgbtBlue + image[i + 1][j].rgbtBlue + image[i + 1][j + 1].rgbtBlue) / 6);
              image[i][j].rgbtRed = blurred_red;
              image[i][j].rgbtGreen = blurred_green;
              image[i][j].rgbtBlue = blurred_blue;
           }
           else if (j == 0 && i != height - 1 && i != 0)
           {
               float blurred_red = 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);
               float blurred_green = 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);
               float blurred_blue = 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);

              image[i][j].rgbtRed = blurred_red;
              image[i][j].rgbtGreen = blurred_green;
              image[i][j].rgbtBlue = blurred_blue;

           }
           else if (i == height - 1 && j != width - 1 && j != 0)
           {
               float blurred_red = round((image[i][j].rgbtRed + image[i - 1][j].rgbtRed + image[i - 1][j - 1].rgbtRed + image[i - 1][j + 1].rgbtRed + image[i][j - 1].rgbtRed + image[i][j + 1].rgbtRed) / 6);
               float blurred_green = round((image[i][j].rgbtGreen + image[i - 1][j].rgbtGreen + image[i - 1][j - 1].rgbtGreen + image[i - 1][j + 1].rgbtGreen + image[i][j - 1].rgbtGreen + image[i][j + 1].rgbtGreen) / 6);
               float blurred_blue = round((image[i][j].rgbtBlue + image[i - 1][j].rgbtBlue + image[i - 1][j - 1].rgbtBlue + image[i - 1][j + 1].rgbtBlue + image[i][j - 1].rgbtBlue + image[i][j + 1].rgbtBlue) / 6);

              image[i][j].rgbtRed = blurred_red;
              image[i][j].rgbtGreen = blurred_green;
              image[i][j].rgbtBlue = blurred_blue;
           }
           else if (j == width - 1 && i != height - 1 && i != 0)
           {
               float blurred_red = round((image[i][j].rgbtRed + image[i - 1][j].rgbtRed + image[i + 1][j].rgbtRed + image[i - 1][j - 1].rgbtRed + image[i][j - 1].rgbtRed + image[i + 1][j - 1].rgbtRed) / 6);
               float blurred_green = round((image[i][j].rgbtGreen + image[i - 1][j].rgbtGreen + image[i + 1][j].rgbtGreen + image[i - 1][j - 1].rgbtGreen + image[i][j - 1].rgbtGreen + image[i + 1][j - 1].rgbtGreen) / 6);
               float blurred_blue = round((image[i][j].rgbtBlue + image[i - 1][j].rgbtBlue + image[i + 1][j].rgbtBlue + image[i - 1][j - 1].rgbtBlue + image[i][j - 1].rgbtBlue + image[i + 1][j - 1].rgbtBlue) / 6);

              image[i][j].rgbtRed = blurred_red;
              image[i][j].rgbtGreen = blurred_green;
              image[i][j].rgbtBlue = blurred_blue;
           }
           else if (j > 0 && i > 0)
           {
               float blurred_red = round((image[i][j].rgbtRed + image[i][j - 1].rgbtRed + image[i][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 + image[i + 1][j].rgbtRed + image[i + 1][j + 1].rgbtRed) / 9);
               float blurred_green = round((image[i][j].rgbtGreen + image[i][j - 1].rgbtGreen + image[i][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 + image[i + 1][j].rgbtGreen + image[i + 1][j + 1].rgbtGreen) / 9);
               float blurred_blue = round((image[i][j].rgbtBlue + image[i][j - 1].rgbtBlue + image[i][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 + image[i + 1][j].rgbtBlue + image[i + 1][j + 1].rgbtBlue) / 9);

              image[i][j].rgbtRed = blurred_red;
              image[i][j].rgbtGreen = blurred_green;
              image[i][j].rgbtBlue = blurred_blue;
           }
       }
    }
    return;
}


1

I was facing this exact problem for a while. It's something you can solve by taking a step back and thinking in what you're doing. You take a pixel and you transform it using the information of the pixels around it. Next you take a pixel next to it ....... If the answer doesn´t come up message me and I´ll try to help you a little more.

| improve this answer | |
  • Thank you so much for your reply. Does that mean that I have to make a copy of the image before blurring? If not, please give me more hints/opinions as to how to fix this code. Thanks again. – Brian Fang Apr 20 at 2:57
  • @tritum I am stuck on this as we speak, is there anyway to construct the algorithm in a way where you don't need to list all cases? I was trying to create a variable I could increment for every pixel that gets used around the pixel you're calculating the average for as it iterates through the rows but I couldn't think of a way to accomplish that. – ayeeitsalpha Apr 21 at 20:30
  • cs50.stackexchange.com/questions/36964/pset4-filter-blur-help here is the link to my code in case you need to see it for more detail. – ayeeitsalpha Apr 21 at 20:32

You must log in to answer this question.

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