Code covered by the BSD License

# Morphological Shared-Weight Neural Network for Face Recognition

31 Dec 2004 (Updated )

MSNN has the ability to learn feature extraction and perform classification at the same time.

geteyelevel.m
```% This function detects the eye level of a face
% using Hough Transform

clear
load XX Dat1; I = Dat1;

% Resizing of image
I = double(imresize(I,2))/255;

% Calculate magnitude of image gradient
H = fspecial('sobel');
V = H';
E = sqrt(filter2(H,I).^2+filter2(V,I).^2);

% we can also use "edge"
% E = edge(I);

% Use the "radon" function to compute Hough Transform
% xp is the radial vectors; R is the rows
theta=(0:179)';

figure(1)
imagesc(theta,xp,R),colorbar;
xlabel('theta(deg)');ylabel('rho(pixels from center)');
title('Line Space');

% Estimate the peaks we want to find
i=find(R>0 & R<100);
% Sort the outputs and pick the top lines
[foo,ind] = sort(-R(i));
k=i(ind(1:80));
% Convert linear indexes into coordinates of peaks
[y,x]=ind2sub(size(R),k);

figure(2)
imagesc(theta,xp,R),colorbar;
xlabel('Theta(deg)'); ylabel('Rho(pixels from center)')
plot([theta(x) xp(y)]);
title('Location of Peaks');

% MATLAB's radon function uses an upside down convention for specifying the rotation
% Negate theta to compensate
t=-theta(x)*pi/180;
r=xp(y);

% Line parameters have the equation Ax+By+C=0 which are invariant to scaling
% This step will produce the distance of a point to the line with the pivot pixel
lines = [cos(t) sin(t) -r];

% Transform the lines to horizontally
cx=size(I,2)/2-1;
cy=size(I,1)/2-1;
lines(:,3) = lines(:,3) - lines(:,1)*cx - lines(:,2)*cy;
lines(:,2) = lines(:,2) - lines(:,1)*cx - lines(:,1)*cy;

% Search for values that equals to -1 or near this value: indicating horizontal lines
att = lines(:,2);

[f,g] = sort(att);

fc = uint8(f);
fc = double(fc);
fc = fc/3;

[n,m] = find(fc==0);

% locate four lines around the centre
lastzeroposition = size(n,1);
secondlastzeroposition = lastzeroposition - 1;
thirdlastzeroposition = secondlastzeroposition - 2;
fourthlastzeroposition = thirdlastzeroposition - 3;
%------------------------------------

% locate line position
u1=g(lastzeroposition);
u2=g(secondlastzeroposition);
u3=g(thirdlastzeroposition);
u4=g(fourthlastzeroposition);

hh1 = lines(u1,3);
hh1 = round(hh1);

hh2 = lines(u2,3);
hh2 = round(hh2);

hh3 = lines(u3,3);
hh3 = round(hh3);

hh4 = lines(u4,3);
hh4 = round(hh4);

range = [hh1 hh2 hh3 hh4];

% Display original image with detected eye-level
I(abs(hh1),:)=1;
I(abs(hh2),:)=1;
I(abs(hh3),:)=1;
I(abs(hh4),:)=1;

% Display filtered image with detected eye-level
E(abs(hh1),:)=1;
E(abs(hh2),:)=1;
E(abs(hh3),:)=1;
E(abs(hh4),:)=1;

figure(3)
subplot(131);imshow(I);title('Eye-Level(Original Image)');

subplot(133);imshow(Fin);title('Eye-Level(by Convo)');

% prune and decide final line
exc=zeros(1,5);
hh1=round(hh1/2);hh2=round(hh2/2);
hh3=round(hh3/2);hh4=round(hh4/2);

if abs(hh1-hh2)>15
exc(1)=1;
end;
if abs(hh2-hh3)>15
exc(2)=1;
end;
if abs(hh3-hh4)>15
exc(3)=1;
end;
if abs(hh1-hh4)>15
exc(4)=1;
end;
if abs(hh2-hh4)>15
exc(5)=1;
end;

fixpoint=zeros(1,4);
if exc(1)==0
fixpoint(1)=hh1;
fixpoint(2)=hh2;

end;
if exc(2)==0
fixpoint(2)=hh2;
fixpoint(3)=hh3;

end;
if exc(3)==0
fixpoint(3)=hh3;
fixpoint(4)=hh4;

end;
if exc(4)==0
fixpoint(1)=hh1;
fixpoint(4)=hh4;

end;
if exc(5)==0
fixpoint(2)=hh2;
fixpoint(4)=hh4;

end;
ccount=0;
for oi=1:4
if fixpoint(oi)==0
ccount=ccount+1;
end;end;
arsize=4-ccount;

eyelevel = round(sum(fixpoint)/arsize);

save HH;```