Path: blob/master/modules/calib3d/test/test_affine3d_estimator.cpp
16337 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"4344namespace opencv_test { namespace {4546class CV_Affine3D_EstTest : public cvtest::BaseTest47{48public:49CV_Affine3D_EstTest();50~CV_Affine3D_EstTest();51protected:52void run(int);5354bool test4Points();55bool testNPoints();56};5758CV_Affine3D_EstTest::CV_Affine3D_EstTest()59{60}61CV_Affine3D_EstTest::~CV_Affine3D_EstTest() {}626364float rngIn(float from, float to) { return from + (to-from) * (float)theRNG(); }656667struct WrapAff68{69const double *F;70WrapAff(const Mat& aff) : F(aff.ptr<double>()) {}71Point3f operator()(const Point3f& p)72{73return Point3f( (float)(p.x * F[0] + p.y * F[1] + p.z * F[2] + F[3]),74(float)(p.x * F[4] + p.y * F[5] + p.z * F[6] + F[7]),75(float)(p.x * F[8] + p.y * F[9] + p.z * F[10] + F[11]) );76}77};7879bool CV_Affine3D_EstTest::test4Points()80{81Mat aff(3, 4, CV_64F);82cv::randu(aff, Scalar(1), Scalar(3));8384// setting points that are no in the same line8586Mat fpts(1, 4, CV_32FC3);87Mat tpts(1, 4, CV_32FC3);8889fpts.ptr<Point3f>()[0] = Point3f( rngIn(1,2), rngIn(1,2), rngIn(5, 6) );90fpts.ptr<Point3f>()[1] = Point3f( rngIn(3,4), rngIn(3,4), rngIn(5, 6) );91fpts.ptr<Point3f>()[2] = Point3f( rngIn(1,2), rngIn(3,4), rngIn(5, 6) );92fpts.ptr<Point3f>()[3] = Point3f( rngIn(3,4), rngIn(1,2), rngIn(5, 6) );9394std::transform(fpts.ptr<Point3f>(), fpts.ptr<Point3f>() + 4, tpts.ptr<Point3f>(), WrapAff(aff));9596Mat aff_est;97vector<uchar> outliers;98estimateAffine3D(fpts, tpts, aff_est, outliers);99100const double thres = 1e-3;101if (cvtest::norm(aff_est, aff, NORM_INF) > thres)102{103//cout << cvtest::norm(aff_est, aff, NORM_INF) << endl;104ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);105return false;106}107return true;108}109110struct Noise111{112float l;113Noise(float level) : l(level) {}114Point3f operator()(const Point3f& p)115{116RNG& rng = theRNG();117return Point3f( p.x + l * (float)rng, p.y + l * (float)rng, p.z + l * (float)rng);118}119};120121bool CV_Affine3D_EstTest::testNPoints()122{123Mat aff(3, 4, CV_64F);124cv::randu(aff, Scalar(-2), Scalar(2));125126// setting points that are no in the same line127128const int n = 100;129const int m = 3*n/5;130const Point3f shift_outl = Point3f(15, 15, 15);131const float noise_level = 20.f;132133Mat fpts(1, n, CV_32FC3);134Mat tpts(1, n, CV_32FC3);135136randu(fpts, Scalar::all(0), Scalar::all(100));137std::transform(fpts.ptr<Point3f>(), fpts.ptr<Point3f>() + n, tpts.ptr<Point3f>(), WrapAff(aff));138139/* adding noise*/140std::transform(tpts.ptr<Point3f>() + m, tpts.ptr<Point3f>() + n, tpts.ptr<Point3f>() + m,141[=] (const Point3f& pt) -> Point3f { return Noise(noise_level)(pt + shift_outl); });142143Mat aff_est;144vector<uchar> outl;145int res = estimateAffine3D(fpts, tpts, aff_est, outl);146147if (!res)148{149ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);150return false;151}152153const double thres = 1e-4;154if (cvtest::norm(aff_est, aff, NORM_INF) > thres)155{156cout << "aff est: " << aff_est << endl;157cout << "aff ref: " << aff << endl;158ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);159return false;160}161162bool outl_good = count(outl.begin(), outl.end(), 1) == m &&163m == accumulate(outl.begin(), outl.begin() + m, 0);164165if (!outl_good)166{167ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);168return false;169}170return true;171}172173174void CV_Affine3D_EstTest::run( int /* start_from */)175{176cvtest::DefaultRngAuto dra;177178if (!test4Points())179return;180181if (!testNPoints())182return;183184ts->set_failed_test_info(cvtest::TS::OK);185}186187TEST(Calib3d_EstimateAffine3D, accuracy) { CV_Affine3D_EstTest test; test.safe_run(); }188189}} // namespace190191192