123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522 |
- /*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.
- //
- //
- // License Agreement
- // For Open Source Computer Vision Library
- //
- // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
- // Copyright (C) 2009, Willow Garage Inc., 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 the copyright holders 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"
- namespace opencv_test { namespace {
- class CV_ECC_BaseTest : public cvtest::BaseTest
- {
- public:
- CV_ECC_BaseTest();
- protected:
- double computeRMS(const Mat& mat1, const Mat& mat2);
- bool isMapCorrect(const Mat& mat);
- double MAX_RMS_ECC;//upper bound for RMS error
- int ntests;//number of tests per motion type
- int ECC_iterations;//number of iterations for ECC
- double ECC_epsilon; //we choose a negative value, so that
- // ECC_iterations are always executed
- };
- CV_ECC_BaseTest::CV_ECC_BaseTest()
- {
- MAX_RMS_ECC=0.1;
- ntests = 3;
- ECC_iterations = 50;
- ECC_epsilon = -1; //-> negative value means that ECC_Iterations will be executed
- }
- bool CV_ECC_BaseTest::isMapCorrect(const Mat& map)
- {
- bool tr = true;
- float mapVal;
- for(int i =0; i<map.rows; i++)
- for(int j=0; j<map.cols; j++){
- mapVal = map.at<float>(i, j);
- tr = tr & (!cvIsNaN(mapVal) && (fabs(mapVal) < 1e9));
- }
- return tr;
- }
- double CV_ECC_BaseTest::computeRMS(const Mat& mat1, const Mat& mat2){
- CV_Assert(mat1.rows == mat2.rows);
- CV_Assert(mat1.cols == mat2.cols);
- Mat errorMat;
- subtract(mat1, mat2, errorMat);
- return sqrt(errorMat.dot(errorMat)/(mat1.rows*mat1.cols));
- }
- class CV_ECC_Test_Translation : public CV_ECC_BaseTest
- {
- public:
- CV_ECC_Test_Translation();
- protected:
- void run(int);
- bool testTranslation(int);
- };
- CV_ECC_Test_Translation::CV_ECC_Test_Translation(){}
- bool CV_ECC_Test_Translation::testTranslation(int from)
- {
- Mat img = imread( string(ts->get_data_path()) + "shared/fruits.png", 0);
- if (img.empty())
- {
- ts->printf( ts->LOG, "test image can not be read");
- ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);
- return false;
- }
- Mat testImg;
- resize(img, testImg, Size(216, 216), 0, 0, INTER_LINEAR_EXACT);
- cv::RNG rng = ts->get_rng();
- int progress=0;
- for (int k=from; k<ntests; k++){
- ts->update_context( this, k, true );
- progress = update_progress(progress, k, ntests, 0);
- Mat translationGround = (Mat_<float>(2,3) << 1, 0, (rng.uniform(10.f, 20.f)),
- 0, 1, (rng.uniform(10.f, 20.f)));
- Mat warpedImage;
- warpAffine(testImg, warpedImage, translationGround,
- Size(200,200), INTER_LINEAR + WARP_INVERSE_MAP);
- Mat mapTranslation = (Mat_<float>(2,3) << 1, 0, 0, 0, 1, 0);
- findTransformECC(warpedImage, testImg, mapTranslation, 0,
- TermCriteria(TermCriteria::COUNT+TermCriteria::EPS, ECC_iterations, ECC_epsilon));
- if (!isMapCorrect(mapTranslation)){
- ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT);
- return false;
- }
- if (computeRMS(mapTranslation, translationGround)>MAX_RMS_ECC){
- ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
- ts->printf( ts->LOG, "RMS = %f",
- computeRMS(mapTranslation, translationGround));
- return false;
- }
- }
- return true;
- }
- void CV_ECC_Test_Translation::run(int from)
- {
- if (!testTranslation(from))
- return;
- ts->set_failed_test_info(cvtest::TS::OK);
- }
- class CV_ECC_Test_Euclidean : public CV_ECC_BaseTest
- {
- public:
- CV_ECC_Test_Euclidean();
- protected:
- void run(int);
- bool testEuclidean(int);
- };
- CV_ECC_Test_Euclidean::CV_ECC_Test_Euclidean() { }
- bool CV_ECC_Test_Euclidean::testEuclidean(int from)
- {
- Mat img = imread( string(ts->get_data_path()) + "shared/fruits.png", 0);
- if (img.empty())
- {
- ts->printf( ts->LOG, "test image can not be read");
- ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);
- return false;
- }
- Mat testImg;
- resize(img, testImg, Size(216, 216), 0, 0, INTER_LINEAR_EXACT);
- cv::RNG rng = ts->get_rng();
- int progress = 0;
- for (int k=from; k<ntests; k++){
- ts->update_context( this, k, true );
- progress = update_progress(progress, k, ntests, 0);
- double angle = CV_PI/30 + CV_PI*rng.uniform((double)-2.f, (double)2.f)/180;
- Mat euclideanGround = (Mat_<float>(2,3) << cos(angle), -sin(angle), (rng.uniform(10.f, 20.f)),
- sin(angle), cos(angle), (rng.uniform(10.f, 20.f)));
- Mat warpedImage;
- warpAffine(testImg, warpedImage, euclideanGround,
- Size(200,200), INTER_LINEAR + WARP_INVERSE_MAP);
- Mat mapEuclidean = (Mat_<float>(2,3) << 1, 0, 0, 0, 1, 0);
- findTransformECC(warpedImage, testImg, mapEuclidean, 1,
- TermCriteria(TermCriteria::COUNT+TermCriteria::EPS, ECC_iterations, ECC_epsilon));
- if (!isMapCorrect(mapEuclidean)){
- ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT);
- return false;
- }
- if (computeRMS(mapEuclidean, euclideanGround)>MAX_RMS_ECC){
- ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
- ts->printf( ts->LOG, "RMS = %f",
- computeRMS(mapEuclidean, euclideanGround));
- return false;
- }
- }
- return true;
- }
- void CV_ECC_Test_Euclidean::run(int from)
- {
- if (!testEuclidean(from))
- return;
- ts->set_failed_test_info(cvtest::TS::OK);
- }
- class CV_ECC_Test_Affine : public CV_ECC_BaseTest
- {
- public:
- CV_ECC_Test_Affine();
- protected:
- void run(int);
- bool testAffine(int);
- };
- CV_ECC_Test_Affine::CV_ECC_Test_Affine(){}
- bool CV_ECC_Test_Affine::testAffine(int from)
- {
- Mat img = imread( string(ts->get_data_path()) + "shared/fruits.png", 0);
- if (img.empty())
- {
- ts->printf( ts->LOG, "test image can not be read");
- ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);
- return false;
- }
- Mat testImg;
- resize(img, testImg, Size(216, 216), 0, 0, INTER_LINEAR_EXACT);
- cv::RNG rng = ts->get_rng();
- int progress = 0;
- for (int k=from; k<ntests; k++){
- ts->update_context( this, k, true );
- progress = update_progress(progress, k, ntests, 0);
- Mat affineGround = (Mat_<float>(2,3) << (1-rng.uniform(-0.05f, 0.05f)),
- (rng.uniform(-0.03f, 0.03f)), (rng.uniform(10.f, 20.f)),
- (rng.uniform(-0.03f, 0.03f)), (1-rng.uniform(-0.05f, 0.05f)),
- (rng.uniform(10.f, 20.f)));
- Mat warpedImage;
- warpAffine(testImg, warpedImage, affineGround,
- Size(200,200), INTER_LINEAR + WARP_INVERSE_MAP);
- Mat mapAffine = (Mat_<float>(2,3) << 1, 0, 0, 0, 1, 0);
- findTransformECC(warpedImage, testImg, mapAffine, 2,
- TermCriteria(TermCriteria::COUNT+TermCriteria::EPS, ECC_iterations, ECC_epsilon));
- if (!isMapCorrect(mapAffine)){
- ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT);
- return false;
- }
- if (computeRMS(mapAffine, affineGround)>MAX_RMS_ECC){
- ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
- ts->printf( ts->LOG, "RMS = %f",
- computeRMS(mapAffine, affineGround));
- return false;
- }
- }
- return true;
- }
- void CV_ECC_Test_Affine::run(int from)
- {
- if (!testAffine(from))
- return;
- ts->set_failed_test_info(cvtest::TS::OK);
- }
- class CV_ECC_Test_Homography : public CV_ECC_BaseTest
- {
- public:
- CV_ECC_Test_Homography();
- protected:
- void run(int);
- bool testHomography(int);
- };
- CV_ECC_Test_Homography::CV_ECC_Test_Homography(){}
- bool CV_ECC_Test_Homography::testHomography(int from)
- {
- Mat img = imread( string(ts->get_data_path()) + "shared/fruits.png", 0);
- if (img.empty())
- {
- ts->printf( ts->LOG, "test image can not be read");
- ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);
- return false;
- }
- Mat testImg;
- resize(img, testImg, Size(216, 216), 0, 0, INTER_LINEAR_EXACT);
- cv::RNG rng = ts->get_rng();
- int progress = 0;
- for (int k=from; k<ntests; k++){
- ts->update_context( this, k, true );
- progress = update_progress(progress, k, ntests, 0);
- Mat homoGround = (Mat_<float>(3,3) << (1-rng.uniform(-0.05f, 0.05f)),
- (rng.uniform(-0.03f, 0.03f)), (rng.uniform(10.f, 20.f)),
- (rng.uniform(-0.03f, 0.03f)), (1-rng.uniform(-0.05f, 0.05f)),(rng.uniform(10.f, 20.f)),
- (rng.uniform(0.0001f, 0.0003f)), (rng.uniform(0.0001f, 0.0003f)), 1.f);
- Mat warpedImage;
- warpPerspective(testImg, warpedImage, homoGround,
- Size(200,200), INTER_LINEAR + WARP_INVERSE_MAP);
- Mat mapHomography = Mat::eye(3, 3, CV_32F);
- findTransformECC(warpedImage, testImg, mapHomography, 3,
- TermCriteria(TermCriteria::COUNT+TermCriteria::EPS, ECC_iterations, ECC_epsilon));
- if (!isMapCorrect(mapHomography)){
- ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT);
- return false;
- }
- if (computeRMS(mapHomography, homoGround)>MAX_RMS_ECC){
- ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
- ts->printf( ts->LOG, "RMS = %f",
- computeRMS(mapHomography, homoGround));
- return false;
- }
- }
- return true;
- }
- void CV_ECC_Test_Homography::run(int from)
- {
- if (!testHomography(from))
- return;
- ts->set_failed_test_info(cvtest::TS::OK);
- }
- class CV_ECC_Test_Mask : public CV_ECC_BaseTest
- {
- public:
- CV_ECC_Test_Mask();
- protected:
- void run(int);
- bool testMask(int);
- };
- CV_ECC_Test_Mask::CV_ECC_Test_Mask(){}
- bool CV_ECC_Test_Mask::testMask(int from)
- {
- Mat img = imread( string(ts->get_data_path()) + "shared/fruits.png", 0);
- if (img.empty())
- {
- ts->printf( ts->LOG, "test image can not be read");
- ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);
- return false;
- }
- Mat scaledImage;
- resize(img, scaledImage, Size(216, 216), 0, 0, INTER_LINEAR_EXACT );
- Mat_<float> testImg;
- scaledImage.convertTo(testImg, testImg.type());
- cv::RNG rng = ts->get_rng();
- int progress=0;
- for (int k=from; k<ntests; k++){
- ts->update_context( this, k, true );
- progress = update_progress(progress, k, ntests, 0);
- Mat translationGround = (Mat_<float>(2,3) << 1, 0, (rng.uniform(10.f, 20.f)),
- 0, 1, (rng.uniform(10.f, 20.f)));
- Mat warpedImage;
- warpAffine(testImg, warpedImage, translationGround,
- Size(200,200), INTER_LINEAR + WARP_INVERSE_MAP);
- Mat mapTranslation = (Mat_<float>(2,3) << 1, 0, 0, 0, 1, 0);
- Mat_<unsigned char> mask = Mat_<unsigned char>::ones(testImg.rows, testImg.cols);
- for (int i=testImg.rows*2/3; i<testImg.rows; i++) {
- for (int j=testImg.cols*2/3; j<testImg.cols; j++) {
- testImg(i, j) = 0;
- mask(i, j) = 0;
- }
- }
- findTransformECC(warpedImage, testImg, mapTranslation, 0,
- TermCriteria(TermCriteria::COUNT+TermCriteria::EPS, ECC_iterations, ECC_epsilon), mask);
- if (!isMapCorrect(mapTranslation)){
- ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT);
- return false;
- }
- if (computeRMS(mapTranslation, translationGround)>MAX_RMS_ECC){
- ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
- ts->printf( ts->LOG, "RMS = %f",
- computeRMS(mapTranslation, translationGround));
- return false;
- }
- // Test with non-default gaussian blur.
- findTransformECC(warpedImage, testImg, mapTranslation, 0,
- TermCriteria(TermCriteria::COUNT+TermCriteria::EPS, ECC_iterations, ECC_epsilon), mask, 1);
- if (!isMapCorrect(mapTranslation)){
- ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT);
- return false;
- }
- if (computeRMS(mapTranslation, translationGround)>MAX_RMS_ECC){
- ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
- ts->printf( ts->LOG, "RMS = %f",
- computeRMS(mapTranslation, translationGround));
- return false;
- }
- }
- return true;
- }
- void CV_ECC_Test_Mask::run(int from)
- {
- if (!testMask(from))
- return;
- ts->set_failed_test_info(cvtest::TS::OK);
- }
- TEST(Video_ECC_Test_Compute, accuracy)
- {
- Mat testImg = (Mat_<float>(3, 3) << 1, 0, 0, 1, 0, 0, 1, 0, 0);
- Mat warpedImage = (Mat_<float>(3, 3) << 0, 1, 0, 0, 1, 0, 0, 1, 0);
- Mat_<unsigned char> mask = Mat_<unsigned char>::ones(testImg.rows, testImg.cols);
- double ecc = computeECC(warpedImage, testImg, mask);
- EXPECT_NEAR(ecc, -0.5f, 1e-5f);
- }
- TEST(Video_ECC_Test_Compute, bug_14657)
- {
- /*
- * Simple test case - a 2 x 2 matrix with 10, 10, 10, 6. When the mean (36 / 4 = 9) is subtracted,
- * it results in 1, 1, 1, 0 for the unsigned int case - compare to 1, 1, 1, -3 in the signed case.
- * For this reason, when the same matrix was provided as the input and the template, we didn't get 1 as expected.
- */
- Mat img = (Mat_<uint8_t>(2, 2) << 10, 10, 10, 6);
- EXPECT_NEAR(computeECC(img, img), 1.0f, 1e-5f);
- }
- TEST(Video_ECC_Translation, accuracy) { CV_ECC_Test_Translation test; test.safe_run();}
- TEST(Video_ECC_Euclidean, accuracy) { CV_ECC_Test_Euclidean test; test.safe_run(); }
- TEST(Video_ECC_Affine, accuracy) { CV_ECC_Test_Affine test; test.safe_run(); }
- TEST(Video_ECC_Homography, accuracy) { CV_ECC_Test_Homography test; test.safe_run(); }
- TEST(Video_ECC_Mask, accuracy) { CV_ECC_Test_Mask test; test.safe_run(); }
- }} // namespace
|