% Read three face database for face recognition and/or face detection
% 
% tag contains three bits respectively indicating reading face.bmp
%     face1.bmp face2.bmp or not
% [ 1 0 0] indicates only face.bmp is read
% 
% All facial images are translated to rows and separated to two matrices
%     respectively for training and testing       
function [A, y, testA, testy]=read_faceDB()
    y=[]; person=0;rd=0;
    A=[];
    testy=[];
    testA=[];
        B=imread('face.bmp','bmp');
        fprintf(' preparing data in file face.bmp ...\n');
        for i=1:40
            person=person+1;
            if mod(i,15)==0
                fprintf('\n');
            end
            fprintf('%d  ',i);
        seq=randperm(10);
        for j=1:10
            I=getface(B,i,j,0);
            if rd
                I=rdface(I);
                %k=8;I=I(1+2*k:56,1+k:46-k);
                %imshow(I(1+2*k:56,1+k:46-k));
            end
            imshow(I);
            %I=histeq(I);
            [m,n]=size(I);
            Iv=reshape(I,1,m*n);
            jj=seq(j);
            if jj > 3
                A=[A; double(Iv)];
                y=[y person];
            else
                testA=[testA; double(Iv)];
                testy=[testy person];
            end
        end
        end
    fprintf('\n %d facial images of %d person\n', size(A,1)+size(testA,1),person);
    fprintf(' %d images for training and %d images for testing\n',size(A,1),size(testA,1))
return
function nface=rdface(face)
% Summary: reduce a face to a half size
% face is an image of format unit8
% nface is an image of format unit 8 whose size is a half of face
[r,c]=size(face);
nface = uint8(zeros(r/2,c/2));
for i = 1:r/2
   for j = 1:c/2
      t1=double(face((i-1)*2+1,(j-1)*2+1));
      t2=double(face((i-1)*2+1,(j-1)*2+2));
      t3=double(face((i-1)*2+2,(j-1)*2+1));
      t4=double(face((i-1)*2+2,(j-1)*2+2));
      nface(i,j)=uint8(floor(t1+t2+t3+t4)/4);
   end
end
%imshow(nface);
