# Q: PSETt#4 - Filter (more) - Edges

I'm stuck with the problem pset4 edges. The checks for the middle pixels work. But not the corner's pixels.
:( edges correctly filters pixel in corner expected "76 117 255\n", not "76 117 66\n"
It looks like I'm getting the right value for the red and green value, but not for the blue one. I checked the math again and again, and I tried to debug in a different program where I created a 3x3 array with the same values that check50 uses. It works properly in the latter, giving me the right values as the check50 ones. I cannot really see where the problem is, I know that I could have wrote the code much better, becasue it's clearly too long, but please help me to find out what's wrong with this one! The following just contains a code for middle pixels and corners pixels. Again the middle pixel works as expected.

``````void edges(int height, int width, RGBTRIPLE image[height][width])
{
float red[height][width], green[height][width], blue[height][width];
float r, g, b, rr, bb, gg;
//copy the values to three different arrays, representing: red, green and blue.
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
red[i][j] = image[i][j].rgbtRed;
green[i][j] = image[i][j].rgbtGreen;
blue[i][j] = image[i][j].rgbtBlue;
}
}
//moves through the pixels
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
//Middle pixels
if (i > 0 && j < width -1  && i < height -1 && j > 0)
{
r = (-1 * red[i-1][j-1]) + red[i-1][j+1] - 2 * red[i][j-1] + 2 * red[i][j+1] - red[i+1]
[j-1] + red[i+1][j+1];
g = (-1 * green[i-1][j-1]) + green[i-1][j+1] -2 * green[i][j-1] + (2 * green[i][j+1]) -
green[i+1][j-1] + green[i+1][j+1];
b = (-1 * blue[i-1][j-1]) + blue[i-1][j+1] -2 * blue[i][j-1] + 2 * blue[i][j+1] -
blue[i+1][j-1] + blue[i+1][j+1];

rr = (-1 * red[i-1][j-1]) -2 * red[i-1][j] - red[i-1][j+1] + red[i+1][j-1] + 2 * red[i+1]
[j] + red[i+1][j+1];
gg = (-1 * green[i-1][j-1]) -2 * green[i-1][j] - green[i-1][j+1] + green[i+1][j-1] + 2 *
green[i+1][j] + green[i+1][j+1];
bb = (-1 * blue[i-1][j-1]) -2 * blue[i-1][j] - blue[i-1][j+1] + blue[i+1][j-1] + 2 *
blue[i+1][j] + blue[i+1][j+1];
}
//top-left corner
if (i == 0 && j== 0)
{
r = 2 * red[i][j+1] + red[i+1][j+1];
g = 2 * green[i][j+1] + green[i+1][j+1];
b = 2 * blue[i][j+1] + blue[i+1][j+1];

rr = 2 * red[i+1][j] + red[i+1][j+1];
gg = 2 * green[i+1][j] + green[i+1][j+1];
bb = 2 * blue[i+1][j] + blue[i+1][j+1];
}
//top-right corner
if (i == 0 && j == width - 1)
{
r = -2 * red[i][j-1] - 1 * red[i+1][j-1];
g = -2 * green[i][j-1] - 1 * green[i+1][j-1];
b = -2 * blue[i][j-1] - 1 * blue[i+1][j-1];

rr = red[i+1][j-1] + 2 * red[i+1][j];
gg = green[i+1][j-1] + 2 * green[i+1][j];
bb = blue[i+1][j-1] + 2 * blue[i+1][j];
}
//bottom-left corner
if (i == height -1 && j == 0)
{
r = red[i-1][j+1] + 2 * red[i][j+1];
g = green[i-1][j+1] + 2 * green[i][j+1];
b = blue[i-1][j+1] + 2 * blue[i][j+1];

rr = -2 * red[i-1][j] - red[i-1][j+1];
gg = -2 * green[i-1][j] - green[i-1][j+1];
bb = -2 * blue[i-1][j] - blue[i-1][j+1];
}
//bottom-right corner
if (i == height - 1 && j == width -1)
{
r = -1 * red[i-1][j-1] - 2 * red[i][j-1];
g = -1 * green[i-1][j-1] - 2 * green[i][j-1];
b = -1 * blue[i-1][j-1] - 2 * blue[i][j-1];

rr = -1 * red[i-1][j-1] - 2 * red[i -1][j];
gg = -1 * green[i-1][j-1] - 2 * green[i -1][j];
bb = -1 * blue[i-1][j-1] - 2 * blue[i -1][j];
}
//Compute each value throw the formula
image[i][j].rgbtRed = round(sqrt((r * r) + (rr * rr)));
image[i][j].rgbtGreen = round(sqrt((g * g) + (gg * gg)));
image[i][j].rgbtBlue = round(sqrt((b * b) + (bb * bb)));

//cap values
if (image[i][j].rgbtRed > 255)
{
image[i][j].rgbtRed = 255;
}
if (image[i][j].rgbtGreen > 255)
{
image[i][j].rgbtGreen = 255;
}
if (image[i][j].rgbtBlue > 255)
{
image[i][j].rgbtBlue = 255;
}
}
}
return;
}
``````
• for top-left corner for getting r why did you multiply 2 * red[i][j+1] + red[i+1][j+1]; why times two for the first part and times positive one for the second part? Oct 19, 2020 at 6:18
• okay after looking at it for a long time. I think I know why. For Gx you imagine a 3x3 matrix for corner pixels and you take what is left from the Gx matrix. so for the top left corner what is left from Gx is the  and  pixels and that is why you multiply 2 and positive 1 Oct 19, 2020 at 6:46