# Pset4 Filter edges - seems to work but does not get approved

The output looks correct. I manually calculated the values for one single pixel and printed out the calculated values in the function using printf() and the values were almost the same, with a slight difference in the result from the Sobel algorithm. When I use check50, all of the tests for the function fails. What am I missing here?

``````// Detect edges
void edges(int height, int width, RGBTRIPLE image[height][width])
{
// Create 3D array to store temp values
float temp[height][width][3];

// Loop over image height and width to reach each pixel
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
// Create arrays to store Gx and Gy values
int gX[3] = { 0, 0, 0 };
int gY[3] = { 0, 0, 0 };

// Add left and right column values to gX
for (int k = i - 1; k < i + 2; k++)
{
if ((k >= 0) && (k < height - 1) && (j > 0) && (j < width - 2))
{
if (k == i)
{
gX[0] += (image[k][j - 1].rgbtBlue * -2) + (image[k][j + 1].rgbtBlue * 2);
gX[1] += (image[k][j - 1].rgbtGreen * -2) + (image[k][j + 1].rgbtGreen * 2);
gX[2] += (image[k][j - 1].rgbtRed * -2) + (image[k][j + 1].rgbtRed * 2);
}
else
{
gX[0] += (image[k][j - 1].rgbtBlue * -1) + image[k][j + 1].rgbtBlue;
gX[1] += (image[k][j - 1].rgbtGreen * -1) + image[k][j + 1].rgbtGreen;
gX[2] += (image[k][j - 1].rgbtRed * -1) + image[k][j + 1].rgbtRed;
}
}
}

// Add top and bottom row values to gY
for (int l = j - 1; l < 2; l++)
{
if ((l >= 0) && (l < width - 1) && ((i > 0) && (i < height - 2)))
{
if (l == j)
{
gY[0] += (image[i - 1][l].rgbtBlue * -2) + (image[i + 1][l].rgbtBlue * 2);
gY[1] += (image[i - 1][l].rgbtGreen * -2) + (image[i + 1][l].rgbtGreen * 2);
gY[2] += (image[i - 1][l].rgbtRed * -2) + (image[i + 1][l].rgbtRed * 2);
}
else
{
gY[0] += (image[i - 1][l].rgbtBlue * -1) + image[i + 1][l].rgbtBlue;
gY[1] += (image[i - 1][l].rgbtGreen * -1) + image[i + 1][l].rgbtGreen;
gY[2] += (image[i - 1][l].rgbtRed * -1) + image[i + 1][l].rgbtRed;
}
}
}

// User sobel filter algorithm and add values to temp array
temp[i][j][0] = sqrt((gX[0] * gX[0]) + (gY[0] * gY[0]));
temp[i][j][1] = sqrt((gX[1] * gX[1]) + (gY[1] * gY[1]));
temp[i][j][2] = sqrt((gX[2] * gX[2]) + (gY[2] * gY[2]));

// Round to nearest integer and cap at 255 for each color
temp[i][j][0] = round(temp[i][j][0]);
if (temp[i][j][0] > 255)
{
temp[i][j][0] = 255;
}

temp[i][j][1] = round(temp[i][j][1]);
if (temp[i][j][1] > 255)
{
temp[i][j][1] = 255;
}

temp[i][j][2] = round(temp[i][j][2]);
if (temp[i][j][2] > 255)
{
temp[i][j][2] = 255;
}
}
}

// Iterate over temp array and add values to image array
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
image[i][j].rgbtBlue = (int) temp[i][j][0];
image[i][j].rgbtGreen = (int) temp[i][j][1];
image[i][j].rgbtRed = (int) temp[i][j][2];
}
}

return;
}
``````

Error log:

``````:( edges correctly filters middle pixel

Cause
expected "210 150 60\n", not "0 0 0\n"
Log
testing with sample 3x3 image
first row: (0, 10, 25), (0, 10, 30), (40, 60, 80)
second row: (20, 30, 90), (30, 40, 100), (80, 70, 90)
third row: (20, 20, 40), (30, 10, 30), (50, 40, 10)
running ./testing 4 0...
checking for output "210 150 60\n"...

Expected Output:
210 150 60
Actual Output:
0 0 0
:( edges correctly filters pixel on edge

Cause
expected "213 228 255\n", not "0 0 0\n"
Log
testing with sample 3x3 image
first row: (0, 10, 25), (0, 10, 30), (40, 60, 80)
second row: (20, 30, 90), (30, 40, 100), (80, 70, 90)
third row: (20, 20, 40), (30, 10, 30), (50, 40, 10)
running ./testing 4 1...
checking for output "213 228 255\n"...

Expected Output:
213 228 255
Actual Output:
0 0 0
:( edges correctly filters pixel in corner

Cause
expected "76 117 255\n", not "0 0 0\n"
Log
testing with sample 3x3 image
first row: (0, 10, 25), (0, 10, 30), (40, 60, 80)
second row: (20, 30, 90), (30, 40, 100), (80, 70, 90)
third row: (20, 20, 40), (30, 10, 30), (50, 40, 10)
running ./testing 4 2...
checking for output "76 117 255\n"...

Expected Output:
76 117 255
Actual Output:
0 0 0
:( edges correctly filters 3x3 image

Cause
expected "76 117 255\n21...", not "0 0 0\n0 0 0\n..."
Log
testing with sample 3x3 image
first row: (0, 10, 25), (0, 10, 30), (40, 60, 80)
second row: (20, 30, 90), (30, 40, 100), (80, 70, 90)
third row: (20, 20, 40), (30, 10, 30), (50, 40, 10)
running ./testing 4 3...
checking for output "76 117 255\n213 228 255\n192 190 255\n114 102 255\n210 150 60\n103 108 255\n114 117 255\n200 197 255\n210 190 255\n"...

Expected Output:
76 117 255
213 228 255
192 190 255
114 102 255
210 150 60
103 108 255
114 117 255
200 197 255
210 190 255
Actual Output:
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
:( edges correctly filters 4x4 image

Cause
expected "76 117 255\n21...", not "0 0 0\n140 140..."
Log
testing with sample 3x3 image
first row: (0, 10, 25), (0, 10, 30), (40, 60, 80), (50, 60, 80)
second row: (20, 30, 90), (30, 40, 100), (80, 70, 90), (80, 80, 90)
third row: (20, 20, 40), (30, 10, 30), (50, 40, 10), (50, 40, 100)
fourth row: (50, 20, 40), (50, 20, 40), (50, 40, 80), (50, 40, 80)
running ./testing 4 4...
checking for output "76 117 255\n213 228 255\n255 255 255\n255 255 255\n114 102 255\n210 150 60\n177 171 156\n250 247 255\n161 89 255\n126 128 181\n114 170 192\n247 220 192\n148 71 156\n133 100 121\n181 148 212\n212 170 255\n"...

Expected Output:
76 117 255
213 228 255
255 255 255
255 255 255
114 102 255
210 150 60
177 171 156
250 247 255
161 89 255
126 128 181
114 170 192
247 220 192
148 71 156
133 100 121
181 148 212
212 170 255
Actual Output:
0 0 0
140 140 110
0 0 0
0 0 0
70 20 30
206 150 29
30 0 0
0 0 0
0 0 0
120 80 60
0 0 0
0 0 0
0 0 0
30 20 30
0 0 0
0 0 0

``````
• What is the error check50 is yelling? Add a print of the error, preferably the complete error log in the link that check50 gives you. Commented Oct 10, 2021 at 1:55
• @KleyHalisson sure! I added the whole log Commented Oct 11, 2021 at 8:42
• I don't know the answer to your question cause it's kind of difficult (atleast for me) to look at that bit code But here is a hint - If we see the "Sobel operater" carefully , we can notice that the three numbers we have to multiply with are -1 , 0, and 1.(for zeroth row) They have the difference of 1. We have to multiply these numbers by 2 for second Row. and for 1st row you can skip that part. what I am saying is You dont need to do all that part manually.
– MWD
Commented Oct 11, 2021 at 9:15
• Of course! Thank you Commented Oct 19, 2021 at 8:34

``````if ((k >= 0) && (k < height - 1) && (j > 0) && (j < width - 2))// What I can see is that j won't be higher than 0, it only stays less or equal.
It just skips those parts and the values are never added to `gX` and `gY`. That's why the math is resulting 0.