I am working on a CS50 problem set in which I need to do a box blur for each pixel of an image. Though my code is a bit redundant, as I had created 8 if statements for special c
You are modifying your image
as you apply blur function to the pixels. This means when you modify few pixels, the adjacent pixels blur values are calculated with "blurred pixel values". This is wrong. All the calculations must be done within original image pixel values. For this, you should create a copy of image
in the beginning (such as temp
) and make all this calculation within that temp
image which has unmodified pixel values.
Add this to the beginning of your code;
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];
}
}
And replace image
with temp
in your calculations (except the assignment at the end).