0

Code:

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

//temporary image to swap
RGBTRIPLE temp_img[height][width];
for (int g = 0; g < height; g++)
{
    for (int h = 0; h < width; h++)
    {
        temp_img[g][h] = image[g][h];
    }
}

//iterate through column
for (int i = 0; i < height; i++)
{
    //iterate through row
    for (int j = 0; j < width; j++)
    {
        sum_red = 0;
        sum_green = 0;
        sum_blue = 0;
        count = 0;
        //iterate over surrounding pixels on column
        for (int b = -1; b < 2; b++)
        {
            if (i + b < 0 || i + b > height - 1)
            {
                continue;
            }

            //iterate through surrounding pixels on rows
            for (int a = -1; a < 2; a++)
            {
                if (j + a < 0 || j + a > width - 1)
                {
                    continue;
                }
                sum_red += temp_img[i + b][j + a].rgbtRed;
                sum_green += temp_img[i + b][j + a].rgbtGreen;
                sum_blue += temp_img[i + b][j + a].rgbtBlue;
                count++;
            }
        }
        
        temp_img[i][j].rgbtRed = round(sum_red / count);
        temp_img[i][j].rgbtGreen = round(sum_green / count);
        temp_img[i][j].rgbtBlue = round(sum_blue / count);
    }
}

for (int i = 0; i < height; i++)
{
    for (int j = 0; j < width; j++)
    {
        image[i][j].rgbtRed = temp_img[i][j].rgbtRed;
        image[i][j].rgbtGreen = temp_img[i][j].rgbtGreen;
        image[i][j].rgbtBlue = temp_img[i][j].rgbtBlue;
    }
}
return;
}

1 Answer 1

1

Your sum and count variables are initialised before your loops, so their value will persist outside of all loop iterations. This might not be the behaviour you want.

Edit: Now that you're resetting your variables appropriately, you just have one issue left:

  1. You create a temporary image to work with. However, you both read the values of the neighbouring pixels from and write the new values to the temporary image. This defeats the purpose (which is to avoid having pixels you've changed 'contaminate' future averages). Instead try reading the values from the original image and writing them to the temporary image (you can do away with your initial loop that copies the image data because it's not really necessary).
3
  • I've modified the code, but it only filters corner pixels now Nov 17, 2020 at 16:21
  • Hi Gloria: I've updated my answer - see if that helps.
    – Sentox
    Nov 17, 2020 at 18:09
  • Thank you sm. It passed check50 now. :) Nov 18, 2020 at 15:08

You must log in to answer this question.

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