-1

I am doing a cs50 assignment filter(blur). i didnt do it really well but it is working. bluring all the pixels except the first row(north) and the final row (south) in the pic. and the corners of the picture are blured but the pixels after the corners the colors are really unusual. i dont know its the conditions or the the color changing that messed this up. so i can use another pair of eyes for finding the problem thank you [click here to see the issues in detail]

void blur(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE changed[height][width];

    for(int i=0 ; i<=height-1 ;i++)
    {
         for(int j=0 ; j<=width-1 ; j++)
         {
             if(i==0) //main if  north corners
             {
                 if (j==0)
                 {
                     changed[i][j].rgbtRed =round( (image[i][j].rgbtRed + image[i][j+1].rgbtRed + image[i+1][j+1].rgbtRed + image[i+1][j].rgbtRed) /4);
                     changed[i][j].rgbtGreen = round((image[i][j].rgbtGreen + image[i][j+1].rgbtGreen + image[i+1][j+1].rgbtGreen + image[i+1][j].rgbtGreen) /4);
                     changed[i][j].rgbtBlue = round((image[i][j].rgbtBlue + image[i][j+1].rgbtBlue + image[i+1][j+1].rgbtBlue + image[i+1][j].rgbtBlue) /4);


                 }

                 else if(j==width-1)
                 {
                     changed[i][j].rgbtRed = round((image[i][j].rgbtRed + image[i][j-1].rgbtRed + image[i+1][j-1].rgbtRed + image[i+1][j].rgbtRed) /4);
                     changed[i][j].rgbtGreen = round( (image[i][j].rgbtGreen + image[i][j-1].rgbtGreen + image[i+1][j-1].rgbtGreen + image[i+1][j].rgbtGreen) /4);
                     changed[i][j].rgbtBlue = round( (image[i][j].rgbtBlue + image[i][j-1].rgbtBlue + image[i+1][j-1].rgbtBlue + image[i+1][j].rgbtBlue) /4);
                 }

             }

             else if(i==height-1) //main if south corners
             {
                 if (j==0)
                 {
                    changed[i][j].rgbtRed = round( (image[i][j].rgbtRed + image[i-1][j].rgbtRed + image[i-1][j+1].rgbtRed + image[i][j+1].rgbtRed) /4);
                    changed[i][j].rgbtGreen = round( (image[i][j].rgbtGreen + image[i-1][j].rgbtGreen + image[i-1][j+1].rgbtGreen + image[i][j+1].rgbtGreen) /4);
                    changed[i][j].rgbtBlue = round((image[i][j].rgbtBlue + image[i-1][j].rgbtBlue + image[i-1][j+1].rgbtBlue + image[i][j+1].rgbtBlue) /4);
                 }

                 else if(j==width-1)
                 {
                    changed[i][j].rgbtRed = round ((image[i][j].rgbtRed + image[i-1][j].rgbtRed + image[i-1][j-1].rgbtRed + image[i][j-1].rgbtRed) /4);
                    changed[i][j].rgbtGreen = round ((image[i][j].rgbtGreen + image[i-1][j].rgbtGreen + image[i-1][j-1].rgbtGreen + image[i][j-1].rgbtGreen) /4);
                    changed[i][j].rgbtBlue = round ((image[i][j].rgbtBlue + image[i-1][j].rgbtBlue + image[i-1][j-1].rgbtBlue + image[i][j-1].rgbtBlue) /4);
                 }

             }

             else if(i>0 && i<height-1 && j==0) //main if left side
             {
                 changed[i][j].rgbtRed = round((image[i][j].rgbtRed + image[i-1][j].rgbtRed + image[i-1][j+1].rgbtRed + image[i][j+1].rgbtRed + image[i+1][j+1].rgbtRed + image[i+1][j].rgbtRed) /6);
                 changed[i][j].rgbtGreen = round( (image[i][j].rgbtGreen + image[i-1][j].rgbtGreen + image[i-1][j+1].rgbtGreen + image[i][j+1].rgbtGreen + image[i+1][j+1].rgbtGreen + image[i+1][j].rgbtGreen) /6);
                 changed[i][j].rgbtBlue = round ((image[i][j].rgbtBlue + image[i-1][j].rgbtBlue + image[i-1][j+1].rgbtBlue + image[i][j+1].rgbtBlue + image[i+1][j+1].rgbtBlue + image[i+1][j].rgbtBlue) /6);

             }
             else if(i>0 && i<height-1 && j==width-1) //main if right side 
             {
                 changed[i][j].rgbtRed = round ((image[i][j].rgbtRed + image[i-1][j].rgbtRed + image[i-1][j-1].rgbtRed + image[i][j-1].rgbtRed + image[i+1][j-1].rgbtRed + image[i+1][j].rgbtRed) /6);
                 changed[i][j].rgbtGreen = round ((image[i][j].rgbtGreen + image[i-1][j].rgbtGreen + image[i-1][j-1].rgbtGreen + image[i][j-1].rgbtGreen + image[i+1][j-1].rgbtGreen + image[i+1][j].rgbtGreen) /6);
                 changed[i][j].rgbtBlue = round ((image[i][j].rgbtBlue + image[i-1][j].rgbtBlue + image[i-1][j-1].rgbtBlue + image[i][j-1].rgbtBlue + image[i+1][j-1].rgbtBlue + image[i+1][j].rgbtBlue) /6);
            }

            else if(i==0 && j>0 && j<width-1) //main if north side
            {
                changed[i][j].rgbtRed = round ((image[i][j].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][j+1].rgbtRed) /6);
                changed[i][j].rgbtGreen = round((image[i][j].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][j+1].rgbtGreen) /6);
                changed[i][j].rgbtBlue = round((image[i][j].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][j+1].rgbtBlue) /6);

            }

            else if(i==height-1 && j>0 && j>width-1) //main if south side
            {
                changed[i][j].rgbtRed = round( (image[i][j].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][j+1].rgbtRed) /6);
                changed[i][j].rgbtGreen = round((image[i][j].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][j+1].rgbtGreen) /6);
                changed[i][j].rgbtBlue = round( (image[i][j].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][j+1].rgbtBlue) /6);
            }

            else //other pixels in the middle
            {
                changed[i][j].rgbtRed = round ((image[i][j].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][j+1].rgbtRed + image[i+1][j+1].rgbtRed + image[i+1][j].rgbtRed + image[i+1][j-1].rgbtRed) /9);
                changed[i][j].rgbtGreen = round ((image[i][j].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][j+1].rgbtGreen + image[i+1][j+1].rgbtGreen + image[i+1][j].rgbtGreen + image[i+1][j-1].rgbtGreen) /9);
                changed[i][j].rgbtBlue = round( (image[i][j].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][j+1].rgbtBlue + image[i+1][j+1].rgbtBlue + image[i+1][j].rgbtBlue + image[i+1][j-1].rgbtBlue) /9);
            }
         }

    }

    for (int i = 0 ;i<=height-1 ; i++)
    {
        for (int j = 0 ; j<=width-1 ; j++)
        {
        image[i][j]=changed[i][j];
        }
    }


return;

}

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Asrapian
  • 13
  • 4
  • I'm guessing rbgtRed etc is a char form. Which would cause the addition a.rgbtred + b.rebtred +.... +n.rgbtred to overflow. – mksteve Aug 08 '21 at 13:35
  • Please separate your question into sentences with punctuation. This is really hard to read. Also, explain what the code is supposed to do and show an example of where it goes wrong. – interjay Aug 08 '21 at 13:40
  • @mksteve the `char` values will be promoted to `int` for summation and the division in the same expression. I would hope they are `unsigned` though. – Weather Vane Aug 08 '21 at 14:33
  • You are doing integer division, which truncates, making the `round()` function ineffective. Please divide by `4.0` or `6.0` or `9.0`. – Weather Vane Aug 08 '21 at 14:41
  • @mksteve rgbtblue is an integer type data. it is the amount of blue color in a pixel of a picture. the pixel is located in height of i and width of j. the calculations i think is correct but i checked and changed the if statement conditions. still no good results – Asrapian Aug 08 '21 at 19:07
  • @interjay sorry my bad. i am trying to blur out a bmp formated picture with box blur method. the method is like this i need to change the color of every pixel to the average color of all the pixels surrounding the pixel. that are distant the original pixel by one pixel to get the blur effect(the centeral pixel is also included in calculating the average). – Asrapian Aug 08 '21 at 19:14
  • @mksteve there is no char value. all the data types that are used in here integer or float type values. and yeah u are right about the round function i did what u told me it solved the small bug thanks to you but the main issue still remians unsolved – Asrapian Aug 08 '21 at 19:17

2 Answers2

0

bluring all the pixels except the first row(north) and the final row (south) in the pic

If you go through the case discriminations with all those ifs and elses, you'll find that the code blocks that should handle the north and south edges between the corners are not entered after the preceding if(i==0) and if(i==height-1); indeed with that many cases that was easy to miss. You could move the code blocks or you could make the whole operation more concise, e. g.:

    for (int i = 0; i < height; ++i)
     for (int j = 0; j < width; ++j)
     {
      int r = 0, g = 0, b = 0, c = 0;   // surrounding pixel color sums and count
      for (int y = i-1; y <= i+1; ++y) if (0 <= y && y < height)
       for (int x = j-1; x <= j+1; ++x) if (0 <= x && x < width)
        r += image[y][x].rgbtRed,
        g += image[y][x].rgbtGreen,
        b += image[y][x].rgbtBlue,
        ++c;
      // now compute the rounded averages
      changed[i][j].rgbtRed   = (r+c/2)/c;
      changed[i][j].rgbtGreen = (g+c/2)/c;
      changed[i][j].rgbtBlue  = (b+c/2)/c;
     }
Armali
  • 18,255
  • 14
  • 57
  • 171
  • 1
    At first I thought the north and south rows had been omitted, but there is `else if(i==0 && j>0 && j0 && j>width-1) //main if south side`. But now I can see that `i==0` has aleady been tested, so those tests will not pass because they are in the wrong code block. – Weather Vane Aug 08 '21 at 20:24
  • True, there are, and yes, those `else if`s are not entered after the preceding `if(i==0)` and `if(i==height-1)`. However, I see I shouldn't have written _there are no code blocks that handle_ that - I'll find a better wording. Thanks for the remark! – Armali Aug 08 '21 at 20:31
  • Your solution would be clearer if you used the `{` braces `}` around each code block. A beginner should not be encouraged to leave them out as 'unnecessary'.... and although I gave the answer an UV, it is incorrect. Braces are *needed* in the inner loop. – Weather Vane Aug 08 '21 at 20:32
  • I'm afraid I can't share your view that it would be clearer with additional braces. Moreover, if a beginner is discouraged from utilizing what a language offers, he will stay a beginner for a longer time. – Armali Aug 08 '21 at 20:36
  • Leaving them out, was a mistake, not an opinion. – Weather Vane Aug 08 '21 at 20:37
  • @Weather Vane - I would be interested to read why you think _Braces are needed in the inner loop._ – Armali Aug 08 '21 at 20:37
  • @Weather Vane - Please don't deface my post - the added braces change nothing. – Armali Aug 08 '21 at 20:38
  • That is terrible coding style. It made me think it was wrong. – Weather Vane Aug 08 '21 at 20:39
  • Just a thought - perhaps you shouldn't consider a coding style _terrible_ just because you misread something. – Armali Aug 08 '21 at 20:41
  • That is what *makes* it terrible. – Weather Vane Aug 08 '21 at 20:41
  • As the comment under this [accepted answer](https://stackoverflow.com/a/17903036/4142924) says, "In other words, the comma operator is mainly useful for obfuscation." – Weather Vane Aug 08 '21 at 20:48
  • As you know, comments are not normative. ;-) And if you don't view the form without unneeded braces as clearer, you should think again whether this is really more than a personal preference. – Armali Aug 08 '21 at 20:55
  • @WeatherVane yeah i thought that was the problem too! the code will be test in first line: if statement (i==0) so it will never ever enter the loop for the north row and the south row. I removed the i conditions in south row and north row. thinking that now pixels will enter the if statement but after i tested the code on check50 it only made the matter worse and lost a point. it ruines the condition somehow. i need to change the first if statement without destorying it and that seems impossible because of how i coded the solution. so maybe because of bad coding i am stuck with this bug – Asrapian Aug 08 '21 at 21:53
  • 1
    @armali yeah thank you for pointing out the issue. and indeed the way you coded the solution is so much better in style but the beginner i am. i almost hardcoded the solution so there is no surprise that there might be issue with it. but i am afraid because i may not be able to use your solution because the algorithm suddenly changes with your solution so there might be chance that they will accuse me of crossing academic honesty policy if i could somehow change the solution without changing the theme it would be amazing. anyway thank you for your amazing solution. i will study this to learn! – Asrapian Aug 08 '21 at 22:02
  • @Armali i moved the codes and replace them on top. happily it passes another test but still fails the two other tests. you can see the tests results here below if you were interested anyway thank you for your help https://submit.cs50.io/check50/ee34648590451964b7fc6dd8cd593a535eb88d10 – Asrapian Aug 08 '21 at 22:37
  • I'd gladly have a look at the changed code, but as I cannot tell its present form from the description _moved the codes and replace them on top_, it would be helpful if you added the change to your question (not removing the original code). – Armali Aug 09 '21 at 07:36
  • 1
    @Armali you can see the changed code! i have added the code as a new answer to this problem – Asrapian Aug 09 '21 at 09:04
  • There's an `else` missing at `//main if north corners`, and there's a typo (`j > width - 1` instead of `j < width - 1`) at `//main if south side`. – Armali Aug 09 '21 at 11:06
  • 1
    @Armali wow i did what u told and it solved the entire bug and it passes all the tests. there is no more problem in it to make bugs thanks to you. wow the problems were quite simple but beacause of how bad in style the code was written. it was so easy to leave it without noticing them. and you helped a lot with finding them thank you!! – Asrapian Aug 09 '21 at 16:47
0

This is the changed code

RGBTRIPLE changed[height][width];

for (int i = 0 ; i <= height - 1 ;i++)
{
     for (int j = 0 ; j <= width - 1 ; j++)
     {
         //main if north side
        if (i == 0 && j > 0 && j < width - 1 )
        {
            changed[i][j].rgbtRed = round((image[i][j].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][j + 1].rgbtRed) / (6.0));
            changed[i][j].rgbtGreen = round((image[i][j].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][j + 1].rgbtGreen) / (6.0));
            changed[i][j].rgbtBlue = round((image[i][j].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][j + 1].rgbtBlue) / (6.0));

        }
        //main if south side
        else if (i==height - 1  && j > 0 && j > width - 1)
        {
            changed[i][j].rgbtRed = round((image[i][j].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][j + 1].rgbtRed) / (6.0));
            changed[i][j].rgbtGreen = round((image[i][j].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][j + 1].rgbtGreen) / (6.0));
            changed[i][j].rgbtBlue = round((image[i][j].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][j + 1].rgbtBlue) / (6.0));
        }


         //main if left side
         else if (i > 0 && i < height - 1 && j == 0)
         {
             changed[i][j].rgbtRed = round((image[i][j].rgbtRed + image[i - 1][j].rgbtRed + image[i - 1][j + 1].rgbtRed + image[i][j + 1].rgbtRed + image[i + 1][j + 1].rgbtRed + image[i + 1][j].rgbtRed) / 6.0);
             changed[i][j].rgbtGreen = round((image[i][j].rgbtGreen + image[i - 1][j].rgbtGreen + image[i - 1][j + 1].rgbtGreen + image[i][j + 1].rgbtGreen + image[i + 1][j + 1].rgbtGreen + image[i + 1][j].rgbtGreen) / 6.0);
             changed[i][j].rgbtBlue = round((image[i][j].rgbtBlue + image[i - 1][j].rgbtBlue + image[i - 1][j + 1].rgbtBlue + image[i][j + 1].rgbtBlue + image[i + 1][j + 1].rgbtBlue + image[i + 1][j].rgbtBlue) / 6.0);

         }
         //main if right side
         else if (i > 0 && i < height - 1 && j == width - 1)
         {
             changed[i][j].rgbtRed = round((image[i][j].rgbtRed + image[i - 1][j].rgbtRed + image[i - 1][j - 1].rgbtRed + image[i][j - 1].rgbtRed + image[i + 1][j - 1].rgbtRed + image[i + 1][j].rgbtRed) / 6.0);
             changed[i][j].rgbtGreen = round((image[i][j].rgbtGreen + image[i - 1][j].rgbtGreen + image[i - 1][j - 1].rgbtGreen + image[i][j - 1].rgbtGreen + image[i + 1][j - 1].rgbtGreen + image[i + 1][j].rgbtGreen) / 6.0);
             changed[i][j].rgbtBlue = round((image[i][j].rgbtBlue + image[i - 1][j].rgbtBlue + image[i - 1][j - 1].rgbtBlue + image[i][j - 1].rgbtBlue + image[i + 1][j - 1].rgbtBlue + image[i + 1][j].rgbtBlue) / 6.0);
         }

         //main if  north corners
         if (i == 0)
         {
             //north left corner
             if (j == 0)
             {
                 changed[i][j].rgbtRed = round((image[i][j].rgbtRed + image[i][j + 1].rgbtRed + image[i + 1][j + 1].rgbtRed + image[i + 1][j].rgbtRed) / 4.0);
                 changed[i][j].rgbtGreen = round((image[i][j].rgbtGreen + image[i][j + 1].rgbtGreen + image[i + 1][j + 1].rgbtGreen + image[i + 1][j].rgbtGreen) / 4.0);
                 changed[i][j].rgbtBlue = round((image[i][j].rgbtBlue + image[i][j + 1].rgbtBlue + image[i + 1][j + 1].rgbtBlue + image[i + 1][j].rgbtBlue) / 4.0);


             }

             //north left corner
             else if (j == width - 1)
             {
                 changed[i][j].rgbtRed = round((image[i][j].rgbtRed + image[i][j - 1].rgbtRed + image[i + 1][j - 1].rgbtRed + image[i + 1][j].rgbtRed) / 4.0);
                 changed[i][j].rgbtGreen = round((image[i][j].rgbtGreen + image[i][j - 1].rgbtGreen + image[i + 1][j - 1].rgbtGreen + image[i + 1][j].rgbtGreen) / 4.0);
                 changed[i][j].rgbtBlue = round((image[i][j].rgbtBlue + image[i][j - 1].rgbtBlue + image[i + 1][j - 1].rgbtBlue + image[i + 1][j].rgbtBlue) / 4.0);
             }

         }
         //main if south corners
         else if (i == height - 1)
         {
             //south right corner
             if (j == 0)
             {
                changed[i][j].rgbtRed = round((image[i][j].rgbtRed + image[i - 1][j].rgbtRed + image[i - 1][j + 1].rgbtRed + image[i][j + 1].rgbtRed) / 4.0);
                changed[i][j].rgbtGreen = round((image[i][j].rgbtGreen + image[i - 1][j].rgbtGreen + image[i - 1][j + 1].rgbtGreen + image[i][j + 1].rgbtGreen) / 4.0);
                changed[i][j].rgbtBlue = round((image[i][j].rgbtBlue + image[i - 1][j].rgbtBlue + image[i - 1][j + 1].rgbtBlue + image[i][j + 1].rgbtBlue) / 4.0);
             }

             //south left corner
             else if (j == width - 1)
             {
                changed[i][j].rgbtRed = round((image[i][j].rgbtRed + image[i - 1][j].rgbtRed + image[i - 1][j - 1].rgbtRed + image[i][j - 1].rgbtRed) / 4.0);
                changed[i][j].rgbtGreen = round((image[i][j].rgbtGreen + image[i - 1][j].rgbtGreen + image[i - 1][j - 1].rgbtGreen + image[i][j - 1].rgbtGreen) / 4.0);
                changed[i][j].rgbtBlue = round((image[i][j].rgbtBlue + image[i - 1][j].rgbtBlue + image[i - 1][j - 1].rgbtBlue + image[i][j - 1].rgbtBlue) / 4.0);
             }
         }

        else //other pixels in the middle
        {
            changed[i][j].rgbtRed = round((image[i][j].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][j + 1].rgbtRed + image[i + 1][j + 1].rgbtRed + image[i + 1][j].rgbtRed + image[i + 1][j - 1].rgbtRed) / (9.0));
            changed[i][j].rgbtGreen = round((image[i][j].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][j + 1].rgbtGreen + image[i + 1][j + 1].rgbtGreen + image[i + 1][j].rgbtGreen + image[i + 1][j - 1].rgbtGreen) / (9.0));
            changed[i][j].rgbtBlue = round((image[i][j].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][j + 1].rgbtBlue + image[i + 1][j + 1].rgbtBlue + image[i + 1][j].rgbtBlue + image[i + 1][j - 1].rgbtBlue) / (9.0));
        }
      }

}

//putting the changed color value in original image(applying the blue filter)
for (int i = 0 ; i <= height - 1   ; i++)
{
    for (int j = 0 ; j <= width - 1 ; j++)
    {
        image[i][j] = changed[i][j];
    }
}


return;
Asrapian
  • 13
  • 4