I had a problem when I was trying to solve the edge detection problem in the CS50.
Below is my code:
// Detect edges
void edges(int height, int width, RGBTRIPLE image[height][width])
{
// Ask for some temparory memories for store blur pixels
RGBTRIPLE temp[height][width];
// Consider every condition you may encounter with pixels
int GxR, GyR, GxG, GyG, GxB, GyB;
// Initialize Gx and Gy metrix
int Gx[3][3] = {{-1, 0, 1}, {-2, 0, 2}, {-1, 0, 1}};
int Gy[3][3] = {{-1, -2, -1}, {0, 0, 0}, {1, 2, 1}};
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
GxR = GyR = GxG = GyG = GxB = GyB= 0;
// Loop over 3x3 pixels
for (int h = -1; h < 2; h++)
{
for (int w = -1; w < 2; w++)
{
// Check if this pixel is outside the image
if (i + h < 0 || i + h > height - 1)
{
continue;
}
if (j + w < 0 || j + w > width - 1)
{
continue;
}
// sum each channel value
// X
GxR += image[i + h][j + w].rgbtRed * Gx[h + 1][w + 1];
GxG += image[i + h][j + w].rgbtGreen * Gx[h + 1][w + 1];
GxB += image[i + h][j + w].rgbtBlue * Gx[h + 1][w + 1];
// Y
GyR += image[i + h][j + w].rgbtRed * Gy[h + 1][w + 1];
GyG += image[i + h][j + w].rgbtGreen * Gy[h + 1][w + 1];
GyB += image[i + h][j + w].rgbtBlue * Gy[h + 1][w + 1];
}
}
// Calculate every Gx and Gy value and store in temp
temp[i][j].rgbtRed = round(sqrt((GxR * GxR + GyR * GyR)));
temp[i][j].rgbtGreen = round(sqrt((GxG * GxG + GyG * GyG)));
temp[i][j].rgbtBlue = round(sqrt((GxB * GxB + GyB * GyB)));
// Capped color value at 255
if (temp[i][j].rgbtRed > 255)
{
temp[i][j].rgbtRed = 255;
}
if (temp[i][j].rgbtGreen > 255)
{
temp[i][j].rgbtGreen = 255;
}
if (temp[i][j].rgbtBlue > 255)
{
temp[i][j].rgbtBlue = 255;
}
}
}
// Ready to iterate whole image from temp to image[i][j]
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
image[i][j] = temp[i][j];
}
}
return;
}
And when I ran check50, the result shows that the pixels' red and green values are correct but only blue is wrong. The result is as below:
:( edges correctly filters pixel on edge
expected "213 228 255\n", not "213 228 140\n"
:( edges correctly filters pixel in corner
expected "76 117 255\n", not "76 117 66\n"
:( edges correctly filters 3x3 image
expected "76 117 255\n21...", not "76 117 66\n213..."
:( edges correctly filters 4x4 image
expected "76 117 255\n21...", not "76 117 66\n213..."
Can somebody tell me what's wrong with my code?
I've tried my best to debug it but I still can't find where went wrong...
Your edges
function is almost correct. You just missed that with
// Calculate every Gx and Gy value and store in temp
temp[i][j].rgbtRed = round(sqrt((GxR * GxR + GyR * GyR)));
temp[i][j].rgbtGreen = round(sqrt((GxG * GxG + GyG * GyG)));
temp[i][j].rgbtBlue = round(sqrt((GxB * GxB + GyB * GyB)));
the members rgbtRed
, rgbtGreen
and rgbtBlue
are only 8 bits and the behavior of assigning floating type values from 256 on to them is undefined; the following code to cap the values can't work. So cap the RGB values before assigning them:
temp[i][j].rgbtRed = fmin(round(sqrt(GxR * GxR + GyR * GyR)), 255);
temp[i][j].rgbtGreen = fmin(round(sqrt(GxG * GxG + GyG * GyG)), 255);
temp[i][j].rgbtBlue = fmin(round(sqrt(GxB * GxB + GyB * GyB)), 255);