I have some problems with the edges function. The code compiles fine and all but the result looks like this:
https://i.sstatic.net/saEBo.jpg
The edges of the image and one third of it don't look as expected.
Here's my code:
void edges(int height, int width, RGBTRIPLE image[height][width])
{
//create copy and copy image to it
RGBTRIPLE copy[height][width];
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
copy[j][i] = image[j][i];
}
}
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
float redGx = 0.00;
float greenGx = 0.00;
float blueGx = 0.00;
float redGy = 0.00;
float greenGy = 0.00;
float blueGy = 0.00;
//Detect upper left corner
if(!(i-1 < 0 || j+1 > height-1))
{
redGx += -1 * image[j+1][i-1].rgbtRed;
greenGx += -1 * image[j+1][i-1].rgbtGreen;
blueGx += -1 * image[j+1][i-1].rgbtBlue;
redGy += -1 * image[j+1][i-1].rgbtRed;
greenGy += -1 * image[j+1][i-1].rgbtGreen;
blueGy += -1 * image[j+1][i-1].rgbtBlue;
}
//Detect up
if(!(j+1 > height-1))
{
redGx += 0 * image[j+1][i].rgbtRed;
greenGx += 0 * image[j+1][i].rgbtGreen;
blueGx += 0 * image[j+1][i].rgbtBlue;
redGy += -2 * image[j+1][i].rgbtRed;
greenGy += -2 * image[j+1][i].rgbtGreen;
blueGy += -2 * image[j+1][i].rgbtBlue;
}
//Detect upper right corner
if(!(i+1 > width-1 || j+1 > height-11))
{
redGx += 1 * image[j+1][i+1].rgbtRed;
greenGx += 1 * image[j+1][i+1].rgbtGreen;
blueGx += 1 * image[j+1][i+1].rgbtBlue;
redGy += -1 * image[j+1][i+1].rgbtRed;
greenGy += -1 * image[j+1][i+1].rgbtGreen;
blueGy += -1 * image[j+1][i+1].rgbtBlue;
}
//Detect left
if(!(i-1 < 0))
{
redGx += -2 * image[j][i-1].rgbtRed;
greenGx += -2 * image[j][i-1].rgbtGreen;
blueGx += -2 * image[j][i-1].rgbtBlue;
redGy += 0 * image[j][i-1].rgbtRed;
greenGy += 0 * image[j][i-1].rgbtGreen;
blueGy += 0 * image[j][i-1].rgbtBlue;
}
//Detect middle
redGx += 0 * image[j][i].rgbtRed;
greenGx += 0 * image[j][i].rgbtGreen;
blueGx += 0 * image[j][i].rgbtBlue;
redGy += 0 * image[j][i].rgbtRed;
greenGy += 0 * image[j][i].rgbtGreen;
blueGy += 0 * image[j][i].rgbtBlue;
//Detect right
if(!(i+1 > width-1))
{
redGx += 2 * image[j][i+1].rgbtRed;
greenGx += 2 * image[j][i+1].rgbtGreen;
blueGx += 2 * image[j][i+1].rgbtBlue;
redGy += 0 * image[j][i+1].rgbtRed;
greenGy += 0 * image[j][i+1].rgbtGreen;
blueGy += 0 * image[j][i+1].rgbtBlue;
}
//Detect lower right corner
if(!(i-1 < 0 || j-1 < 0))
{
redGx += -1 * image[j-1][i-1].rgbtRed;
greenGx += -1 * image[j-1][i-1].rgbtGreen;
blueGx += -1 * image[j-1][i-1].rgbtBlue;
redGy += 1 * image[j-1][i-1].rgbtRed;
greenGy += 1 * image[j-1][i-1].rgbtGreen;
blueGy += 1 * image[j-1][i-1].rgbtBlue;
}
//Detect down
if(!(j-1 < 0))
{
redGx += 0 * image[j-1][i].rgbtRed;
greenGx += 0 * image[j-1][i].rgbtGreen;
blueGx += 0 * image[j-1][i].rgbtBlue;
redGy += 2 * image[j-1][i].rgbtRed;
greenGy += 2 * image[j-1][i].rgbtGreen;
blueGy += 2 * image[j-1][i].rgbtBlue;
}
//Detect lower right corner
if(!(i+1 > height-1 || j-1 < 0))
{
redGx += 1 * image[j-1][i+1].rgbtRed;
greenGx += 1 * image[j-1][i+1].rgbtGreen;
blueGx += 1 * image[j-1][i+1].rgbtBlue;
redGy += 1 * image[j-1][i+1].rgbtRed;
greenGy += 1 * image[j-1][i+1].rgbtGreen;
blueGy += 1 * image[j-1][i+1].rgbtBlue;
}
//end
copy[j][i].rgbtRed = (int) cap(round(sqrt((pow(redGx, 2))+(pow(redGy, 2)))));
copy[j][i].rgbtGreen = (int) cap(round(sqrt((pow(greenGx, 2))+(pow(greenGy, 2)))));
copy[j][i].rgbtBlue = (int) cap(round(sqrt((pow(blueGx, 2))+(pow(blueGy, 2)))));
}
}
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
image[j][i] = copy[j][i];
}
}
return;
}
//nothing can become more than 255
int cap(double n)
{
int result = 0;
if (n >= 255)
{
result = 255;
}
else if(n < 0)
{
result = 0;
}
else
{
result = (int) round(n);
}
return (result);
}
I have done a bit of research but haven't found anything that works for me
Can someone help?