source: proiecte/PPPP/ica/align_Faces.m @ 138

Last change on this file since 138 was 138, checked in by (none), 14 years ago

PPPP - ica

File size: 3.6 KB
Line 
1%Script align_Faces
2%
3% Aligns the eye positions of a directory of face images. Reads in Labels.mat,
4% obtained using getLabels.m, writes jpegs to a specified directory.
5% Specify directory paths at the top of the file.
6%LabelDir is where Labels.mat is.
7%imgDir is where the original images are
8%DestDir is where you want the cropped jpegs to go.
9
10LabelDir = 'C:\Users\Andrei\Desktop\yalefaces\'
11imgDir = 'C:\Users\Andrei\Desktop\yalefaces\normal\'
12DestDir = 'C:\Users\Andrei\Desktop\yalefaces\'
13 
14homeDir = pwd;
15
16% CHANGE THESE VARIABLES AS NEEDED
17%XSIZE =  YSIZE =       %Size of desired cropped image
18%EYES = %Number of pixels desired between the eyes
19%TEETH_EYES = %Desired no. of pixels from teeth to eyes.
20
21XSIZE = 300; YSIZE = 150;     
22EYES = 50;
23TEETH_EYES = 80;
24%SEE ALSO PARAMETERS IN CROP ROUTINE. EYES BELOW MIDPOINT.
25 
26%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
27%Here is where we load the images and do the preprocessing
28%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
29      cd (LabelDir)
30      load Labels
31      cd (imgDir)
32      r = dir;
33
34for i = 3:(size(r,1))
35   imgName = r(i).name
36   %[X,map] = imread([ t ]);
37      I=imread(imgName);
38      %I=rgb2gray(I);
39
40     %Extract face measurements:
41     %dxeyes =  x distance between eyes in original image                     
42             % Average the locations of inner & outer corners.
43     %dyeyes =  y distance between eyes in original image
44     %dEeyes = Euclidean norm distance between eyes
45     %dEteeth_eyes = Euclidean norm distance from teeth to midpt b/neyes
46                 
47     [height, width] = size(I);
48     dxeyes = marks(i-2,3) - marks(i-2,1); %Check these.
49     dyeyes = marks (i-2,4) - marks(i-2,2);
50     dEeyes = sqrt(dxeyes^2 + dyeyes^2);
51     mean_eye_x = mean([marks(i-2,1), marks(i-2,3)]);
52     mean_eye_y = mean([marks(i-2,2), marks(i-2,4)]);
53     dEteeth_eyes = sqrt((marks(i-2,5)-mean_eye_x)^2 + (marks(i-2,6)-mean_eye_y)^2);
54                                         
55     %scale
56     yscale = TEETH_EYES / dEteeth_eyes; xscale = EYES / dEeyes;
57     height_new = yscale*height; width_new = xscale*width;
58     tmp0=imresize(I,[height_new,width_new],'bicubic');
59   
60      %rotate (Problem: imrotate rotates about the center of the image.
61      %To avoid losing feature position information, must first center the
62      %image on the right eye before rotating.
63      %Then use right eye position to determine cropping.
64      Reye_x = marks(i-2,1);
65      Reye_y = marks(i-2,2);
66
67      %crop a 200x200 window centered on left eye:
68      %Zero-pad to make sure window never falls outside of image.
69      W = 100; %Window radius
70      %W = 500;  %For bigger images (Gwen's params).
71      padcols = zeros(size(tmp0,1),W); padrows = zeros(W,size(tmp0,2)+W);
72      padcols = uint8(padcols); padrows=uint8(padrows);
73      tmp = [padrows;padcols,tmp0];
74
75      tmpx = xscale*Reye_x - W +W; tmpy = yscale*Reye_y - W +W;     
76      tmp1 = imcrop(tmp,[tmpx,tmpy,2*W,2*W]);
77      %figure(2);imshow(tmp1)
78
79      angle = 180/pi*atan((yscale*dyeyes)/(xscale*dxeyes));
80      tmp2 = imrotate(tmp1,angle,'bicubic','crop');
81      %figure(2); imshow(tmp2);
82
83      %crop
84      % x and y give the upper left corner of cropped image
85      % Reye is centered at (W,W) = (100,100).
86      % For bigger images (W,W) = (500,500)
87      x = W - (XSIZE-EYES)/2;
88      %y = W - YSIZE/2;   %Eyes at midpoint
89      y = W - YSIZE*1/3;  %Face box
90      tmp3=imcrop(tmp2,[x,y,XSIZE,YSIZE]);
91      figure(1); imshow(tmp3);
92
93      %save
94      [imgName, R] = strtok(imgName, '.');
95      fname = [DestDir,imgName, '.pgm'];
96      imwrite(tmp3,fname,'pgm')
97end
Note: See TracBrowser for help on using the repository browser.