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 using namespace cv;
46 using namespace std;
47
48 #include <string>
49 #include <iostream>
50 #include <fstream>
51 #include <functional>
52 #include <iterator>
53 #include <limits>
54 #include <numeric>
55
56 class CV_Affine3D_EstTest : public cvtest::BaseTest
57 {
58 public:
59 CV_Affine3D_EstTest();
60 ~CV_Affine3D_EstTest();
61 protected:
62 void run(int);
63
64 bool test4Points();
65 bool testNPoints();
66 };
67
CV_Affine3D_EstTest()68 CV_Affine3D_EstTest::CV_Affine3D_EstTest()
69 {
70 }
~CV_Affine3D_EstTest()71 CV_Affine3D_EstTest::~CV_Affine3D_EstTest() {}
72
73
rngIn(float from,float to)74 float rngIn(float from, float to) { return from + (to-from) * (float)theRNG(); }
75
76
77 struct WrapAff
78 {
79 const double *F;
WrapAffWrapAff80 WrapAff(const Mat& aff) : F(aff.ptr<double>()) {}
operator ()WrapAff81 Point3f operator()(const Point3f& p)
82 {
83 return Point3f( (float)(p.x * F[0] + p.y * F[1] + p.z * F[2] + F[3]),
84 (float)(p.x * F[4] + p.y * F[5] + p.z * F[6] + F[7]),
85 (float)(p.x * F[8] + p.y * F[9] + p.z * F[10] + F[11]) );
86 }
87 };
88
test4Points()89 bool CV_Affine3D_EstTest::test4Points()
90 {
91 Mat aff(3, 4, CV_64F);
92 cv::randu(aff, Scalar(1), Scalar(3));
93
94 // setting points that are no in the same line
95
96 Mat fpts(1, 4, CV_32FC3);
97 Mat tpts(1, 4, CV_32FC3);
98
99 fpts.ptr<Point3f>()[0] = Point3f( rngIn(1,2), rngIn(1,2), rngIn(5, 6) );
100 fpts.ptr<Point3f>()[1] = Point3f( rngIn(3,4), rngIn(3,4), rngIn(5, 6) );
101 fpts.ptr<Point3f>()[2] = Point3f( rngIn(1,2), rngIn(3,4), rngIn(5, 6) );
102 fpts.ptr<Point3f>()[3] = Point3f( rngIn(3,4), rngIn(1,2), rngIn(5, 6) );
103
104 transform(fpts.ptr<Point3f>(), fpts.ptr<Point3f>() + 4, tpts.ptr<Point3f>(), WrapAff(aff));
105
106 Mat aff_est;
107 vector<uchar> outliers;
108 estimateAffine3D(fpts, tpts, aff_est, outliers);
109
110 const double thres = 1e-3;
111 if (cvtest::norm(aff_est, aff, NORM_INF) > thres)
112 {
113 //cout << cvtest::norm(aff_est, aff, NORM_INF) << endl;
114 ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
115 return false;
116 }
117 return true;
118 }
119
120 struct Noise
121 {
122 float l;
NoiseNoise123 Noise(float level) : l(level) {}
operator ()Noise124 Point3f operator()(const Point3f& p)
125 {
126 RNG& rng = theRNG();
127 return Point3f( p.x + l * (float)rng, p.y + l * (float)rng, p.z + l * (float)rng);
128 }
129 };
130
testNPoints()131 bool CV_Affine3D_EstTest::testNPoints()
132 {
133 Mat aff(3, 4, CV_64F);
134 cv::randu(aff, Scalar(-2), Scalar(2));
135
136 // setting points that are no in the same line
137
138 const int n = 100;
139 const int m = 3*n/5;
140 const Point3f shift_outl = Point3f(15, 15, 15);
141 const float noise_level = 20.f;
142
143 Mat fpts(1, n, CV_32FC3);
144 Mat tpts(1, n, CV_32FC3);
145
146 randu(fpts, Scalar::all(0), Scalar::all(100));
147 transform(fpts.ptr<Point3f>(), fpts.ptr<Point3f>() + n, tpts.ptr<Point3f>(), WrapAff(aff));
148
149 /* adding noise*/
150 transform(tpts.ptr<Point3f>() + m, tpts.ptr<Point3f>() + n, tpts.ptr<Point3f>() + m, bind2nd(plus<Point3f>(), shift_outl));
151 transform(tpts.ptr<Point3f>() + m, tpts.ptr<Point3f>() + n, tpts.ptr<Point3f>() + m, Noise(noise_level));
152
153 Mat aff_est;
154 vector<uchar> outl;
155 int res = estimateAffine3D(fpts, tpts, aff_est, outl);
156
157 if (!res)
158 {
159 ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
160 return false;
161 }
162
163 const double thres = 1e-4;
164 if (cvtest::norm(aff_est, aff, NORM_INF) > thres)
165 {
166 cout << "aff est: " << aff_est << endl;
167 cout << "aff ref: " << aff << endl;
168 ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
169 return false;
170 }
171
172 bool outl_good = count(outl.begin(), outl.end(), 1) == m &&
173 m == accumulate(outl.begin(), outl.begin() + m, 0);
174
175 if (!outl_good)
176 {
177 ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
178 return false;
179 }
180 return true;
181 }
182
183
run(int)184 void CV_Affine3D_EstTest::run( int /* start_from */)
185 {
186 cvtest::DefaultRngAuto dra;
187
188 if (!test4Points())
189 return;
190
191 if (!testNPoints())
192 return;
193
194 ts->set_failed_test_info(cvtest::TS::OK);
195 }
196
TEST(Calib3d_EstimateAffineTransform,accuracy)197 TEST(Calib3d_EstimateAffineTransform, accuracy) { CV_Affine3D_EstTest test; test.safe_run(); }
198