void grayscale(int height, int width, RGBTRIPLE image[height][width])
    //Image[0] is the first row of the image and Image[height - 1] is the last row of the image
    //Image[0][0] is the first pixel in the first row
    //Since each pixel is a part of the struct called RGBT we reference it using example image[2][3].rgbtRed = 0; image[2][3].rgbtGreen = 0;
    int count_pixels = 3;
    int pixel_average;
    for (int i = 0; i < height; i++)
        for (int j = 0; j < width; j++)
            pixel_average = image[i][j].rgbtRed + image[i][j].rgbtGreen + image[i][j].rgbtBlue;
            pixel_average = (pixel_average)/count_pixels;
            pixel_average = round(pixel_average);
            image[i][j].rgbtRed = pixel_average;
            image[i][j].rgbtGreen = pixel_average;
            image[i][j].rgbtBlue = pixel_average;

When decimal point result is expected in a division, like in this case, working with 'int' variables, will result in a truncated results. For example if the result is 3.9 when assigned to an 'int' the value will be trimmed to '3'.

To avoid this you can use a 'float' variable to receive the result of the division, or you can round the result before assign it to the int variable. You also can force the compiler to do a decimal point division by a '.0' to the divider as: pixel_average/3.0, but remember to round it before assign it to an 'int'

  • Hi thanks for seeing that, but the issues is still there when I run the grayscale nothing changes in the out image, it stays exactly the same – RushT Jun 5 '20 at 0:57

You must log in to answer this question.

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