-
Notifications
You must be signed in to change notification settings - Fork 0
/
multi_view_geometry.cpp
838 lines (658 loc) · 24.6 KB
/
multi_view_geometry.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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
/**
* This file is part of OV²SLAM.
*
* Copyright (C) 2020 ONERA
*
* For more information see <https://github.com/ov2slam/ov2slam>
*
* OV²SLAM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OV²SLAM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OV²SLAM. If not, see <https://www.gnu.org/licenses/>.
*
* Authors: Maxime Ferrera <maxime.ferrera at gmail dot com> (ONERA, DTIS - IVA),
* Alexandre Eudes <first.last at onera dot fr> (ONERA, DTIS - IVA),
* Julien Moras <first.last at onera dot fr> (ONERA, DTIS - IVA),
* Martial Sanfourche <first.last at onera dot fr> (ONERA, DTIS - IVA)
*/
#ifdef USE_OPENGV
#include <opengv/types.hpp>
#include <opengv/triangulation/methods.hpp>
#include <opengv/sac/Ransac.hpp>
#include <opengv/sac/Lmeds.hpp>
#include <opengv/absolute_pose/methods.hpp>
#include <opengv/absolute_pose/CentralAbsoluteAdapter.hpp>
#include <opengv/sac_problems/absolute_pose/AbsolutePoseSacProblem.hpp>
#include <opengv/relative_pose/methods.hpp>
#include <opengv/relative_pose/CentralRelativeAdapter.hpp>
#include <opengv/sac_problems/relative_pose/CentralRelativePoseSacProblem.hpp>
#endif
#include "multi_view_geometry.hpp"
#include "ceres_parametrization.hpp"
#include <opencv2/core/eigen.hpp>
#include <opencv2/calib3d.hpp>
// Triangulation methods
// =====================
// Generic
Eigen::Vector3d MultiViewGeometry::triangulate(const Sophus::SE3d &Tlr,
const Eigen::Vector3d &bvl, const Eigen::Vector3d &bvr)
{
#ifdef USE_OPENGV
return opengvTriangulate2(Tlr, bvl, bvr);
#else
return opencvTriangulate(Tlr, bvl, bvr);
#endif
}
// OpenGV based
#ifdef USE_OPENGV
Eigen::Vector3d MultiViewGeometry::opengvTriangulate1(const Sophus::SE3d &Tlr,
const Eigen::Vector3d &bvl, const Eigen::Vector3d &bvr)
{
opengv::bearingVectors_t bv1(1,bvl);
opengv::bearingVectors_t bv2(1,bvr);
opengv::rotation_t R12 = Tlr.rotationMatrix();
opengv::translation_t t12 = Tlr.translation();
opengv::relative_pose::CentralRelativeAdapter
adapter(bv1, bv2, t12, R12);
opengv::point_t pt =
opengv::triangulation::triangulate(adapter, 0);
return pt;
}
Eigen::Vector3d MultiViewGeometry::opengvTriangulate2(const Sophus::SE3d &Tlr,
const Eigen::Vector3d &bvl, const Eigen::Vector3d &bvr)
{
opengv::bearingVectors_t bv1(1,bvl);
opengv::bearingVectors_t bv2(1,bvr);
opengv::rotation_t R12 = Tlr.rotationMatrix();
opengv::translation_t t12 = Tlr.translation();
opengv::relative_pose::CentralRelativeAdapter
adapter(bv1, bv2, t12, R12);
opengv::point_t pt =
opengv::triangulation::triangulate2(adapter, 0);
return pt;
}
#endif
// OpenCV based
Eigen::Vector3d MultiViewGeometry::opencvTriangulate(const Sophus::SE3d &Tlr,
const Eigen::Vector3d &bvl, const Eigen::Vector3d &bvr)
{
std::vector<cv::Point2f> lpt, rpt;
lpt.push_back( cv::Point2f(bvl.x()/bvl.z(), bvl.y()/bvl.z()) );
rpt.push_back( cv::Point2f(bvr.x()/bvr.z(), bvr.y()/bvr.z()) );
cv::Matx34f P0 = cv::Matx34f(1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0);
Sophus::SE3d Tcw = Tlr.inverse();
Eigen::Matrix3d R = Tcw.rotationMatrix();
Eigen::Vector3d t = Tcw.translation();
cv::Matx34f P1 = cv::Matx34f(R(0, 0), R(0, 1), R(0, 2), t(0),
R(1, 0), R(1, 1), R(1, 2), t(1),
R(2, 0), R(2, 1), R(2, 2), t(2));
cv::Mat campt;
cv::triangulatePoints(P0, P1, lpt, rpt, campt);
if( campt.col(0).at<float>(3) != 1. ) {
campt.col(0) /= campt.col(0).at<float>(3);
}
Eigen::Vector3d pt(
campt.col(0).at<float>(0),
campt.col(0).at<float>(1),
campt.col(0).at<float>(2)
);
return pt;
}
// Triangulation methods
// =====================
// Generic
bool MultiViewGeometry::p3pRansac(
const std::vector<Eigen::Vector3d, Eigen::aligned_allocator<Eigen::Vector3d> > &bvs,
const std::vector<Eigen::Vector3d, Eigen::aligned_allocator<Eigen::Vector3d> > &vwpts,
const int nmaxiter, const float errth, const bool boptimize, const bool bdorandom,
const float fx, const float fy, Sophus::SE3d &Twc, std::vector<int> &voutliersidx,
bool use_lmeds)
{
#ifdef USE_OPENGV
if( use_lmeds ) {
return opengvP3PLMeds(bvs, vwpts, nmaxiter, errth,
boptimize, bdorandom, fx, fy, Twc, voutliersidx);
} else {
return opengvP3PRansac(bvs, vwpts, nmaxiter, errth,
boptimize, bdorandom, fx, fy, Twc, voutliersidx);
}
#else
return opencvP3PRansac(bvs, vwpts, nmaxiter, errth,
fx, fy, boptimize, Twc, voutliersidx);
#endif
}
// OpenGV based
#ifdef USE_OPENGV
bool MultiViewGeometry::opengvP3PRansac(
const std::vector<Eigen::Vector3d, Eigen::aligned_allocator<Eigen::Vector3d> > &bvs,
const std::vector<Eigen::Vector3d, Eigen::aligned_allocator<Eigen::Vector3d> > &vwpts,
const int nmaxiter, const float errth, const bool boptimize, const bool bdorandom,
const float fx, const float fy, Sophus::SE3d &Twc, std::vector<int> &voutliersidx)
{
assert( bvs.size() == vwpts.size() );
size_t nb3dpts = bvs.size();
if( nb3dpts < 4 ) {
return false;
}
voutliersidx.reserve(nb3dpts);
opengv::bearingVectors_t gvbvs;
opengv::points_t gvwpt;
gvbvs.reserve(nb3dpts);
gvwpt.reserve(nb3dpts);
for( size_t i = 0 ; i < nb3dpts ; i++ )
{
gvbvs.push_back(bvs.at(i));
gvwpt.push_back(vwpts.at(i));
}
opengv::absolute_pose::CentralAbsoluteAdapter
adapter(gvbvs, gvwpt);
//Create an AbsolutePoseSac problem and Ransac
//The method can be set to KNEIP, GAO or EPNP
opengv::sac::Ransac<opengv::sac_problems::absolute_pose::AbsolutePoseSacProblem> ransac;
std::shared_ptr<
opengv::sac_problems::absolute_pose::AbsolutePoseSacProblem> absposeproblem_ptr(
new opengv::sac_problems::absolute_pose::AbsolutePoseSacProblem(
adapter,
opengv::sac_problems::absolute_pose::AbsolutePoseSacProblem::KNEIP,
bdorandom));
float focal = fx + fy;
focal /= 2.;
ransac.sac_model_ = absposeproblem_ptr;
ransac.threshold_ = (1.0 - cos(atan(errth/focal)));
ransac.max_iterations_ = nmaxiter;
// Computing the pose from P3P
ransac.computeModel(0);
// If no solution found, return false
if( ransac.inliers_.size() < 5 ) {
return false;
}
// Might happen apparently...
if( !Sophus::isOrthogonal(ransac.model_coefficients_.block<3,3>(0,0)) )
return false;
// Optimize the computed pose with inliers only
opengv::transformation_t T_opt;
if( boptimize ) {
ransac.sac_model_->optimizeModelCoefficients(ransac.inliers_, ransac.model_coefficients_, T_opt);
Twc.translation() = T_opt.rightCols(1);
Twc.setRotationMatrix(T_opt.leftCols(3));
} else {
Twc.translation() = ransac.model_coefficients_.rightCols(1);
Twc.setRotationMatrix(ransac.model_coefficients_.leftCols(3));
}
size_t k = 0;
for( size_t i = 0 ; i < nb3dpts ; i++ ) {
if( ransac.inliers_.at(k) == (int)i ) {
k++;
if( k == ransac.inliers_.size() ) {
k = 0;
}
} else {
voutliersidx.push_back(i);
}
}
return true;
}
bool MultiViewGeometry::opengvP3PLMeds(
const std::vector<Eigen::Vector3d, Eigen::aligned_allocator<Eigen::Vector3d> > &bvs,
const std::vector<Eigen::Vector3d, Eigen::aligned_allocator<Eigen::Vector3d> > &vwpts,
const int nmaxiter, const float errth, const bool boptimize, const bool bdorandom,
const float fx, const float fy, Sophus::SE3d &Twc, std::vector<int> &voutliersidx)
{
assert( bvs.size() == vwpts.size() );
size_t nb3dpts = bvs.size();
if( nb3dpts < 4 ) {
return false;
}
voutliersidx.reserve(nb3dpts);
opengv::bearingVectors_t gvbvs;
opengv::points_t gvwpt;
gvbvs.reserve(nb3dpts);
gvwpt.reserve(nb3dpts);
for( size_t i = 0 ; i < nb3dpts ; i++ )
{
gvbvs.push_back(bvs.at(i));
gvwpt.push_back(vwpts.at(i));
}
opengv::absolute_pose::CentralAbsoluteAdapter
adapter(gvbvs, gvwpt);
//Create an AbsolutePoseSac problem and Ransac
//The method can be set to KNEIP, GAO or EPNP
opengv::sac::Lmeds<opengv::sac_problems::absolute_pose::AbsolutePoseSacProblem> ransac;
std::shared_ptr<
opengv::sac_problems::absolute_pose::AbsolutePoseSacProblem> absposeproblem_ptr(
new opengv::sac_problems::absolute_pose::AbsolutePoseSacProblem(
adapter,
opengv::sac_problems::absolute_pose::AbsolutePoseSacProblem::KNEIP,
bdorandom));
float focal = fx + fy;
focal /= 2.;
ransac.sac_model_ = absposeproblem_ptr;
ransac.threshold_ = (1.0 - cos(atan(errth/focal)));
ransac.max_iterations_ = nmaxiter;
// Computing the pose from P3P
ransac.computeModel(0);
// If no solution found, return false
if( ransac.inliers_.size() < 5 ) {
return false;
}
// Might happen apparently...
if( !Sophus::isOrthogonal(ransac.model_coefficients_.block<3,3>(0,0)) )
return false;
// Optimize the computed pose with inliers only
opengv::transformation_t T_opt;
if( boptimize ) {
ransac.sac_model_->optimizeModelCoefficients(ransac.inliers_, ransac.model_coefficients_, T_opt);
Twc.translation() = T_opt.rightCols(1);
Twc.setRotationMatrix(T_opt.leftCols(3));
} else {
Twc.translation() = ransac.model_coefficients_.rightCols(1);
Twc.setRotationMatrix(ransac.model_coefficients_.leftCols(3));
}
size_t k = 0;
for( size_t i = 0 ; i < nb3dpts ; i++ ) {
if( ransac.inliers_.at(k) == (int)i ) {
k++;
if( k == ransac.inliers_.size() ) {
k = 0;
}
} else {
voutliersidx.push_back(i);
}
}
return true;
}
#endif
// OpenCV based
bool MultiViewGeometry::opencvP3PRansac(
const std::vector<Eigen::Vector3d, Eigen::aligned_allocator<Eigen::Vector3d> > &bvs,
const std::vector<Eigen::Vector3d, Eigen::aligned_allocator<Eigen::Vector3d> > &vwpts,
const int nmaxiter, const float errth, const float fx, const float fy,
const bool boptimize, Sophus::SE3d &Twc, std::vector<int> &voutliersidx)
{
assert( bvs.size() == vwpts.size() );
size_t nb3dpts = bvs.size();
if( nb3dpts < 4 ) {
return false;
}
voutliersidx.reserve(nb3dpts);
std::vector<cv::Point2f> cvbvs;
cvbvs.reserve(nb3dpts);
std::vector<cv::Point3f> cvwpts;
cvwpts.reserve(nb3dpts);
for( size_t i=0 ; i < nb3dpts ; i++ )
{
cvbvs.push_back(
cv::Point2f(
bvs.at(i).x()/bvs.at(i).z(),
bvs.at(i).y()/bvs.at(i).z()
)
);
cvwpts.push_back(
cv::Point3f(
vwpts.at(i).x(),
vwpts.at(i).y(),
vwpts.at(i).z()
)
);
}
// Using homoegeneous pts here so no dist or calib.
cv::Mat D;
cv::Mat K = cv::Mat::eye(3,3,CV_32F);
cv::Mat tvec, rvec;
cv::Mat inliers;
bool use_extrinsic_guess = false;
float confidence = 0.99;
float focal = (fx + fy) / 2.;
cv::solvePnPRansac(
cvwpts,
cvbvs,
K,
D,
rvec,
tvec,
use_extrinsic_guess,
nmaxiter,
errth / focal,
confidence,
inliers,
cv::SOLVEPNP_P3P
);
if( inliers.rows == 0 ) {
return false;
}
int k = 0;
for( size_t i = 0 ; i < nb3dpts ; i++ ) {
if( inliers.at<int>(k) == (int)i ) {
k++;
if( k == inliers.rows ) {
k = 0;
}
} else {
voutliersidx.push_back(i);
}
}
if( voutliersidx.size() >= nb3dpts-5 ) {
return false;
}
if( boptimize ) {
use_extrinsic_guess = true;
// Filter outliers
std::vector<cv::Point2f> in_cvbvs;
in_cvbvs.reserve(inliers.rows);
std::vector<cv::Point3f> in_cvwpts;
in_cvwpts.reserve(inliers.rows);
for( int i=0 ; i < inliers.rows ; i++ )
{
in_cvbvs.push_back( cvbvs.at(inliers.at<int>(i)) );
in_cvwpts.push_back( cvwpts.at(inliers.at<int>(i)) );
}
cv::solvePnP(
in_cvwpts,
in_cvbvs,
K,
D,
rvec,
tvec,
use_extrinsic_guess,
cv::SOLVEPNP_ITERATIVE
);
}
// Store the resulting pose
cv::Mat cvR;
cv::Rodrigues(rvec, cvR);
Eigen::Vector3d tcw;
Eigen::Matrix3d Rcw;
cv::cv2eigen(cvR, Rcw);
cv::cv2eigen(tvec, tcw);
Twc.translation() = -1. * Rcw.transpose() * tcw;
Twc.setRotationMatrix( Rcw.transpose() );
return true;
}
// Ceres Based
bool MultiViewGeometry::ceresPnP(
const std::vector<Eigen::Vector2d, Eigen::aligned_allocator<Eigen::Vector2d> > &vunkps,
const std::vector<Eigen::Vector3d, Eigen::aligned_allocator<Eigen::Vector3d> > &vwpts,
Sophus::SE3d &Twc, const int nmaxiter, const float chi2th, const bool buse_robust,
const bool bapply_l2_after_robust, const float fx, const float fy,
const float cx, const float cy, std::vector<int> &voutliersidx)
{
std::vector<int> vscales(vunkps.size(), 0);
return ceresPnP(vunkps, vwpts, vscales, Twc, nmaxiter, chi2th, buse_robust, bapply_l2_after_robust, fx, fy, cx, cy, voutliersidx);
}
bool MultiViewGeometry::ceresPnP(
const std::vector<Eigen::Vector2d, Eigen::aligned_allocator<Eigen::Vector2d> > &vunkps,
const std::vector<Eigen::Vector3d, Eigen::aligned_allocator<Eigen::Vector3d> > &vwpts,
const std::vector<int> &vscales, Sophus::SE3d &Twc, const int nmaxiter,
const float chi2th, const bool buse_robust, const bool bapply_l2_after_robust,
const float fx, const float fy, const float cx, const float cy,
std::vector<int> &voutliersidx)
{
assert( vunkps.size() == vwpts.size() );
ceres::Problem problem;
double chi2thrtsq = std::sqrt(chi2th);
ceres::LossFunctionWrapper *loss_function;
loss_function = new ceres::LossFunctionWrapper(new ceres::HuberLoss(chi2thrtsq), ceres::TAKE_OWNERSHIP);
if( !buse_robust ) {
loss_function->Reset(NULL, ceres::TAKE_OWNERSHIP);
}
size_t nbkps = vunkps.size();
ceres::LocalParameterization *local_parameterization = new SE3LeftParameterization();
PoseParametersBlock posepar = PoseParametersBlock(0, Twc);
problem.AddParameterBlock(posepar.values(), 7, local_parameterization);
std::vector<DirectLeftSE3::ReprojectionErrorSE3*> verrors_;
std::vector<ceres::ResidualBlockId> vrids_;
for( size_t i = 0 ; i < nbkps ; i++ )
{
DirectLeftSE3::ReprojectionErrorSE3 *f =
new DirectLeftSE3::ReprojectionErrorSE3(
vunkps[i].x(), vunkps[i].y(),
fx, fy, cx, cy, vwpts.at(i),
std::pow(2.,vscales[i]));
ceres::ResidualBlockId rid = problem.AddResidualBlock(f, loss_function, posepar.values());
verrors_.push_back(f);
vrids_.push_back(rid);
}
ceres::Solver::Options options;
options.linear_solver_type = ceres::DENSE_QR;
// options.linear_solver_type = ceres::DENSE_SCHUR;
// options.linear_solver_type = ceres::SPARSE_NORMAL_CHOLESKY;
options.trust_region_strategy_type = ceres::LEVENBERG_MARQUARDT;
options.num_threads = 1;
options.max_num_iterations = nmaxiter;
options.max_solver_time_in_seconds = 0.005;
options.function_tolerance = 1.e-3;
options.minimizer_progress_to_stdout = false;
ceres::Solver::Summary summary;
ceres::Solve(options, &problem, &summary);
// std::cout << summary.BriefReport() << std::endl;
// std::cout << "\n Prev trans : " << twc.transpose();
size_t nbbad = 0;
for( size_t i = 0 ; i < nbkps ; i++ )
{
auto err = verrors_.at(i);
if( err->chi2err_ > chi2th || !err->isdepthpositive_ )
{
if( bapply_l2_after_robust ) {
auto rid = vrids_.at(i);
problem.RemoveResidualBlock(rid);
}
voutliersidx.push_back(i);
nbbad++;
}
}
if( nbbad == nbkps ) {
return false;
}
if( bapply_l2_after_robust && !voutliersidx.empty() ) {
loss_function->Reset(NULL, ceres::TAKE_OWNERSHIP);
ceres::Solve(options, &problem, &summary);
// std::cout << summary.BriefReport() << std::endl;
}
Twc = posepar.getPose();
return summary.IsSolutionUsable();
}
// 2D-2D Epipolar Geometry
// =======================
// Generic
bool MultiViewGeometry::compute5ptEssentialMatrix(
const std::vector<Eigen::Vector3d, Eigen::aligned_allocator<Eigen::Vector3d> > &bvs1,
const std::vector<Eigen::Vector3d, Eigen::aligned_allocator<Eigen::Vector3d> > &bvs2,
const int nmaxiter, const float errth, const bool boptimize,
const bool bdorandom, const float fx, const float fy, Eigen::Matrix3d &Rwc,
Eigen::Vector3d &twc, std::vector<int> &voutliersidx)
{
#ifdef USE_OPENGV
return opengv5ptEssentialMatrix(bvs1, bvs2, nmaxiter, errth, boptimize,
bdorandom, fx, fy, Rwc, twc, voutliersidx);
#else
return opencv5ptEssentialMatrix(bvs1, bvs2, nmaxiter, errth, boptimize,
fx, fy, Rwc, twc, voutliersidx);
#endif
}
// OpenGV
#ifdef USE_OPENGV
bool MultiViewGeometry::opengv5ptEssentialMatrix(
const std::vector<Eigen::Vector3d, Eigen::aligned_allocator<Eigen::Vector3d> > &bvs1,
const std::vector<Eigen::Vector3d, Eigen::aligned_allocator<Eigen::Vector3d> > &bvs2,
const int nmaxiter, const float errth, const bool boptimize,
const bool bdorandom, const float fx, const float fy, Eigen::Matrix3d &Rwc,
Eigen::Vector3d &twc, std::vector<int> &voutliersidx)
{
assert( bvs1.size() == bvs2.size() );
size_t nbpts = bvs1.size();
if( nbpts < 8 ) {
return false;
}
voutliersidx.reserve(nbpts);
opengv::bearingVectors_t vbv1, vbv2;
vbv1.reserve(nbpts);
vbv2.reserve(nbpts);
for( size_t i = 0 ; i < nbpts ; i++ )
{
vbv1.push_back(bvs1.at(i));
vbv2.push_back(bvs2.at(i));
}
//create a central relative adapter
opengv::relative_pose::CentralRelativeAdapter
adapter(vbv1, vbv2);
opengv::sac::Ransac<
opengv::sac_problems::relative_pose::CentralRelativePoseSacProblem> ransac;
std::shared_ptr<
opengv::sac_problems::relative_pose::CentralRelativePoseSacProblem> relposeproblem_ptr(
new opengv::sac_problems::relative_pose::CentralRelativePoseSacProblem(
adapter,
opengv::sac_problems::relative_pose::CentralRelativePoseSacProblem::NISTER,
bdorandom));
float focal = fx + fy;
focal /= 2.;
ransac.sac_model_ = relposeproblem_ptr;
ransac.threshold_ = 2.0*(1.0 - cos(atan(errth/focal)));
// ransac.threshold_ = (1.0 - cos(atan(errth/focal)));
ransac.max_iterations_ = nmaxiter;
ransac.computeModel(0);
// If no solution found, return false
if( ransac.inliers_.size() < 10 ) {
return false;
}
twc = ransac.model_coefficients_.rightCols(1);
Rwc = ransac.model_coefficients_.leftCols(3);
// Optimize the computed pose with inliers only
opengv::transformation_t T_opt;
if( boptimize ) {
ransac.sac_model_->optimizeModelCoefficients(ransac.inliers_, ransac.model_coefficients_, T_opt);
Rwc = T_opt.leftCols(3);
twc = T_opt.rightCols(1);
twc.normalize();
}
size_t k = 0;
for( size_t i = 0 ; i < nbpts ; i++ ) {
if( ransac.inliers_.at(k) == (int)i ) {
k++;
if( k == ransac.inliers_.size() ) {
k = 0;
}
} else {
voutliersidx.push_back(i);
}
}
return true;
}
#endif
bool MultiViewGeometry::opencv5ptEssentialMatrix(
const std::vector<Eigen::Vector3d, Eigen::aligned_allocator<Eigen::Vector3d> > &bvs1,
const std::vector<Eigen::Vector3d, Eigen::aligned_allocator<Eigen::Vector3d> > &bvs2,
const int nmaxiter, const float errth, const bool boptimize, const float fx, const float fy,
Eigen::Matrix3d &Rwc, Eigen::Vector3d &twc, std::vector<int> &voutliersidx)
{
assert( bvs1.size() == bvs2.size() );
size_t nbpts = bvs1.size();
if( nbpts < 5 ) {
return false;
}
voutliersidx.reserve(nbpts);
std::vector<cv::Point2f> cvbvs1, cvbvs2;
cvbvs1.reserve(nbpts);
cvbvs2.reserve(nbpts);
for( size_t i = 0 ; i < nbpts ; i++ )
{
cvbvs1.push_back(
cv::Point2f(
bvs1.at(i).x()/bvs1.at(i).z(),
bvs1.at(i).y()/bvs1.at(i).z()
)
);
cvbvs2.push_back(
cv::Point2f(
bvs2.at(i).x()/bvs2.at(i).z(),
bvs2.at(i).y()/bvs2.at(i).z()
)
);
}
// Using homoegeneous pts here so no dist or calib.
cv::Mat K = cv::Mat::eye(3,3,CV_32F);
cv::Mat inliers;
float confidence = 0.99;
if( boptimize ) {
confidence = 0.999;
}
float focal = (fx+fy) / 2.;
cv::Mat E =
cv::findEssentialMat(
cvbvs1,
cvbvs2,
K,
cv::RANSAC,
confidence,
errth / focal,
inliers
);
for( size_t i = 0 ; i < nbpts ; i++ ) {
if( !inliers.at<uchar>(i) ) {
voutliersidx.push_back(i);
}
}
if( voutliersidx.size() >= nbpts-10 ) {
return false;
}
cv::Mat tvec, cvR;
cv::recoverPose(
E,
cvbvs1,
cvbvs2,
K,
cvR,
tvec,
inliers
);
// Store the resulting pose
Eigen::Vector3d tcw;
Eigen::Matrix3d Rcw;
cv::cv2eigen(cvR, Rcw);
cv::cv2eigen(tvec, tcw);
twc = -1. * Rcw.transpose() * tcw;
Rwc = Rcw.transpose();
return true;
}
// Misc.
// =====
float MultiViewGeometry::computeSampsonDistance(const Eigen::Matrix3d &Frl, const Eigen::Vector3d &leftpt, const Eigen::Vector3d &rightpt)
{
float num = rightpt.transpose() * Frl * leftpt;
num *= num;
float x1, x2, y1, y2;
x1 = (Frl.transpose() * rightpt).x();
x2 = (Frl * leftpt).x();
y1 = (Frl.transpose() * rightpt).y();
y2 = (Frl * leftpt).y();
float den = x1 * x1 + y1 * y1 + x2 * x2 + y2 * y2;
return std::sqrt(num / den);
}
float MultiViewGeometry::computeSampsonDistance(const Eigen::Matrix3d &Frl, const cv::Point2f &leftpt, const cv::Point2f &rightpt)
{
Eigen::Vector3d lpt(leftpt.x,leftpt.y,1.);
Eigen::Vector3d rpt(rightpt.x,rightpt.y,1.);
return computeSampsonDistance(Frl,lpt,rpt);
}
Eigen::Matrix3d MultiViewGeometry::computeFundamentalMat12(const Sophus::SE3d &Tw1, const Sophus::SE3d &Tw2,
const Eigen::Matrix3d &K1, const Eigen::Matrix3d &K2)
{
// TODO Maxime Validate
Sophus::SE3d T12 = Tw1.inverse() * Tw2;
Eigen::Matrix3d skewt12 = Sophus::SO3d::hat(T12.translation());
return K1.transpose().inverse() * skewt12 * T12.rotationMatrix() * K2.inverse();
}
Eigen::Matrix3d MultiViewGeometry::computeFundamentalMat12(const Sophus::SE3d &Tw1, const Sophus::SE3d &Tw2,
const Eigen::Matrix3d &K)
{
return computeFundamentalMat12(Tw1, Tw2, K, K);
}