edge detection using sobel operator

my question is related to edge detection using sobel operator. I am doing my project related to this subject on FPGA so i want to see that what will be the result in matlab can u tell me how to do edge detection using sobel oerator in matlab. Thank you

3 Comments

function [ lenaOutput] = sobel(X)
%X input color image
X= double(X); height = size(X, 1); width = size(X, 2); channel = size(X, 3);
lenaOutput = X;
Gx = [1 +2 +1; 0 0 0; -1 -2 -1]; Gy = Gx';
for i = 2 : height-1
for j = 2 : width-1
for k = 1 : channel
tempLena = X(i - 1 : i + 1, j - 1 : j + 1, k);
a=(sum(Gx.* tempLena));
x = sum(a);
b= (sum(Gy.* tempLena));
y = sum(b);
pixValue =sqrt(x.^2+ y.^2);
% pixValue =(x-y);
lenaOutput(i, j, k) = pixValue;
end
end
end
lenaOutput = uint8(lenaOutput); figure; imshow(abs(lenaOutput),[]); title(' Sobel Edge Detection');
Above works but it's slow (1.218 seconds for my image). Here is a faster way:
Gx = [1 +2 +1; 0 0 0; -1 -2 -1]; Gy = Gx';
tic
temp_x = conv2(X, Gx, 'same');
temp_y = conv2(X, Gy, 'same');
lenaOutput = sqrt(temp_x.^2 + temp_y.^2);
toc
Elapsed time is 0.005787 seconds.
Hi nenad,
I try your code and I didnt get the same result as Akshay code. Your code resulting in matrix dimension while Akshay code in single value. Am I making mistake?

Sign in to comment.

 Accepted Answer

Use imgradient() to get the true Sobel filtered image. If you use edge() it doesn't give it to you but gives you a thresholded and skeletonized version of the Sobel filtered image instead.

3 Comments

See attached full blown demo (below in blue text) where you can choose a number of images.
megha, are you still there?????
Hello, Image Analyst, who is so kind to answer most of the questions asked by anyone here.
Let me ask you one thing, I am a student of doing signature recognition process. Regardless of signature, can you please give me some example (tiny example) for training and testing. My code writing in Mat file is ok in training phase but there are some mismatch (attribute mismatch) occurs in loading .mat for recognition process. I would be very grateful if you can show small code (extract features with anymethod for training and save in .mat file) and use any method (e.g KNN) for recognition process for only image.
Thanks you so much in advance and I am sorry if this request bothers you. I am really stuck in it.

Sign in to comment.

More Answers (1)

chitresh
chitresh on 7 Dec 2013
I = imread('image_file');
BW1 = edge(I,'sobel');
imshow(BW1);

Asked:

on 6 Dec 2013

Commented:

on 2 Oct 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!