0

I'm tasked with writing a program that applies a gray scale filter to a given image. This is done by looping over each pixel and averaging the rgb values. When I check my code, I receive an error which seems to say "ERROR: expected X, received X". Any clarifications of the error message or comments about possible bugs are appreciated.

My code loops over each pixel in each row of an array of pixels which are described as RGBTRIPLES, structs comprised of 3 uint8_t's. In the case of a non-whole-number average the program rounds up to the nearest whole number. My answer is the code included below.

My problem comes when I run check50 cs50/problems/2021/x/filter/more, a program which tests students' programs against keys made by the course designers. The 7th test check's the program's accuracy in filtering a "complex" 3x3 image; I get the following result:

:( grayscale correctly filters more complex 3x3 image
    expected "20 20 20\n50 5...", not "20 20 20\n50 5..."

I am confused as it seems that my output matches the expected. Could someone give me a hint as to what my problem is?

My code:

void grayscale(int height, int width, RGBTRIPLE image[height][width])
{
    //Loop over pixels
    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {

            //Average out rgbs to get a corresponding greyscale value
            BYTE average_activation = ceil(((float) image[y][x].rgbtBlue + image[y][x].rgbtRed + image[y][x].rgbtGreen) / 3);
            RGBTRIPLE resultant_pixel = {average_activation, average_activation, average_activation};

            //apply
            image[y][x] = resultant_pixel;
        }
    }

    return;
}
  • The expected/actual result seem to have been cut off by a `...`. It's probably something different after that point. Is there a way for you to view the entire expected output? – Nick ODell Jun 22 '21 at 02:44
  • 1
    I am not sure what the error is, but is there a reason you convert to floating-point to do this? It could be done more easily (and in a less computationally demanding way) as follows: `BYTE average_activation = (image[y][x].rgbtBlue + image[y][x].rgbtRed + image[y][x].rgbtGreen + 2) / 3;` The plus two handles the rounding-up. – SGeorgiades Jun 22 '21 at 03:18

1 Answers1

0

I think you are overcomplicating things you should use round instead of ceil using #include <math.h>. Also you shouldn't define the new pixel per each pixel in the image. Instead define a new image that is the same width and height as the old image and replace every pixel in this image. At the end substitute the new image with the old image.