|
function [trans_img,trans_facial5point]=face_align_512(impath,facial5point,savedir) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
imgSize = [512,512]; |
|
coord5point = [180,230; |
|
300,230; |
|
240,301; |
|
186,365.6; |
|
294,365.6]; |
|
coord5point = (coord5point-240)/560 * 512 + 256; |
|
|
|
|
|
|
|
|
|
img = imread(impath); |
|
facial5point = double(facial5point); |
|
transf = cp2tform(facial5point, coord5point, 'similarity'); |
|
trans_img = imtransform(img, transf, 'XData', [1 imgSize(2)],... |
|
'YData', [1 imgSize(1)],... |
|
'Size', imgSize,... |
|
'FillValues', [255;255;255]); |
|
trans_facial5point = round(tformfwd(transf,facial5point)); |
|
|
|
|
|
|
|
if ~exist(savedir,'dir') |
|
mkdir(savedir) |
|
end |
|
[~,name,~] = fileparts(impath); |
|
|
|
imwrite(trans_img, fullfile(savedir,[name,'_aligned.png'])); |
|
fprintf('write aligned image to %s\n',fullfile(savedir,[name,'_aligned.png'])); |
|
|
|
write_5pt(fullfile(savedir, [name, '_aligned.txt']), trans_facial5point); |
|
fprintf('write transformed facial landmark to %s\n',fullfile(savedir,[name,'_aligned.txt'])); |
|
|
|
|
|
imshow(trans_img); hold on; |
|
plot(trans_facial5point(:,1),trans_facial5point(:,2),'b'); |
|
plot(trans_facial5point(:,1),trans_facial5point(:,2),'r+'); |
|
|
|
end |
|
|
|
function [] = write_5pt(fn, trans_pt) |
|
fid = fopen(fn, 'w'); |
|
for i = 1:5 |
|
fprintf(fid, '%d %d\n', trans_pt(i,1), trans_pt(i,2)); |
|
end |
|
fclose(fid); |
|
end |