#include "AlignEyesSuite.h" #include "rimage_ser.h" //#include "rimage_matlab.h" //#include "mat.h" #include #include #include "mpt_dataIterator.h" static const int patchSize = 96; /** Compare two RImages * First check dimentions, then compare every pixel. Currently using an arbitrary small * value to compare pixels, but something a little more flexible that measured how * different images are in some way relevant to CERT would be better. */ bool operator==( const RImage &lhs, const RImage &rhs ) { if( lhs.width != rhs.width ) return false; if( lhs.height != rhs.height ) return false; if( lhs.numpixels != rhs.numpixels ) return false; float sum = 0.0; for( int index=0; index < rhs.numpixels; ++index ) { float diff = fabs( lhs.getPixel(index) - rhs.getPixel(index) ); // This is an arbitrary value, we should have a more useful way to // measure differences between the two images // I think summing the differences will be more useful // if( diff > 0.2 ) // return false; sum += diff; } // Again, an arbitrary value if( sum > 50.0 ) return false; return true; } CPPUNIT_TEST_SUITE_REGISTRATION( AlignEyesSuite ); AlignEyesSuite::AlignEyesSuite() { } void AlignEyesSuite::setUp() { } void AlignEyesSuite::tearDown() { } /** Test rotation of three images that have widely varying head tilts. * Read in a config file with image names, eye locations and good-aligned image names * e.g. US_SanDiego_1632821.rimage 180.0 75 200.0 77 US_SanDiego_1632821_aligned.rimage * For each image * Read in the image * Align it based on eye data from config file * Compare with stored, pre-aligned image */ void AlignEyesSuite::testRotationRange() { std::string datapath( "testImages.txt" ); mpt_dataIterator iter( datapath ); RImage inPlace; MP_RotateFace _rotator2( patchSize ); _rotator2.setEyeLenRate( 0.60 ); while( !iter.eof() ) { std::string fname; mpt::eyes testEyes; float eyeAngle; if( iter.nextDataLine( fname, eyeAngle, testEyes ) ) { RImage testImage; restoreRImageFromFile( testImage, fname ); MP_RotateFace _rotator( &testImage, patchSize, static_cast(testEyes.left.x), static_cast(testEyes.right.x), static_cast(testEyes.left.y), static_cast(testEyes.right.y) ); _rotator.setEyeLenRate( 0.60 ); RImage *rotated = _rotator.rotatedImage(); bool rotOK = _rotator2.rotateImageIntoPatch( &testImage, static_cast(testEyes.left.x), static_cast(testEyes.right.x), static_cast(testEyes.left.y), static_cast(testEyes.right.y), inPlace ); std::string errorMessage( "Image: " ); errorMessage += fname; // Make sure we got something CPPUNIT_ASSERT_MESSAGE( errorMessage , (rotated != NULL) ); CPPUNIT_ASSERT_MESSAGE( errorMessage, !(*rotated == emptyImage) ); // Now make sure that both rotation methods give the same results. CPPUNIT_ASSERT_MESSAGE( errorMessage, rotOK ); CPPUNIT_ASSERT_MESSAGE( errorMessage, (*rotated == inPlace) ); RImage goodRotated; std::string goodRotName( fname ); goodRotName.replace( goodRotName.find(".rimage"), 7, "_good.rimage" ); // saveRImageToFile( *rotated, goodRotName ); restoreRImageFromFile( goodRotated, goodRotName ); // Now compare it to the 'known' good version of the rotated patch CPPUNIT_ASSERT_MESSAGE( errorMessage, (*rotated == goodRotated) ); delete rotated; } } } /* * 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. */