Carven
Carven

Reputation: 15648

Does the sobel mask from fspecial find horizontal or vertical edges?

So I did something like this in Matlab:

s = fspecial('sobel');
imshow(conv2(image, s));

In matlab when I create a sobel mask with fspecial and use that mask with conv2 on an image, are the edges in the convoluted image the horizontal or vertical edges or has it already added both the horizontal and vertical edges? And what about the diagonal edges?

Upvotes: 1

Views: 6779

Answers (1)

sfstewman
sfstewman

Reputation: 5677

The documentation to fspecial tells us

h = fspecial('sobel') returns a 3-by-3 filter h (shown below) that emphasizes horizontal edges using the smoothing effect by approximating a vertical gradient. If you need to emphasize vertical edges, transpose the filter

To transpose the filter, use

hvert = ( fspecial('sobel') )'

The Sobel filter is basically a smoothed derivative operator. By detecting both horizontal and vertical edges, you essentially retrieve a Sobel approximation to the gradient of the image, which also gives you diagonal edges.

To actually emphasize edges without worrying about their direction, use the magnitude of this gradient:

hy = fspecial('sobel');
hx = hy';
gx = imfilter(image, hx); % x component of Sobel gradient
gy = imfilter(image, hy); % y component 

gmag = hypot(gx,gy); % magnitude of Sobel gradient

Upvotes: 5

Related Questions