Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/calib3d/src/solvepnp.cpp
16354 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 "precomp.hpp"
44
#include "upnp.h"
45
#include "dls.h"
46
#include "epnp.h"
47
#include "p3p.h"
48
#include "ap3p.h"
49
#include "opencv2/calib3d/calib3d_c.h"
50
51
#include <iostream>
52
53
namespace cv
54
{
55
56
bool solvePnP( InputArray _opoints, InputArray _ipoints,
57
InputArray _cameraMatrix, InputArray _distCoeffs,
58
OutputArray _rvec, OutputArray _tvec, bool useExtrinsicGuess, int flags )
59
{
60
CV_INSTRUMENT_REGION();
61
62
Mat opoints = _opoints.getMat(), ipoints = _ipoints.getMat();
63
int npoints = std::max(opoints.checkVector(3, CV_32F), opoints.checkVector(3, CV_64F));
64
CV_Assert( ( (npoints >= 4) || (npoints == 3 && flags == SOLVEPNP_ITERATIVE && useExtrinsicGuess) )
65
&& npoints == std::max(ipoints.checkVector(2, CV_32F), ipoints.checkVector(2, CV_64F)) );
66
67
Mat rvec, tvec;
68
if( flags != SOLVEPNP_ITERATIVE )
69
useExtrinsicGuess = false;
70
71
if( useExtrinsicGuess )
72
{
73
int rtype = _rvec.type(), ttype = _tvec.type();
74
Size rsize = _rvec.size(), tsize = _tvec.size();
75
CV_Assert( (rtype == CV_32F || rtype == CV_64F) &&
76
(ttype == CV_32F || ttype == CV_64F) );
77
CV_Assert( (rsize == Size(1, 3) || rsize == Size(3, 1)) &&
78
(tsize == Size(1, 3) || tsize == Size(3, 1)) );
79
}
80
else
81
{
82
int mtype = CV_64F;
83
// use CV_32F if all PnP inputs are CV_32F and outputs are empty
84
if (_ipoints.depth() == _cameraMatrix.depth() && _ipoints.depth() == _opoints.depth() &&
85
_rvec.empty() && _tvec.empty())
86
mtype = _opoints.depth();
87
88
_rvec.create(3, 1, mtype);
89
_tvec.create(3, 1, mtype);
90
}
91
rvec = _rvec.getMat();
92
tvec = _tvec.getMat();
93
94
Mat cameraMatrix0 = _cameraMatrix.getMat();
95
Mat distCoeffs0 = _distCoeffs.getMat();
96
Mat cameraMatrix = Mat_<double>(cameraMatrix0);
97
Mat distCoeffs = Mat_<double>(distCoeffs0);
98
bool result = false;
99
100
if (flags == SOLVEPNP_EPNP || flags == SOLVEPNP_DLS || flags == SOLVEPNP_UPNP)
101
{
102
Mat undistortedPoints;
103
undistortPoints(ipoints, undistortedPoints, cameraMatrix, distCoeffs);
104
epnp PnP(cameraMatrix, opoints, undistortedPoints);
105
106
Mat R;
107
PnP.compute_pose(R, tvec);
108
Rodrigues(R, rvec);
109
result = true;
110
}
111
else if (flags == SOLVEPNP_P3P)
112
{
113
CV_Assert( npoints == 4);
114
Mat undistortedPoints;
115
undistortPoints(ipoints, undistortedPoints, cameraMatrix, distCoeffs);
116
p3p P3Psolver(cameraMatrix);
117
118
Mat R;
119
result = P3Psolver.solve(R, tvec, opoints, undistortedPoints);
120
if (result)
121
Rodrigues(R, rvec);
122
}
123
else if (flags == SOLVEPNP_AP3P)
124
{
125
CV_Assert( npoints == 4);
126
Mat undistortedPoints;
127
undistortPoints(ipoints, undistortedPoints, cameraMatrix, distCoeffs);
128
ap3p P3Psolver(cameraMatrix);
129
130
Mat R;
131
result = P3Psolver.solve(R, tvec, opoints, undistortedPoints);
132
if (result)
133
Rodrigues(R, rvec);
134
}
135
else if (flags == SOLVEPNP_ITERATIVE)
136
{
137
CvMat c_objectPoints = cvMat(opoints), c_imagePoints = cvMat(ipoints);
138
CvMat c_cameraMatrix = cvMat(cameraMatrix), c_distCoeffs = cvMat(distCoeffs);
139
CvMat c_rvec = cvMat(rvec), c_tvec = cvMat(tvec);
140
cvFindExtrinsicCameraParams2(&c_objectPoints, &c_imagePoints, &c_cameraMatrix,
141
(c_distCoeffs.rows && c_distCoeffs.cols) ? &c_distCoeffs : 0,
142
&c_rvec, &c_tvec, useExtrinsicGuess );
143
result = true;
144
}
145
/*else if (flags == SOLVEPNP_DLS)
146
{
147
Mat undistortedPoints;
148
undistortPoints(ipoints, undistortedPoints, cameraMatrix, distCoeffs);
149
150
dls PnP(opoints, undistortedPoints);
151
152
Mat R, rvec = _rvec.getMat(), tvec = _tvec.getMat();
153
bool result = PnP.compute_pose(R, tvec);
154
if (result)
155
Rodrigues(R, rvec);
156
return result;
157
}
158
else if (flags == SOLVEPNP_UPNP)
159
{
160
upnp PnP(cameraMatrix, opoints, ipoints);
161
162
Mat R, rvec = _rvec.getMat(), tvec = _tvec.getMat();
163
PnP.compute_pose(R, tvec);
164
Rodrigues(R, rvec);
165
return true;
166
}*/
167
else
168
CV_Error(CV_StsBadArg, "The flags argument must be one of SOLVEPNP_ITERATIVE, SOLVEPNP_P3P, SOLVEPNP_EPNP or SOLVEPNP_DLS");
169
return result;
170
}
171
172
class PnPRansacCallback CV_FINAL : public PointSetRegistrator::Callback
173
{
174
175
public:
176
177
PnPRansacCallback(Mat _cameraMatrix=Mat(3,3,CV_64F), Mat _distCoeffs=Mat(4,1,CV_64F), int _flags=SOLVEPNP_ITERATIVE,
178
bool _useExtrinsicGuess=false, Mat _rvec=Mat(), Mat _tvec=Mat() )
179
: cameraMatrix(_cameraMatrix), distCoeffs(_distCoeffs), flags(_flags), useExtrinsicGuess(_useExtrinsicGuess),
180
rvec(_rvec), tvec(_tvec) {}
181
182
/* Pre: True */
183
/* Post: compute _model with given points and return number of found models */
184
int runKernel( InputArray _m1, InputArray _m2, OutputArray _model ) const CV_OVERRIDE
185
{
186
Mat opoints = _m1.getMat(), ipoints = _m2.getMat();
187
188
bool correspondence = solvePnP( _m1, _m2, cameraMatrix, distCoeffs,
189
rvec, tvec, useExtrinsicGuess, flags );
190
191
Mat _local_model;
192
hconcat(rvec, tvec, _local_model);
193
_local_model.copyTo(_model);
194
195
return correspondence;
196
}
197
198
/* Pre: True */
199
/* Post: fill _err with projection errors */
200
void computeError( InputArray _m1, InputArray _m2, InputArray _model, OutputArray _err ) const CV_OVERRIDE
201
{
202
203
Mat opoints = _m1.getMat(), ipoints = _m2.getMat(), model = _model.getMat();
204
205
int i, count = opoints.checkVector(3);
206
Mat _rvec = model.col(0);
207
Mat _tvec = model.col(1);
208
209
210
Mat projpoints(count, 2, CV_32FC1);
211
projectPoints(opoints, _rvec, _tvec, cameraMatrix, distCoeffs, projpoints);
212
213
const Point2f* ipoints_ptr = ipoints.ptr<Point2f>();
214
const Point2f* projpoints_ptr = projpoints.ptr<Point2f>();
215
216
_err.create(count, 1, CV_32FC1);
217
float* err = _err.getMat().ptr<float>();
218
219
for ( i = 0; i < count; ++i)
220
err[i] = (float)norm( Matx21f(ipoints_ptr[i] - projpoints_ptr[i]), NORM_L2SQR );
221
222
}
223
224
225
Mat cameraMatrix;
226
Mat distCoeffs;
227
int flags;
228
bool useExtrinsicGuess;
229
Mat rvec;
230
Mat tvec;
231
};
232
233
bool solvePnPRansac(InputArray _opoints, InputArray _ipoints,
234
InputArray _cameraMatrix, InputArray _distCoeffs,
235
OutputArray _rvec, OutputArray _tvec, bool useExtrinsicGuess,
236
int iterationsCount, float reprojectionError, double confidence,
237
OutputArray _inliers, int flags)
238
{
239
CV_INSTRUMENT_REGION();
240
241
Mat opoints0 = _opoints.getMat(), ipoints0 = _ipoints.getMat();
242
Mat opoints, ipoints;
243
if( opoints0.depth() == CV_64F || !opoints0.isContinuous() )
244
opoints0.convertTo(opoints, CV_32F);
245
else
246
opoints = opoints0;
247
if( ipoints0.depth() == CV_64F || !ipoints0.isContinuous() )
248
ipoints0.convertTo(ipoints, CV_32F);
249
else
250
ipoints = ipoints0;
251
252
int npoints = std::max(opoints.checkVector(3, CV_32F), opoints.checkVector(3, CV_64F));
253
CV_Assert( npoints >= 4 && npoints == std::max(ipoints.checkVector(2, CV_32F), ipoints.checkVector(2, CV_64F)) );
254
255
CV_Assert(opoints.isContinuous());
256
CV_Assert(opoints.depth() == CV_32F || opoints.depth() == CV_64F);
257
CV_Assert((opoints.rows == 1 && opoints.channels() == 3) || opoints.cols*opoints.channels() == 3);
258
CV_Assert(ipoints.isContinuous());
259
CV_Assert(ipoints.depth() == CV_32F || ipoints.depth() == CV_64F);
260
CV_Assert((ipoints.rows == 1 && ipoints.channels() == 2) || ipoints.cols*ipoints.channels() == 2);
261
262
_rvec.create(3, 1, CV_64FC1);
263
_tvec.create(3, 1, CV_64FC1);
264
265
Mat rvec = useExtrinsicGuess ? _rvec.getMat() : Mat(3, 1, CV_64FC1);
266
Mat tvec = useExtrinsicGuess ? _tvec.getMat() : Mat(3, 1, CV_64FC1);
267
Mat cameraMatrix = _cameraMatrix.getMat(), distCoeffs = _distCoeffs.getMat();
268
269
int model_points = 5;
270
int ransac_kernel_method = SOLVEPNP_EPNP;
271
272
if( flags == SOLVEPNP_P3P || flags == SOLVEPNP_AP3P)
273
{
274
model_points = 4;
275
ransac_kernel_method = flags;
276
}
277
else if( npoints == 4 )
278
{
279
model_points = 4;
280
ransac_kernel_method = SOLVEPNP_P3P;
281
}
282
283
if( model_points == npoints )
284
{
285
bool result = solvePnP(opoints, ipoints, cameraMatrix, distCoeffs, _rvec, _tvec, useExtrinsicGuess, ransac_kernel_method);
286
287
if(!result)
288
{
289
if( _inliers.needed() )
290
_inliers.release();
291
292
return false;
293
}
294
295
if(_inliers.needed())
296
{
297
_inliers.create(npoints, 1, CV_32S);
298
Mat _local_inliers = _inliers.getMat();
299
for(int i = 0; i < npoints; i++)
300
{
301
_local_inliers.at<int>(i) = i;
302
}
303
}
304
305
return true;
306
}
307
308
Ptr<PointSetRegistrator::Callback> cb; // pointer to callback
309
cb = makePtr<PnPRansacCallback>( cameraMatrix, distCoeffs, ransac_kernel_method, useExtrinsicGuess, rvec, tvec);
310
311
double param1 = reprojectionError; // reprojection error
312
double param2 = confidence; // confidence
313
int param3 = iterationsCount; // number maximum iterations
314
315
Mat _local_model(3, 2, CV_64FC1);
316
Mat _mask_local_inliers(1, opoints.rows, CV_8UC1);
317
318
// call Ransac
319
int result = createRANSACPointSetRegistrator(cb, model_points,
320
param1, param2, param3)->run(opoints, ipoints, _local_model, _mask_local_inliers);
321
322
if( result <= 0 || _local_model.rows <= 0)
323
{
324
_rvec.assign(rvec); // output rotation vector
325
_tvec.assign(tvec); // output translation vector
326
327
if( _inliers.needed() )
328
_inliers.release();
329
330
return false;
331
}
332
333
vector<Point3d> opoints_inliers;
334
vector<Point2d> ipoints_inliers;
335
opoints = opoints.reshape(3);
336
ipoints = ipoints.reshape(2);
337
opoints.convertTo(opoints_inliers, CV_64F);
338
ipoints.convertTo(ipoints_inliers, CV_64F);
339
340
const uchar* mask = _mask_local_inliers.ptr<uchar>();
341
int npoints1 = compressElems(&opoints_inliers[0], mask, 1, npoints);
342
compressElems(&ipoints_inliers[0], mask, 1, npoints);
343
344
opoints_inliers.resize(npoints1);
345
ipoints_inliers.resize(npoints1);
346
result = solvePnP(opoints_inliers, ipoints_inliers, cameraMatrix,
347
distCoeffs, rvec, tvec, useExtrinsicGuess,
348
(flags == SOLVEPNP_P3P || flags == SOLVEPNP_AP3P) ? SOLVEPNP_EPNP : flags) ? 1 : -1;
349
350
if( result <= 0 )
351
{
352
_rvec.assign(_local_model.col(0)); // output rotation vector
353
_tvec.assign(_local_model.col(1)); // output translation vector
354
355
if( _inliers.needed() )
356
_inliers.release();
357
358
return false;
359
}
360
else
361
{
362
_rvec.assign(rvec); // output rotation vector
363
_tvec.assign(tvec); // output translation vector
364
}
365
366
if(_inliers.needed())
367
{
368
Mat _local_inliers;
369
for (int i = 0; i < npoints; ++i)
370
{
371
if((int)_mask_local_inliers.at<uchar>(i) != 0) // inliers mask
372
_local_inliers.push_back(i); // output inliers vector
373
}
374
_local_inliers.copyTo(_inliers);
375
}
376
return true;
377
}
378
379
int solveP3P( InputArray _opoints, InputArray _ipoints,
380
InputArray _cameraMatrix, InputArray _distCoeffs,
381
OutputArrayOfArrays _rvecs, OutputArrayOfArrays _tvecs, int flags) {
382
CV_INSTRUMENT_REGION();
383
384
Mat opoints = _opoints.getMat(), ipoints = _ipoints.getMat();
385
int npoints = std::max(opoints.checkVector(3, CV_32F), opoints.checkVector(3, CV_64F));
386
CV_Assert( npoints == 3 && npoints == std::max(ipoints.checkVector(2, CV_32F), ipoints.checkVector(2, CV_64F)) );
387
CV_Assert( flags == SOLVEPNP_P3P || flags == SOLVEPNP_AP3P );
388
389
Mat cameraMatrix0 = _cameraMatrix.getMat();
390
Mat distCoeffs0 = _distCoeffs.getMat();
391
Mat cameraMatrix = Mat_<double>(cameraMatrix0);
392
Mat distCoeffs = Mat_<double>(distCoeffs0);
393
394
Mat undistortedPoints;
395
undistortPoints(ipoints, undistortedPoints, cameraMatrix, distCoeffs);
396
std::vector<Mat> Rs, ts;
397
398
int solutions = 0;
399
if (flags == SOLVEPNP_P3P)
400
{
401
p3p P3Psolver(cameraMatrix);
402
solutions = P3Psolver.solve(Rs, ts, opoints, undistortedPoints);
403
}
404
else if (flags == SOLVEPNP_AP3P)
405
{
406
ap3p P3Psolver(cameraMatrix);
407
solutions = P3Psolver.solve(Rs, ts, opoints, undistortedPoints);
408
}
409
410
if (solutions == 0) {
411
return 0;
412
}
413
414
if (_rvecs.needed()) {
415
_rvecs.create(solutions, 1, CV_64F);
416
}
417
418
if (_tvecs.needed()) {
419
_tvecs.create(solutions, 1, CV_64F);
420
}
421
422
for (int i = 0; i < solutions; i++) {
423
Mat rvec;
424
Rodrigues(Rs[i], rvec);
425
_tvecs.getMatRef(i) = ts[i];
426
_rvecs.getMatRef(i) = rvec;
427
}
428
429
return solutions;
430
}
431
432
}
433
434