I'm currently working on the filters assignment in pset4 of CS50. My code compiles and runs without issue and produces the results it should on all the stock images. However when I run check50 I get error messages for grayscale
and blur
.
Grayscale
Error Messages:
:( grayscale correctly filters single pixel without whole number average
expected "28 28 28\n", not "27 27 27\n"
:( grayscale correctly filters more complex 3x3 image
expected "20 20 20\n50 5...", not "20 20 20\n50 5..."
:( grayscale correctly filters 4x4 image
expected "20 20 20\n50 5...", not "20 20 20\n50 5..."
I understand that there must be a rounding issue somewhere, but I can't find it.
Blur
Error Messages:
:( blur correctly filters middle pixel
expected "127 140 149\n", not "145 159 169\n"
:( blur correctly filters pixel on edge
expected "80 95 105\n", not "90 105 115\n"
:( 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..."
I'd really appeciate it if someone would be willing to dig through my code and even just give me a hint as to what is wrong, because I'm at the end of my rope.
Here is my code:
void grayscale(int height, int width, RGBTRIPLE image[height][width])
{
float avg;
for (int i = 0; i < height; i++) //iterate over each row
{
for (int j = 0; j < width; j++) //iterate over each pixel
{
avg = (image[i][j].rgbtBlue + image[i][j].rgbtGreen + image[i][j].rgbtRed) / 3; //calculate average
image[i][j].rgbtBlue = round(avg);
image[i][j].rgbtGreen = round(avg);
image[i][j].rgbtRed = round(avg);
}
}
}
// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
float blur_Red;
float blur_Green;
float blur_Blue;
RGBTRIPLE pixel[height][width];
for (int i = 0; i < height; i++) //iterate over each row
{
for (int j = 0; j < width; j++) //iterate over each pixel
{
blur_Red = 0;
blur_Green = 0;
blur_Blue = 0;
if (i == 0 && j == 0)// top left corner
{
blur_Red = (image[i][j + 1].rgbtRed + image[i][j].rgbtRed + image[i + 1][j].rgbtRed + image[i + 1][j + 1].rgbtRed) / 4;
blur_Green = (image[i][j + 1].rgbtGreen + image[i][j].rgbtGreen + image[i + 1][j].rgbtGreen + image[i+ 1][j + 1].rgbtGreen) / 4;
blur_Blue = (image[i][j + 1].rgbtBlue + image[i][j].rgbtBlue + image[i + 1][j].rgbtBlue + image[i + 1][j + 1].rgbtBlue) / 4;
pixel[i][j].rgbtRed = round(blur_Red);
pixel[i][j].rgbtGreen = blur_Green;
pixel[i][j].rgbtBlue = blur_Blue;
}
if (i == 0 && j == width - 1)// top right corner
{
blur_Red = (image[i + 1][j].rgbtRed + image[i][j].rgbtRed + image[i][j - 1].rgbtRed + image[i + 1][j - 1].rgbtRed) / 4;
blur_Green = (image[i + 1][j].rgbtGreen + image[i][j].rgbtGreen + image[i][j - 1].rgbtGreen + image[i+ 1][j - 1].rgbtGreen) / 4;
blur_Blue = (image[i + 1][j].rgbtBlue + image[i][j].rgbtBlue + image[i][j - 1].rgbtBlue + image[i + 1][j - 1].rgbtBlue) / 4;
pixel[i][j].rgbtRed = round(blur_Red);
pixel[i][j].rgbtGreen = round(blur_Green);
pixel[i][j].rgbtBlue = round(blur_Blue);
}
if (i == height - 1 && j == width - 1)// bottom right corner
{
blur_Red = (image[i][j - 1].rgbtRed + image[i - 1][j].rgbtRed + image[i][j].rgbtRed + image[i - 1][j - 1].rgbtRed) / 4;
blur_Green = (image[i][j - 1].rgbtGreen + image[i - 1][j].rgbtGreen + image[i][j].rgbtGreen + image[i - 1][j - 1].rgbtGreen) / 4;
blur_Blue = (image[i][j - 1].rgbtBlue + image[i - 1][j].rgbtBlue + image[i][j].rgbtBlue + image[i - 1][j - 1].rgbtBlue) / 4;
pixel[i][j].rgbtRed = round(blur_Red);
pixel[i][j].rgbtGreen = round(blur_Green);
pixel[i][j].rgbtBlue = round(blur_Blue);
}
if (i == height -1 && j == 0)// bottom left corner
{
blur_Red = (image[i - 1][j + 1].rgbtRed + image[i][j].rgbtRed + image[i - 1][j].rgbtRed + image[i][j + 1].rgbtRed) / 4;
blur_Green = (image[i - 1][j + 1].rgbtGreen + image[i][j].rgbtGreen + image[i - 1][j].rgbtGreen + image[i][j + 1].rgbtGreen) / 4;
blur_Blue = (image[i - 1][j + 1].rgbtBlue + image[i][j].rgbtBlue + image[i - 1][j].rgbtBlue + image[i][j + 1].rgbtBlue) / 4;
pixel[i][j].rgbtRed = round(blur_Red);
pixel[i][j].rgbtGreen = round(blur_Green);
pixel[i][j].rgbtBlue = round(blur_Blue);
}
if (i == 0 && j > 0 && j < width - 1) //top side
{
blur_Red = (image[i][j - 1].rgbtRed + image[i][j].rgbtRed + image[i][j + 1].rgbtRed + image[i + 1][j - 1].rgbtRed + image[i + 1][j].rgbtRed + image[i + 1][j + 1].rgbtRed) / 6;
blur_Green = (image[i][j - 1].rgbtGreen + image[i][j].rgbtGreen + image[i][j + 1].rgbtGreen + image[i + 1][j - 1].rgbtGreen + image[i + 1][j].rgbtGreen + image[i + 1][j + 1].rgbtGreen) / 6;
blur_Blue = (image[i][j - 1].rgbtBlue + image[i][j].rgbtBlue + image[i][j + 1].rgbtBlue + image[i + 1][j - 1].rgbtBlue + image[i + 1][j].rgbtBlue + image[i + 1][j + 1].rgbtBlue) / 6;
pixel[i][j].rgbtRed = round(blur_Red);
pixel[i][j].rgbtGreen = round(blur_Green);
pixel[i][j].rgbtBlue = round(blur_Blue);
}
if (i == height - 1 && j > 0 && j < width - 1) //bottom side
{
blur_Red = (image[i][j + 1].rgbtRed + image[i][j].rgbtRed + image[i][j - 1].rgbtRed + image[i - 1][j + 1].rgbtRed + image[i - 1][j].rgbtRed + image[i - 1][j - 1].rgbtRed) / 6;
blur_Green = (image[i][j + 1].rgbtGreen + image[i][j].rgbtGreen + image[i][j - 1].rgbtGreen + image[i - 1][j + 1].rgbtGreen + image[i - 1][j].rgbtGreen + image[i - 1][j - 1].rgbtGreen) / 6;
blur_Blue = (image[i][j + 1].rgbtBlue + image[i][j].rgbtBlue + image[i][j - 1].rgbtBlue + image[i - 1][j + 1].rgbtBlue + image[i - 1][j].rgbtBlue + image[i - 1][j - 1].rgbtBlue) / 6;
pixel[i][j].rgbtRed = round(blur_Red);
pixel[i][j].rgbtGreen = round(blur_Green);
pixel[i][j].rgbtBlue = round(blur_Blue);
}
if (j == 0 && i > 0 && i < width - 1) //left side
{
blur_Red = (image[i + 1][j].rgbtRed + image[i][j].rgbtRed + image[i - 1][j].rgbtRed + image[i + 1][j + 1].rgbtRed + image[i][j + 1].rgbtRed + image[i - 1][j + 1].rgbtRed) / 6;
blur_Green = (image[i + 1][j].rgbtGreen + image[i][j].rgbtGreen + image[i - 1][j].rgbtGreen + image[i + 1][j + 1].rgbtGreen + image[i][j + 1].rgbtGreen + image[i - 1][j + 1].rgbtGreen) / 6;
blur_Blue = (image[i + 1][j].rgbtBlue + image[i][j].rgbtBlue + image[i - 1][j].rgbtBlue + image[i + 1][j + 1].rgbtBlue + image[i][j + 1].rgbtBlue + image[i - 1][j + 1].rgbtBlue) / 6;
pixel[i][j].rgbtRed = round(blur_Red);
pixel[i][j].rgbtGreen = round(blur_Green);
pixel[i][j].rgbtBlue = round(blur_Blue);
}
if (j == width - 1 && i != 0 && i != height - 1) //right side
{
blur_Red = (image[i - 1][j].rgbtRed + image[i][j].rgbtRed + image[i + 1][j].rgbtRed + image[i - 1][j - 1].rgbtRed + image[i][j - 1].rgbtRed + image[i + 1][j - 1].rgbtRed) / 6;
blur_Green = (image[i - 1][j].rgbtGreen + image[i][j].rgbtGreen + image[i + 1][j].rgbtGreen + image[i - 1][j - 1].rgbtGreen + image[i][j - 1].rgbtGreen + image[i + 1][j - 1].rgbtGreen) / 6;
blur_Blue = (image[i - 1][j].rgbtBlue + image[i][j].rgbtBlue + image[i + 1][j].rgbtBlue + image[i - 1][j - 1].rgbtBlue + image[i][j - 1].rgbtBlue + image[i + 1][j - 1].rgbtBlue) / 6;
pixel[i][j].rgbtRed = round(blur_Red);
pixel[i][j].rgbtGreen = round(blur_Green);
pixel[i][j].rgbtBlue = round(blur_Blue);
}
if ( i > 0 && i < height -1 && j > 0 && j < width - 1) //main body
{
blur_Red = (image[i - 1][j - 1].rgbtRed + image[i - 1][j].rgbtRed + image[i - 1][j + 1].rgbtRed + image[i][j - 1].rgbtRed + image[i][j].rgbtRed + image[i][j + 1].rgbtRed + image[i + 1][j - 1].rgbtRed + image[i + 1][j].rgbtRed + image[i + 1][j + 1].rgbtRed) / 9;
blur_Green = (image[i - 1][j - 1].rgbtGreen + image[i - 1][j].rgbtGreen + image[i - 1][j + 1].rgbtGreen + image[i][j - 1].rgbtGreen + image[i][j].rgbtGreen + image[i][j + 1].rgbtGreen + image[i + 1][j - 1].rgbtGreen + image[i + 1][j].rgbtGreen + image[i + 1][j + 1].rgbtGreen) / 9;
blur_Blue = (image[i - 1][j - 1].rgbtBlue + image[i - 1][j].rgbtBlue + image[i - 1][j + 1].rgbtBlue + image[i][j - 1].rgbtBlue + image[i][j].rgbtBlue + image[i][j + 1].rgbtBlue + image[i + 1][j - 1].rgbtBlue + image[i + 1][j].rgbtBlue + image[i + 1][j + 1].rgbtBlue) / 9;
pixel[i][j].rgbtRed = round(blur_Red);
pixel[i][j].rgbtGreen = round(blur_Green);
pixel[i][j].rgbtBlue = round(blur_Blue);
}
image[i][j].rgbtRed = pixel[i][j].rgbtRed;
image[i][j].rgbtGreen = pixel[i][j].rgbtGreen;
image[i][j].rgbtBlue = pixel[i][j].rgbtBlue;
}
}
}