Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/calib3d/test/test_affine_partial2d_estimator.cpp
16337 views
1
/*M///////////////////////////////////////////////////////////////////////////////////////
2
//
3
// By downloading, copying, installing or using the software you agree to this license.
4
// If you do not agree to this license, do not download, install,
5
// copy or use the software.
6
//
7
//
8
// License Agreement
9
// For Open Source Computer Vision Library
10
// (3-clause BSD License)
11
//
12
// Copyright (C) 2015-2016, OpenCV Foundation, all rights reserved.
13
// Third party copyrights are property of their respective owners.
14
//
15
// Redistribution and use in source and binary forms, with or without modification,
16
// are permitted provided that the following conditions are met:
17
//
18
// * Redistributions of source code must retain the above copyright notice,
19
// this list of conditions and the following disclaimer.
20
//
21
// * Redistributions in binary form must reproduce the above copyright notice,
22
// this list of conditions and the following disclaimer in the documentation
23
// and/or other materials provided with the distribution.
24
//
25
// * Neither the names of the copyright holders nor the names of the contributors
26
// may be used to endorse or promote products derived from this software
27
// without specific prior written permission.
28
//
29
// This software is provided by the copyright holders and contributors "as is" and
30
// any express or implied warranties, including, but not limited to, the implied
31
// warranties of merchantability and fitness for a particular purpose are disclaimed.
32
// In no event shall copyright holders or contributors be liable for any direct,
33
// indirect, incidental, special, exemplary, or consequential damages
34
// (including, but not limited to, procurement of substitute goods or services;
35
// loss of use, data, or profits; or business interruption) however caused
36
// and on any theory of liability, whether in contract, strict liability,
37
// or tort (including negligence or otherwise) arising in any way out of
38
// the use of this software, even if advised of the possibility of such damage.
39
//
40
//M*/
41
42
#include "test_precomp.hpp"
43
44
namespace opencv_test { namespace {
45
46
CV_ENUM(Method, RANSAC, LMEDS)
47
typedef TestWithParam<Method> EstimateAffinePartial2D;
48
49
static float rngIn(float from, float to) { return from + (to-from) * (float)theRNG(); }
50
51
// get random matrix of affine transformation limited to combinations of translation,
52
// rotation, and uniform scaling
53
static Mat rngPartialAffMat() {
54
double theta = rngIn(0, (float)CV_PI*2.f);
55
double scale = rngIn(0, 3);
56
double tx = rngIn(-2, 2);
57
double ty = rngIn(-2, 2);
58
double aff[2*3] = { std::cos(theta) * scale, -std::sin(theta) * scale, tx,
59
std::sin(theta) * scale, std::cos(theta) * scale, ty };
60
return Mat(2, 3, CV_64F, aff).clone();
61
}
62
63
TEST_P(EstimateAffinePartial2D, test2Points)
64
{
65
// try more transformations
66
for (size_t i = 0; i < 500; ++i)
67
{
68
Mat aff = rngPartialAffMat();
69
70
// setting points that are no in the same line
71
Mat fpts(1, 2, CV_32FC2);
72
Mat tpts(1, 2, CV_32FC2);
73
74
fpts.at<Point2f>(0) = Point2f( rngIn(1,2), rngIn(5,6) );
75
fpts.at<Point2f>(1) = Point2f( rngIn(3,4), rngIn(3,4) );
76
77
transform(fpts, tpts, aff);
78
79
vector<uchar> inliers;
80
Mat aff_est = estimateAffinePartial2D(fpts, tpts, inliers, GetParam() /* method */);
81
82
EXPECT_NEAR(0., cvtest::norm(aff_est, aff, NORM_INF), 1e-3);
83
84
// all must be inliers
85
EXPECT_EQ(countNonZero(inliers), 2);
86
}
87
}
88
89
TEST_P(EstimateAffinePartial2D, testNPoints)
90
{
91
// try more transformations
92
for (size_t i = 0; i < 500; ++i)
93
{
94
Mat aff = rngPartialAffMat();
95
96
const int method = GetParam();
97
const int n = 100;
98
int m;
99
// LMEDS can't handle more than 50% outliers (by design)
100
if (method == LMEDS)
101
m = 3*n/5;
102
else
103
m = 2*n/5;
104
const float shift_outl = 15.f;
105
const float noise_level = 20.f;
106
107
Mat fpts(1, n, CV_32FC2);
108
Mat tpts(1, n, CV_32FC2);
109
110
randu(fpts, 0., 100.);
111
transform(fpts, tpts, aff);
112
113
/* adding noise to some points */
114
Mat outliers = tpts.colRange(m, n);
115
outliers.reshape(1) += shift_outl;
116
117
Mat noise (outliers.size(), outliers.type());
118
randu(noise, 0., noise_level);
119
outliers += noise;
120
121
vector<uchar> inliers;
122
Mat aff_est = estimateAffinePartial2D(fpts, tpts, inliers, method);
123
124
EXPECT_FALSE(aff_est.empty());
125
126
EXPECT_NEAR(0., cvtest::norm(aff_est, aff, NORM_INF), 1e-4);
127
128
bool inliers_good = count(inliers.begin(), inliers.end(), 1) == m &&
129
m == accumulate(inliers.begin(), inliers.begin() + m, 0);
130
131
EXPECT_TRUE(inliers_good);
132
}
133
}
134
135
// test conversion from other datatypes than float
136
TEST_P(EstimateAffinePartial2D, testConversion)
137
{
138
Mat aff = rngPartialAffMat();
139
aff.convertTo(aff, CV_32S); // convert to int to transform ints properly
140
141
std::vector<Point> fpts(3);
142
std::vector<Point> tpts(3);
143
144
fpts[0] = Point2f( rngIn(1,2), rngIn(5,6) );
145
fpts[1] = Point2f( rngIn(3,4), rngIn(3,4) );
146
fpts[2] = Point2f( rngIn(1,2), rngIn(3,4) );
147
148
transform(fpts, tpts, aff);
149
150
vector<uchar> inliers;
151
Mat aff_est = estimateAffinePartial2D(fpts, tpts, inliers, GetParam() /* method */);
152
153
ASSERT_FALSE(aff_est.empty());
154
155
aff.convertTo(aff, CV_64F); // need to convert back before compare
156
EXPECT_NEAR(0., cvtest::norm(aff_est, aff, NORM_INF), 1e-3);
157
158
// all must be inliers
159
EXPECT_EQ(countNonZero(inliers), 3);
160
}
161
162
INSTANTIATE_TEST_CASE_P(Calib3d, EstimateAffinePartial2D, Method::all());
163
164
}} // namespace
165
166