0

I have been struggling with the edge detection program. It produces weird output!!!
The code is as given below:

void edges(int height, int width, RGBTRIPLE image[height][width])
{
    float avgRedGx, avgGreenGx, avgBlueGx;
    float avgRedGy, avgGreenGy, avgBlueGy;
    int n;

    RGBTRIPLE temp[height+2][width+2];

    for(int i = 0; i < height+2; i++)
    {
        for (int j = 0; j < width+2; j++)
        {
            if(i == 0 || j == 0 || i == height+1 || j == width+1)
            {
                temp[i][j].rgbtRed = 0;
                temp[i][j].rgbtBlue = 0;
                temp[i][j].rgbtGreen = 0;

            }
            else
            {
                temp[i][j].rgbtRed = image[i-1][j-1].rgbtRed;
                temp[i][j].rgbtBlue = image[i-1][j-1].rgbtBlue;
                temp[i][j].rgbtGreen = image[i-1][j-1].rgbtGreen;
            }
        }
    }

    for(int i = 1; i < height + 1; i++)
    {
        for(int j = 1; j < width + 1; j++)
        {

                avgRedGx = (temp[i][j].rgbtRed*0 +
                            temp[i+1][j].rgbtRed*0 +
                            temp[i-1][j].rgbtRed*0 +
                            temp[i][j-1].rgbtRed*(-2) +
                            temp[i-1][j-1].rgbtRed*(-1) +
                            temp[i+1][j-1].rgbtRed*(-1) +
                            temp[i][j+1].rgbtRed*(2) +
                            temp[i-1][j+1].rgbtRed +
                            temp[i+1][j+1].rgbtRed);

                avgRedGy = (temp[i][j].rgbtRed*0 +
                            temp[i+1][j].rgbtRed*2 +
                            temp[i-1][j].rgbtRed*2 +
                            temp[i][j-1].rgbtRed*0 +
                            temp[i-1][j-1].rgbtRed*(-1) +
                            temp[i+1][j-1].rgbtRed +
                            temp[i][j+1].rgbtRed*0 +
                            temp[i-1][j+1].rgbtRed*(-1) +
                            temp[i+1][j+1].rgbtRed);


                n = roundf(sqrt(avgRedGx*avgRedGx + avgRedGy*avgRedGy));

                if(n > 255)
                    image[i-1][j-1].rgbtRed = 255;
                else
                    image[i-1][j-1].rgbtRed = n;

//---------------------- Green Color -----------------------------------------------------------------
                avgGreenGx = (temp[i][j].rgbtGreen*0 +
                            temp[i+1][j].rgbtGreen*0 +
                            temp[i-1][j].rgbtGreen*0 +
                            temp[i][j-1].rgbtGreen*(-2) +
                            temp[i-1][j-1].rgbtGreen*(-1) +
                            temp[i+1][j-1].rgbtGreen*(-1) +
                            temp[i][j+1].rgbtGreen*(2) +
                            temp[i-1][j+1].rgbtGreen +
                            temp[i+1][j+1].rgbtGreen);

                avgGreenGy = (temp[i][j].rgbtGreen*0 +
                            temp[i+1][j].rgbtGreen*2 +
                            temp[i-1][j].rgbtGreen*2 +
                            temp[i][j-1].rgbtGreen*0 +
                            temp[i-1][j-1].rgbtGreen*(-1) +
                            temp[i+1][j-1].rgbtGreen +
                            temp[i][j+1].rgbtGreen*0 +
                            temp[i-1][j+1].rgbtGreen*(-1) +
                            temp[i+1][j+1].rgbtGreen);


                n = roundf(sqrt(avgGreenGx*avgGreenGx + avgGreenGy*avgGreenGy));

                if(n > 255)
                    image[i-1][j-1].rgbtGreen = 255;
                else
                    image[i-1][j-1].rgbtGreen = n;

//----------------------------            Blue Color -----------------------------------------------------

                avgBlueGx = (temp[i][j].rgbtBlue*0 +
                            temp[i+1][j].rgbtBlue*0 +
                            temp[i-1][j].rgbtBlue*0 +
                            temp[i][j-1].rgbtBlue*(-2) +
                            temp[i-1][j-1].rgbtBlue*(-1) +
                            temp[i+1][j-1].rgbtBlue*(-1) +
                            temp[i][j+1].rgbtBlue*(2) +
                            temp[i-1][j+1].rgbtBlue +
                            temp[i+1][j+1].rgbtBlue);

                avgBlueGy = (temp[i][j].rgbtBlue*0 +
                            temp[i+1][j].rgbtBlue*2 +
                            temp[i-1][j].rgbtBlue*2 +
                            temp[i][j-1].rgbtBlue*0 +
                            temp[i-1][j-1].rgbtBlue*(-1) +
                            temp[i+1][j-1].rgbtBlue +
                            temp[i][j+1].rgbtBlue*0 +
                            temp[i-1][j+1].rgbtBlue*(-1) +
                            temp[i+1][j+1].rgbtBlue);


                n = roundf(sqrt(avgBlueGx*avgBlueGx + avgBlueGy*avgBlueGy));

                if(n > 255)
                    image[i-1][j-1].rgbtBlue = 255;
                else
                    image[i-1][j-1].rgbtBlue = n;

        }
    }

}

The output of code on the command prompt is as shown below

enter image description here

Also, the picture generated by this code looks something like this.

enter image description here

The idea behind this code is that I have created another array called temp which is, in a way, a copy of image array; the only difference is that temp array has border of zeros just to avoid lots of if-else statements in main code.

I am unable to figure out where did go wrong? Could you please help me with this?

You must log in to answer this question.

Browse other questions tagged .