Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/calib3d/test/test_solvepnp_ransac.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_solvePnPRansac_Test : public cvtest::BaseTest
48
{
49
public:
50
CV_solvePnPRansac_Test()
51
{
52
eps[SOLVEPNP_ITERATIVE] = 1.0e-2;
53
eps[SOLVEPNP_EPNP] = 1.0e-2;
54
eps[SOLVEPNP_P3P] = 1.0e-2;
55
eps[SOLVEPNP_AP3P] = 1.0e-2;
56
eps[SOLVEPNP_DLS] = 1.0e-2;
57
eps[SOLVEPNP_UPNP] = 1.0e-2;
58
totalTestsCount = 10;
59
pointsCount = 500;
60
}
61
~CV_solvePnPRansac_Test() {}
62
protected:
63
void generate3DPointCloud(vector<Point3f>& points,
64
Point3f pmin = Point3f(-1, -1, 5),
65
Point3f pmax = Point3f(1, 1, 10))
66
{
67
RNG rng = cv::theRNG(); // fix the seed to use "fixed" input 3D points
68
69
for (size_t i = 0; i < points.size(); i++)
70
{
71
float _x = rng.uniform(pmin.x, pmax.x);
72
float _y = rng.uniform(pmin.y, pmax.y);
73
float _z = rng.uniform(pmin.z, pmax.z);
74
points[i] = Point3f(_x, _y, _z);
75
}
76
}
77
78
void generateCameraMatrix(Mat& cameraMatrix, RNG& rng)
79
{
80
const double fcMinVal = 1e-3;
81
const double fcMaxVal = 100;
82
cameraMatrix.create(3, 3, CV_64FC1);
83
cameraMatrix.setTo(Scalar(0));
84
cameraMatrix.at<double>(0,0) = rng.uniform(fcMinVal, fcMaxVal);
85
cameraMatrix.at<double>(1,1) = rng.uniform(fcMinVal, fcMaxVal);
86
cameraMatrix.at<double>(0,2) = rng.uniform(fcMinVal, fcMaxVal);
87
cameraMatrix.at<double>(1,2) = rng.uniform(fcMinVal, fcMaxVal);
88
cameraMatrix.at<double>(2,2) = 1;
89
}
90
91
void generateDistCoeffs(Mat& distCoeffs, RNG& rng)
92
{
93
distCoeffs = Mat::zeros(4, 1, CV_64FC1);
94
for (int i = 0; i < 3; i++)
95
distCoeffs.at<double>(i,0) = rng.uniform(0.0, 1.0e-6);
96
}
97
98
void generatePose(Mat& rvec, Mat& tvec, RNG& rng)
99
{
100
const double minVal = 1.0e-3;
101
const double maxVal = 1.0;
102
rvec.create(3, 1, CV_64FC1);
103
tvec.create(3, 1, CV_64FC1);
104
for (int i = 0; i < 3; i++)
105
{
106
rvec.at<double>(i,0) = rng.uniform(minVal, maxVal);
107
tvec.at<double>(i,0) = rng.uniform(minVal, maxVal/10);
108
}
109
}
110
111
virtual bool runTest(RNG& rng, int mode, int method, const vector<Point3f>& points, const double* epsilon, double& maxError)
112
{
113
Mat rvec, tvec;
114
vector<int> inliers;
115
Mat trueRvec, trueTvec;
116
Mat intrinsics, distCoeffs;
117
generateCameraMatrix(intrinsics, rng);
118
if (method == 4) intrinsics.at<double>(1,1) = intrinsics.at<double>(0,0);
119
if (mode == 0)
120
distCoeffs = Mat::zeros(4, 1, CV_64FC1);
121
else
122
generateDistCoeffs(distCoeffs, rng);
123
generatePose(trueRvec, trueTvec, rng);
124
125
vector<Point2f> projectedPoints;
126
projectedPoints.resize(points.size());
127
projectPoints(Mat(points), trueRvec, trueTvec, intrinsics, distCoeffs, projectedPoints);
128
for (size_t i = 0; i < projectedPoints.size(); i++)
129
{
130
if (i % 20 == 0)
131
{
132
projectedPoints[i] = projectedPoints[rng.uniform(0,(int)points.size()-1)];
133
}
134
}
135
136
solvePnPRansac(points, projectedPoints, intrinsics, distCoeffs, rvec, tvec, false, pointsCount, 0.5f, 0.99, inliers, method);
137
138
bool isTestSuccess = inliers.size() >= points.size()*0.95;
139
140
double rvecDiff = cvtest::norm(rvec, trueRvec, NORM_L2), tvecDiff = cvtest::norm(tvec, trueTvec, NORM_L2);
141
isTestSuccess = isTestSuccess && rvecDiff < epsilon[method] && tvecDiff < epsilon[method];
142
double error = rvecDiff > tvecDiff ? rvecDiff : tvecDiff;
143
//cout << error << " " << inliers.size() << " " << eps[method] << endl;
144
if (error > maxError)
145
maxError = error;
146
147
return isTestSuccess;
148
}
149
150
virtual void run(int)
151
{
152
ts->set_failed_test_info(cvtest::TS::OK);
153
154
vector<Point3f> points, points_dls;
155
points.resize(pointsCount);
156
generate3DPointCloud(points);
157
158
RNG rng = ts->get_rng();
159
160
161
for (int mode = 0; mode < 2; mode++)
162
{
163
for (int method = 0; method < SOLVEPNP_MAX_COUNT; method++)
164
{
165
double maxError = 0;
166
int successfulTestsCount = 0;
167
for (int testIndex = 0; testIndex < totalTestsCount; testIndex++)
168
{
169
if (runTest(rng, mode, method, points, eps, maxError))
170
{
171
successfulTestsCount++;
172
}
173
}
174
if (successfulTestsCount < 0.7*totalTestsCount)
175
{
176
ts->printf( cvtest::TS::LOG, "Invalid accuracy for method %d, failed %d tests from %d, maximum error equals %f, distortion mode equals %d\n",
177
method, totalTestsCount - successfulTestsCount, totalTestsCount, maxError, mode);
178
ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
179
}
180
cout << "mode: " << mode << ", method: " << method << " -> "
181
<< ((double)successfulTestsCount / totalTestsCount) * 100 << "%"
182
<< " (err < " << maxError << ")" << endl;
183
}
184
}
185
}
186
double eps[SOLVEPNP_MAX_COUNT];
187
int totalTestsCount;
188
int pointsCount;
189
};
190
191
class CV_solvePnP_Test : public CV_solvePnPRansac_Test
192
{
193
public:
194
CV_solvePnP_Test()
195
{
196
eps[SOLVEPNP_ITERATIVE] = 1.0e-6;
197
eps[SOLVEPNP_EPNP] = 1.0e-6;
198
eps[SOLVEPNP_P3P] = 2.0e-4;
199
eps[SOLVEPNP_AP3P] = 1.0e-4;
200
eps[SOLVEPNP_DLS] = 1.0e-4;
201
eps[SOLVEPNP_UPNP] = 1.0e-4;
202
totalTestsCount = 1000;
203
}
204
205
~CV_solvePnP_Test() {}
206
protected:
207
virtual bool runTest(RNG& rng, int mode, int method, const vector<Point3f>& points, const double* epsilon, double& maxError)
208
{
209
Mat rvec, tvec;
210
Mat trueRvec, trueTvec;
211
Mat intrinsics, distCoeffs;
212
generateCameraMatrix(intrinsics, rng);
213
if (method == SOLVEPNP_DLS)
214
{
215
intrinsics.at<double>(1,1) = intrinsics.at<double>(0,0);
216
}
217
if (mode == 0)
218
{
219
distCoeffs = Mat::zeros(4, 1, CV_64FC1);
220
}
221
else
222
{
223
generateDistCoeffs(distCoeffs, rng);
224
}
225
generatePose(trueRvec, trueTvec, rng);
226
227
std::vector<Point3f> opoints;
228
switch(method)
229
{
230
case SOLVEPNP_P3P:
231
case SOLVEPNP_AP3P:
232
opoints = std::vector<Point3f>(points.begin(), points.begin()+4);
233
break;
234
case SOLVEPNP_UPNP:
235
opoints = std::vector<Point3f>(points.begin(), points.begin()+50);
236
break;
237
default:
238
opoints = points;
239
break;
240
}
241
242
vector<Point2f> projectedPoints;
243
projectedPoints.resize(opoints.size());
244
projectPoints(Mat(opoints), trueRvec, trueTvec, intrinsics, distCoeffs, projectedPoints);
245
246
bool isEstimateSuccess = solvePnP(opoints, projectedPoints, intrinsics, distCoeffs, rvec, tvec, false, method);
247
if (isEstimateSuccess == false)
248
{
249
return isEstimateSuccess;
250
}
251
252
double rvecDiff = cvtest::norm(rvec, trueRvec, NORM_L2), tvecDiff = cvtest::norm(tvec, trueTvec, NORM_L2);
253
bool isTestSuccess = rvecDiff < epsilon[method] && tvecDiff < epsilon[method];
254
255
double error = rvecDiff > tvecDiff ? rvecDiff : tvecDiff;
256
if (error > maxError)
257
{
258
maxError = error;
259
}
260
261
return isTestSuccess;
262
}
263
};
264
265
class CV_solveP3P_Test : public CV_solvePnPRansac_Test
266
{
267
public:
268
CV_solveP3P_Test()
269
{
270
eps[SOLVEPNP_P3P] = 2.0e-4;
271
eps[SOLVEPNP_AP3P] = 1.0e-4;
272
totalTestsCount = 1000;
273
}
274
275
~CV_solveP3P_Test() {}
276
protected:
277
virtual bool runTest(RNG& rng, int mode, int method, const vector<Point3f>& points, const double* epsilon, double& maxError)
278
{
279
std::vector<Mat> rvecs, tvecs;
280
Mat trueRvec, trueTvec;
281
Mat intrinsics, distCoeffs;
282
generateCameraMatrix(intrinsics, rng);
283
if (mode == 0)
284
distCoeffs = Mat::zeros(4, 1, CV_64FC1);
285
else
286
generateDistCoeffs(distCoeffs, rng);
287
generatePose(trueRvec, trueTvec, rng);
288
289
std::vector<Point3f> opoints;
290
opoints = std::vector<Point3f>(points.begin(), points.begin()+3);
291
292
vector<Point2f> projectedPoints;
293
projectedPoints.resize(opoints.size());
294
projectPoints(Mat(opoints), trueRvec, trueTvec, intrinsics, distCoeffs, projectedPoints);
295
296
int num_of_solutions = solveP3P(opoints, projectedPoints, intrinsics, distCoeffs, rvecs, tvecs, method);
297
if (num_of_solutions != (int) rvecs.size() || num_of_solutions != (int) tvecs.size() || num_of_solutions == 0)
298
return false;
299
300
bool isTestSuccess = false;
301
double error = DBL_MAX;
302
for (unsigned int i = 0; i < rvecs.size() && !isTestSuccess; ++i) {
303
double rvecDiff = cvtest::norm(rvecs[i], trueRvec, NORM_L2);
304
double tvecDiff = cvtest::norm(tvecs[i], trueTvec, NORM_L2);
305
isTestSuccess = rvecDiff < epsilon[method] && tvecDiff < epsilon[method];
306
error = std::min(error, std::max(rvecDiff, tvecDiff));
307
}
308
309
if (error > maxError)
310
maxError = error;
311
312
return isTestSuccess;
313
}
314
315
virtual void run(int)
316
{
317
ts->set_failed_test_info(cvtest::TS::OK);
318
319
vector<Point3f> points;
320
points.resize(pointsCount);
321
generate3DPointCloud(points);
322
323
const int methodsCount = 2;
324
int methods[] = {SOLVEPNP_P3P, SOLVEPNP_AP3P};
325
RNG rng = ts->get_rng();
326
327
for (int mode = 0; mode < 2; mode++)
328
{
329
for (int method = 0; method < methodsCount; method++)
330
{
331
double maxError = 0;
332
int successfulTestsCount = 0;
333
for (int testIndex = 0; testIndex < totalTestsCount; testIndex++)
334
{
335
if (runTest(rng, mode, methods[method], points, eps, maxError))
336
successfulTestsCount++;
337
}
338
if (successfulTestsCount < 0.7*totalTestsCount)
339
{
340
ts->printf( cvtest::TS::LOG, "Invalid accuracy for method %d, failed %d tests from %d, maximum error equals %f, distortion mode equals %d\n",
341
method, totalTestsCount - successfulTestsCount, totalTestsCount, maxError, mode);
342
ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
343
}
344
cout << "mode: " << mode << ", method: " << method << " -> "
345
<< ((double)successfulTestsCount / totalTestsCount) * 100 << "%"
346
<< " (err < " << maxError << ")" << endl;
347
}
348
}
349
}
350
};
351
352
353
TEST(Calib3d_SolveP3P, accuracy) { CV_solveP3P_Test test; test.safe_run();}
354
TEST(Calib3d_SolvePnPRansac, accuracy) { CV_solvePnPRansac_Test test; test.safe_run(); }
355
TEST(Calib3d_SolvePnP, accuracy) { CV_solvePnP_Test test; test.safe_run(); }
356
357
TEST(Calib3d_SolvePnPRansac, concurrency)
358
{
359
int count = 7*13;
360
361
Mat object(1, count, CV_32FC3);
362
randu(object, -100, 100);
363
364
Mat camera_mat(3, 3, CV_32FC1);
365
randu(camera_mat, 0.5, 1);
366
camera_mat.at<float>(0, 1) = 0.f;
367
camera_mat.at<float>(1, 0) = 0.f;
368
camera_mat.at<float>(2, 0) = 0.f;
369
camera_mat.at<float>(2, 1) = 0.f;
370
371
Mat dist_coef(1, 8, CV_32F, cv::Scalar::all(0));
372
373
vector<cv::Point2f> image_vec;
374
Mat rvec_gold(1, 3, CV_32FC1);
375
randu(rvec_gold, 0, 1);
376
Mat tvec_gold(1, 3, CV_32FC1);
377
randu(tvec_gold, 0, 1);
378
projectPoints(object, rvec_gold, tvec_gold, camera_mat, dist_coef, image_vec);
379
380
Mat image(1, count, CV_32FC2, &image_vec[0]);
381
382
Mat rvec1, rvec2;
383
Mat tvec1, tvec2;
384
385
int threads = getNumThreads();
386
{
387
// limit concurrency to get deterministic result
388
theRNG().state = 20121010;
389
setNumThreads(1);
390
solvePnPRansac(object, image, camera_mat, dist_coef, rvec1, tvec1);
391
}
392
393
{
394
setNumThreads(threads);
395
Mat rvec;
396
Mat tvec;
397
// parallel executions
398
for(int i = 0; i < 10; ++i)
399
{
400
cv::theRNG().state = 20121010;
401
solvePnPRansac(object, image, camera_mat, dist_coef, rvec, tvec);
402
}
403
}
404
405
{
406
// single thread again
407
theRNG().state = 20121010;
408
setNumThreads(1);
409
solvePnPRansac(object, image, camera_mat, dist_coef, rvec2, tvec2);
410
}
411
412
double rnorm = cvtest::norm(rvec1, rvec2, NORM_INF);
413
double tnorm = cvtest::norm(tvec1, tvec2, NORM_INF);
414
415
EXPECT_LT(rnorm, 1e-6);
416
EXPECT_LT(tnorm, 1e-6);
417
}
418
419
TEST(Calib3d_SolvePnPRansac, input_type)
420
{
421
const int numPoints = 10;
422
Matx33d intrinsics(5.4794130238156129e+002, 0., 2.9835545700043139e+002, 0.,
423
5.4817724002728005e+002, 2.3062194051986233e+002, 0., 0., 1.);
424
425
std::vector<cv::Point3f> points3d;
426
std::vector<cv::Point2f> points2d;
427
for (int i = 0; i < numPoints; i+=2)
428
{
429
points3d.push_back(cv::Point3i(5+i, 3, 2));
430
points3d.push_back(cv::Point3i(5+i, 3+i, 2+i));
431
points2d.push_back(cv::Point2i(0, i));
432
points2d.push_back(cv::Point2i(-i, i));
433
}
434
Mat R1, t1, R2, t2, R3, t3, R4, t4;
435
436
EXPECT_TRUE(solvePnPRansac(points3d, points2d, intrinsics, cv::Mat(), R1, t1));
437
438
Mat points3dMat(points3d);
439
Mat points2dMat(points2d);
440
EXPECT_TRUE(solvePnPRansac(points3dMat, points2dMat, intrinsics, cv::Mat(), R2, t2));
441
442
points3dMat = points3dMat.reshape(3, 1);
443
points2dMat = points2dMat.reshape(2, 1);
444
EXPECT_TRUE(solvePnPRansac(points3dMat, points2dMat, intrinsics, cv::Mat(), R3, t3));
445
446
points3dMat = points3dMat.reshape(1, numPoints);
447
points2dMat = points2dMat.reshape(1, numPoints);
448
EXPECT_TRUE(solvePnPRansac(points3dMat, points2dMat, intrinsics, cv::Mat(), R4, t4));
449
450
EXPECT_LE(cvtest::norm(R1, R2, NORM_INF), 1e-6);
451
EXPECT_LE(cvtest::norm(t1, t2, NORM_INF), 1e-6);
452
EXPECT_LE(cvtest::norm(R1, R3, NORM_INF), 1e-6);
453
EXPECT_LE(cvtest::norm(t1, t3, NORM_INF), 1e-6);
454
EXPECT_LE(cvtest::norm(R1, R4, NORM_INF), 1e-6);
455
EXPECT_LE(cvtest::norm(t1, t4, NORM_INF), 1e-6);
456
}
457
458
TEST(Calib3d_SolvePnP, double_support)
459
{
460
Matx33d intrinsics(5.4794130238156129e+002, 0., 2.9835545700043139e+002, 0.,
461
5.4817724002728005e+002, 2.3062194051986233e+002, 0., 0., 1.);
462
std::vector<cv::Point3d> points3d;
463
std::vector<cv::Point2d> points2d;
464
std::vector<cv::Point3f> points3dF;
465
std::vector<cv::Point2f> points2dF;
466
for (int i = 0; i < 10 ; i+=2)
467
{
468
points3d.push_back(cv::Point3d(5+i, 3, 2));
469
points3dF.push_back(cv::Point3d(5+i, 3, 2));
470
points3d.push_back(cv::Point3d(5+i, 3+i, 2+i));
471
points3dF.push_back(cv::Point3d(5+i, 3+i, 2+i));
472
points2d.push_back(cv::Point2d(0, i));
473
points2dF.push_back(cv::Point2d(0, i));
474
points2d.push_back(cv::Point2d(-i, i));
475
points2dF.push_back(cv::Point2d(-i, i));
476
}
477
Mat R,t, RF, tF;
478
vector<int> inliers;
479
480
solvePnPRansac(points3dF, points2dF, intrinsics, cv::Mat(), RF, tF, true, 100, 8.f, 0.999, inliers, cv::SOLVEPNP_P3P);
481
solvePnPRansac(points3d, points2d, intrinsics, cv::Mat(), R, t, true, 100, 8.f, 0.999, inliers, cv::SOLVEPNP_P3P);
482
483
EXPECT_LE(cvtest::norm(R, Mat_<double>(RF), NORM_INF), 1e-3);
484
EXPECT_LE(cvtest::norm(t, Mat_<double>(tF), NORM_INF), 1e-3);
485
}
486
487
TEST(Calib3d_SolvePnP, translation)
488
{
489
Mat cameraIntrinsic = Mat::eye(3,3, CV_32FC1);
490
vector<float> crvec;
491
crvec.push_back(0.f);
492
crvec.push_back(0.f);
493
crvec.push_back(0.f);
494
vector<float> ctvec;
495
ctvec.push_back(100.f);
496
ctvec.push_back(100.f);
497
ctvec.push_back(0.f);
498
vector<Point3f> p3d;
499
p3d.push_back(Point3f(0,0,0));
500
p3d.push_back(Point3f(0,0,10));
501
p3d.push_back(Point3f(0,10,10));
502
p3d.push_back(Point3f(10,10,10));
503
p3d.push_back(Point3f(2,5,5));
504
505
vector<Point2f> p2d;
506
projectPoints(p3d, crvec, ctvec, cameraIntrinsic, noArray(), p2d);
507
Mat rvec;
508
Mat tvec;
509
rvec =(Mat_<float>(3,1) << 0, 0, 0);
510
tvec = (Mat_<float>(3,1) << 100, 100, 0);
511
512
solvePnP(p3d, p2d, cameraIntrinsic, noArray(), rvec, tvec, true);
513
EXPECT_TRUE(checkRange(rvec));
514
EXPECT_TRUE(checkRange(tvec));
515
516
rvec =(Mat_<double>(3,1) << 0, 0, 0);
517
tvec = (Mat_<double>(3,1) << 100, 100, 0);
518
solvePnP(p3d, p2d, cameraIntrinsic, noArray(), rvec, tvec, true);
519
EXPECT_TRUE(checkRange(rvec));
520
EXPECT_TRUE(checkRange(tvec));
521
522
solvePnP(p3d, p2d, cameraIntrinsic, noArray(), rvec, tvec, false);
523
EXPECT_TRUE(checkRange(rvec));
524
EXPECT_TRUE(checkRange(tvec));
525
}
526
527
TEST(Calib3d_SolvePnP, iterativeInitialGuess3pts)
528
{
529
{
530
Matx33d intrinsics(605.4, 0.0, 317.35,
531
0.0, 601.2, 242.63,
532
0.0, 0.0, 1.0);
533
534
double L = 0.1;
535
vector<Point3d> p3d;
536
p3d.push_back(Point3d(-L, -L, 0.0));
537
p3d.push_back(Point3d(L, -L, 0.0));
538
p3d.push_back(Point3d(L, L, 0.0));
539
540
Mat rvec_ground_truth = (Mat_<double>(3,1) << 0.3, -0.2, 0.75);
541
Mat tvec_ground_truth = (Mat_<double>(3,1) << 0.15, -0.2, 1.5);
542
543
vector<Point2d> p2d;
544
projectPoints(p3d, rvec_ground_truth, tvec_ground_truth, intrinsics, noArray(), p2d);
545
546
Mat rvec_est = (Mat_<double>(3,1) << 0.2, -0.1, 0.6);
547
Mat tvec_est = (Mat_<double>(3,1) << 0.05, -0.05, 1.0);
548
549
solvePnP(p3d, p2d, intrinsics, noArray(), rvec_est, tvec_est, true, SOLVEPNP_ITERATIVE);
550
551
std::cout << "rvec_ground_truth: " << rvec_ground_truth.t() << std::endl;
552
std::cout << "rvec_est: " << rvec_est.t() << std::endl;
553
std::cout << "tvec_ground_truth: " << tvec_ground_truth.t() << std::endl;
554
std::cout << "tvec_est: " << tvec_est.t() << std::endl;
555
556
EXPECT_LE(cvtest::norm(rvec_ground_truth, rvec_est, NORM_INF), 1e-6);
557
EXPECT_LE(cvtest::norm(tvec_ground_truth, tvec_est, NORM_INF), 1e-6);
558
}
559
560
{
561
Matx33f intrinsics(605.4f, 0.0f, 317.35f,
562
0.0f, 601.2f, 242.63f,
563
0.0f, 0.0f, 1.0f);
564
565
float L = 0.1f;
566
vector<Point3f> p3d;
567
p3d.push_back(Point3f(-L, -L, 0.0f));
568
p3d.push_back(Point3f(L, -L, 0.0f));
569
p3d.push_back(Point3f(L, L, 0.0f));
570
571
Mat rvec_ground_truth = (Mat_<float>(3,1) << -0.75f, 0.4f, 0.34f);
572
Mat tvec_ground_truth = (Mat_<float>(3,1) << -0.15f, 0.35f, 1.58f);
573
574
vector<Point2f> p2d;
575
projectPoints(p3d, rvec_ground_truth, tvec_ground_truth, intrinsics, noArray(), p2d);
576
577
Mat rvec_est = (Mat_<float>(3,1) << -0.5f, 0.2f, 0.2f);
578
Mat tvec_est = (Mat_<float>(3,1) << 0.0f, 0.2f, 1.0f);
579
580
solvePnP(p3d, p2d, intrinsics, noArray(), rvec_est, tvec_est, true, SOLVEPNP_ITERATIVE);
581
582
std::cout << "rvec_ground_truth: " << rvec_ground_truth.t() << std::endl;
583
std::cout << "rvec_est: " << rvec_est.t() << std::endl;
584
std::cout << "tvec_ground_truth: " << tvec_ground_truth.t() << std::endl;
585
std::cout << "tvec_est: " << tvec_est.t() << std::endl;
586
587
EXPECT_LE(cvtest::norm(rvec_ground_truth, rvec_est, NORM_INF), 1e-6);
588
EXPECT_LE(cvtest::norm(tvec_ground_truth, tvec_est, NORM_INF), 1e-6);
589
}
590
}
591
592
}} // namespace
593
594