1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
23 // * Redistribution's in binary form must reproduce the above copyright notice,
24 // this list of conditions and the following disclaimer in the documentation
25 // and/or other materials provided with the distribution.
27 // * The name of the copyright holders may not be used to endorse or promote products
28 // derived from this software without specific prior written permission.
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
43 #include "test_precomp.hpp"
45 namespace opencv_test { namespace {
47 class CV_Affine3D_EstTest : public cvtest::BaseTest
50 CV_Affine3D_EstTest();
51 ~CV_Affine3D_EstTest();
59 CV_Affine3D_EstTest::CV_Affine3D_EstTest()
62 CV_Affine3D_EstTest::~CV_Affine3D_EstTest() {}
65 float rngIn(float from, float to) { return from + (to-from) * (float)theRNG(); }
71 WrapAff(const Mat& aff) : F(aff.ptr<double>()) {}
72 Point3f operator()(const Point3f& p)
74 return Point3f( (float)(p.x * F[0] + p.y * F[1] + p.z * F[2] + F[3]),
75 (float)(p.x * F[4] + p.y * F[5] + p.z * F[6] + F[7]),
76 (float)(p.x * F[8] + p.y * F[9] + p.z * F[10] + F[11]) );
80 bool CV_Affine3D_EstTest::test4Points()
82 Mat aff(3, 4, CV_64F);
83 cv::randu(aff, Scalar(1), Scalar(3));
85 // setting points that are no in the same line
87 Mat fpts(1, 4, CV_32FC3);
88 Mat tpts(1, 4, CV_32FC3);
90 fpts.ptr<Point3f>()[0] = Point3f( rngIn(1,2), rngIn(1,2), rngIn(5, 6) );
91 fpts.ptr<Point3f>()[1] = Point3f( rngIn(3,4), rngIn(3,4), rngIn(5, 6) );
92 fpts.ptr<Point3f>()[2] = Point3f( rngIn(1,2), rngIn(3,4), rngIn(5, 6) );
93 fpts.ptr<Point3f>()[3] = Point3f( rngIn(3,4), rngIn(1,2), rngIn(5, 6) );
95 std::transform(fpts.ptr<Point3f>(), fpts.ptr<Point3f>() + 4, tpts.ptr<Point3f>(), WrapAff(aff));
98 vector<uchar> outliers;
99 estimateAffine3D(fpts, tpts, aff_est, outliers);
101 const double thres = 1e-3;
102 if (cvtest::norm(aff_est, aff, NORM_INF) > thres)
104 //cout << cvtest::norm(aff_est, aff, NORM_INF) << endl;
105 ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
114 Noise(float level) : l(level) {}
115 Point3f operator()(const Point3f& p)
118 return Point3f( p.x + l * (float)rng, p.y + l * (float)rng, p.z + l * (float)rng);
122 bool CV_Affine3D_EstTest::testNPoints()
124 Mat aff(3, 4, CV_64F);
125 cv::randu(aff, Scalar(-2), Scalar(2));
127 // setting points that are no in the same line
131 const Point3f shift_outl = Point3f(15, 15, 15);
132 const float noise_level = 20.f;
134 Mat fpts(1, n, CV_32FC3);
135 Mat tpts(1, n, CV_32FC3);
137 randu(fpts, Scalar::all(0), Scalar::all(100));
138 std::transform(fpts.ptr<Point3f>(), fpts.ptr<Point3f>() + n, tpts.ptr<Point3f>(), WrapAff(aff));
142 std::transform(tpts.ptr<Point3f>() + m, tpts.ptr<Point3f>() + n, tpts.ptr<Point3f>() + m,
143 [=] (const Point3f& pt) -> Point3f { return Noise(noise_level)(pt + shift_outl); });
145 std::transform(tpts.ptr<Point3f>() + m, tpts.ptr<Point3f>() + n, tpts.ptr<Point3f>() + m, std::bind2nd(std::plus<Point3f>(), shift_outl));
146 std::transform(tpts.ptr<Point3f>() + m, tpts.ptr<Point3f>() + n, tpts.ptr<Point3f>() + m, Noise(noise_level));
151 int res = estimateAffine3D(fpts, tpts, aff_est, outl);
155 ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
159 const double thres = 1e-4;
160 if (cvtest::norm(aff_est, aff, NORM_INF) > thres)
162 cout << "aff est: " << aff_est << endl;
163 cout << "aff ref: " << aff << endl;
164 ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
168 bool outl_good = count(outl.begin(), outl.end(), 1) == m &&
169 m == accumulate(outl.begin(), outl.begin() + m, 0);
173 ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
180 void CV_Affine3D_EstTest::run( int /* start_from */)
182 cvtest::DefaultRngAuto dra;
190 ts->set_failed_test_info(cvtest::TS::OK);
193 TEST(Calib3d_EstimateAffine3D, accuracy) { CV_Affine3D_EstTest test; test.safe_run(); }
195 TEST(Calib3d_EstimateAffine3D, regression_16007)
197 std::vector<cv::Point3f> m1, m2;
198 m1.push_back(Point3f(1.0f, 0.0f, 0.0f)); m2.push_back(Point3f(1.0f, 1.0f, 0.0f));
199 m1.push_back(Point3f(1.0f, 0.0f, 1.0f)); m2.push_back(Point3f(1.0f, 1.0f, 1.0f));
200 m1.push_back(Point3f(0.5f, 0.0f, 0.5f)); m2.push_back(Point3f(0.5f, 1.0f, 0.5f));
201 m1.push_back(Point3f(2.5f, 0.0f, 2.5f)); m2.push_back(Point3f(2.5f, 1.0f, 2.5f));
202 m1.push_back(Point3f(2.0f, 0.0f, 1.0f)); m2.push_back(Point3f(2.0f, 1.0f, 1.0f));
205 int res = cv::estimateAffine3D(m1, m2, m3D, inl);