0

I seem to be having problems with rounding numbers in these two functions. I am making sure to work with floats and I'm using the round() function so I'm stuck scratching my head at the minute! Grayscale is really baffling me as the code is so simple I can't see where I'm going wrong with it. The single pixel without whole number average is resulting in 27 rather than the expected 28.

// Convert image to grayscale
void grayscale(int height, int width, RGBTRIPLE image[height][width])
{
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            float avg = round(image[i][j].rgbtRed + image[i][j].rgbtGreen + image[i][j].rgbtBlue) / 3;

            image[i][j].rgbtRed = avg;
            image[i][j].rgbtGreen = avg;
            image[i][j].rgbtBlue = avg;
        }
    }

    return;
}

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE imagecopy[height][width];
    float totalRed;
    float totalGreen;
    float totalBlue;
    float counter;


    for (int i = 0; i < height; i++) //loop through rows
    {
        for (int j = 0; j < width; j++) //loop through columns
        {
            totalRed = 0;
            totalGreen = 0;
            totalBlue = 0;
            counter = 0;

            //loop through all possible pixels in 3 x 3 box
            for (int k = -1; k < 2; k++ )
            {
                {
                    for(int l = -1; l < 2; l++)
                    {
                        if((i + k) >= 0 && (i + k) <= height && (j + l) >= 0 && (j + l) < width)
                        {
                            counter++;
                            totalRed += image[i + k][j + l].rgbtRed;
                            totalGreen += image[i + k][j + l].rgbtGreen;
                            totalBlue += image[i + k][j + l].rgbtBlue;
                        }
                    }
                }
            }

            imagecopy[i][j].rgbtRed = round(totalRed / counter);
            imagecopy[i][j].rgbtGreen = round(totalGreen / counter);
            imagecopy[i][j].rgbtBlue = round(totalBlue / counter);
        }
    }

    //copy blurred value pixels into image array

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            image[i][j].rgbtRed = imagecopy[i][j].rgbtRed;
            image[i][j].rgbtGreen = imagecopy[i][j].rgbtGreen;
            image[i][j].rgbtBlue = imagecopy[i][j].rgbtBlue;

        }
    }

    return;
}
0

You're very close on the gray-scale function. I'd recommend you walk through the line that calculates avg item by item keeping your order of operations in mind (PEMDAS).

For your blur function you're correct, changing your total and counter variables from float to int will solve your rounding problem. Also, if you want to save some keystrokes don't forget you can initialize variables in one line separated by commas if they're all the same type

0

I think my issue was I wasn't thinking every value used in a calculation had to be a float rather than the variable that stored the result? Having to first convert each pixel to a float variable before working with them.

You must log in to answer this question.

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