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