# PSET4 Filter, edge detection process only works for square-like pictures. How to make it work for the whole picture?

I have written my code based on how I structured the code for the blur filter.

For some reason the result is pretty weird, the processing of edge detection is only taking place in a square. See result here: https://ibb.co/k5RtLXx

So, if the picture is a square I get it working but if the picture is rectangle-like then the processing happens only in a square on the image. I can't get my head around as to why is my code doing this.

Any help would be most appreciated.

Here is my code to get edge detection working:

``````

// Detect edges
void edges(int height, int width, RGBTRIPLE image[height][width])
{
// Clone original image to make calculations
RGBTRIPLE copy[height][width];

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

// Main loop to sum up all values for each channel in each pixel relative to Gx and Gy
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
float rGx, gGx, bGx, rGy, gGy, bGy;
rGx = gGx = bGx = rGy = gGy = bGy = 0;

// Up
if (i > 0)
{
rGy += -2 * copy[i-1][j].rgbtRed;
gGy += -2 * copy[i-1][j].rgbtGreen;
bGy += -2 * copy[i-1][j].rgbtBlue;
}
// Down
if (i < height - 1)
{

rGy += 2 * copy[i+1][j].rgbtRed;
gGy += 2 * copy[i+1][j].rgbtGreen;
bGy += 2 * copy[i+1][j].rgbtBlue;
}
// Left
if (j > 0)
{
rGx += -2 * copy[i][j-1].rgbtRed;
gGx += -2 * copy[i][j-1].rgbtGreen;
bGx += -2 * copy[i][j-1].rgbtBlue;

}
// Right
if (j < height - 1)
{
rGx += 2 * copy[i][j+1].rgbtRed;
gGx += 2 * copy[i][j+1].rgbtGreen;
bGx += 2 * copy[i][j+1].rgbtBlue;

}
// Up left
if (i > 0 && j > 0)
{
// Note: change sign in Gx values here and the result changes side
rGx += -1 * copy[i-1][j-1].rgbtRed;
gGx += -1 * copy[i-1][j-1].rgbtGreen;
bGx += -1 * copy[i-1][j-1].rgbtBlue;
rGy += -1 * copy[i-1][j-1].rgbtRed;
gGy += -1 * copy[i-1][j-1].rgbtGreen;
bGy += -1 * copy[i-1][j-1].rgbtBlue;
}
// Up right
if (i > 0 && j < width - 1)
{
rGx += 1 * copy[i-1][j+1].rgbtRed;
gGx += 1 * copy[i-1][j+1].rgbtGreen;
bGx += 1 * copy[i-1][j+1].rgbtBlue;
rGy += -1 * copy[i-1][j+1].rgbtRed;
gGy += -1 * copy[i-1][j+1].rgbtGreen;
bGy += -1 * copy[i-1][j+1].rgbtBlue;
}
// Bottom right
if (i < height - 1  && j < width - 1)
{
rGx += 1 * copy[i+1][j+1].rgbtRed;
gGx += 1 * copy[i+1][j+1].rgbtGreen;
bGx += 1 * copy[i+1][j+1].rgbtBlue;
rGy += 1 * copy[i+1][j+1].rgbtRed;
gGy += 1 * copy[i+1][j+1].rgbtGreen;
bGy += 1 * copy[i+1][j+1].rgbtBlue;
}
// Bottom left
if (i < height - 1  && j > 0)
{
rGx += -1 * copy[i+1][j-1].rgbtRed;
gGx += -1 * copy[i+1][j-1].rgbtGreen;
bGx += -1 * copy[i+1][j-1].rgbtBlue;
rGy += 1 * copy[i+1][j-1].rgbtRed;
gGy += 1 * copy[i+1][j-1].rgbtGreen;
bGy += 1 * copy[i+1][j-1].rgbtBlue;
}

// Compute result
image[i][j].rgbtRed = (int) cap255(sqrt((rGx*rGx)+(rGy*rGy)));
image[i][j].rgbtGreen = (int) cap255(sqrt((gGx*gGx)+(gGy*gGy)));
image[i][j].rgbtBlue = (int) cap255(sqrt((bGx*bGx)+(bGy*bGy)));
}
}
return;
}

// Function to cap results at 255 maximum
int cap255(double n)
{
int result = 0;

if (n >= 255)
{
result = 255;
}
else if(n < 0)
{
result = 0;
}
else
{
result = (int) round(n);
}
return (result);
}
``````

``````if (j < height - 1)
• It solved my problem! Thank you so much. I have overlooked this detail so many times... Thank you for leading me in the right direction. I was basically stopping the program to go any further than j > height. The correct, and obvious, solution was: `if (j < width - 1)` Mar 6 '20 at 13:36
• The "square" component of this kind of gave it away. Since `height` is smaller than `width`, I assumed you used one where you meant the other. Mar 6 '20 at 15:19