Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/calib3d/test/test_affine3d_estimator.cpp
16337 views
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
std::transform(tpts.ptr<Point3f>() + m, tpts.ptr<Point3f>() + n, tpts.ptr<Point3f>() + m,
142
[=] (const Point3f& pt) -> Point3f { return Noise(noise_level)(pt + shift_outl); });
143
144
Mat aff_est;
145
vector<uchar> outl;
146
int res = estimateAffine3D(fpts, tpts, aff_est, outl);
147
148
if (!res)
149
{
150
ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
151
return false;
152
}
153
154
const double thres = 1e-4;
155
if (cvtest::norm(aff_est, aff, NORM_INF) > thres)
156
{
157
cout << "aff est: " << aff_est << endl;
158
cout << "aff ref: " << aff << endl;
159
ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
160
return false;
161
}
162
163
bool outl_good = count(outl.begin(), outl.end(), 1) == m &&
164
m == accumulate(outl.begin(), outl.begin() + m, 0);
165
166
if (!outl_good)
167
{
168
ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
169
return false;
170
}
171
return true;
172
}
173
174
175
void CV_Affine3D_EstTest::run( int /* start_from */)
176
{
177
cvtest::DefaultRngAuto dra;
178
179
if (!test4Points())
180
return;
181
182
if (!testNPoints())
183
return;
184
185
ts->set_failed_test_info(cvtest::TS::OK);
186
}
187
188
TEST(Calib3d_EstimateAffine3D, accuracy) { CV_Affine3D_EstTest test; test.safe_run(); }
189
190
}} // namespace
191
192