# Edge detection produces weird outputs

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])
{

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)
{

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];

}
}
for (int x = 0; x < width; ++x)
{
for (int y = 0; y < height; ++y)
{
}
}

return;
}

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

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.