# I don't understand what's wrong with my Edge filter. Pset4

There are lots of runtime errors and overflowed integers with my code, and I have no idea why that is the case. Please could you check it out?

``````void edges(int height, int width, RGBTRIPLE image[height][width])
{
int Gx = {{-1, 0 ,1}, {-2, 0, 2}, {-1, 0, 1}};
int Gy = {{-1, -2 ,-1}, {0, 0, 0}, {1, 2, 1}};

RGBTRIPLE copy[height][width];
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
copy[i][j] = image[i][j];
}
}

for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
// create the new pixels
int nrx = 0, nbx = 0, ngx = 0;
int nry = 0, nby = 0, ngy = 0;
// loop 3x3
for (int k = 0; k < 3; k++)
{
for (int l = 0; l < 3; l++)
{
// -1 is to make up for the offset
// conditions to make sure there's no IndexError
if (i + k - 1 >=0 && i + k - 1 < height)
{
if (j + l - 1 >= 0 && j + l - 1 < width)
{
// RED
nrx += copy[i + k - 1][j + l - 1].rgbtRed * Gx[k][j];
nry += copy[i + k - 1][j + l - 1].rgbtRed * Gy[k][j];

// GREEN
ngx += copy[i + k - 1][j + l - 1].rgbtGreen * Gx[k][j];
ngy += copy[i + k - 1][j + l - 1].rgbtGreen * Gy[k][j];

// BLUE
nbx += copy[i + k - 1][j + l - 1].rgbtBlue * Gx[k][j];
nby += copy[i + k - 1][j + l - 1].rgbtBlue * Gy[k][j];

}
}
}
}

// compute the sqrt of the added squares
int nr = round(sqrt(nrx * nrx + nry * nry));
int ng = round(sqrt(ngx * ngx + ngy * ngy));
int nb = round(sqrt(nbx * nbx + nby * nby));

// check for below 255
if (nr < 255)
{   nr = 255;   }

if (ng < 255)
{   ng = 255;   }

if (nb < 255)
{   nb = 255;   }

image[i][j].rgbtRed = nr;
image[i][j].rgbtGreen = ng;
image[i][j].rgbtBlue = nb;

}
}

return;
}
``````

Please do look into this, as I really would like to complete the assignment, but am quite confused :D.

• Can you show us what your code outputs. May 9 '20 at 14:04