0

I'm doing CS50 and trying really hard to keep sane while I try to finish the last part of the Filter (less) exercise. I'm stuck on "Blur". I've tried to resist looking for help online because I really want to understand what I'm doing wrong. In my head my code works, so I'm afraid maybe I'm already blind to any mistakes.

I appreciate all the help! Thanks in advance, Ana

Here's my code:

void blur(int height, int width, RGBTRIPLE image[height][width])
{
for (int i = 0; i < height; i++)
{
    for (int j = 0; j < width; j++)
    {
        int sum_red = 0;
        int sum_green = 0;
        int sum_blue = 0;

        int counter = 0;
        
        for (int add_i = -1; add_i < 2; add_i++)
        {
            for (int add_j = -1; add_j < 2; add_j++)
            {
                if (!(add_i + i < 0) && !(add_j + j < 0) && (add_i + i <= height) && (add_j + j <= width))
                {
                    sum_red = sum_red + image[i + add_i][j + add_j].rgbtRed;
                    sum_green = sum_green + image[i + add_i][j + add_j].rgbtGreen;
                    sum_blue = sum_blue + image[i + add_i][j + add_j].rgbtBlue;
                
                    counter++;
                }
            }
        }
        
        image[i][j].rgbtRed = round(sum_red / counter);
        image[i][j].rgbtGreen = round(sum_green / counter);
        image[i][j].rgbtBlue = round(sum_blue / counter);
    }
}

return;

}

2
  • Hi Ana, could you please elaborate on what is failing/doesn't work when you run this code? I've tried it without changes and it seems to blur image successfully for me. Also, you might want to review maximum 'acceptable' indexs for height and width. Commented Jun 24, 2020 at 0:51
  • Hi, yes it blurs! I only get the first edge pixel correct. The rest it's blurred but with the incorrect values of rgb Commented Jun 25, 2020 at 14:38

1 Answer 1

0

The code is updating each cell in the original image array as it goes. That means that updated cells will be used to calculate subsequent new cells, not the original values before the update. The code should store the calculated values for the new image in a new array.

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

1
  • Thanks! It makes sense! I'll try this. Thanks for your help :) Commented Jun 25, 2020 at 14:39

You must log in to answer this question.

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