You are on page 1of 4

Sobel and Prewitt edge detection

Prewitt edge detection:clc;


im=imread('cameraman.tif');
[row col]=size(im);
a=double(im);
subplot(3,2,1);
imshow(im);
title('Original image');
gx=[-1 0 1;-1 0 1;-1 0 1];
gy=[-1 -1 -1;0 0 0;1 1 1];
%X-gradient
for i=2:row-1
for j=2:col-1
fx(i,j)=a(i-1,j-1)*gx(1)+a(i-1,j)*gx(2)+a(i-1,j+1)*gx(3)+a(i,j-1)*gx(4)+
a(i,j)*gx(5)+a(i,j+1)*gx(6)+a(i+1,j-1)*gx(7)+a(i+1,j)*gx(8)+a(i+1,j+1)*gx(9);
end
end
subplot(3,2,2);
imshow(uint8(fx));
title('X-Gradient');
%Y-gradient
for i=2:row-1
for j=2:col-1
fy(i,j)=a(i-1,j-1)*gy(1)+a(i-1,j)*gy(2)+a(i-1,j+1)*gy(3)+a(i,j-1)*gy(4)+
a(i,j)*gy(5)+a(i,j+1)*gy(6)+a(i+1,j-1)*gy(7)+a(i+1,j)*gy(8)+a(i+1,j+1)*gy(9);
end
end
subplot(3,2,3);
imshow(uint8(fy));
title('Y-Gradient');
F=fx+fy;
subplot(3,2,4);
imshow(uint8(F));
title('Edge detected using prewitt algo');

Sobel edge detection:clc;


im=imread('cameraman.tif');
[row col]=size(im);
a=double(im);
subplot(3,2,1);
imshow(im);
title('Original image');
gx=[-1 0 1;-2 0 2;-1 0 1];
gy=[-1 -2 -1;0 0 0;1 2 1];
%X-gradient
for i=2:row-1
for j=2:col-1
fx(i,j)=a(i-1,j-1)*gx(1)+a(i-1,j)*gx(2)+a(i-1,j+1)*gx(3)+a(i,j-1)*gx(4)+
a(i,j)*gx(5)+a(i,j+1)*gx(6)+a(i+1,j-1)*gx(7)+a(i+1,j)*gx(8)+a(i+1,j+1)*gx(9);
end
end
subplot(3,2,2);
imshow(uint8(fx));
title('X-Gradient');
%Y-gradient
for i=2:row-1
for j=2:col-1
fy(i,j)=a(i-1,j-1)*gy(1)+a(i-1,j)*gy(2)+a(i-1,j+1)*gy(3)+a(i,j-1)*gy(4)+
a(i,j)*gy(5)+a(i,j+1)*gy(6)+a(i+1,j1)*gy(7)+a(i+1,j)*gy(8)+a(i+1,j+1)*gy(9);
end
end
subplot(3,2,3);
imshow(uint8(fy));
title('Y-Gradient');
F=fx+fy;
subplot(3,2,4);
imshow(uint8(F));
title('Edge detected using sobel algo');

Output:-

Using prewitt:-

Using Sobel:-

You might also like