Code covered by the BSD License

# Canny Edge Detector

### Mohamed Athiq (view profile)

Simple implementation of Canny edge detector in matlab and c++ from textbook algorithm.

CannyEdgedetector.m
```clear all;
im=im2double(im);

% smoothening using a filter
filter= [2 4 5 4 2;
4 9 12 9 4;
5 12 15 12 5;
4 9 12 9 4;
2 4 5 4 2];
filter=filter/115;
smim= conv2(im,filter);

-2 0 2;
-1 0 1];
0 0 0;
-1 -2 -1];

theta=zeros([a b]);
for i=1:a
for j=1:b
else
end
end
end
theta=theta*(180/3.14);
for i=1:a
for j=1:b
if(theta(i,j)<0)
theta(i,j)= theta(i,j)-90;
theta(i,j)=abs(theta(i,j));
end
end
end
for i=1:a
for j=1:b
if ((0<theta(i,j))&&(theta(i,j)<22.5))||((157.5<theta(i,j))&&(theta(i,j)<181))
theta(i,j)=0;
elseif (22.5<theta(i,j))&&(theta(i,j)<67.5)
theta(i,j)=45;
elseif (67.5<theta(i,j))&&(theta(i,j)<112.5)
theta(i,j)=90;
elseif (112.5<theta(i,j))&&(theta(i,j)<157.5)
theta(i,j)=135;
end
end
end

%non maximum suppression

[a,b]=size(theta);

for i=2:a-2
for j=2:b-2

if (theta(i,j)==135)
if ((nmx(i-1,j+1)>nmx(i,j))||(nmx(i+1,j-1)>nmx(i,j)))
nmx(i,j)=0;
end
elseif (theta(i,j)==45)
if ((nmx(i+1,j+1)>nmx(i,j))||(nmx(i-1,j-1)>nmx(i,j)))
nmx(i,j)=0;
end
elseif (theta(i,j)==90)
if ((nmx(i,j+1)>nmx(i,j))||(nmx(i,j-1)>nmx(i,j)))
nmx(i,j)=0;
end
elseif (theta(i,j)==0)
if ((nmx(i+1,j)>nmx(i,j))||(nmx(i-1,j)>nmx(i,j)))
nmx(i,j)=0;
end
end
end
end

nmx1=im2uint8(nmx);

tl=85; %lower threshold
th=150; %upper threshold

% grouping edges based on threshold

[a,b]=size(nmx1);
gedge=zeros([a,b]);
for i=1:a
for j=1:b
if(nmx1(i,j)>th)
gedge(i,j)=nmx1(i,j);
elseif (tl<nmx1(i,j))&&(nmx1(i,j)<th)
gedge(i,j)=nmx1(i,j);
end
end
end

[a,b]= size(gedge);
finaledge=zeros([a b]);
for i=1:a
for j=1:b
if (gedge(i,j)>th)
finaledge(i,j)=gedge(i,j);
for i2=(i-1):(i+1)
for j2= (j-1):(j+1)
if (gedge(i2,j2)>tl)&&(gedge(i2,j2)<th)
finaledge(i2,j2)=gedge(i,j);
end
end
end
end
end
end

%clearing the border
finaledge= im2uint8(finaledge(10:end-10,10:end-10));

figure;
subplot(2,3,1);imshow(im);title('Original image');
subplot(2,3,2);imshow(smim);title('Smoothened image');