Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/calib3d/test/test_posit.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
// Intel License Agreement
11
// For Open Source Computer Vision Library
12
//
13
// Copyright (C) 2000, Intel Corporation, all rights reserved.
14
// Third party copyrights are property of their respective owners.
15
//
16
// Redistribution and use in source and binary forms, with or without modification,
17
// are permitted provided that the following conditions are met:
18
//
19
// * Redistribution's of source code must retain the above copyright notice,
20
// this list of conditions and the following disclaimer.
21
//
22
// * Redistribution's in binary form must reproduce the above copyright notice,
23
// this list of conditions and the following disclaimer in the documentation
24
// and/or other materials provided with the distribution.
25
//
26
// * The name of Intel Corporation may not be used to endorse or promote products
27
// derived from this software 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 the Intel Corporation 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
#include "opencv2/calib3d/calib3d_c.h"
44
45
namespace opencv_test { namespace {
46
47
class CV_POSITTest : public cvtest::BaseTest
48
{
49
public:
50
CV_POSITTest();
51
protected:
52
void run(int);
53
};
54
55
56
CV_POSITTest::CV_POSITTest()
57
{
58
test_case_count = 20;
59
}
60
61
void CV_POSITTest::run( int start_from )
62
{
63
int code = cvtest::TS::OK;
64
65
/* fixed parameters output */
66
/*float rot[3][3]={ 0.49010f, 0.85057f, 0.19063f,
67
-0.56948f, 0.14671f, 0.80880f,
68
0.65997f, -0.50495f, 0.55629f };
69
70
float trans[3] = { 0.0f, 0.0f, 40.02637f };
71
*/
72
73
/* Some variables */
74
int i, counter;
75
76
CvTermCriteria criteria;
77
CvPoint3D32f* obj_points;
78
CvPoint2D32f* img_points;
79
CvPOSITObject* object;
80
81
float angleX, angleY, angleZ;
82
RNG& rng = ts->get_rng();
83
int progress = 0;
84
85
CvMat* true_rotationX = cvCreateMat( 3, 3, CV_32F );
86
CvMat* true_rotationY = cvCreateMat( 3, 3, CV_32F );
87
CvMat* true_rotationZ = cvCreateMat( 3, 3, CV_32F );
88
CvMat* tmp_matrix = cvCreateMat( 3, 3, CV_32F );
89
CvMat* true_rotation = cvCreateMat( 3, 3, CV_32F );
90
CvMat* rotation = cvCreateMat( 3, 3, CV_32F );
91
CvMat* translation = cvCreateMat( 3, 1, CV_32F );
92
CvMat* true_translation = cvCreateMat( 3, 1, CV_32F );
93
94
const float flFocalLength = 760.f;
95
const float flEpsilon = 0.5f;
96
97
/* Initialization */
98
criteria.type = CV_TERMCRIT_EPS|CV_TERMCRIT_ITER;
99
criteria.epsilon = flEpsilon;
100
criteria.max_iter = 10000;
101
102
/* Allocating source arrays; */
103
obj_points = (CvPoint3D32f*)cvAlloc( 8 * sizeof(CvPoint3D32f) );
104
img_points = (CvPoint2D32f*)cvAlloc( 8 * sizeof(CvPoint2D32f) );
105
106
/* Fill points arrays with values */
107
108
/* cube model with edge size 10 */
109
obj_points[0].x = 0; obj_points[0].y = 0; obj_points[0].z = 0;
110
obj_points[1].x = 10; obj_points[1].y = 0; obj_points[1].z = 0;
111
obj_points[2].x = 10; obj_points[2].y = 10; obj_points[2].z = 0;
112
obj_points[3].x = 0; obj_points[3].y = 10; obj_points[3].z = 0;
113
obj_points[4].x = 0; obj_points[4].y = 0; obj_points[4].z = 10;
114
obj_points[5].x = 10; obj_points[5].y = 0; obj_points[5].z = 10;
115
obj_points[6].x = 10; obj_points[6].y = 10; obj_points[6].z = 10;
116
obj_points[7].x = 0; obj_points[7].y = 10; obj_points[7].z = 10;
117
118
/* Loop for test some random object positions */
119
for( counter = start_from; counter < test_case_count; counter++ )
120
{
121
ts->update_context( this, counter, true );
122
progress = update_progress( progress, counter, test_case_count, 0 );
123
124
/* set all rotation matrix to zero */
125
cvZero( true_rotationX );
126
cvZero( true_rotationY );
127
cvZero( true_rotationZ );
128
129
/* fill random rotation matrix */
130
angleX = (float)(cvtest::randReal(rng)*2*CV_PI);
131
angleY = (float)(cvtest::randReal(rng)*2*CV_PI);
132
angleZ = (float)(cvtest::randReal(rng)*2*CV_PI);
133
134
true_rotationX->data.fl[0 *3+ 0] = 1;
135
true_rotationX->data.fl[1 *3+ 1] = (float)cos(angleX);
136
true_rotationX->data.fl[2 *3+ 2] = true_rotationX->data.fl[1 *3+ 1];
137
true_rotationX->data.fl[1 *3+ 2] = -(float)sin(angleX);
138
true_rotationX->data.fl[2 *3+ 1] = -true_rotationX->data.fl[1 *3+ 2];
139
140
true_rotationY->data.fl[1 *3+ 1] = 1;
141
true_rotationY->data.fl[0 *3+ 0] = (float)cos(angleY);
142
true_rotationY->data.fl[2 *3+ 2] = true_rotationY->data.fl[0 *3+ 0];
143
true_rotationY->data.fl[0 *3+ 2] = -(float)sin(angleY);
144
true_rotationY->data.fl[2 *3+ 0] = -true_rotationY->data.fl[0 *3+ 2];
145
146
true_rotationZ->data.fl[2 *3+ 2] = 1;
147
true_rotationZ->data.fl[0 *3+ 0] = (float)cos(angleZ);
148
true_rotationZ->data.fl[1 *3+ 1] = true_rotationZ->data.fl[0 *3+ 0];
149
true_rotationZ->data.fl[0 *3+ 1] = -(float)sin(angleZ);
150
true_rotationZ->data.fl[1 *3+ 0] = -true_rotationZ->data.fl[0 *3+ 1];
151
152
cvMatMul( true_rotationX, true_rotationY, tmp_matrix);
153
cvMatMul( tmp_matrix, true_rotationZ, true_rotation);
154
155
/* fill translation vector */
156
true_translation->data.fl[2] = (float)(cvtest::randReal(rng)*(2*flFocalLength-40) + 60);
157
true_translation->data.fl[0] = (float)((cvtest::randReal(rng)*2-1)*true_translation->data.fl[2]);
158
true_translation->data.fl[1] = (float)((cvtest::randReal(rng)*2-1)*true_translation->data.fl[2]);
159
160
/* calculate perspective projection */
161
for ( i = 0; i < 8; i++ )
162
{
163
float vec[3];
164
CvMat Vec = cvMat( 3, 1, CV_32F, vec );
165
CvMat Obj_point = cvMat( 3, 1, CV_32F, &obj_points[i].x );
166
167
cvMatMul( true_rotation, &Obj_point, &Vec );
168
169
vec[0] += true_translation->data.fl[0];
170
vec[1] += true_translation->data.fl[1];
171
vec[2] += true_translation->data.fl[2];
172
173
img_points[i].x = flFocalLength * vec[0] / vec[2];
174
img_points[i].y = flFocalLength * vec[1] / vec[2];
175
}
176
177
/*img_points[0].x = 0 ; img_points[0].y = 0;
178
img_points[1].x = 80; img_points[1].y = -93;
179
img_points[2].x = 245;img_points[2].y = -77;
180
img_points[3].x = 185;img_points[3].y = 32;
181
img_points[4].x = 32; img_points[4].y = 135;
182
img_points[5].x = 99; img_points[5].y = 35;
183
img_points[6].x = 247; img_points[6].y = 62;
184
img_points[7].x = 195; img_points[7].y = 179;
185
*/
186
187
object = cvCreatePOSITObject( obj_points, 8 );
188
cvPOSIT( object, img_points, flFocalLength, criteria,
189
rotation->data.fl, translation->data.fl );
190
cvReleasePOSITObject( &object );
191
192
Mat _rotation = cvarrToMat(rotation), _true_rotation = cvarrToMat(true_rotation);
193
Mat _translation = cvarrToMat(translation), _true_translation = cvarrToMat(true_translation);
194
code = cvtest::cmpEps2( ts, _rotation, _true_rotation, flEpsilon, false, "rotation matrix" );
195
if( code < 0 )
196
break;
197
198
code = cvtest::cmpEps2( ts, _translation, _true_translation, flEpsilon, false, "translation vector" );
199
if( code < 0 )
200
break;
201
}
202
203
cvFree( &obj_points );
204
cvFree( &img_points );
205
206
cvReleaseMat( &true_rotationX );
207
cvReleaseMat( &true_rotationY );
208
cvReleaseMat( &true_rotationZ );
209
cvReleaseMat( &tmp_matrix );
210
cvReleaseMat( &true_rotation );
211
cvReleaseMat( &rotation );
212
cvReleaseMat( &translation );
213
cvReleaseMat( &true_translation );
214
215
if( code < 0 )
216
ts->set_failed_test_info( code );
217
}
218
219
TEST(Calib3d_POSIT, accuracy) { CV_POSITTest test; test.safe_run(); }
220
221
}} // namespace
222
/* End of file. */
223
224