/*M/////////////////////////////////////////////////////////////////////////////////////// // // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. // // By downloading, copying, installing or using the software you agree to this license. // If you do not agree to this license, do not download, install, // copy or use the software. // // // Intel License Agreement // For Open Source Computer Vision Library // // Copyright (C) 2000, Intel Corporation, all rights reserved. // Third party copyrights are property of their respective owners. // // Redistribution and use in source and binary forms, with or without modification, // are permitted provided that the following conditions are met: // // * Redistribution's of source code must retain the above copyright notice, // this list of conditions and the following disclaimer. // // * Redistribution's 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. // // * The name of Intel Corporation may not be used to endorse or promote products // derived from this software without specific prior written permission. // // This software is provided by the copyright holders and contributors "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 Intel Corporation or contributors 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. // //M*/ #include "test_precomp.hpp" #include using namespace cv; using namespace std; const int NSN=5;//10;//20; //number of shapes per class const float CURRENT_MAX_ACCUR=85; //90% and 91% reached in several tests, 85 is fixed as minimum boundary class CV_HaussTest : public cvtest::BaseTest { public: CV_HaussTest(); ~CV_HaussTest(); protected: void run(int); private: float computeShapeDistance(vector &query1, vector &query2, vector &query3, vector &testq); vector convertContourType(const Mat& currentQuery, int n=180); vector normalizeContour(const vector & contour); void listShapeNames( vector &listHeaders); void mpegTest(); void displayMPEGResults(); }; CV_HaussTest::CV_HaussTest() { } CV_HaussTest::~CV_HaussTest() { } vector CV_HaussTest::normalizeContour(const vector &contour) { vector output(contour.size()); Mat disMat(contour.size(),contour.size(),CV_32F); Point2f meanpt(0,0); float meanVal=1; for (size_t ii=0; ii(ii,jj)=0; else { disMat.at(ii,jj)= fabs(contour[ii].x*contour[jj].x)+fabs(contour[ii].y*contour[jj].y); } } meanpt.x+=contour[ii].x; meanpt.y+=contour[ii].y; } meanpt.x/=contour.size(); meanpt.y/=contour.size(); meanVal=cv::mean(disMat)[0]; for (size_t ii=0; ii &listHeaders) { listHeaders.push_back("apple"); //ok listHeaders.push_back("children"); // ok listHeaders.push_back("device7"); // ok listHeaders.push_back("Heart"); // ok listHeaders.push_back("teddy"); // ok } vector CV_HaussTest::convertContourType(const Mat& currentQuery, int n) { vector > _contoursQuery; vector contoursQuery; findContours(currentQuery, _contoursQuery, RETR_LIST, CHAIN_APPROX_NONE); for (size_t border=0; border<_contoursQuery.size(); border++) { for (size_t p=0; p<_contoursQuery[border].size(); p++) { contoursQuery.push_back(_contoursQuery[border][p]); } } // In case actual number of points is less than n for (int add=contoursQuery.size()-1; add cont; for (int i=0; i& query1, vector & query2, vector & query3, vector & testq) { Ptr haus = createHausdorffDistanceExtractor(); return std::min(haus->computeDistance(query1,testq), std::min(haus->computeDistance(query2,testq), haus->computeDistance(query3,testq))); } void CV_HaussTest::mpegTest() { string baseTestFolder="shape/mpeg_test/"; string path = cvtest::TS::ptr()->get_data_path() + baseTestFolder; vector namesHeaders; listShapeNames(namesHeaders); // distance matrix // Mat distanceMat=Mat::zeros(NSN*namesHeaders.size(), NSN*namesHeaders.size(), CV_32F); // query contours (normal v flipped, h flipped) and testing contour // vector contoursQuery1, contoursQuery2, contoursQuery3, contoursTesting; // reading query and computing its properties // int counter=0; const int loops=NSN*namesHeaders.size()*NSN*namesHeaders.size(); for (size_t n=0; n origContour; contoursQuery1=convertContourType(currentQuery); origContour=contoursQuery1; contoursQuery2=convertContourType(flippedHQuery); contoursQuery3=convertContourType(flippedVQuery); // compare with all the rest of the images: testing // for (size_t nt=0; nt(NSN*n+i-1, NSN*nt+it-1)=0; continue; } // read testing image // stringstream thetestpathandname; thetestpathandname<(NSN*n+i-1, NSN*nt+it-1)= computeShapeDistance(contoursQuery1, contoursQuery2, contoursQuery3, contoursTesting); std::cout<(NSN*n+i-1, NSN*nt+it-1)<get_data_path() + baseTestFolder + "distanceMatrixMPEGTest.yml", FileStorage::WRITE); fs << "distanceMat" << distanceMat; } const int FIRST_MANY=2*NSN; void CV_HaussTest::displayMPEGResults() { string baseTestFolder="shape/mpeg_test/"; Mat distanceMat; FileStorage fs(cvtest::TS::ptr()->get_data_path() + baseTestFolder + "distanceMatrixMPEGTest.yml", FileStorage::READ); vector namesHeaders; listShapeNames(namesHeaders); // Read generated MAT // fs["distanceMat"]>>distanceMat; int corrects=0; int divi=0; for (int row=0; row(row,col)>distanceMat.at(row,i)) { nsmall++; } } if (nsmall<=FIRST_MANY) { corrects++; } } } float porc = 100*float(corrects)/(NSN*distanceMat.rows); std::cout<<"%="<= CURRENT_MAX_ACCUR) ts->set_failed_test_info(cvtest::TS::OK); else ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY); } void CV_HaussTest::run(int /* */) { mpegTest(); displayMPEGResults(); ts->set_failed_test_info(cvtest::TS::OK); } TEST(Hauss, regression) { CV_HaussTest test; test.safe_run(); }