0

I've been stuck on this for the past while and I'm not sure what's wrong in my algorithm. I get complete nonsense for outputs and I'm kind of lost on where to go from here. Any help would be appreciated

// Detect edges
void edges(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE x_gradient[height][width];
    RGBTRIPLE y_gradient[height][width];
    RGBTRIPLE gradient_magnitude[height][width];

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

    for (int x = 0; x < width; ++x)
    {
        for (int y = 0; y < height; ++y)
        {
            x_gradient[y][x].rgbtBlue = 0;
            x_gradient[y][x].rgbtGreen = 0;
            x_gradient[y][x].rgbtRed = 0;
            y_gradient[y][x].rgbtBlue = 0;
            y_gradient[y][x].rgbtGreen = 0;
            y_gradient[y][x].rgbtRed = 0;

            if (x - 1 < 0 || x + 1 > width - 1 || y - 1 < 0 || y + 1 > height - 1)
                    {
                        continue;
                    }

            x_gradient[y][x].rgbtBlue += image[y - 1][x - 1].rgbtBlue * xweights[0][0];
            x_gradient[y][x].rgbtBlue += image[y - 1][x + 1].rgbtBlue * xweights[0][2];
            x_gradient[y][x].rgbtBlue += image[y][x - 1].rgbtBlue * xweights[1][0];
            x_gradient[y][x].rgbtBlue += image[y][x + 1].rgbtBlue * xweights[1][2];
            x_gradient[y][x].rgbtBlue += image[y + 1][x - 1].rgbtBlue * xweights[2][0];
            x_gradient[y][x].rgbtBlue += image[y + 1][x + 1].rgbtBlue * xweights[2][2];

            x_gradient[y][x].rgbtGreen += image[y - 1][x - 1].rgbtGreen * xweights[0][0];
            x_gradient[y][x].rgbtGreen += image[y - 1][x + 1].rgbtGreen * xweights[0][2];
            x_gradient[y][x].rgbtGreen += image[y][x - 1].rgbtGreen * xweights[1][0];
            x_gradient[y][x].rgbtGreen += image[y][x + 1].rgbtGreen * xweights[1][2];
            x_gradient[y][x].rgbtGreen += image[y + 1][x - 1].rgbtGreen * xweights[2][0];
            x_gradient[y][x].rgbtGreen += image[y + 1][x + 1].rgbtGreen * xweights[2][2];

            x_gradient[y][x].rgbtRed += image[y - 1][x - 1].rgbtRed * xweights[0][0];
            x_gradient[y][x].rgbtRed += image[y - 1][x + 1].rgbtRed * xweights[0][2];
            x_gradient[y][x].rgbtRed += image[y][x - 1].rgbtRed * xweights[1][0];
            x_gradient[y][x].rgbtRed += image[y][x + 1].rgbtRed * xweights[1][2];
            x_gradient[y][x].rgbtRed += image[y + 1][x - 1].rgbtRed * xweights[2][0];
            x_gradient[y][x].rgbtRed += image[y + 1][x + 1].rgbtRed * xweights[2][2];

            y_gradient[y][x].rgbtBlue += image[y - 1][x - 1].rgbtBlue * yweights[0][0];
            y_gradient[y][x].rgbtBlue += image[y - 1][x + 1].rgbtBlue * yweights[0][1];
            y_gradient[y][x].rgbtBlue += image[y][x - 1].rgbtBlue * yweights[0][2];
            y_gradient[y][x].rgbtBlue += image[y][x + 1].rgbtBlue * yweights[2][0];
            y_gradient[y][x].rgbtBlue += image[y + 1][x - 1].rgbtBlue * yweights[2][1];
            y_gradient[y][x].rgbtBlue += image[y + 1][x + 1].rgbtBlue * yweights[2][2];

            y_gradient[y][x].rgbtGreen += image[y - 1][x - 1].rgbtGreen * yweights[0][0];
            y_gradient[y][x].rgbtGreen += image[y - 1][x + 1].rgbtGreen * yweights[0][1];
            y_gradient[y][x].rgbtGreen += image[y][x - 1].rgbtGreen * yweights[0][2];
            y_gradient[y][x].rgbtGreen += image[y][x + 1].rgbtGreen * yweights[2][0];
            y_gradient[y][x].rgbtGreen += image[y + 1][x - 1].rgbtGreen * yweights[2][1];
            y_gradient[y][x].rgbtGreen += image[y + 1][x + 1].rgbtGreen * yweights[2][2];

            y_gradient[y][x].rgbtRed += image[y - 1][x - 1].rgbtRed * yweights[0][0];
            y_gradient[y][x].rgbtRed += image[y - 1][x + 1].rgbtRed * yweights[0][1];
            y_gradient[y][x].rgbtRed += image[y][x - 1].rgbtRed * yweights[0][2];
            y_gradient[y][x].rgbtRed += image[y][x + 1].rgbtRed * yweights[2][0];
            y_gradient[y][x].rgbtRed += image[y + 1][x - 1].rgbtRed * yweights[2][1];
            y_gradient[y][x].rgbtRed += image[y + 1][x + 1].rgbtRed * yweights[2][2];

            gradient_magnitude[y][x].rgbtBlue = normalize(sqrt(y_gradient[y][x].rgbtBlue * y_gradient[y][x].rgbtBlue + x_gradient[y][x].rgbtBlue * x_gradient[y][x].rgbtBlue));
            gradient_magnitude[y][x].rgbtGreen = normalize(sqrt(y_gradient[y][x].rgbtGreen * y_gradient[y][x].rgbtGreen + x_gradient[y][x].rgbtGreen * x_gradient[y][x].rgbtGreen));
            gradient_magnitude[y][x].rgbtRed = normalize(sqrt(y_gradient[y][x].rgbtRed * y_gradient[y][x].rgbtRed + x_gradient[y][x].rgbtRed * x_gradient[y][x].rgbtRed));

        }
    }
    for (int x = 0; x < width; ++x)
    {
        for (int y = 0; y < height; ++y)
        {
            image[y][x].rgbtBlue = gradient_magnitude[y][x].rgbtBlue;
            image[y][x].rgbtGreen = gradient_magnitude[y][x].rgbtGreen;
            image[y][x].rgbtRed = gradient_magnitude[y][x].rgbtRed;
        }
    }

    return;
}

//normalize values
int normalize(double input)
{
    if (input > 255)
    {
        return 255;
    }
    else if (input < 0)
    {
        return 0;
    }
    return  (int)round(input);
}
1

May be some more issues but, a big one, is that you are not processing the edge and border pixels at all: Here: if (x - 1 < 0 || x + 1 > width - 1 || y - 1 < 0 || y + 1 > height - 1) { continue; } you jump to the next pixel without taking in consideration the ones that are not out of boundaries. With that fixed your code should work fine.

As per how to get it working, you have to think in a way to go around the 3x3 matrix surrounding the current pixel, discarding the ones out of boundaries.

1
  • Would that not just effect my the border of the image? I planned on implementing it once I got some edge detection working but currently it's completely wrong and doesn't detect any edges within the image – Andy D May 26 '20 at 1:43

You must log in to answer this question.

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