0

I have some problems with the edges function. The code compiles fine and all but the result looks like this:

https://imgur.com/a/S2MQLom

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?

1

//Detect upper right corner if(!(i+1 > width-1 || j+1 > height-11))

maybe because of this it is happening.. typo in if statement

1

It might be an issue to do with having height and width the wrong way round. I think this could be causing the issue with the right hand third of the image.

i think iteration starts in top left with [0][0] for i and j. The largest values of i and j would therefore be associated with the bottom and right hand side of the image. You therefore need to check values are not < 0 along the top and left, and the values are not > width or height along the bottom and right. This isn't what you have done.

1
  • Thank you for answering, but I fixed it already using a different approach. – 4RZG4 Jul 16 '20 at 7:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .