0

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

5

When doing math operations that could result in decimal point number, although the recieving variable is a decimal point capable, the math on the right side of the asign simbol'=', when working with ints variables, will be done with the int rules, so the decimal point will be truncated.

One trick to avoid this, is to add '.00' to a number of the operation which will 'force' to the operation to be calculated with decimal point numbers. For example if you want to divide by 3 then use 3.00 instead.

| improve this answer | |
  • Thank you!!! that seemes to havetaken care of the issue with grayscale!! However I'm still at a loss on what to do with blur. – Renée Apr 29 at 15:18
  • A couple of mistakes: 'if (j == 0 && i > 0 && i < width - 1) //left side' and pixel[i][j].rgbtRed = round(blur_Red); pixel[i][j].rgbtGreen = blur_Green; pixel[i][j].rgbtBlue = blur_Blue; Not sure if there are more. – Tritum Apr 29 at 17:51
  • Thank you for your help. I have applied the corrections, unfortunately they do not seem to solve the problem. – Renée Apr 30 at 9:54
  • Hi, in your code you assign the values of your pixel array to the image array inside the i an j loops, so in the next itterations they won't have the original values. Changing the values after the loops are finished will solve the problem. Well almost: I've found another mispell, but I think that you should find it yourself. – Tritum Apr 30 at 12:43
  • THANK YOU!!!! I've yet to find the misspell, but I will keep looking. I appeciate the help. – Renée May 4 at 14:17
0

just make another loop for copying the pixel array to the image.

| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .