Wednesday, April 22, 2009

SOBEL Edge Detection with C++ Code


Based on this one-dimensional analysis, the theory can be carried over to two-dimensions as long as there is an accurate approximation to calculate the derivative of a two-dimensional image. The Sobel operator performs a 2-D spatial gradient measurement on an image. Typically it is used to find the approximate absolute gradient magnitude at each point in an input grayscale image. The Sobel edge detector uses a pair of 3x3 convolution masks, one estimating the gradient in the x-direction (columns) and the other estimating the gradient in the y-direction (rows). A convolution mask is usually much smaller than the actual image. As a result, the mask is slid over the image, manipulating a square of pixels at a time.


void sobel( int *a, int sx, int sy, int *out)
{
// (In) a: The image (row major) containing elements of int.
// (In) sx: size of image along x-dimension.
// (In) sy: size of image along y-dimension.
// (Out) out: The output image with dimensions (sx-2, sy-2).

int i, j, ctr = 0;
int *p1, *p2, *p3;

p1 = a;
p2 = p1+sx;
p3 = p2+sx;

for( j = 0; j <>
for( i = 0; i <>
out[ctr++] = ( abs((p1[0]+ 2*p1[1] + p1[2]) - (p3[0] + 2*p3[1]+ p3[2])) + abs((p1[2] + 2*p2[2] + p3[2]) - (p1[0] + 2*p2[0] + p3[0])) ) / 6;
++p1;
++p2;
++p3;
}
p1 += 2;
p2 += 2;
p3 +=2; }
}





No comments:

Post a Comment