Advertisement
Shailrshah

Edge Detection using Sobel, Perwitt and Roberts

Oct 16th, 2015
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 0.84 KB | None | 0 0
  1. [S,P,R]=deal(double(rgb2gray(imread('peppers.png'))));
  2. [r c] = size(S);
  3. for i=1:r-2
  4.     for j=1:c-2
  5.         %Sobel mask
  6.         Sx=((2*S(i+2,j+1)+S(i+2,j)+S(i+2,j+2))-(2*S(i,j+1)+S(i,j)+S(i,j+2)));
  7.         Sy=((2*S(i+1,j+2)+S(i,j+2)+S(i+2,j+2))-(2*S(i+1,j)+S(i,j)+S(i+2,j)));
  8.         S(i,j) = sqrt(Sx.^2+Sy.^2);
  9.        
  10.         %Perwitt mask
  11.         Px=((P(i+2,j+1)+P(i+2,j)+P(i+2,j+2))-(P(i,j+1)+C(i,j)+P(i,j+2)));
  12.         Py=((P(i+1,j+2)+P(i,j+2)+P(i+2,j+2))-(P(i+1,j)+P(i,j)+P(i+2,j)));
  13.         P(i,j)=sqrt(Px.^2+Py.^2);
  14.        
  15.         %Roberts mask
  16.         Rx = R(i+2, j+2)-R(i+1,j+1);
  17.         Ry = R(i+1, j+2)-R(i+2, j+1);
  18.         R(i,j)=sqrt(Rx.^2+Ry.^2);  
  19.     end
  20. end
  21. figure(1),imshow(uint8(S)); title('Sobel gradient');
  22. figure(2),imshow(uint8(P)); title('Prewitt gradient');
  23. figure(3),imshow(uint8(R)); title('Roberts gradient');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement