Dienstag, 30. Juli 2013

Sobel Edge Detection - Part 2

In Edge Detection- fundamentals, we have seen how the first and second order derivatives are used in finding the edge strength. Now lets see another version of sobel edge detection.

Basic Steps followed in Sobel Edge Detection:

1.       Obtain the gradient of the image.
2.       Find the magnitude
3.       Threshold the gradient image.


SOBEL EDGE DETECTION USING ‘edge’ FUNCTION:

%Input Image
A=imread('coins.png');

%Image obtained using MATLAB function 'edge'
[E,th]=edge(A,'sobel','nothinning');
figure,imshow(E);title('Image obtained using MATLAB function')




Edge Detection without using the 'edge' function: 

MATLAB CODE:

%Input Image
A=imread('coins.png');

%Preallocate the matrices with zeros
I=zeros(size(A));


%Filter Masks
F1=[-1 0 1;-2 0 2; -1 0 1];
F2=[-1 -2 -1;0 0 0; 1 2 1];

A=double(A);


for i=1:size(A,1)-2
    for j=1:size(A,2)-2
        %Gradient operations
        Gx=sum(sum(F1.*A(i:i+2,j:j+2)));
        Gy=sum(sum(F2.*A(i:i+2,j:j+2)));
               
        %Magnitude of vector
         I(i+1,j+1)=sqrt(Gx.^2+Gy.^2);
       
    end
end


I=uint8(I);
figure,imshow(I);title('Filtered Image');



%Define a threshold value
Thresh=210;
B=max(I,Thresh);
B(B==round(Thresh))=0;

B=im2bw(B);
figure,imshow(B);title('Edge detected Image');



EXPLANATION:

1.       Read the image
2.       Convert the image to double
3.       Use the mask F1 for x direction and F2 for y direction and obtain the gradient of the image.
4.       Find the magnitude of the vector.
5.       Since we need 3x3 image pixels, the border pixels are not considered, and so starting from the pixel (2, 2) the edge detection process starts.
       %Magnitude of vector
    I(i+1,j+1)=sqrt(Gx.^2+Gy.^2);
    When i=1 and j =1, then Image I pixel position will be I(2,2).Thus we are not considering the borders.
Example:

    In the for loop 2 is subtracted.
"
for i=1:size(A,1)-2
    for j=1:size(A,2)-2 "
     The filter mask is 3x3, so the last position to be processed in our example is I(3,3).And normally it will be I(size(A,1)-2,size(A,2)-2). Thus the borders are left.

Example :
               


6.       Threshold the image
7.       Display the logical image


Advantage:

1. Sobel masks perform better noise suppression.

2. Image smoothing


Disadvantage:

1.       Diagonal direction points are not preserved always.

Keine Kommentare:

Kommentar veröffentlichen