sobel filter in matlab

To apply the Sobel filter in MATLAB for edge detection, follow these steps:

  1. Read the image using the imread function:
img = imread('image.jpg');
27 chars
2 lines
  1. Convert the image to grayscale using the rgb2gray function:
gray_img = rgb2gray(img);
26 chars
2 lines
  1. Apply the Sobel filter using the edge function with the 'Sobel' option:
sobel_img = edge(gray_img, 'Sobel');
37 chars
2 lines
  1. Alternatively, you can apply the Sobel filter separately on the x and y directions using the imfilter function:
sobel_x = [-1 0 1; -2 0 2; -1 0 1];
sobel_y = sobel_x';

sobel_x_img = imfilter(double(gray_img), sobel_x);
sobel_y_img = imfilter(double(gray_img), sobel_y);

sobel_img = sqrt(sobel_x_img.^2 + sobel_y_img.^2);
211 chars
8 lines

The resulting image sobel_img will show the edges in the original image detected using the Sobel filter.

gistlibby LogSnag