CS50 PSet4 - Blur Filter, I am getting black image as an output

Viewed 676

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;
}
1 Answers

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;
    }
}
Related