Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/video/test/test_estimaterigid.cpp
16339 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
// this is test for a deprecated function. let's ignore deprecated warnings in this file
46
#if defined(__clang__)
47
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
48
#elif defined(__GNUC__)
49
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
50
#elif defined(_MSC_VER)
51
#pragma warning( disable : 4996)
52
#endif
53
54
namespace opencv_test { namespace {
55
56
class CV_RigidTransform_Test : public cvtest::BaseTest
57
{
58
public:
59
CV_RigidTransform_Test();
60
~CV_RigidTransform_Test();
61
protected:
62
void run(int);
63
64
bool testNPoints(int);
65
bool testImage();
66
};
67
68
CV_RigidTransform_Test::CV_RigidTransform_Test()
69
{
70
}
71
CV_RigidTransform_Test::~CV_RigidTransform_Test() {}
72
73
struct WrapAff2D
74
{
75
const double *F;
76
WrapAff2D(const Mat& aff) : F(aff.ptr<double>()) {}
77
Point2f operator()(const Point2f& p)
78
{
79
return Point2f( (float)(p.x * F[0] + p.y * F[1] + F[2]),
80
(float)(p.x * F[3] + p.y * F[4] + F[5]) );
81
}
82
};
83
84
bool CV_RigidTransform_Test::testNPoints(int from)
85
{
86
cv::RNG rng = ts->get_rng();
87
88
int progress = 0;
89
int k, ntests = 10000;
90
91
for( k = from; k < ntests; k++ )
92
{
93
ts->update_context( this, k, true );
94
progress = update_progress(progress, k, ntests, 0);
95
96
Mat aff(2, 3, CV_64F);
97
rng.fill(aff, RNG::UNIFORM, Scalar(-2), Scalar(2));
98
99
int n = (unsigned)rng % 100 + 10;
100
101
Mat fpts(1, n, CV_32FC2);
102
Mat tpts(1, n, CV_32FC2);
103
104
rng.fill(fpts, RNG::UNIFORM, Scalar(0,0), Scalar(10,10));
105
std::transform(fpts.ptr<Point2f>(), fpts.ptr<Point2f>() + n, tpts.ptr<Point2f>(), WrapAff2D(aff));
106
107
Mat noise(1, n, CV_32FC2);
108
rng.fill(noise, RNG::NORMAL, Scalar::all(0), Scalar::all(0.001*(n<=7 ? 0 : n <= 30 ? 1 : 10)));
109
tpts += noise;
110
111
Mat aff_est = estimateRigidTransform(fpts, tpts, true);
112
113
double thres = 0.1*cvtest::norm(aff, NORM_L2);
114
double d = cvtest::norm(aff_est, aff, NORM_L2);
115
if (d > thres)
116
{
117
double dB=0, nB=0;
118
if (n <= 4)
119
{
120
Mat A = fpts.reshape(1, 3);
121
Mat B = A - repeat(A.row(0), 3, 1), Bt = B.t();
122
B = Bt*B;
123
dB = cv::determinant(B);
124
nB = cvtest::norm(B, NORM_L2);
125
if( fabs(dB) < 0.01*nB )
126
continue;
127
}
128
ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
129
ts->printf( cvtest::TS::LOG, "Threshold = %f, norm of difference = %f", thres, d );
130
return false;
131
}
132
}
133
return true;
134
}
135
136
bool CV_RigidTransform_Test::testImage()
137
{
138
Mat img;
139
Mat testImg = imread( string(ts->get_data_path()) + "shared/graffiti.png", 1);
140
if (testImg.empty())
141
{
142
ts->printf( ts->LOG, "test image can not be read");
143
ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);
144
return false;
145
}
146
pyrDown(testImg, img);
147
148
Mat aff = cv::getRotationMatrix2D(Point(img.cols/2, img.rows/2), 1, 0.99);
149
aff.ptr<double>()[2]+=3;
150
aff.ptr<double>()[5]+=3;
151
152
Mat rotated;
153
warpAffine(img, rotated, aff, img.size());
154
155
Mat aff_est = estimateRigidTransform(img, rotated, true);
156
157
const double thres = 0.033;
158
if (cvtest::norm(aff_est, aff, NORM_INF) > thres)
159
{
160
ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
161
ts->printf( cvtest::TS::LOG, "Threshold = %f, norm of difference = %f", thres,
162
cvtest::norm(aff_est, aff, NORM_INF) );
163
return false;
164
}
165
166
return true;
167
}
168
169
void CV_RigidTransform_Test::run( int start_from )
170
{
171
cvtest::DefaultRngAuto dra;
172
173
if (!testNPoints(start_from))
174
return;
175
176
if (!testImage())
177
return;
178
179
ts->set_failed_test_info(cvtest::TS::OK);
180
}
181
182
TEST(Video_RigidFlow, accuracy) { CV_RigidTransform_Test test; test.safe_run(); }
183
184
}} // namespace
185