问题
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 cases of pixels (like edges and corners), it blurs the image as expected, so I'm not really sure how to fix the problem. Here's the error code:
:( blur correctly filters middle pixel
expected "127 140 149\n", not "145 160 169\n"
:( blur correctly filters pixel on edge
expected "80 95 105\n", not "90 106 116\n"
:) blur correctly filters pixel in corner
:( blur correctly filters 3x3 image
expected "70 85 95\n80 9...", not "70 85 95\n90 1..."
:( blur correctly filters 4x4 image
expected "70 85 95\n80 9...", not "70 85 95\n90 1..."
There's also a more detailed error code here (only look at the "blur" errors)
Here's my code below:
// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
int blue;
int green;
int red;
int counter = 0;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if (i == 0 && j == 0)
{
// top left corner
blue = (image[i][j].rgbtBlue + image[i + 1][j].rgbtBlue + image[i][j+1].rgbtBlue + image[i + 1][j+1].rgbtBlue);
green = (image[i][j].rgbtGreen + image[i + 1][j].rgbtGreen + image[i][j+1].rgbtGreen + image[i + 1][j+1].rgbtGreen);
red = (image[i][j].rgbtRed + image[i + 1][j].rgbtRed + image[i][j+1].rgbtRed + image[i + 1][j+1].rgbtRed);
counter = 4;
}
else if (i == 0 && j == (width - 1))
{
// top right corner
blue = (image[i][j].rgbtBlue + image[i + 1][j].rgbtBlue + image[i][j-1].rgbtBlue + image[i + 1][j-1].rgbtBlue);
green = (image[i][j].rgbtGreen + image[i + 1][j].rgbtGreen + image[i][j-1].rgbtGreen + image[i + 1][j-1].rgbtGreen);
red = (image[i][j].rgbtRed + image[i + 1][j].rgbtRed + image[i][j-1].rgbtRed + image[i + 1][j-1].rgbtRed);
counter = 4;
}
else if (i == 0 && (j != 0 || j != (width - 1)))
{
// top edge
blue = (image[i][j - 1].rgbtBlue + image[i][j].rgbtBlue + image[i][j + 1].rgbtBlue + image[i + 1][j].rgbtBlue + image[i+1][j - 1].rgbtBlue + image[i + 1][j + 1].rgbtBlue);
green = (image[i][j - 1].rgbtGreen + image[i][j].rgbtGreen + image[i][j + 1].rgbtGreen + image[i + 1][j].rgbtGreen + image[i+1][j - 1].rgbtGreen + image[i + 1][j + 1].rgbtGreen);
red = (image[i][j - 1].rgbtRed + image[i][j].rgbtRed + image[i][j + 1].rgbtRed + image[i + 1][j].rgbtRed + image[i+1][j - 1].rgbtRed + image[i + 1][j + 1].rgbtRed);
counter = 6;
}
else if (i == (height - 1) && j == 0)
{
// bottom left corner
blue = (image[i-1][j + 1].rgbtBlue + image[i][j + 1].rgbtBlue + image[i - 1][j].rgbtBlue + image[i][j].rgbtBlue);
green = (image[i-1][j + 1].rgbtGreen + image[i][j + 1].rgbtGreen + image[i - 1][j].rgbtGreen + image[i][j].rgbtGreen);
red = (image[i-1][j + 1].rgbtRed + image[i][j + 1].rgbtRed + image[i - 1][j].rgbtRed + image[i][j].rgbtRed);
counter = 4;
}
else if (i == (height - 1) && j == (width - 1))
{
// bottom right corner
blue = (image[i][j].rgbtBlue + image[i - 1][j].rgbtBlue + image[i][j-1].rgbtBlue + image[i - 1][j-1].rgbtBlue);
green = (image[i][j].rgbtGreen + image[i - 1][j].rgbtGreen + image[i][j-1].rgbtGreen + image[i - 1][j-1].rgbtGreen);
red = (image[i][j].rgbtRed + image[i - 1][j].rgbtRed + image[i][j-1].rgbtRed + image[i - 1][j-1].rgbtRed);
counter = 4;
}
else if (i == (height - 1) && (j != 0 || j != (width - 1)))
{
// bottom edge
blue = (image[i][j].rgbtBlue + image[i][j - 1].rgbtBlue + image[i][j + 1].rgbtBlue + image[i - 1][j].rgbtBlue + image[i-1][j - 1].rgbtBlue + image[i - 1][j + 1].rgbtBlue);
green = (image[i][j].rgbtGreen + image[i][j - 1].rgbtGreen + image[i][j + 1].rgbtGreen + image[i - 1][j].rgbtGreen + image[i-1][j - 1].rgbtGreen + image[i - 1][j + 1].rgbtGreen);
red = (image[i][j].rgbtRed + image[i][j - 1].rgbtRed + image[i][j + 1].rgbtRed + image[i - 1][j].rgbtRed + image[i-1][j - 1].rgbtRed + image[i - 1][j + 1].rgbtRed);
counter = 6;
}
else if (j == 0 && (i != 0 || i != (height - 1)))
{
// left edge
blue = (image[i][j].rgbtBlue + image[i - 1][j].rgbtBlue + image[i+1][j].rgbtBlue + image[i][j + 1].rgbtBlue + image[i-1][j + 1].rgbtBlue + image[i + 1][j + 1].rgbtBlue);
green = (image[i][j].rgbtGreen + image[i - 1][j].rgbtGreen + image[i+1][j].rgbtGreen + image[i][j + 1].rgbtGreen + image[i-1][j + 1].rgbtGreen + image[i + 1][j + 1].rgbtGreen);
red = (image[i][j].rgbtRed + image[i - 1][j].rgbtRed + image[i+1][j].rgbtRed + image[i][j + 1].rgbtRed + image[i-1][j + 1].rgbtRed + image[i + 1][j + 1].rgbtRed);
counter = 6;
}
else if (j == (width - 1) && (i != 0 || i != (height - 1)))
{
// right edge
blue = (image[i][j].rgbtBlue + image[i-1][j].rgbtBlue + image[i + 1][j].rgbtBlue + image[i][j - 1].rgbtBlue + image[i + 1][j-1].rgbtBlue + image[i-1][j - 1].rgbtBlue);
green = (image[i][j].rgbtGreen + image[i-1][j].rgbtGreen + image[i + 1][j].rgbtGreen + image[i][j - 1].rgbtGreen + image[i + 1][j-1].rgbtGreen + image[i-1][j - 1].rgbtGreen);
red = (image[i][j].rgbtRed + image[i-1][j].rgbtRed + image[i + 1][j].rgbtRed + image[i][j - 1].rgbtRed + image[i + 1][j-1].rgbtRed + image[i-1][j - 1].rgbtRed);
counter = 6;
}
else
{
blue = (image[i][j].rgbtBlue + image[i - 1][j].rgbtBlue + image[i + 1][j].rgbtBlue + image[i][j - 1].rgbtBlue + image[i][j + 1].rgbtBlue + image[i - 1][j + 1].rgbtBlue + image[i-1][j - 1].rgbtBlue + image[i + 1][j - 1].rgbtBlue + image[i + 1][j + 1].rgbtBlue);
green = (image[i][j].rgbtGreen + image[i - 1][j].rgbtGreen + image[i + 1][j].rgbtGreen + image[i][j - 1].rgbtGreen + image[i][j + 1].rgbtGreen + image[i - 1][j + 1].rgbtGreen + image[i-1][j - 1].rgbtGreen + image[i + 1][j - 1].rgbtGreen + image[i + 1][j + 1].rgbtGreen);
red = (image[i][j].rgbtRed + image[i - 1][j].rgbtRed + image[i + 1][j].rgbtRed + image[i][j - 1].rgbtRed + image[i][j + 1].rgbtRed + image[i - 1][j + 1].rgbtRed + image[i-1][j - 1].rgbtRed + image[i + 1][j - 1].rgbtRed + image[i + 1][j + 1].rgbtRed);
counter = 9;
}
image[i][j].rgbtBlue = round((float) blue / counter);
image[i][j].rgbtGreen = round((float) green / counter);
image[i][j].rgbtRed = round((float) red / counter);
}
}
return;
}
Looking at other people's answers for this same problem, I see that some have made a second nested for
loop to store the original value of the pixel. I initially tried to implement this, but it ended up causing problems, so I figured it was not necessary. Is this the problem with my code, and if so, how would I properly implement the "original value" pixel within my code? If not, does anyone know the problem? Thanks in advance.
回答1:
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).
来源:https://stackoverflow.com/questions/62209966/cs50-blur-function-does-not-pass-check50-even-though-image-is-being-blurred