I have applied sobel operator on an image from wikipedia (Bikesgray). But I am not getting desired output. My output is more brighter than the output shown in wikipedia. My code runs on .pgm file.
Here are the output of my code and the output shown in wikipedia
Here is my C code:
//image[][] is input image. temp_image[][] is output image. temp_image[][] and image[][] are global.
// 3 x 3 region of an image will be [z1 z2 z3]
[z4 z5 z6]
[z7 z8 z9]
//x direction mask [-1 -2 -1]
[0 0 0]
[1 2 1]
//y direction mask [-1 0 1]
[-2 0 2]
[-1 0 1]
void find_sobel_gradient_image()
{
int i,j;
int gx,gy;
double m;
int padded_image[700][700]={0};
//create padded image of 1pixel zero padding and copy data from image[][]
for(i=1;i<=y_size;i++)
{
for(j=1;j<=x_size;j++)
{
padded_image[i][j]=image[i-1][j-1];
}
}
//resulting image
for(i=1;i<=y_size;i++)
{
for(j=1;j<=x_size;j++)
{
//gx = (z7 + 2*z8 + z9)-(z1 + 2*z2+ z3)
gx=(padded_image[i+1][j-1]+2*padded_image[i+1][j]+padded_image[i+1][j+1])-(padded_image[i-1][j-1]+2*padded_image[i-1][j]+padded_image[i-1][j+1]);
//gy = (z3 + 2*z6 +z9) - (z1+ 2*z4 +z7)
gy=(padded_image[i-1][j+1]+2*padded_image[i][j+1]+padded_image[i+1][j+1])-(padded_image[i-1][j-1]+2*padded_image[i][j-1]+padded_image[i+1][j-1]);
m = sqrt(gx*gx+gy*gy);
temp_image[i][j]=round(m);
}
}
}
Note: each temp[ i ][ j ] > 255 ==> temp[ i ][ j ] = 255
The description in Wikipedia says that it is the normalized magnitude, yet you've clamped the magnitude. To normalize the magnitude, find the maximum value and a divisor that maps this value to 255, and then divide each pixel value by that divisor.