%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % function cropFace() % z.li, 06-18-2004 % find face id and relevance to a model with certain distance metric % function dependency: % - n/a % input: % img - input image % lex,ley, rex, rey - eyes location % side, top, bottom - face model spec, in length of eye distances % fw, fh - desired face size % output: % vec - fwxfh x1 % 'cropFaceSave.jpg' - optionally saved face. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %function [vec]=cropFace(img, lex, ley, rex, rey, side, top, bottom, fw, fh) function [vec]=cropFace(img, lex, ley, rex, rey, side, top, bottom, fw, fh) dbg ='y'; if dbg == 'y' k=5; img = imread('caj11.jpg'); imshow(img); grid on; [lex, ley]=ginput(1);hold on;plot(lex, ley, '+r'); [rex, rey]=ginput(1);plot(rex, rey, '+m'); side = 0.75; top=1.0; bottom= 1.8; fw=20; fh=24; end % const dbgPlot = 'y'; hEq = 'n'; % no hist equalization saveFace = 'y'; [imgH imgW]=size(img); d = norm(([lex ley] - [rex rey]), 2); % find the face icon window faceW = fix((1+2*side)*d); faceH = fix((top+bottom)*d); leftMargin = fix(side*d); topMargin = fix(top*d); % upper left corner ulX = lex - leftMargin; ulY = ley - topMargin; % lower right corner lrX = ulX + faceW -1; lrY = ulY + faceH -1; % boundary check if ulX < 1 | ulY < 1 | lrX > imgW | lrY > imgH % out of pic vec = -1; return; end % detection window if dbgPlot=='y' imshow(img, 256); hold on; plot(lex, ley, '+r'); plot(rex, rey, '+m'); hold on; px =[ulX lrX]; py=[ulY ulY]; plot(px,py,'.',px,py,'-g'); px =[ulX ulX]; py=[ulY lrY]; plot(px,py,'.',px,py,'-g'); px =[ulX lrX]; py=[lrY lrY]; plot(px,py,'.',px,py,'-g'); px =[lrX lrX]; py=[lrY ulY]; plot(px,py,'.',px,py,'-g'); end % facial area face(1:faceH,1:faceW) = img(ulY:lrY,ulX:lrX); if hEq == 'y' face = histeq(face); end % save face image if saveFace == 'y' imwrite(face, 'cropFaceSave.jpg', 'jpg'); end faceIconImg = imresize(face, [fh fw], 'bilinear'); vec = im2vec(faceIconImg); return;