2

I'm writing the code for pset4, filter, blur of cs50. But my output image is black, or it simply doesn't give the correct output. I keep running into this problem.

What am I doing wrong in my blur function?

void blur(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE temp[height][width];
    int sumRed;
    int sumGreen;
    int sumBlue;
    int counter;

    for (int i = 0; i < width; i++)
    {
        for (int n = 0; n < height; n++)
        {
            //Resetting sums to 0
            sumRed = 0;
            sumGreen = 0;
            sumBlue = 0;
            counter = 0;

            for (int x = -1; x < 2; x++)
            {

                if (x + i < 0 || x + i > width - 1)
                {
                    continue;
                }

                for (int y = -1; y < 2; y++)
                {
                    if (y + n < 0 || y + n > height - 1)
                    {
                        continue;
                    }

                    sumRed += temp[y + n][x + i].rgbtRed;
                    sumGreen += temp[y + n][x + i].rgbtGreen;
                    sumBlue += temp[y + n][x + i].rgbtBlue;
                    counter++;

                }

            }

            temp[n][i].rgbtRed = round(sumRed / counter);
            temp[n][i].rgbtGreen = round(sumGreen / counter);
            temp[n][i].rgbtBlue = round(sumBlue / counter);
        }
    }

    for (int k = 0; k < width; k++)
    {
        for (int l = 0; l < height; l++)
        {
            image[l][k].rgbtRed = temp[k][l].rgbtRed;
            image[l][k].rgbtGreen = temp[k][l].rgbtGreen;
            image[l][k].rgbtBlue = temp[k][l].rgbtBlue;
        }
    }

    return;
}
Tarick Welling
  • 3,119
  • 3
  • 19
  • 44
Nicolas F
  • 505
  • 6
  • 17
  • 1
    Did you mean to reverse `k` & `l` when accessing `temp` here: `image[l][k].rgbtRed = temp[k][l].rgbtRed;` – Retired Ninja Jun 12 '20 at 00:54
  • Doesn't your compiler shout some warnings at you? "Using `temp` without assigning a value" or similar? If not, turn up your warnings level. For GCC you can do this with `-Wall -Wextra`. While your question is probably already solved, you should do this for every program you compile. – Gerhardh Dec 05 '20 at 19:19

1 Answers1

3

The real cause of black image is that temp is not initialized. To initialize it, we can simply copy the image into temp. Thanks @Gerhardh for pointing this out.

RGBTRIPLE temp[height][width]; // create a temporary array to store a duplicate of image.

// save a new copy of image as temp per color.
for (int i = 0; i < height; i++) //Loop for height of image.
{
    for (int j = 0; j < width; j++) //Loop for width of image and save color values in temp.
    {
        temp[i][j] = image[i][j];
    }
}

There is another issue in your algorithm. You do calculation on temp and change temp at the same time which is wrong. Because as temp changes, new pixel blur calculations will be done on modified pixels. Calculations should be always done on raw image, then image should be assigned to temp values. A more detailed explanation is here.

Basically, this piece of code,

temp[n][i].rgbtRed = round(sumRed / counter);
temp[n][i].rgbtGreen = round(sumGreen / counter);
temp[n][i].rgbtBlue = round(sumBlue / counter);

should be this one,

image[n][i].rgbtRed = round(sumRed / counter);
image[n][i].rgbtGreen = round(sumGreen / counter);
image[n][i].rgbtBlue = round(sumBlue / counter);

And remove this, since we already do assignment right after calculation.

for (int k = 0; k < width; k++)
{
    for (int l = 0; l < height; l++)
    {
        image[l][k].rgbtRed = temp[k][l].rgbtRed;
        image[l][k].rgbtGreen = temp[k][l].rgbtGreen;
        image[l][k].rgbtBlue = temp[k][l].rgbtBlue;
    }
}
Enis Arik
  • 665
  • 10
  • 24