Merge pull request #16011 from sebastien-wybo:fix_16007
[platform/upstream/opencv.git] / modules / calib3d / test / test_affine3d_estimator.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
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.
8 //
9 //
10 //                           License Agreement
11 //                For Open Source Computer Vision Library
12 //
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.
16 //
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
19 //
20 //   * Redistribution's of source code must retain the above copyright notice,
21 //     this list of conditions and the following disclaimer.
22 //
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.
26 //
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.
29 //
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.
40 //
41 //M*/
42
43 #include "test_precomp.hpp"
44
45 namespace opencv_test { namespace {
46
47 class CV_Affine3D_EstTest : public cvtest::BaseTest
48 {
49 public:
50     CV_Affine3D_EstTest();
51     ~CV_Affine3D_EstTest();
52 protected:
53     void run(int);
54
55     bool test4Points();
56     bool testNPoints();
57 };
58
59 CV_Affine3D_EstTest::CV_Affine3D_EstTest()
60 {
61 }
62 CV_Affine3D_EstTest::~CV_Affine3D_EstTest() {}
63
64
65 float rngIn(float from, float to) { return from + (to-from) * (float)theRNG(); }
66
67
68 struct WrapAff
69 {
70     const double *F;
71     WrapAff(const Mat& aff) : F(aff.ptr<double>()) {}
72     Point3f operator()(const Point3f& p)
73     {
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])  );
77     }
78 };
79
80 bool CV_Affine3D_EstTest::test4Points()
81 {
82     Mat aff(3, 4, CV_64F);
83     cv::randu(aff, Scalar(1), Scalar(3));
84
85     // setting points that are no in the same line
86
87     Mat fpts(1, 4, CV_32FC3);
88     Mat tpts(1, 4, CV_32FC3);
89
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) );
94
95     std::transform(fpts.ptr<Point3f>(), fpts.ptr<Point3f>() + 4, tpts.ptr<Point3f>(), WrapAff(aff));
96
97     Mat aff_est;
98     vector<uchar> outliers;
99     estimateAffine3D(fpts, tpts, aff_est, outliers);
100
101     const double thres = 1e-3;
102     if (cvtest::norm(aff_est, aff, NORM_INF) > thres)
103     {
104         //cout << cvtest::norm(aff_est, aff, NORM_INF) << endl;
105         ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
106         return false;
107     }
108     return true;
109 }
110
111 struct Noise
112 {
113     float l;
114     Noise(float level) : l(level) {}
115     Point3f operator()(const Point3f& p)
116     {
117         RNG& rng = theRNG();
118         return Point3f( p.x + l * (float)rng,  p.y + l * (float)rng,  p.z + l * (float)rng);
119     }
120 };
121
122 bool CV_Affine3D_EstTest::testNPoints()
123 {
124     Mat aff(3, 4, CV_64F);
125     cv::randu(aff, Scalar(-2), Scalar(2));
126
127     // setting points that are no in the same line
128
129     const int n = 100;
130     const int m = 3*n/5;
131     const Point3f shift_outl = Point3f(15, 15, 15);
132     const float noise_level = 20.f;
133
134     Mat fpts(1, n, CV_32FC3);
135     Mat tpts(1, n, CV_32FC3);
136
137     randu(fpts, Scalar::all(0), Scalar::all(100));
138     std::transform(fpts.ptr<Point3f>(), fpts.ptr<Point3f>() + n, tpts.ptr<Point3f>(), WrapAff(aff));
139
140     /* adding noise*/
141 #ifdef CV_CXX11
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); });
144 #else
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));
147 #endif
148
149     Mat aff_est;
150     vector<uchar> outl;
151     int res = estimateAffine3D(fpts, tpts, aff_est, outl);
152
153     if (!res)
154     {
155         ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
156         return false;
157     }
158
159     const double thres = 1e-4;
160     if (cvtest::norm(aff_est, aff, NORM_INF) > thres)
161     {
162         cout << "aff est: " << aff_est << endl;
163         cout << "aff ref: " << aff << endl;
164         ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
165         return false;
166     }
167
168     bool outl_good = count(outl.begin(), outl.end(), 1) == m &&
169         m == accumulate(outl.begin(), outl.begin() + m, 0);
170
171     if (!outl_good)
172     {
173         ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
174         return false;
175     }
176     return true;
177 }
178
179
180 void CV_Affine3D_EstTest::run( int /* start_from */)
181 {
182     cvtest::DefaultRngAuto dra;
183
184     if (!test4Points())
185         return;
186
187     if (!testNPoints())
188         return;
189
190     ts->set_failed_test_info(cvtest::TS::OK);
191 }
192
193 TEST(Calib3d_EstimateAffine3D, accuracy) { CV_Affine3D_EstTest test; test.safe_run(); }
194
195 TEST(Calib3d_EstimateAffine3D, regression_16007)
196 {
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));
203
204     cv::Mat m3D, inl;
205     int res = cv::estimateAffine3D(m1, m2, m3D, inl);
206     EXPECT_EQ(1, res);
207 }
208
209 }} // namespace