Path: blob/master/modules/video/test/test_estimaterigid.cpp
16339 views
/*M///////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.13// Copyright (C) 2009, Willow Garage Inc., all rights reserved.14// Third party copyrights are property of their respective owners.15//16// Redistribution and use in source and binary forms, with or without modification,17// are permitted provided that the following conditions are met:18//19// * Redistribution's of source code must retain the above copyright notice,20// this list of conditions and the following disclaimer.21//22// * Redistribution's in binary form must reproduce the above copyright notice,23// this list of conditions and the following disclaimer in the documentation24// and/or other materials provided with the distribution.25//26// * The name of the copyright holders may not be used to endorse or promote products27// derived from this software without specific prior written permission.28//29// This software is provided by the copyright holders and contributors "as is" and30// any express or implied warranties, including, but not limited to, the implied31// warranties of merchantability and fitness for a particular purpose are disclaimed.32// In no event shall the Intel Corporation or contributors be liable for any direct,33// indirect, incidental, special, exemplary, or consequential damages34// (including, but not limited to, procurement of substitute goods or services;35// loss of use, data, or profits; or business interruption) however caused36// and on any theory of liability, whether in contract, strict liability,37// or tort (including negligence or otherwise) arising in any way out of38// the use of this software, even if advised of the possibility of such damage.39//40//M*/4142#include "test_precomp.hpp"4344// this is test for a deprecated function. let's ignore deprecated warnings in this file45#if defined(__clang__)46#pragma clang diagnostic ignored "-Wdeprecated-declarations"47#elif defined(__GNUC__)48#pragma GCC diagnostic ignored "-Wdeprecated-declarations"49#elif defined(_MSC_VER)50#pragma warning( disable : 4996)51#endif5253namespace opencv_test { namespace {5455class CV_RigidTransform_Test : public cvtest::BaseTest56{57public:58CV_RigidTransform_Test();59~CV_RigidTransform_Test();60protected:61void run(int);6263bool testNPoints(int);64bool testImage();65};6667CV_RigidTransform_Test::CV_RigidTransform_Test()68{69}70CV_RigidTransform_Test::~CV_RigidTransform_Test() {}7172struct WrapAff2D73{74const double *F;75WrapAff2D(const Mat& aff) : F(aff.ptr<double>()) {}76Point2f operator()(const Point2f& p)77{78return Point2f( (float)(p.x * F[0] + p.y * F[1] + F[2]),79(float)(p.x * F[3] + p.y * F[4] + F[5]) );80}81};8283bool CV_RigidTransform_Test::testNPoints(int from)84{85cv::RNG rng = ts->get_rng();8687int progress = 0;88int k, ntests = 10000;8990for( k = from; k < ntests; k++ )91{92ts->update_context( this, k, true );93progress = update_progress(progress, k, ntests, 0);9495Mat aff(2, 3, CV_64F);96rng.fill(aff, RNG::UNIFORM, Scalar(-2), Scalar(2));9798int n = (unsigned)rng % 100 + 10;99100Mat fpts(1, n, CV_32FC2);101Mat tpts(1, n, CV_32FC2);102103rng.fill(fpts, RNG::UNIFORM, Scalar(0,0), Scalar(10,10));104std::transform(fpts.ptr<Point2f>(), fpts.ptr<Point2f>() + n, tpts.ptr<Point2f>(), WrapAff2D(aff));105106Mat noise(1, n, CV_32FC2);107rng.fill(noise, RNG::NORMAL, Scalar::all(0), Scalar::all(0.001*(n<=7 ? 0 : n <= 30 ? 1 : 10)));108tpts += noise;109110Mat aff_est = estimateRigidTransform(fpts, tpts, true);111112double thres = 0.1*cvtest::norm(aff, NORM_L2);113double d = cvtest::norm(aff_est, aff, NORM_L2);114if (d > thres)115{116double dB=0, nB=0;117if (n <= 4)118{119Mat A = fpts.reshape(1, 3);120Mat B = A - repeat(A.row(0), 3, 1), Bt = B.t();121B = Bt*B;122dB = cv::determinant(B);123nB = cvtest::norm(B, NORM_L2);124if( fabs(dB) < 0.01*nB )125continue;126}127ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);128ts->printf( cvtest::TS::LOG, "Threshold = %f, norm of difference = %f", thres, d );129return false;130}131}132return true;133}134135bool CV_RigidTransform_Test::testImage()136{137Mat img;138Mat testImg = imread( string(ts->get_data_path()) + "shared/graffiti.png", 1);139if (testImg.empty())140{141ts->printf( ts->LOG, "test image can not be read");142ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);143return false;144}145pyrDown(testImg, img);146147Mat aff = cv::getRotationMatrix2D(Point(img.cols/2, img.rows/2), 1, 0.99);148aff.ptr<double>()[2]+=3;149aff.ptr<double>()[5]+=3;150151Mat rotated;152warpAffine(img, rotated, aff, img.size());153154Mat aff_est = estimateRigidTransform(img, rotated, true);155156const double thres = 0.033;157if (cvtest::norm(aff_est, aff, NORM_INF) > thres)158{159ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);160ts->printf( cvtest::TS::LOG, "Threshold = %f, norm of difference = %f", thres,161cvtest::norm(aff_est, aff, NORM_INF) );162return false;163}164165return true;166}167168void CV_RigidTransform_Test::run( int start_from )169{170cvtest::DefaultRngAuto dra;171172if (!testNPoints(start_from))173return;174175if (!testImage())176return;177178ts->set_failed_test_info(cvtest::TS::OK);179}180181TEST(Video_RigidFlow, accuracy) { CV_RigidTransform_Test test; test.safe_run(); }182183}} // namespace184185