-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTestSolvePnp.cpp
More file actions
383 lines (315 loc) · 13.1 KB
/
TestSolvePnp.cpp
File metadata and controls
383 lines (315 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#include <opencv2/core/core.hpp>
#include <opencv2/calib3d/calib3d.hpp>
#include <iostream>
#include <iomanip>
#include "epnp/epnp.h"
//
// Utility functions
//
void printMat(const std::string & name, const cv::Mat & mat)
{
std::cout << name << std::endl;
const int precision = 4;
for(int i=0; i<mat.size().height; i++)
{
std::cout << "[";
for(int j=0; j<mat.size().width; j++)
{
std::cout << std::setprecision(precision) << mat.at<double>(i,j);
if(j != mat.size().width-1)
std::cout << ", ";
else
std::cout << "]" << std::endl;
}
}
}
cv::Point2f ImageCoordsToIdealCameraCoords(const cv::Mat_<double> & cameraIntrinsicParams, const cv::Point2f & pt)
{
return cv::Point2f(
( pt.x - cameraIntrinsicParams.at<double>(0,2) ) / cameraIntrinsicParams.at<double>(0,0),
( pt.y - cameraIntrinsicParams.at<double>(1,2) ) / cameraIntrinsicParams.at<double>(1,1) );
}
cv::Point2f IdealCameraCoordsToImageCoords(const cv::Mat_<double> & cameraIntrinsicParams, const cv::Point2f & pt)
{
return cv::Point2f(
pt.x * cameraIntrinsicParams.at<double>(0,0) + cameraIntrinsicParams.at<double>(0,2),
pt.y * cameraIntrinsicParams.at<double>(1,1) + cameraIntrinsicParams.at<double>(1,2) );
}
//cv::undistortPoints is quite tricky by default : it takes image coords as input and return ideal camera coords !
//(the opencv documentation does not agree with the source code here...)
cv::Point2f UnDistortPoint_ImageCoords(const cv::Point2f & pt, const cv::Mat_<double> & cameraIntrinsicParams, const std::vector<double> & distCoeffs)
{
std::vector<cv::Point2f> src, dst;
src.push_back(pt);
cv::fisheye::undistortPoints(src, dst, cameraIntrinsicParams, distCoeffs);
cv::Point2f pt_undistorted = IdealCameraCoordsToImageCoords(cameraIntrinsicParams, dst[0]);
return pt_undistorted;
}
std::vector<cv::Point2f> UnDistortPoints_ImageCoords(const std::vector<cv::Point2f> & points, const cv::Mat_<double> & cameraIntrinsicParams, const std::vector<double> & distCoeffs)
{
std::vector<cv::Point2f> points_ideal_undistorted(points.size());
cv::fisheye::undistortPoints(points, points_ideal_undistorted, cameraIntrinsicParams, distCoeffs);
std::vector<cv::Point2f> points_undistorted;
for (const auto & pt_ideal_undistorted : points_ideal_undistorted)
points_undistorted.push_back( IdealCameraCoordsToImageCoords(cameraIntrinsicParams, pt_ideal_undistorted) );
return points_undistorted;
}
//
// <MySolvePnpEpnp> : tries reproduces the expected behavior of solvePnp with the original source code of the Epnp library
//
void MySolvePnpEpnp(
const std::vector<cv::Point3f> &objectPoints,
const std::vector<cv::Point2f> &imagePoints,
const cv::Mat_<double> &cameraIntrinsicParams,
const std::vector<double> &distCoeffs,
cv::Mat_<double> &outRotationEstimated,
cv::Mat_<double> &outTranslationEstimated)
{
std::vector<cv::Point2f> imagePoints_undistorted = UnDistortPoints_ImageCoords(imagePoints, cameraIntrinsicParams, distCoeffs);
epnp epnpCaller;
epnpCaller.set_internal_parameters(
cameraIntrinsicParams.at<double>(0, 2),
cameraIntrinsicParams.at<double>(1, 2),
cameraIntrinsicParams.at<double>(0, 0),
cameraIntrinsicParams.at<double>(1, 1)
);
epnpCaller.set_maximum_number_of_correspondences(objectPoints.size());
epnpCaller.reset_correspondences();
for (size_t i = 0; i < objectPoints.size(); i++)
{
epnpCaller.add_correspondence(
objectPoints[i].x, objectPoints[i].y, objectPoints[i].z,
imagePoints_undistorted[i].x, imagePoints_undistorted[i].y
);
}
double rotationArray[3][3];
double translationArray[3];
epnpCaller.compute_pose(rotationArray, translationArray);
cv::Mat_<double> rotation3x3(cv::Size(3, 3));
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
rotation3x3(j,i) = rotationArray[j][i];
outRotationEstimated = cv::Mat_<double>(cv::Size(3, 1));
outTranslationEstimated = cv::Mat_<double>(cv::Size(1, 3));
for (int j = 0; j < 3; j++)
outTranslationEstimated(j, 0) = translationArray[j];
cv::Rodrigues(rotation3x3, outRotationEstimated);
}
// </MySolvePnpEpnp>
//
// MySolvePnpPosit : adapter for cvPOSIT with a C++ prototype close to solvePnp
//
namespace
{
//cvPOSIT wrapper
void Posit_IdealCameraCoords(const std::vector<cv::Point3f> & objectPoints, const std::vector<cv::Point2f> & imagePoints,
cv::Mat_<double> &outRotationEstimated, cv::Mat_<double> & outTranslationEstimated)
{
CvPoint2D32f * imagePoints_c = (CvPoint2D32f *) malloc(sizeof(CvPoint2D32f) * imagePoints.size());
{
for (size_t i = 0; i < imagePoints.size(); i++)
imagePoints_c[i] = cvPoint2D32f(imagePoints[i].x, imagePoints[i].y);
}
CvPoint3D32f * objectPoints_c = (CvPoint3D32f *) malloc(sizeof(CvPoint3D32f) * objectPoints.size());
{
for (size_t i = 0; i < objectPoints.size(); i++)
objectPoints_c[i] = cvPoint3D32f(objectPoints[i].x, objectPoints[i].y, objectPoints[i].z);
}
CvPOSITObject * positObject = cvCreatePOSITObject(objectPoints_c, objectPoints.size() );
CvTermCriteria criteria;
criteria.type = CV_TERMCRIT_EPS|CV_TERMCRIT_ITER;
criteria.epsilon = 0.00000000000000010;
criteria.max_iter = 30;
//criteria.epsilon = 0.10;
//criteria.max_iter = 6;
float positTranslationArray[3];
float positRotationArray[9];
const double idealFocal = 1.;
cvPOSIT(positObject, imagePoints_c,
idealFocal, criteria,
positRotationArray, positTranslationArray);
cv::Mat_<double> positRotationMat1x3;
{
cv::Mat_<double> positRotationMat3x3(cv::Size(3, 3));
{
int idx = 0;
for (int j = 0; j < 3; j++)
{
for (int i = 0; i < 3; i++)
{
positRotationMat3x3(j, i) = (double)positRotationArray[idx++];
}
}
}
cv::Rodrigues(positRotationMat3x3, positRotationMat1x3);
}
outRotationEstimated = positRotationMat1x3;
outTranslationEstimated = cv::Mat_<double>(cv::Size(1, 3));
for (int i = 0; i < 3; i++)
outTranslationEstimated.at<double>(i, 0) = (double)positTranslationArray[i];
cvReleasePOSITObject(&positObject);
free(imagePoints_c);
free(objectPoints_c);
}
}
// MySolvePnpPosit implementation
void MySolvePnpPosit(const std::vector<cv::Point3f> &objectPoints, const std::vector<cv::Point2f> &imagePoints,
const cv::Mat_<double> &cameraIntrinsicParams, const std::vector<double> &distorsionCoeffs,
cv::Mat_<double> &outRotationEstimated, cv::Mat_<double> &outTranslationEstimated)
{
std::vector<cv::Point2f> imagePoints_IdealCameraCoords_undistorted;
cv::fisheye::undistortPoints(imagePoints, imagePoints_IdealCameraCoords_undistorted, cameraIntrinsicParams, distorsionCoeffs);
Posit_IdealCameraCoords(objectPoints, imagePoints_IdealCameraCoords_undistorted, outRotationEstimated, outTranslationEstimated);
}
void TestSolvePnp()
{
enum SolvePnpStrategy
{
Strategy_MySolvePnp_Epnp,
Strategy_MySolvePnpPosit,
Strategy_solvePnp_P3p,
Strategy_solvePnp_Iterative_InitialGuess,
Strategy_solvePnp_Epnp
};
SolvePnpStrategy strategy = Strategy_MySolvePnp_Epnp;
//SolvePnpStrategy strategy = Strategy_MySolvePnpPosit;
//SolvePnpStrategy strategy = Strategy_solvePnp_P3p;
//SolvePnpStrategy strategy = Strategy_solvePnp_Iterative_InitialGuess;
//SolvePnpStrategy strategy = Strategy_solvePnp_Epnp;
std::vector<cv::Point3f> objectPoints;
// Based on my experimentations
// The order of the 3d points and image points *does* matter
// It has to be adapted depending upon the strategy !
// With opencv's SOLVEPNP_EPNP the error can go down to 23.03 pixel with the following order.
//the order of the points does matter
if (strategy == Strategy_solvePnp_Epnp)
{
objectPoints.push_back(cv::Point3f(-62.1225319f, 15.7540569f, 0.819464564f));
objectPoints.push_back(cv::Point3f(62.3174629f, 15.7940502f, 0.819983721f));
objectPoints.push_back(cv::Point3f(-0.372639507f, 16.4230633f, -36.5060043f));
objectPoints.push_back(cv::Point3f(0.f, 0.f, 0.f));
}
// With MySolvePnpEpnp (an home baked adapter of epnp using the epnp library source code),
// the error is about 6.742 pixels, and the order *is* important
// It is strange that this "rewrite" gives different results
else if (strategy == Strategy_MySolvePnp_Epnp)
{
objectPoints.push_back(cv::Point3f(-62.1225319f, 15.7540569f, 0.819464564f));
objectPoints.push_back(cv::Point3f(62.3174629f, 15.7940502f, 0.819983721f));
objectPoints.push_back(cv::Point3f(0.f, 0.f, 0.f));
objectPoints.push_back(cv::Point3f(-0.372639507f, 16.4230633f, -36.5060043f));
}
// With MySolvePnpPosit, the error is about 4.911 pixels
// and the order *is* important (in other cases the reprojection error is about 1278 pixels !)
else if (strategy == Strategy_MySolvePnpPosit)
{
objectPoints.push_back(cv::Point3f(0.f, 0.f, 0.f));
objectPoints.push_back(cv::Point3f(-62.1225319f, 15.7540569f, 0.819464564f));
objectPoints.push_back(cv::Point3f(62.3174629f, 15.7940502f, 0.819983721f));
objectPoints.push_back(cv::Point3f(-0.372639507f, 16.4230633f, -36.5060043f));
}
// With solvePnp_P3p (cv::SOLVEPNP_P3P) the error is about 0.02961 pixels and the order does not matter much
else if (strategy == Strategy_solvePnp_P3p)
{
objectPoints.push_back(cv::Point3f(0.f, 0.f, 0.f));
objectPoints.push_back(cv::Point3f(-62.1225319f, 15.7540569f, 0.819464564f));
objectPoints.push_back(cv::Point3f(62.3174629f, 15.7940502f, 0.819983721f));
objectPoints.push_back(cv::Point3f(-0.372639507f, 16.4230633f, -36.5060043f));
}
// With solvePnp_P3p (cv::SOLVEPNP_ITERATIVE) the error can be 0 pixels
// *if a good initial extrinsic guess is given* (otherwise don't hope for any convergence)
// the order does not matter much
else if (strategy == Strategy_solvePnp_Iterative_InitialGuess)
{
objectPoints.push_back(cv::Point3f(0.f, 0.f, 0.f));
objectPoints.push_back(cv::Point3f(-62.1225319f, 15.7540569f, 0.819464564f));
objectPoints.push_back(cv::Point3f(62.3174629f, 15.7940502f, 0.819983721f));
objectPoints.push_back(cv::Point3f(-0.372639507f, 16.4230633f, -36.5060043f));
}
cv::Mat_<double> cameraIntrinsicParams(cv::Size(3, 3));
cameraIntrinsicParams = 0.;
cameraIntrinsicParams(0, 0) = 3844.4400000000001f;
cameraIntrinsicParams(1, 1) = 3841.0599999999999f;
cameraIntrinsicParams(0, 2) = 640.f;
cameraIntrinsicParams(1, 2) = 380.f;
cameraIntrinsicParams(2, 2) = 1.f;
std::vector<double> distCoeffs(4);
distCoeffs[0] = -0.063500002026557922;
distCoeffs[1] = -2.5915000438690186;
distCoeffs[2] = -0.0023300000466406345;
distCoeffs[3] = 0.0008411200251430273;
cv::Mat_<double> rotation(cv::Size(1, 3));
rotation(0,0) = 0.07015543380659847f;
rotation(0,1) = 0.06922079477774973f;
rotation(0,2) = -0.00254676088325f;
cv::Mat_<double> translation(cv::Size(1, 3));
translation(0,0) = -35.3236f;
translation(0,1) = -48.1699f;
translation(0,2) = 769.068f;
std::vector<cv::Point2f> imagePoints;
cv::projectPoints(objectPoints, rotation, translation, cameraIntrinsicParams, distCoeffs, imagePoints);
cv::Mat_<double> rotation2(cv::Size(1, 3));
cv::Mat_<double> translation2(cv::Size(1, 3));
rotation2.setTo(0.);
translation2.setTo(0.);
translation2(2, 0) = 600.; //Hint for SOLVEPNP_ITERATIVE
switch(strategy)
{
case Strategy_MySolvePnp_Epnp:
MySolvePnpEpnp(
objectPoints,
imagePoints,
cameraIntrinsicParams,
distCoeffs,
rotation2,
translation2);
break;
case Strategy_MySolvePnpPosit:
MySolvePnpPosit(
objectPoints,
imagePoints,
cameraIntrinsicParams,
distCoeffs,
rotation2,
translation2);
break;
case Strategy_solvePnp_P3p:
cv::solvePnP(objectPoints, imagePoints,
cameraIntrinsicParams, distCoeffs,
rotation2, translation2,
false,//useExtrinsicGuess
cv::SOLVEPNP_P3P
);
break;
case Strategy_solvePnp_Iterative_InitialGuess:
cv::solvePnP(objectPoints, imagePoints,
cameraIntrinsicParams, distCoeffs,
rotation2, translation2,
true,//useExtrinsicGuess
cv::SOLVEPNP_ITERATIVE
);
break;
case Strategy_solvePnp_Epnp:
cv::solvePnP(objectPoints, imagePoints,
cameraIntrinsicParams, distCoeffs,
rotation2, translation2,
true,//useExtrinsicGuess
cv::SOLVEPNP_EPNP
);
break;
}
std::vector<cv::Point2f> imagePoints_Reproj(3);
cv::projectPoints(objectPoints, rotation2, translation2, cameraIntrinsicParams, distCoeffs, imagePoints_Reproj);
float sum = 0.;
for (size_t i = 0; i < imagePoints.size(); i++)
sum += cv::norm(imagePoints_Reproj[i] - imagePoints[i]);
printMat("rotation", rotation);
printMat("rotation2", rotation2);
printMat("translation", translation);
printMat("translation2", translation2);
std::cout << "Reproj Error=" << sum << std::endl;
}
int main(int argc, char **argv)
{
TestSolvePnp();
}