0

I can't get edges to work I would really like some hints to what's wrong with my code.

// Detect edges
void edges(int height, int width, RGBTRIPLE image[height][width])
{
    //copying original image into copy
    RGBTRIPLE copy[height][width];
    for (int v = 0; v < height; v++)
    {
        for (int w = 0; w < width; w++)
        {
            copy[v][w] = image[v][w];

        }
    }

    //variables to store the 3 color channels
    RGBTRIPLE Gx;
    RGBTRIPLE Gy;

    //store kernels values in 2 arrays x & y
    int x[]= {-1, 0, 1, -2, 0, 2, -1, 0, 1};
    int y[] = {-1, -2, -1, 0, 0, 0, 1, 2, 1};

    //variable that will remember whether a pixel is within the 3x3 grid
    bool outside = false;

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            //initialize variables for every pixel
            int red = 0;
            int green = 0;
            int blue = 0;

            //initialize all Gx & Gy channels to 0 (default values in case of an outside pixel)
            Gx.rgbtRed = 0;
            Gx.rgbtGreen = 0;
            Gx.rgbtBlue = 0;
            Gy.rgbtRed = 0;
            Gy.rgbtGreen = 0;
            Gy.rgbtBlue = 0;

            //remembers current index of arrays x & y
            int index = 0;

            for (int k = i - 1; k < i + 2; k++)
            {

                for (int l = j - 1; l < j + 2; l++)
                {
                    //if pixel inside the grid compute Gx & Gy for each channel
                    if (l >= 0 && l <= width - 1 && k >= 0 && k <= height - 1)
                    {

                        Gx.rgbtRed = Gx.rgbtRed + copy[k][l].rgbtRed * x[index];
                        Gy.rgbtRed = Gy.rgbtRed + copy[k][l].rgbtRed * y[index];
                        Gx.rgbtGreen = Gx.rgbtGreen + copy[k][l].rgbtGreen * x[index];
                        Gy.rgbtGreen = Gy.rgbtGreen + copy[k][l].rgbtGreen * y[index];
                        Gx.rgbtBlue = Gx.rgbtBlue + copy[k][l].rgbtBlue * x[index];
                        Gy.rgbtBlue = Gy.rgbtBlue + copy[k][l].rgbtBlue * y[index];

                        outside = false;
                    }
                    //in case pixel is outside of the 3x3 grid
                    else
                    {
                        outside = true;

                    }
                    // move to next values in arrays x & y
                    index++;

                }
                //compute the new value of each channel of pixel image[i][j]
                red = limit(sqrt(pow(Gx.rgbtRed, 2) + pow(Gy.rgbtRed, 2)));
                green = limit(sqrt(pow(Gx.rgbtGreen, 2) + pow(Gy.rgbtGreen, 2)));
                blue = limit(sqrt(pow(Gx.rgbtBlue, 2) + pow(Gy.rgbtBlue, 2)));

            }
            //change the values of each channel to new values
            image[i][j].rgbtRed = red;
            image[i][j].rgbtGreen = green;
            image[i][j].rgbtBlue = blue;

        }
    }

    return;
}


int limit(int a)
{
    if (a > 255)
    {
        a = 255;
    }

    return a;
}
0

Perhaps this has been worked out already, the question has sat for a few weeks. This is a complicated problem, I wrote my own test.c which calls the functions in helpers. It takes an input height and width, and generates the rgb triples for the size given. Based on comparisons of the output, I identify 4 problems with your code. The right and bottom edges do not get the values I'd expect. On the other hand, once the other 2 problems are fixed, the left and upper edges seem correct, including the upper left corner. Now for the questions which should lead you to solve the other 2 problems.
How many bits are needed to store the largest color value? How many bits are available for each color in the RGBTRIPLE? How many bits are needed to store the intermediate values?
What is the integer cast of the square root of 24?

You must log in to answer this question.

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