forked from MasteringOpenCV/code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindCameraMatrices.cpp
executable file
·457 lines (396 loc) · 14.9 KB
/
FindCameraMatrices.cpp
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/*****************************************************************************
* ExploringSfMWithOpenCV
******************************************************************************
* by Roy Shilkrot, 5th Dec 2012
* http://www.morethantechnical.com/
******************************************************************************
* Ch4 of the book "Mastering OpenCV with Practical Computer Vision Projects"
* Copyright Packt Publishing 2012.
* http://www.packtpub.com/cool-projects-with-opencv/book
*****************************************************************************/
#include "FindCameraMatrices.h"
#include "Triangulation.h"
#include <vector>
#include <iostream>
#include <opencv2/calib3d/calib3d.hpp>
using namespace cv;
using namespace std;
#ifdef USE_EIGEN
#include <Eigen/Eigen>
#endif
#define DECOMPOSE_SVD
#ifndef CV_PCA_DATA_AS_ROW
#define CV_PCA_DATA_AS_ROW 0
#endif
void DecomposeEssentialUsingHorn90(double _E[9], double _R1[9], double _R2[9], double _t1[3], double _t2[3]) {
//from : http://people.csail.mit.edu/bkph/articles/Essential.pdf
#ifdef USE_EIGEN
using namespace Eigen;
Matrix3d E = Map<Matrix<double,3,3,RowMajor> >(_E);
Matrix3d EEt = E * E.transpose();
Vector3d e0e1 = E.col(0).cross(E.col(1)),e1e2 = E.col(1).cross(E.col(2)),e2e0 = E.col(2).cross(E.col(2));
Vector3d b1,b2;
#if 1
//Method 1
Matrix3d bbt = 0.5 * EEt.trace() * Matrix3d::Identity() - EEt; //Horn90 (12)
Vector3d bbt_diag = bbt.diagonal();
if (bbt_diag(0) > bbt_diag(1) && bbt_diag(0) > bbt_diag(2)) {
b1 = bbt.row(0) / sqrt(bbt_diag(0));
b2 = -b1;
} else if (bbt_diag(1) > bbt_diag(0) && bbt_diag(1) > bbt_diag(2)) {
b1 = bbt.row(1) / sqrt(bbt_diag(1));
b2 = -b1;
} else {
b1 = bbt.row(2) / sqrt(bbt_diag(2));
b2 = -b1;
}
#else
//Method 2
if (e0e1.norm() > e1e2.norm() && e0e1.norm() > e2e0.norm()) {
b1 = e0e1.normalized() * sqrt(0.5 * EEt.trace()); //Horn90 (18)
b2 = -b1;
} else if (e1e2.norm() > e0e1.norm() && e1e2.norm() > e2e0.norm()) {
b1 = e1e2.normalized() * sqrt(0.5 * EEt.trace()); //Horn90 (18)
b2 = -b1;
} else {
b1 = e2e0.normalized() * sqrt(0.5 * EEt.trace()); //Horn90 (18)
b2 = -b1;
}
#endif
//Horn90 (19)
Matrix3d cofactors; cofactors.col(0) = e1e2; cofactors.col(1) = e2e0; cofactors.col(2) = e0e1;
cofactors.transposeInPlace();
//B = [b]_x , see Horn90 (6) and http://en.wikipedia.org/wiki/Cross_product#Conversion_to_matrix_multiplication
Matrix3d B1; B1 << 0,-b1(2),b1(1),
b1(2),0,-b1(0),
-b1(1),b1(0),0;
Matrix3d B2; B2 << 0,-b2(2),b2(1),
b2(2),0,-b2(0),
-b2(1),b2(0),0;
Map<Matrix<double,3,3,RowMajor> > R1(_R1),R2(_R2);
//Horn90 (24)
R1 = (cofactors.transpose() - B1*E) / b1.dot(b1);
R2 = (cofactors.transpose() - B2*E) / b2.dot(b2);
Map<Vector3d> t1(_t1),t2(_t2);
t1 = b1; t2 = b2;
cout << "Horn90 provided " << endl << R1 << endl << "and" << endl << R2 << endl;
#endif
}
bool CheckCoherentRotation(cv::Mat_<double>& R) {
if(fabsf(determinant(R))-1.0 > 1e-07) {
cerr << "det(R) != +-1.0, this is not a rotation matrix" << endl;
return false;
}
return true;
}
Mat GetFundamentalMat(const vector<KeyPoint>& imgpts1,
const vector<KeyPoint>& imgpts2,
vector<KeyPoint>& imgpts1_good,
vector<KeyPoint>& imgpts2_good,
vector<DMatch>& matches
#ifdef __SFM__DEBUG__
,const Mat& img_1,
const Mat& img_2
#endif
)
{
//Try to eliminate keypoints based on the fundamental matrix
//(although this is not the proper way to do this)
vector<uchar> status(imgpts1.size());
#ifdef __SFM__DEBUG__
std::vector< DMatch > good_matches_;
std::vector<KeyPoint> keypoints_1, keypoints_2;
#endif
// undistortPoints(imgpts1, imgpts1, cam_matrix, distortion_coeff);
// undistortPoints(imgpts2, imgpts2, cam_matrix, distortion_coeff);
//
imgpts1_good.clear(); imgpts2_good.clear();
vector<KeyPoint> imgpts1_tmp;
vector<KeyPoint> imgpts2_tmp;
if (matches.size() <= 0) {
//points already aligned...
imgpts1_tmp = imgpts1;
imgpts2_tmp = imgpts2;
} else {
GetAlignedPointsFromMatch(imgpts1, imgpts2, matches, imgpts1_tmp, imgpts2_tmp);
}
Mat F;
{
vector<Point2f> pts1,pts2;
KeyPointsToPoints(imgpts1_tmp, pts1);
KeyPointsToPoints(imgpts2_tmp, pts2);
#ifdef __SFM__DEBUG__
cout << "pts1 " << pts1.size() << " (orig pts " << imgpts1_tmp.size() << ")" << endl;
cout << "pts2 " << pts2.size() << " (orig pts " << imgpts2_tmp.size() << ")" << endl;
#endif
double minVal,maxVal;
cv::minMaxIdx(pts1,&minVal,&maxVal);
F = findFundamentalMat(pts1, pts2, FM_RANSAC, 0.006 * maxVal, 0.99, status); //threshold from [Snavely07 4.1]
}
vector<DMatch> new_matches;
cout << "F keeping " << countNonZero(status) << " / " << status.size() << endl;
for (unsigned int i=0; i<status.size(); i++) {
if (status[i])
{
imgpts1_good.push_back(imgpts1_tmp[i]);
imgpts2_good.push_back(imgpts2_tmp[i]);
if (matches.size() <= 0) { //points already aligned...
new_matches.push_back(DMatch(matches[i].queryIdx,matches[i].trainIdx,matches[i].distance));
} else {
new_matches.push_back(matches[i]);
}
#ifdef __SFM__DEBUG__
good_matches_.push_back(DMatch(imgpts1_good.size()-1,imgpts1_good.size()-1,1.0));
keypoints_1.push_back(imgpts1_tmp[i]);
keypoints_2.push_back(imgpts2_tmp[i]);
#endif
}
}
cout << matches.size() << " matches before, " << new_matches.size() << " new matches after Fundamental Matrix\n";
matches = new_matches; //keep only those points who survived the fundamental matrix
#if 0
//-- Draw only "good" matches
#ifdef __SFM__DEBUG__
if(!img_1.empty() && !img_2.empty()) {
vector<Point2f> i_pts,j_pts;
Mat img_orig_matches;
{ //draw original features in red
vector<uchar> vstatus(imgpts1_tmp.size(),1);
vector<float> verror(imgpts1_tmp.size(),1.0);
img_1.copyTo(img_orig_matches);
KeyPointsToPoints(imgpts1_tmp, i_pts);
KeyPointsToPoints(imgpts2_tmp, j_pts);
drawArrows(img_orig_matches, i_pts, j_pts, vstatus, verror, Scalar(0,0,255));
}
{ //superimpose filtered features in green
vector<uchar> vstatus(imgpts1_good.size(),1);
vector<float> verror(imgpts1_good.size(),1.0);
i_pts.resize(imgpts1_good.size());
j_pts.resize(imgpts2_good.size());
KeyPointsToPoints(imgpts1_good, i_pts);
KeyPointsToPoints(imgpts2_good, j_pts);
drawArrows(img_orig_matches, i_pts, j_pts, vstatus, verror, Scalar(0,255,0));
imshow( "Filtered Matches", img_orig_matches );
}
int c = waitKey(0);
if (c=='s') {
imwrite("fundamental_mat_matches.png", img_orig_matches);
}
destroyWindow("Filtered Matches");
}
#endif
#endif
return F;
}
void TakeSVDOfE(Mat_<double>& E, Mat& svd_u, Mat& svd_vt, Mat& svd_w) {
#if 1
//Using OpenCV's SVD
SVD svd(E,SVD::MODIFY_A);
svd_u = svd.u;
svd_vt = svd.vt;
svd_w = svd.w;
#else
//Using Eigen's SVD
cout << "Eigen3 SVD..\n";
Eigen::Matrix3f e = Eigen::Map<Eigen::Matrix<double,3,3,Eigen::RowMajor> >((double*)E.data).cast<float>();
Eigen::JacobiSVD<Eigen::MatrixXf> svd(e, Eigen::ComputeThinU | Eigen::ComputeThinV);
Eigen::MatrixXf Esvd_u = svd.matrixU();
Eigen::MatrixXf Esvd_v = svd.matrixV();
svd_u = (Mat_<double>(3,3) << Esvd_u(0,0), Esvd_u(0,1), Esvd_u(0,2),
Esvd_u(1,0), Esvd_u(1,1), Esvd_u(1,2),
Esvd_u(2,0), Esvd_u(2,1), Esvd_u(2,2));
Mat_<double> svd_v = (Mat_<double>(3,3) << Esvd_v(0,0), Esvd_v(0,1), Esvd_v(0,2),
Esvd_v(1,0), Esvd_v(1,1), Esvd_v(1,2),
Esvd_v(2,0), Esvd_v(2,1), Esvd_v(2,2));
svd_vt = svd_v.t();
svd_w = (Mat_<double>(1,3) << svd.singularValues()[0] , svd.singularValues()[1] , svd.singularValues()[2]);
#endif
cout << "----------------------- SVD ------------------------\n";
cout << "U:\n"<<svd_u<<"\nW:\n"<<svd_w<<"\nVt:\n"<<svd_vt<<endl;
cout << "----------------------------------------------------\n";
}
bool TestTriangulation(const vector<CloudPoint>& pcloud, const Matx34d& P, vector<uchar>& status) {
vector<Point3d> pcloud_pt3d = CloudPointsToPoints(pcloud);
vector<Point3d> pcloud_pt3d_projected(pcloud_pt3d.size());
Matx44d P4x4 = Matx44d::eye();
for(int i=0;i<12;i++) P4x4.val[i] = P.val[i];
perspectiveTransform(pcloud_pt3d, pcloud_pt3d_projected, P4x4);
status.resize(pcloud.size(),0);
for (int i=0; i<pcloud.size(); i++) {
status[i] = (pcloud_pt3d_projected[i].z > 0) ? 1 : 0;
}
int count = countNonZero(status);
double percentage = ((double)count / (double)pcloud.size());
cout << count << "/" << pcloud.size() << " = " << percentage*100.0 << "% are in front of camera" << endl;
if(percentage < 0.75)
return false; //less than 75% of the points are in front of the camera
//check for coplanarity of points
if(false) //not
{
cv::Mat_<double> cldm(pcloud.size(),3);
for(unsigned int i=0;i<pcloud.size();i++) {
cldm.row(i)(0) = pcloud[i].pt.x;
cldm.row(i)(1) = pcloud[i].pt.y;
cldm.row(i)(2) = pcloud[i].pt.z;
}
cv::Mat_<double> mean;
cv::PCA pca(cldm,mean,CV_PCA_DATA_AS_ROW);
int num_inliers = 0;
cv::Vec3d nrm = pca.eigenvectors.row(2); nrm = nrm / norm(nrm);
cv::Vec3d x0 = pca.mean;
double p_to_plane_thresh = sqrt(pca.eigenvalues.at<double>(2));
for (int i=0; i<pcloud.size(); i++) {
Vec3d w = Vec3d(pcloud[i].pt) - x0;
double D = fabs(nrm.dot(w));
if(D < p_to_plane_thresh) num_inliers++;
}
cout << num_inliers << "/" << pcloud.size() << " are coplanar" << endl;
if((double)num_inliers / (double)(pcloud.size()) > 0.85)
return false;
}
return true;
}
bool DecomposeEtoRandT(
Mat_<double>& E,
Mat_<double>& R1,
Mat_<double>& R2,
Mat_<double>& t1,
Mat_<double>& t2)
{
#ifdef DECOMPOSE_SVD
//Using HZ E decomposition
Mat svd_u, svd_vt, svd_w;
TakeSVDOfE(E,svd_u,svd_vt,svd_w);
//check if first and second singular values are the same (as they should be)
double singular_values_ratio = fabsf(svd_w.at<double>(0) / svd_w.at<double>(1));
if(singular_values_ratio>1.0) singular_values_ratio = 1.0/singular_values_ratio; // flip ratio to keep it [0,1]
if (singular_values_ratio < 0.7) {
cout << "singular values are too far apart\n";
return false;
}
Matx33d W(0,-1,0, //HZ 9.13
1,0,0,
0,0,1);
Matx33d Wt(0,1,0,
-1,0,0,
0,0,1);
R1 = svd_u * Mat(W) * svd_vt; //HZ 9.19
R2 = svd_u * Mat(Wt) * svd_vt; //HZ 9.19
t1 = svd_u.col(2); //u3
t2 = -svd_u.col(2); //u3
#else
//Using Horn E decomposition
DecomposeEssentialUsingHorn90(E[0],R1[0],R2[0],t1[0],t2[0]);
#endif
return true;
}
bool FindCameraMatrices(const Mat& K,
const Mat& Kinv,
const Mat& distcoeff,
const vector<KeyPoint>& imgpts1,
const vector<KeyPoint>& imgpts2,
vector<KeyPoint>& imgpts1_good,
vector<KeyPoint>& imgpts2_good,
Matx34d& P,
Matx34d& P1,
vector<DMatch>& matches,
vector<CloudPoint>& outCloud
#ifdef __SFM__DEBUG__
,const Mat& img_1,
const Mat& img_2
#endif
)
{
//Find camera matrices
{
cout << "Find camera matrices...";
double t = getTickCount();
Mat F = GetFundamentalMat(imgpts1,imgpts2,imgpts1_good,imgpts2_good,matches
#ifdef __SFM__DEBUG__
,img_1,img_2
#endif
);
if(matches.size() < 100) { // || ((double)imgpts1_good.size() / (double)imgpts1.size()) < 0.25
cerr << "not enough inliers after F matrix" << endl;
return false;
}
//Essential matrix: compute then extract cameras [R|t]
Mat_<double> E = K.t() * F * K; //according to HZ (9.12)
//according to http://en.wikipedia.org/wiki/Essential_matrix#Properties_of_the_essential_matrix
if(fabsf(determinant(E)) > 1e-07) {
cout << "det(E) != 0 : " << determinant(E) << "\n";
P1 = 0;
return false;
}
Mat_<double> R1(3,3);
Mat_<double> R2(3,3);
Mat_<double> t1(1,3);
Mat_<double> t2(1,3);
//decompose E to P' , HZ (9.19)
{
if (!DecomposeEtoRandT(E,R1,R2,t1,t2)) return false;
if(determinant(R1)+1.0 < 1e-09) {
//according to http://en.wikipedia.org/wiki/Essential_matrix#Showing_that_it_is_valid
cout << "det(R) == -1 ["<<determinant(R1)<<"]: flip E's sign" << endl;
E = -E;
DecomposeEtoRandT(E,R1,R2,t1,t2);
}
if (!CheckCoherentRotation(R1)) {
cout << "resulting rotation is not coherent\n";
P1 = 0;
return false;
}
P1 = Matx34d(R1(0,0), R1(0,1), R1(0,2), t1(0),
R1(1,0), R1(1,1), R1(1,2), t1(1),
R1(2,0), R1(2,1), R1(2,2), t1(2));
cout << "Testing P1 " << endl << Mat(P1) << endl;
vector<CloudPoint> pcloud,pcloud1; vector<KeyPoint> corresp;
double reproj_error1 = TriangulatePoints(imgpts1_good, imgpts2_good, K, Kinv, distcoeff, P, P1, pcloud, corresp);
double reproj_error2 = TriangulatePoints(imgpts2_good, imgpts1_good, K, Kinv, distcoeff, P1, P, pcloud1, corresp);
vector<uchar> tmp_status;
//check if pointa are triangulated --in front-- of cameras for all 4 ambiguations
if (!TestTriangulation(pcloud,P1,tmp_status) || !TestTriangulation(pcloud1,P,tmp_status) || reproj_error1 > 100.0 || reproj_error2 > 100.0) {
P1 = Matx34d(R1(0,0), R1(0,1), R1(0,2), t2(0),
R1(1,0), R1(1,1), R1(1,2), t2(1),
R1(2,0), R1(2,1), R1(2,2), t2(2));
cout << "Testing P1 "<< endl << Mat(P1) << endl;
pcloud.clear(); pcloud1.clear(); corresp.clear();
reproj_error1 = TriangulatePoints(imgpts1_good, imgpts2_good, K, Kinv, distcoeff, P, P1, pcloud, corresp);
reproj_error2 = TriangulatePoints(imgpts2_good, imgpts1_good, K, Kinv, distcoeff, P1, P, pcloud1, corresp);
if (!TestTriangulation(pcloud,P1,tmp_status) || !TestTriangulation(pcloud1,P,tmp_status) || reproj_error1 > 100.0 || reproj_error2 > 100.0) {
if (!CheckCoherentRotation(R2)) {
cout << "resulting rotation is not coherent\n";
P1 = 0;
return false;
}
P1 = Matx34d(R2(0,0), R2(0,1), R2(0,2), t1(0),
R2(1,0), R2(1,1), R2(1,2), t1(1),
R2(2,0), R2(2,1), R2(2,2), t1(2));
cout << "Testing P1 "<< endl << Mat(P1) << endl;
pcloud.clear(); pcloud1.clear(); corresp.clear();
reproj_error1 = TriangulatePoints(imgpts1_good, imgpts2_good, K, Kinv, distcoeff, P, P1, pcloud, corresp);
reproj_error2 = TriangulatePoints(imgpts2_good, imgpts1_good, K, Kinv, distcoeff, P1, P, pcloud1, corresp);
if (!TestTriangulation(pcloud,P1,tmp_status) || !TestTriangulation(pcloud1,P,tmp_status) || reproj_error1 > 100.0 || reproj_error2 > 100.0) {
P1 = Matx34d(R2(0,0), R2(0,1), R2(0,2), t2(0),
R2(1,0), R2(1,1), R2(1,2), t2(1),
R2(2,0), R2(2,1), R2(2,2), t2(2));
cout << "Testing P1 "<< endl << Mat(P1) << endl;
pcloud.clear(); pcloud1.clear(); corresp.clear();
reproj_error1 = TriangulatePoints(imgpts1_good, imgpts2_good, K, Kinv, distcoeff, P, P1, pcloud, corresp);
reproj_error2 = TriangulatePoints(imgpts2_good, imgpts1_good, K, Kinv, distcoeff, P1, P, pcloud1, corresp);
if (!TestTriangulation(pcloud,P1,tmp_status) || !TestTriangulation(pcloud1,P,tmp_status) || reproj_error1 > 100.0 || reproj_error2 > 100.0) {
cout << "Shit." << endl;
return false;
}
}
}
}
for (unsigned int i=0; i<pcloud.size(); i++) {
outCloud.push_back(pcloud[i]);
}
}
t = ((double)getTickCount() - t)/getTickFrequency();
cout << "Done. (" << t <<"s)"<< endl;
}
return true;
}