# Use of the Sobel Filter for Image Gradient [MATLAB]

I am using the Sobel filter for an RGB image. I have found two different ways to do that and the results look a little different. What is the difference between these two methods?

Method 1

 sbl=fspecial('Sobel');
sbl_red=imfilter(Image(:,:,1));


Method 2

 [magred,dirred]=imgradient(Image(:,:,1),'Sobel')


Is there an advantage over one of the two methods? I am trying to find a structure of the image that is parallel to the x axis.

• Build your own filters. That is the only way to know what you are doing – mathreadler Jun 9 '18 at 6:17

Well, if you go through the documentation of fspecial() you'd see it returns the following filter:
h = fspecial('sobel') returns a 3-by-3 filter h that emphasizes horizontal edges using the smoothing effect by approximating a vertical gradient. To emphasize vertical edges, transpose the filter h'.

While imgradient() returns the image of the magnitude of the Gradient (Which has Vertical and Horizontal elements) and its direction.