Sobel edge detection algorithm

188 views Asked by At

Trying to implement the Sobel edge detection algorithm. But it gives me a different output.

void edges(int height, int width, RGBTRIPLE image[height][width])
{
    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}};

    int sobelRed_X = 0;
    int sobelGreen_X = 0;
    int sobelBlue_X = 0;

    int sobelRed_Y = 0;
    int sobelGreen_Y = 0;
    int sobelBlue_Y = 0;

    for(int j=0; j<height-1; j++) //Columns
    {
        for(int i = 0; i<width-1; i++) //Rows
        {
            for(int k = 0; k<3; k++)//Columns
            {
                for(int l = 0; l<3 ;l++)
                {
                    sobelRed_X = sobelRed_X + (image[j+k][l+i].rgbtRed*gX[k][l]);
                    sobelGreen_X = sobelGreen_X + (image[j+k][l+i].rgbtGreen*gX[k][l]);
                    sobelBlue_X = sobelBlue_X + (image[j+k][l+i].rgbtBlue*gX[k][l]);

                    sobelRed_Y = sobelRed_Y + (image[j+k][l+i].rgbtRed*gY[k][l]);
                    sobelGreen_Y = sobelGreen_Y + (image[j+k][l+i].rgbtGreen*gY[k][l]);
                    sobelBlue_Y = sobelBlue_Y + (image[j+k][l+i].rgbtBlue*gY[k][l]);
                }
            }

            image[j+1][i+1].rgbtRed = sobel(sobelRed_X, sobelRed_Y);
            image[j+1][i+1].rgbtGreen= sobel(sobelGreen_X, sobelGreen_Y);
            image[j+1][i+1].rgbtBlue= sobel(sobelBlue_X, sobelBlue_Y);

            sobelRed_X = 0;
            sobelGreen_X = 0;
            sobelBlue_X = 0;

            sobelRed_Y = 0;
            sobelGreen_Y = 0;
            sobelBlue_Y = 0;
        }
    }
    return;
}

Function for sobel operation

int sobel(int Gx, int Gy)
{
    int n = sqrt((Gx*Gx) + (Gy*Gy));
    if(n>255)
    {
       return 255;
    }
    return n;
}

RGBTRIPLE is a struct which contains rgbtRed, rgbtGreen, rgbtBlue essentially the data about pixels.

Input file: Output file:

Output file seems to have overexposed and completely ignored detecting any edges. I tried it with grayscale image but it doesn't seem to be working.

0

There are 0 answers