/* * mp_rotateFace * * Author:Andrew Salamon * Date: Wed Mar 29 2006 * * Copyright (c) 2006 Machine Perception Laboratory * University of California San Diego. * * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ #include "mp_rotateFace.h" /** Relatively simple wrapper around the integrating inerpolator. * Closest match to the original matlab code seems to be with an eyeTopRate of 0.2564 and an eyeLenRate of 0.60 * (interpolator's default). I arrived at those values through trial and error, so I'm not sure why they are * needed. */ MP_RotateFace::MP_RotateFace( int _patchSize ) : pixels(NULL), patchSize(_patchSize), xl(-1), xr(-1), yl(-1), yr(-1), eye_top_rate(0.2564), override_top(true), override_len(false), pfIntegral( 0, 0 ) { } MP_RotateFace::MP_RotateFace( const RImage *_pixels, int _patchSize, int _xl, int _xr, int _yl, int _yr ) : pixels(_pixels), patchSize(_patchSize), xl(_xl), xr(_xr), yl(_yl), yr(_yr), eye_top_rate(0.2564), override_top(true), override_len(false), pfIntegral( 0, 0 ) { } MP_RotateFace::~MP_RotateFace() { } void MP_RotateFace::setImage( const RImage *_pixels, int _xl, int _xr, int _yl, int _yr ) { pixels = _pixels; xl = _xl; xr = _xr, yl = _yl, yr = _yr; } void MP_RotateFace::setEyeTopRate( float newRate ) { eye_top_rate = newRate; override_top = true; } float MP_RotateFace::getEyeTopRate() { return eye_top_rate; } void MP_RotateFace::setEyeLenRate( float newRate ) { eye_len_rate = newRate; override_len = true; } float MP_RotateFace::getEyeLenRate() { return eye_len_rate; } RImage *MP_RotateFace::rotatedImage() { RImage *facePixels = new RImage(patchSize, patchSize); if( !rotateIntoPatch( *facePixels ) ) { delete facePixels; return NULL; } return facePixels; } inline RIntegral * MP_RotateFace::setupIntegral( RIntegral *integral, const RImage *pixels ) { if( !integral ) { integral = &pfIntegral; integral->setSize( pixels->width, pixels->height ); integral->integrate( *pixels ); } return integral; } inline void MP_RotateFace::setupPatch( RImage &patch ) { if( (patch.width != patchSize) || (patch.height != patchSize) ) patch.setSize( patchSize, patchSize ); } bool MP_RotateFace::rotateImageIntoPatch( const RImage *_pixels, int xl, int xr, int yl, int yr, RImage &patch, RIntegral *integral ) { setImage( _pixels, xl, xr, yl, yr ); return rotateIntoPatch( patch, integral ); } bool MP_RotateFace::rotateIntoPatch( RImage &patch, RIntegral *integral ) { int iErr = 0; CIntegralInterpolate integrator; if( override_top ) integrator.setEyeTopRate( eye_top_rate ); // CIntegralInterpolate default is 0.20 // This sets up the coordinates for the face, which are then used to calculate the rotation angle if( override_len ) iErr = integrator.iGetFaceFromEye( *pixels, xl, yl, xr, yr, &faceBox, eye_len_rate ); // integrator's default is 0.5 else iErr = integrator.iGetFaceFromEye( *pixels, xl, yl, xr, yr, &faceBox ); if( iErr ) { return false; } integral = setupIntegral( integral, pixels ); setupPatch( patch ); iErr = integrator.iIntegralInterpolate( *pixels, patch, integral, patchSize, patchSize ); if( iErr ) { return false; } return true; } bool MP_RotateFace::rotateImageUsingFeatures( FeatureDetector *fd, const RImage *pixels, OWFList &allFaces, RImage &patch, FeatureAlignment::alignment_method method, RIntegral *integral ) { std::vector< Matrix2d > alignedFaceBoxes; // Procrustes requires four features: eyes(2), nose, mouth. Otherwise it will throw an excpetion // if( (FeatureAlignment::procrustes == method) && (fd->ciVector.size() != 4) ) // return false; try { fd->alignFaces( allFaces, alignedFaceBoxes, method ); } catch(...) { if( (FeatureAlignment::procrustes == method) && (fd->ciVector.size() != 4) ) { std::string msg( "Procrustes alignment requires four features: eyes(2), nose, mouth" ); throw std::logic_error(msg); } else throw; } Matrix2d box = alignedFaceBoxes[0]; faceBox.vSetFourCoordinates( box[0][0], box[0][1], // top left box[1][0], box[1][1], // top right box[2][0], box[2][1], // bottom right box[3][0], box[3][1] ); // bottom left int iErr = 0; CIntegralInterpolate integrator; if( override_top ) integrator.setEyeTopRate( eye_top_rate ); integral = setupIntegral( integral, pixels ); setupPatch( patch ); iErr = integrator.iIntegralInterpolate( *pixels, patch, integral, patchSize, patchSize, &faceBox ); if( iErr ) { return false; } return true; }