1

When I run check 50 I get the red green channels correct but no matter what I do the blue channel comes out with seemingly random values what am I missing here?

void edges(int height, int width, RGBTRIPLE image[height][width])
{
    // Temporary arrays to store gradients in x and y directions
    int Gx[3][3] = {{-1, 0, 1}, {-2, 0, 2}, {-1, 0, 1}};

    int Gy[3][3] = {{-1, -2, -1}, {0, 0, 0}, {1, 2, 1}};

    RGBTRIPLE buffer[height][width];


    // Iterate through each pixel
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            // Reset g values
            int xred = 0, xgreen = 0, xblue = 0;
            int yred = 0, ygreen = 0, yblue = 0;

            for (int k = -1; k <= 1; k++)
            {
                for (int l = -1; l <= 1; l++)
                {
                    int row = i + k;
                    int col = j + l;

                    if (row >= 0 && row < height && col >= 0 && col < width)
                    {
                        // Compute gradients in x and y directions for each channel
                        xred += image[row][col].rgbtRed * Gx[k + 1][l + 1];
                        yred += image[row][col].rgbtRed * Gy[k + 1][l + 1];

                        xgreen += image[row][col].rgbtGreen * Gx[k + 1][l + 1];
                        ygreen += image[row][col].rgbtGreen * Gy[k + 1][l + 1];

                        xblue += image[row][col].rgbtBlue * Gx[k + 1][l + 1];
                        yblue += image[row][col].rgbtBlue * Gy[k + 1][l + 1];
                    }
                }
            }

            // square root of gx2 + gy2
            float red = sqrt((xred * xred) + (yred * yred));
            float green = sqrt((xgreen * xgreen) + (ygreen * ygreen));
            float blue = sqrt((xblue * xblue) + (yblue * yblue));

            // Set pixels color channels
            buffer[i][j].rgbtRed = round(red);
            buffer[i][j].rgbtGreen =  round(green);
            buffer[i][j].rgbtBlue = round(blue);

        }
    }
    // Copy the buffer back to the original image
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            image[i][j] = buffer[i][j];
        }
    }
}

1 Answer 1

1

your whole average computation piece at the end is incomplete and not exactly typesafe but I think you are getting lucky because only the blue values tend to skyrocket beyond the limits....

If you recall the spec said the maximum value of any RGB value should be 255? (this is because we only have 8 bits for color, max 8 bit value is 255.) You aren't enforcing this and as a result you are very likely getting predominantly blue values that are way too big.

Here's how to conceptually fix it: in your buffer assignment you want to assign the minimum value of round(color) and 255 to the buffer location's appropriate color.

that said I think you should be careful about your type conversions here. I personally wouldn't trust a call to sqrt with an int. I would cast my int to a float first. i would then use fmin since i still have a float (even though i used round on the square root result), and cast the result of fmin back to int. but thats just what i did and that's mostly safe because i took min of 255.0 and a round; you could also cast the result of round back to int, and use an integer min function but i couldn't remember where it was defined so i used fmin

You must log in to answer this question.

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