Thursday, October 25, 2012

Matlab Code for Edge Detection Robert, Prewitt, Sobel


%%%% EDGE Detection

clc;
close all;
clear all;
a=imread('C:\Documents and Settings\student\Desktop\lenna1.bmp');



b=im2double(a);
[m,n]=size(a);

%ROBERT
L(1:m,1:n)=0
for i=1:m-2;
    for j=1:m-2;
        L(i,j)=-1*b(i,j)+0+0+1*b(i+1,j+1);
    end;
end;



M(1:m,1:n)=0
for i=1:m-2;
    for j=1:m-2;
        M(i,j)=0-1*b(i,j+1)+1*b(i+1,j)+0;
    end;
end;

figure;
subplot(2,2,1)
imshow(L)
title('Robert    Gx');
subplot(2,2,2)
imshow(M)
title('Robert    Gy');
N=M+L;
subplot(2,2,3)
imshow(N)
title('Robert    Gx+Gy');
subplot(2,2,4)
imshow(b)
title('Original Image');



%PREWIT
N(1:m,1:n)=0
for i=1:m-2;
    for j=1:m-2;
        N(i,j)=-1*b(i,j)-1*b(i,j+1)-1*b(i,j+2)+0+0+0+1*b(i+2,j)+1*b(i+2,j+1)+1*b(i+2,j+2);
    end;
end;
O(1:m,1:n)=0
for i=1:m-2;
    for j=1:m-2;
        O(i,j)=-1*b(i,j)+0+1*b(i,j+2)-1*b(i+2,j)+0+1*b(i+1,j+2)-1*b(i+2,j)+0+1*b(i+2,j+2);
    end;
end;
figure;
subplot(2,2,1)
imshow(N)
title('Prewit    Gx');
subplot(2,2,2)
imshow(O)
title('Prewit    Gy');
Z=N+O;
subplot(2,2,3)
imshow(Z)
title('Prewit    Gx+Gy');
subplot(2,2,4)
imshow(b)
title('Original Image');

%SOBEL
P(1:m,1:n)=0
for i=1:m-2;
    for j=1:m-2;
        P(i,j)=-1*b(i,j)-2*b(i,j+1)-1*b(i,j+2)+0+0+0+1*b(i+2,j)+2*b(i+2,j+1)+1*b(i+2,j+2);
    end;
end;


R(1:m,1:n)=0
for i=1:m-2;
    for j=1:m-2;
        R(i,j)=-1*b(i,j)+0+1*b(i,j+2)-2*b(i+1,j)+0+2*b(i+1,j+2)-1*b(i+2,j)+0+1*b(i+2,j+2);
    end;
end;

 figure;
subplot(2,2,1)
imshow(P)
title('Sobel    Gx');
subplot(2,2,2)
imshow(R)
title('Sobel   Gy');

Y=P+R;
subplot(2,2,3)
imshow(Y)
title('Soble  Gx+Gy');
subplot(2,2,4)
imshow(b)

title('Original  Image');








7 comments:

  1. Thanks. Really Helped.
    in second "for" loop you should change m to n though:

    for i=1:m-2;
    for j=1:n-2; % Here n is correct not m

    ReplyDelete
  2. sir i need matlab code for robert edge detection on satellite image..............plzzzzz send code to ghareesh5@gamil.com

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  3. This comment has been removed by the author.

    ReplyDelete
  4. clc
    close all
    clear

    %Input Image
    bw=imread('Bikesgray.bmp');
    figure()
    imshow(bw)
    title('Original Image')

    %Preallocate the matrices with zeros
    Gx=zeros(size(bw));
    Gy=zeros(size(bw));
    G=zeros(size(bw));
    tangentImage=zeros(size(bw));

    %Filter Masks
    F1=[-1 0 1;-2 0 2; -1 0 1]; %%% sobel Mask x-axis "Vertical Direction"
    F2=[-1 -2 -1;0 0 0; 1 2 1]; %%% sobel Mask y-axis "Horizental Direction"

    bw=double(bw);

    for i=1:size(bw,1)-2
    for j=1:size(bw,2)-2

    %Gradient operations
    Gxp=sum(sum(F1.*bw(i:i+2,j:j+2)));
    Gyp=sum(sum(F2.*bw(i:i+2,j:j+2)));
    Gx(i+1,j+1)=Gxp;
    Gy(i+1,j+1)=Gyp;

    %Magnitude of vector
    G(i+1,j+1)=sqrt(Gx(i+1,j+1).^2 + Gy(i+1,j+1).^2);

    %%%% Image Directions / Orientations "Theta"
    if (Gy(i+1,j+1) == 0 && Gx(i+1,j+1) == 0)
    tangentImage(i+1,j+1) = 0;
    elseif (Gy(i+1,j+1) ~= 0) && (Gx(i+1,j+1) == 0)
    tangentImage(i+1,j+1)= 90;
    else tangentImage(i+1,j+1) = atan2( Gy(i+1,j+1),Gx(i+1,j+1));
    tangentImage(i+1,j+1)= tangentImage(i+1,j+1) * 180 / pi;
    if (tangentImage(i+1,j+1)<0)
    tangentImage(i+1,j+1) = 360 + tangentImage(i+1,j+1);
    end
    end
    end
    end

    Gx=uint8(Gx);
    figure()
    imshow(Gx,[])
    title('Sobel x direction')

    Gy=uint8(Gy);
    figure()
    imshow(Gy,[])
    title('Sobel y direction')

    G=uint8(G);
    figure()
    imshow(G,[])
    title('Gradient Magnitude')

    figure()
    imshow(tangentImage,[])
    title('Gradient Direction')

    ReplyDelete
  5. Sir i need matlab code for Threshold Based Edge Detection Algorithm for only these filters (Hormonic,wiener,STD,correlation) Please send code as soon possible
    engineeryousif@gmail.com

    ReplyDelete