sobel x filter in matlab

To apply a Sobel X filter in MATLAB for edge detection, you can use the fspecial and imfilter functions as demonstrated below:

main.m
% Load the image
img = imread('example.jpg');

% Convert to grayscale
gray_img = rgb2gray(img);

% Create a Sobel X filter
sobel_x = [-1 0 1; -2 0 2; -1 0 1];

% Apply the filter using the 'imfilter' function
edges = imfilter(double(gray_img), sobel_x);

% Display the results
imshow(edges);
292 chars
15 lines

In the code above, we first load an image and convert it to grayscale. We then define a 3x3 Sobel X filter and apply it to the grayscale image using the imfilter function. Finally, we display the resulting edge map using the imshow function.

gistlibby LogSnag