#include "HistogramTest.h" #include #include // for cout #include // for setprecision( int p ) #include // For auto_ptr #include #include #include "svm_rbf.h" extern "C" { #include "calcHistograms.h" } using namespace std; template void printVec( const std::vector &vec, std::ostream &os = std::cout ) { for( unsigned int i = 0; i < vec.size(); ++i ) { os << vec[i] << ' '; } os << std::endl; } CPPUNIT_TEST_SUITE_REGISTRATION( HistogramTest ); HistogramTest::HistogramTest() : deception(NULL) { } HistogramTest::~HistogramTest() { } /** */ void HistogramTest::setUp() { deception = new mpt::Deception( "w2.txt", "vs.txt" ); } /** */ void HistogramTest::tearDown() { if( deception ) delete deception; } void HistogramTest::testRandomGabor() { mpt::vectorD3D gabor(12); double val=-1.623; double step=4.6510e-04; for( unsigned int i = 0; i < 12; ++i ) { gabor[i].resize(23); for( unsigned int j = 0; j < 23; ++j ) { gabor[i][j].resize(32); for( unsigned int k = 0; k < 32; ++k ) { gabor[i][j][k] = val; val += step; } } } const double d = 1.26; const int b = 4; mpt::vectorD1D hist; hist = deception->newhist( gabor, d, b ); // make sure it's the right size CPPUNIT_ASSERT( mpt::svm_rbf::feature_vector_length == hist.size() ); // load the output from matlab // compare with what we got mpt::vectorD1D goodHist = deception->loadHistogram( "goodHistogram.txt" ); CPPUNIT_ASSERT( mpt::svm_rbf::feature_vector_length == goodHist.size() ); double diff = 0.0; for( unsigned int i = 0; i < hist.size(); ++i ) { diff += abs( hist[i] - goodHist[i] ); } // diff is probably somewhere around 1e10-7 CPPUNIT_ASSERT( diff < 0.00001 ); } void HistogramTest::testSmoothedHistogram() { int n1 = 12; int n2 = 23; int n3 = 32; double val=-1.623; double step=4.6510e-04; mpt::vectorD3D ai(n1); double ***raw_ai = (double ***) malloc(sizeof(double **) * n1); for( int i = 0; i < n1; ++i ) { raw_ai[i] = (double **) malloc(sizeof(double *) * n2); ai[i].resize(n2); for( int j = 0; j < n2; ++j ) { raw_ai[i][j] = (double *) malloc(sizeof(double) * n3); ai[i][j].resize(n3); for( int k = 0; k < n3; ++k ) { ai[i][j][k] = val; raw_ai[i][j][k] = val; val += step; } } } double *raw_hist = calcHistograms( raw_ai, n1, n2, n3); const double d0 = 1; const double dx = 2.2; const double w = d0 * 0.7; int b = 4; mpt::vectorD1D hist = deception->smoothHistograms( ai, b, d0, dx, w ); // ofstream ofs1( "myhist.txt" ); // printVec( hist, ofs1 ); // // mpt::vectorD1D raw_hist_vec( raw_hist, (raw_hist + (n1 * n2 * b)) ); // ofstream ofs2( "rawhist.txt" ); // printVec( raw_hist_vec, ofs2 ); CPPUNIT_ASSERT( hist.size() == (n1 * n2 * b) ); double diff = 0.0; for( unsigned int i = 0; i < hist.size(); ++i ) { diff += abs( hist[i] - raw_hist[i] ); } // diff is probably somewhere around 1e10-7 CPPUNIT_ASSERT( diff < 0.00001 ); for ( int i = 0; i < n1; i++) { for ( int j = 0; j < n2; j++) { free(raw_ai[i][j]); } free(raw_ai[i]); } free(raw_ai); free(raw_hist); }