%This illustrates Sobel edge detection on the letters and clown image.
%For Canny edge detection: Use MATLAB's Image Processing Toolbox.
clear;load letters;T=200;%T=threshold by trial-and-error.
figure,imagesc(X),axis off,colormap(gray),title('Letters image')
H=conv2(X,[1 0 -1;2 0 -2;1 0 -1],'valid');%Horizontal edge detector
V=conv2(X,[1 2 1;0 0 0;-1 -2 -1],'valid');%Vertical edge detector
W=sqrt(H.^2+V.^2);W(abs(W)<T)=0;W(abs(W)>T)=1;%Threshold gradient
figure,imagesc(W),axis off,colormap(gray),title('Edge-detected image')
%Now repeat on clown image:
clear;load clown;T=1.3;%T=threshold by trial-and-error.
figure,imagesc(X),axis off,colormap(gray),title('Clown image')
H=conv2(X,[1 0 -1;2 0 -2;1 0 -1],'valid');%Horizontal edge detector
V=conv2(X,[1 2 1;0 0 0;-1 -2 -1],'valid');%Vertical edge detector
W=sqrt(H.^2+V.^2);W(abs(W)<T)=0;W(abs(W)>T)=1;%Threshold gradient
figure,imagesc(W),axis off,colormap(gray),title('Edge-detected image')


