I am having problems with CS50 PSet 4 - Edge. The method passes the middle pixel test (which has no value of 255 on any channel) but on any other test all the values in each channel are correct unless the value should be 255. I am at a loss why this is occurring.
I used a series of "If" statements to determine if the pixels surrounding image[I][j] existed. If so I added each channel value to the appropriate accumulating channel variable. After all surrounding pixels were checked I normalized and rounded.
I did a simple If statement for each color to bring any values over 255 back down to 255. The test for > 255 works but the method never returns a value high enough to be capped. Any help would be greatly appreciated. Thank you.
// Detect edges
void edges(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE edge_image[height][width]; // Working array to preserve original image values
for (int i = 0; i < height; i++) // Iterate through Rows
{
for (int j = 0; j < width; j++) // Iterate through Columns
{
edge_image[i][j] = image[i][j]; // Transfer values to working array
}
}
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
float x_red_value = 0.0; // Declaring variables to accumulate channel values
float y_red_value = 0.0;
float x_green_value = 0.0;
float y_green_value = 0.0;
float x_blue_value = 0.0;
float y_blue_value = 0.0;
if (i > 0) // Pixel above exists @ (i-1,j)(0,2) no x component
{
y_red_value += edge_image[i - 1][j].rgbtRed * 2;
y_green_value += edge_image[i - 1][j].rgbtGreen * 2;
y_blue_value += edge_image[i - 1][j].rgbtBlue * 2;
}
if (i < height - 1) // Pixel below exists @ (i+1,j)(0,-2) no x component
{
y_red_value += edge_image[i + 1][j].rgbtRed * -2;
y_green_value += edge_image[i + 1][j].rgbtGreen * -2;
y_blue_value += edge_image[i + 1][j].rgbtBlue * -2;
}
if (j > 0) // Pixel to the left exists at (i, j-1)(2,0) no y component
{
x_red_value += edge_image[i][j - 1].rgbtRed * 2;
x_green_value += edge_image[i][j - 1].rgbtGreen * 2;
x_blue_value += edge_image[i][j - 1].rgbtBlue * 2;
}
if (j < width - 1) // Pixel to the right exists @ (i,j+1)(-2,0) no y component
{
x_red_value += edge_image[i][j + 1].rgbtRed * -2;
x_green_value += edge_image[i][j + 1].rgbtGreen * -2;
x_blue_value += edge_image[i][j + 1].rgbtBlue * -2;
}
if (i > 0 && j > 0) // Pixel up and to the left exists (i - 1, j - 1)(1,1)
{
x_red_value += edge_image[i - 1][j - 1].rgbtRed * 1;
y_red_value += edge_image[i - 1][j - 1].rgbtRed * 1;
x_green_value += edge_image[i - 1][j - 1].rgbtGreen * 1;
y_green_value += edge_image[i - 1][j - 1].rgbtGreen * 1;
x_blue_value += edge_image[i - 1][j - 1].rgbtBlue * 1;
y_blue_value += edge_image[i - 1][j - 1].rgbtBlue * 1;
}
if (i > 0 && j < width - 1) // Pixel up and to the right exists (i - 1, j + 1)(-1,1)
{
x_red_value += edge_image[i - 1][j + 1].rgbtRed * -1;
y_red_value += edge_image[i - 1][j + 1].rgbtRed * 1;
x_green_value += edge_image[i - 1][j + 1].rgbtGreen *-1;
y_green_value += edge_image[i - 1][j + 1].rgbtGreen * 1;
x_blue_value += edge_image[i - 1][j + 1].rgbtBlue * -1;
y_blue_value += edge_image[i - 1][j + 1].rgbtBlue * 1;
}
if (i < height - 1 && j > 0) // Pixel down and to the left exists (i + 1, j - 1)(1,-1)
{
x_red_value += edge_image[i + 1][j - 1].rgbtRed * 1;
y_red_value += edge_image[i + 1][j - 1].rgbtRed * -1;
x_green_value += edge_image[i + 1][j - 1].rgbtGreen *1;
y_green_value += edge_image[i + 1][j - 1].rgbtGreen * -1;
x_blue_value += edge_image[i + 1][j - 1].rgbtBlue * 1;
y_blue_value += edge_image[i + 1][j - 1].rgbtBlue * -1;
}
if (i < height - 1 && j < width - 1) // Pixel down and to the right exists (i + 1, j +1)(-1,-1)
{
x_red_value += edge_image[i + 1][j + 1].rgbtRed * -1;
y_red_value += edge_image[i + 1][j + 1].rgbtRed * -1;
x_green_value += edge_image[i + 1][j + 1].rgbtGreen * -1;
y_green_value += edge_image[i + 1][j + 1].rgbtGreen * -1;
x_blue_value += edge_image[i + 1][j + 1].rgbtBlue * -1;
y_blue_value += edge_image[i + 1][j + 1].rgbtBlue * -1;
}
image[i][j].rgbtRed = round(sqrt(pow(x_red_value, 2)+ pow(y_red_value, 2)));
if( image[i][j].rgbtRed > 255)
{
image[i][j].rgbtRed = 255;
}
image[i][j].rgbtGreen = round(sqrt(pow(x_green_value, 2)+ pow(y_green_value, 2)));
if( image[i][j].rgbtGreen > 255)
{
image[i][j].rgbtGreen = 255;
}
image[i][j].rgbtBlue = round(sqrt(pow(x_blue_value, 2)+ pow(y_blue_value, 2)));
if( image[i][j].rgbtBlue > 255)
{
image[i][j].rgbtBlue = 255;
}
}
}
return;
}