find gradient using sobel filter in matlab

To find gradient using the Sobel filter in MATLAB, you can use the imgradient function along with the 'Sobel' option for the method input. Here is an example code:

main.m
% Read in the image
im = imread('image.jpg');

% Convert the image to grayscale
im_gray = rgb2gray(im);

% Apply the Sobel filter to compute the gradient in the x and y directions
[Gx, Gy] = imgradient(im_gray, 'Sobel');

% Compute the magnitude and direction of the gradient
[Gmag, Gdir] = imgradient(Gx, Gy);

% Display the results
figure; imshow(im_gray); title('Original Image');
figure; imshowpair(Gx, Gy, 'montage'); title('Gradient in X and Y Directions (Sobel)');
figure; imshowpair(Gmag, Gdir, 'montage'); title('Gradient Magnitude and Direction (Sobel)');
566 chars
17 lines

In this code, we first read in an image and convert it to grayscale. We then apply the Sobel filter to compute the gradients in the x and y directions using the imgradient function. We then compute the magnitude and direction of the gradient using the same function. Finally, we display the results using imshow and imshowpair functions.

gistlibby LogSnag