0

Hello guys I have been trying to figure out what is wrong with my code for hours now but to no avail. I really wanted to finish this problem myself but I am so close and hope that someone can figure out my issue. Please pardon my messy presentation as I am still new to programming.

    void edges(int height, int width, RGBTRIPLE image[height][width])
{
    // Creating a copy of image with an extra border of black layer
    RGBTRIPLE copy[height + 2][width + 2];
    for (int i = 0; i < height + 2; i++)
    {
        for (int j = 0; j < width + 2; j++)
        {
            if (i == 0 || j == 0 || i == height + 1 || j == width + 1)
            {
                copy[i][j].rgbtRed = 0;
                copy[i][j].rgbtGreen = 0;
                copy[i][j].rgbtBlue = 0;
            }
            else
            {
                copy[i][j] = image[i - 1][j - 1];
            }
        }
    }
    // Create the matrix Gx and Gy
    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}
    };
    for (int i = 1; i < height + 1; i++)
    {
        for (int j = 1; j < width + 1; j++)
        {
            // Total values for red-x, green-x, blue-x, red-y, green-y and blue-y
            int aRx = 0;
            int aGx = 0;
            int aBx = 0;
            int aRy = 0;
            int aGy = 0;
            int aBy = 0;
            for (int a = -1; a < 2; a++)
            {
                for (int b = -1; b < 2; b++)
                {
                    aRx += (copy[i + a][j + b].rgbtRed * Gx[a + 1][b + 1]);
                    aGx += (copy[i + a][j + b].rgbtGreen * Gx[a + 1][b + 1]);
                    aBx += (copy[i + a][j + b].rgbtBlue * Gx[a + 1][b + 1]);
                    aRy += (copy[i + a][j + b].rgbtRed * Gy[a + 1][b + 1]);
                    aGy += (copy[i + a][j + b].rgbtGreen * Gy[a + 1][b + 1]);
                    aBy += (copy[i + a][j + b].rgbtBlue * Gy[a + 1][b + 1]);
                }
            }
            int redG = round(sqrt(pow(aRx, 2) + pow(aRy, 2)));
            int greenG = round(sqrt(pow(aGx, 2) + pow(aGy, 2)));
            int blueG = round(sqrt(pow(aBx, 2) + pow(aBy, 2)));
            // Wrapping values over 255 to within 1 to 255
            if (redG > 255)
            {
                redG %= 255;
            }
            if (greenG > 255)
            {
                greenG %= 255;
            }
            if (blueG > 255)
            {
                blueG %= 255;
            }
            image[i - 1][j - 1].rgbtRed = redG;
            image[i - 1][j - 1].rgbtGreen = greenG;
            image[i - 1][j - 1].rgbtBlue = blueG;
        }
    }
    return;
}

enter image description here

For some reason, my output is accurate for the first 2 columns in check50, and accurate for the last column only for values that is not 255. I have no idea how to debug this myself and I have tried going through my logic numerous times but I cannot find the mistake.

1 Answer 1

0

Just set them = 255, don't use the modulo operator, it works like this for example the cap is 15 and the value of red is 17, if we follow your formula it looks like 15 % 17 which is equal to 2...

You must log in to answer this question.

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