├── .gitignore
├── .idea
├── codeStyles
│ └── Project.xml
├── misc.xml
├── modules.xml
├── sfm_learn.iml
├── vcs.xml
└── workspace.xml
├── BA.cpp
├── BA.h
├── CMakeLists.txt
├── CVVisual
├── Camera.cpp
├── Camera.h
├── Frame.cpp
├── Frame.h
├── Map.cpp
├── Map.h
├── MapPoint.cpp
├── MapPoint.h
├── README.md
├── SFM.cpp
├── SFM.h
├── cmake_modules
├── FindCSparse.cmake
└── FindG2O.cmake
├── common_include.h
├── datasets
├── fountain_dense_cameras
│ ├── 0000.png.camera
│ ├── 0001.png.camera
│ ├── 0002.png.camera
│ ├── 0003.png.camera
│ ├── 0004.png.camera
│ ├── 0005.png.camera
│ ├── 0006.png.camera
│ ├── 0007.png.camera
│ ├── 0008.png.camera
│ ├── 0009.png.camera
│ └── 0010.png.camera
└── fountain_dense_images
│ ├── 0000.png
│ ├── 0001.png
│ ├── 0002.png
│ ├── 0003.png
│ ├── 0004.png
│ ├── 0005.png
│ ├── 0006.png
│ ├── 0007.png
│ ├── 0008.png
│ ├── 0009.png
│ └── 0010.png
├── images
├── 2D2D.png
├── 3D2D.png
├── afterBA.png
└── beforeBA.png
└── main.cpp
/.gitignore:
--------------------------------------------------------------------------------
1 | CMakeCache.txt
2 | CMakeFiles
3 | CMakeScripts
4 | Makefile
5 | cmake_install.cmake
6 | install_manifest.txt
7 |
8 | #build文件
9 | cmake-build-*/
10 | bin/
--------------------------------------------------------------------------------
/.idea/codeStyles/Project.xml:
--------------------------------------------------------------------------------
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 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.idea/sfm_learn.iml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/workspace.xml:
--------------------------------------------------------------------------------
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 | SE3
170 | keypoints1
171 | descriptors1
172 | matchPoints2
173 | image1aaa
174 | camera
175 | keypoints
176 | descriptors2
177 | goodMatches
178 | CVVISUAL_DEBUGMODE
179 | keyFrame2->matchPoints
180 | RotatePoint
181 |
182 |
183 | Sophus::SE3
184 | keyFrame1->keypoints
185 | keyFrame1->keyPoints
186 | keyFrame2->descriptors
187 | keyFrame2->matchPoints
188 | keyFrame1->image
189 | keyframe2->keypoints
190 | keyframe2->keyPoints
191 | keyframe2->descriptors
192 | matches
193 | matchPoints2
194 |
195 |
196 |
197 |
198 |
199 |
200 |
221 |
222 |
223 |
224 |
225 | true
226 | DEFINITION_ORDER
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 | 1528875516274
323 |
324 |
325 | 1528875516274
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 |
403 |
404 |
405 |
406 |
407 |
408 |
409 |
410 |
411 |
412 |
413 |
414 |
415 |
416 | keyFrame1->frame->getTcw34MatCV().type()
417 | ObjectiveC
418 | EXPRESSION
419 |
420 |
421 | this->keyFrame1->keyPoints.size()
422 | ObjectiveC
423 | EXPRESSION
424 |
425 |
426 | this->->descriptors.rows
427 | ObjectiveC
428 | EXPRESSION
429 |
430 |
431 | this->keyFrame1->descriptors.rows
432 | ObjectiveC
433 | EXPRESSION
434 |
435 |
436 | this->keyFrame1->descriptors.cols
437 | ObjectiveC
438 | EXPRESSION
439 |
440 |
441 | this->keyFrame1->descriptors.size()
442 | ObjectiveC
443 | EXPRESSION
444 |
445 |
446 | this->keyFrame1.get()->keyPoints.size()
447 | ObjectiveC
448 | EXPRESSION
449 |
450 |
451 | matchPointsNorm2.size()
452 | ObjectiveC
453 | EXPRESSION
454 |
455 |
456 | matchPointsNorm1.size()
457 | ObjectiveC
458 | EXPRESSION
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 |
839 |
840 |
841 |
842 |
843 |
844 |
845 |
846 |
847 |
848 |
849 |
850 |
851 |
852 |
853 |
854 |
855 |
856 |
857 |
858 |
859 |
860 |
861 |
862 |
863 |
864 |
865 |
866 |
867 |
868 |
869 |
870 |
871 |
872 |
873 |
874 |
875 |
876 |
877 |
878 |
--------------------------------------------------------------------------------
/BA.cpp:
--------------------------------------------------------------------------------
1 | //
2 | // Created by pidan1231239 on 18-6-23.
3 | //
4 |
5 | #include "BA.h"
6 |
7 | namespace sky{
8 |
9 | void BA::loadMap() {
10 | #ifdef DEBUG
11 | cout << "BA::loadMap: " << endl;
12 | #endif
13 | //加载mapPointsPos
14 | for (auto &mapPoints:map->mapPoints) {
15 | mapPointsPos[mapPoints] = mapPoints->getPosMatx13();
16 | }
17 | //加载frameExtrinsics和cameraIntrinsics
18 | for (auto &frame:map->frames) {
19 | auto angleAxis = frame->getAngleAxisWcMatxCV();
20 | auto t = frame->Tcw.translation();
21 | frameExtrinsics[frame] = Matx23d(angleAxis(0), angleAxis(1), angleAxis(2),
22 | t[0], t[1], t[2]);
23 | if (cameraIntrinsics.find(frame->camera) == cameraIntrinsics.end())
24 | cameraIntrinsics[frame->camera] = Matx14d(
25 | frame->camera->fx, frame->camera->fy, frame->camera->cx, frame->camera->cy);
26 | }
27 | #ifdef DEBUG
28 | cout << "\t" << mapPointsPos.size() << " map points" << endl;
29 | /* int i = 0;
30 | for (auto &mapPoints:mapPointsPos) {
31 | cout << mapPoints.second << endl;
32 | ++i;
33 | if (i >= 5)break;
34 | }
35 | cout << "..." << endl;*/
36 |
37 | cout << "\t" << frameExtrinsics.size() << " frames" << endl;
38 | /* i = 0;
39 | for (auto &frame:frameExtrinsics) {
40 | cout << frame.second << endl;
41 | ++i;
42 | if (i >= 5)break;
43 | }
44 | cout << "..." << endl;*/
45 |
46 | cout << "\t" << cameraIntrinsics.size() << " cameras" << endl;
47 | /* i = 0;
48 | for (auto &camera:cameraIntrinsics) {
49 | cout << camera.second << endl;
50 | ++i;
51 | if (i >= 5)break;
52 | }
53 | cout << "..." << endl;*/
54 | #endif
55 | }
56 |
57 | void BA::bundleAdjustment() {
58 | #ifdef DEBUG
59 | cout << "BA::bundleAdjustment: " << endl;
60 | #endif
61 | ceres::Problem problem;
62 |
63 | #ifdef DEBUG
64 | cout << "\tloading frameExtrinsics..." << endl;
65 | #endif
66 | for (auto &frameExtrinsic:frameExtrinsics)
67 | problem.AddParameterBlock(frameExtrinsic.second.val, 6);
68 | problem.SetParameterBlockConstant(frameExtrinsics[map->frames.front()].val);
69 |
70 | #ifdef DEBUG
71 | cout << "\tloading cameraIntrinsics..." << endl;
72 | #endif
73 | for (auto &cameraIntrinsic:cameraIntrinsics)
74 | problem.AddParameterBlock(cameraIntrinsic.second.val, 4);
75 |
76 | #ifdef DEBUG
77 | cout << "\tloading mapPointsPos..." << endl;
78 | #endif
79 | ceres::LossFunction *lossFunction = new ceres::HuberLoss(4);
80 | for (auto &mapPointPos:mapPointsPos) {
81 | for (auto &observedFrame:mapPointPos.first->observedFrames) {
82 | ceres::CostFunction *costFunction =
83 | new ceres::AutoDiffCostFunction(
84 | new ReprojectCost(observedFrame.second));
85 | problem.AddResidualBlock(
86 | costFunction,
87 | lossFunction,
88 | cameraIntrinsics[observedFrame.first->camera].val, // Intrinsic
89 | frameExtrinsics[observedFrame.first].val, // View Rotation and Translation
90 | mapPointPos.second.val // Point in 3D space
91 | );
92 | }
93 | }
94 |
95 | #ifdef DEBUG
96 | cout << "\tsolving BA..." << endl;
97 | #endif
98 |
99 | ceres::Solver::Summary summary;
100 | ceres::Solve(ceres_config_options, &problem, &summary);
101 |
102 | if (!summary.IsSolutionUsable()) {
103 | std::cout << "Bundle Adjustment failed." << std::endl;
104 | } else {
105 | // Display statistics about the minimization
106 | std::cout << "Bundle Adjustment statistics (approximated RMSE):\n"
107 | << " #views: " << frameExtrinsics.size() << "\n"
108 | << " #residuals: " << summary.num_residuals << "\n"
109 | << " Initial RMSE: " << std::sqrt(summary.initial_cost / summary.num_residuals) << "\n"
110 | << " Final RMSE: " << std::sqrt(summary.final_cost / summary.num_residuals) << "\n"
111 | << " Time (s): " << summary.total_time_in_seconds << "\n";
112 | }
113 | }
114 |
115 | void BA::writeMap() {
116 | //写mapPointsPos
117 | for (auto &mapPointPos:mapPointsPos) {
118 | mapPointPos.first->setPos(mapPointPos.second);
119 | }
120 | //写frameExtrinsics
121 | for (auto &frameExtrinsic:frameExtrinsics) {
122 | frameExtrinsic.first->setTcw(frameExtrinsic.second);
123 | }
124 | //写cameraIntrinsics
125 | for (auto &cameraIntrinsic:cameraIntrinsics) {
126 | cameraIntrinsic.first->setIntrinsic(cameraIntrinsic.second);
127 | }
128 | }
129 |
130 | void BA::clear() {
131 | map = nullptr;
132 |
133 | cameraIntrinsics.clear();
134 | frameExtrinsics.clear();
135 | mapPointsPos.clear();
136 | }
137 |
138 | BA::BA() {
139 | ceres_config_options.minimizer_progress_to_stdout = false;
140 | ceres_config_options.logging_type = ceres::SILENT;
141 | ceres_config_options.num_threads = 1;
142 | ceres_config_options.preconditioner_type = ceres::JACOBI;
143 | ceres_config_options.linear_solver_type = ceres::SPARSE_SCHUR;
144 | ceres_config_options.sparse_linear_algebra_library_type = ceres::EIGEN_SPARSE;
145 | }
146 |
147 | void BA::operator()(Map::Ptr &map) {
148 | this->map = map;
149 | loadMap();
150 | bundleAdjustment();
151 | writeMap();
152 | clear();
153 | }
154 |
155 | }
--------------------------------------------------------------------------------
/BA.h:
--------------------------------------------------------------------------------
1 | //
2 | // Created by pidan1231239 on 18-6-23.
3 | //
4 |
5 | #ifndef SFM_LEARN_BA_H
6 | #define SFM_LEARN_BA_H
7 |
8 | #include
9 | #include
10 | #include
11 | #include "Map.h"
12 | #include "Frame.h"
13 | #include "common_include.h"
14 | #include
15 |
16 | namespace sky {
17 |
18 | using namespace std;
19 |
20 | class BA {
21 |
22 | class ReprojectCost {
23 | cv::Point2d observation;
24 | public:
25 | ReprojectCost(const cv::Point2d &observation) : observation(observation) {}
26 |
27 | template
28 | bool
29 | operator()(const T *const intrinsic, const T *const extrinsic, const T *const pos3d, T *residuals) const {
30 | const T *r = extrinsic;
31 | const T *t = &extrinsic[3];
32 |
33 | T pos_proj[3];
34 | ceres::AngleAxisRotatePoint(r, pos3d, pos_proj);
35 |
36 | // Apply the camera translation
37 | pos_proj[0] += t[0];
38 | pos_proj[1] += t[1];
39 | pos_proj[2] += t[2];
40 |
41 | const T x = pos_proj[0] / pos_proj[2];
42 | const T y = pos_proj[1] / pos_proj[2];
43 |
44 | const T fx = intrinsic[0];
45 | const T fy = intrinsic[1];
46 | const T cx = intrinsic[2];
47 | const T cy = intrinsic[3];
48 |
49 | // Apply intrinsic
50 | const T u = fx * x + cx;
51 | const T v = fy * y + cy;
52 |
53 | residuals[0] = u - T(observation.x);
54 | residuals[1] = v - T(observation.y);
55 |
56 | return true;
57 | }
58 | };
59 |
60 | Map::Ptr map;
61 | ceres::Solver::Options ceres_config_options;
62 |
63 | unordered_map cameraIntrinsics;
64 | unordered_map frameExtrinsics;
65 | unordered_map mapPointsPos;
66 |
67 | void loadMap();
68 |
69 | void bundleAdjustment();
70 |
71 | void writeMap();
72 |
73 | void clear();
74 |
75 | public:
76 |
77 | BA();
78 |
79 | void operator()(Map::Ptr &map);
80 |
81 |
82 | };
83 |
84 | }
85 |
86 |
87 | #endif //SFM_LEARN_BA_H
88 |
--------------------------------------------------------------------------------
/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | cmake_minimum_required(VERSION 2.8)
2 | project(sfm_learn)
3 |
4 | set(CMAKE_CXX_STANDARD 14)
5 |
6 | # (un)set: cmake -DCVV_DEBUG_MODE=OFF ..
7 | OPTION(CVV_DEBUG_MODE "cvvisual-debug-mode" ON)
8 | if(CVV_DEBUG_MODE MATCHES ON)
9 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DCVVISUAL_DEBUGMODE")
10 | endif()
11 |
12 | # (un)set: cmake -CLOUDVIEWER_DEBUG_MODE=OFF ..
13 | OPTION(CLOUDVIEWER_DEBUG_MODE "cloudviewer-debug-mode" ON)
14 | if(CLOUDVIEWER_DEBUG_MODE MATCHES ON)
15 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DCLOUDVIEWER_DEBUG")
16 | endif()
17 |
18 | # (un)set: cmake -DDEBUG_MODE=OFF ..
19 | OPTION(DEBUG_MODE "debug-mode" ON)
20 | if(DEBUG_MODE MATCHES ON)
21 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DDEBUG")
22 | endif()
23 |
24 |
25 |
26 | list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake_modules)
27 | set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
28 | set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
29 |
30 | # Eigen
31 | include_directories("/usr/include/eigen3")
32 | # OpenCV
33 | find_package(OpenCV 3.1 REQUIRED)
34 | include_directories(${OpenCV_INCLUDE_DIRS})
35 | # Sophus
36 | find_package(Sophus REQUIRED)
37 | include_directories(${Sophus_INCLUDE_DIRS})
38 | # G2O
39 | find_package(G2O REQUIRED)
40 | include_directories(${G2O_INCLUDE_DIRS})
41 | # Ceres
42 | find_package(Ceres REQUIRED)
43 | include_directories(${CERES_INCLUDE_DIRS} )
44 | # Boost
45 | find_package(Boost REQUIRED COMPONENTS system chrono filesystem unit_test_framework program_options)
46 | include_directories(${Boost_INCLUDE_DIR})
47 | # PCL
48 | find_package(PCL REQUIRED COMPONENTS common io visualization)
49 | include_directories(${PCL_INCLUDE_DIRS})
50 | link_directories(${PCL_LIBRARY_DIRS})
51 | add_definitions(${PCL_DEFINITIONS})
52 |
53 | set(THIRD_PARTY_LIBS
54 | ${OpenCV_LIBS}
55 | ${Sophus_LIBRARIES}
56 | g2o_core g2o_stuff g2o_types_sba
57 | ${CERES_LIBRARIES}
58 | ${Boost_LIBRARIES}
59 | opencv_cvv
60 | opencv_xfeatures2d
61 | ${PCL_LIBRARIES}
62 | )
63 |
64 | add_executable(sfm_learn main.cpp Camera.cpp Camera.h common_include.h Frame.cpp Frame.h MapPoint.cpp MapPoint.h Map.cpp Map.h SFM.cpp SFM.h BA.cpp BA.h)
65 | target_link_libraries(sfm_learn ${THIRD_PARTY_LIBS})
--------------------------------------------------------------------------------
/CVVisual:
--------------------------------------------------------------------------------
1 | [STFLEngine]
2 | Overview\settings=type match, raw
3 | Rawview\settings=group by keypoint_type
4 |
5 | [default_views]
6 | default_filter_view=DefaultFilterView
7 | default_match_view=TranslationMatchView
8 |
9 | [overview]
10 | imgsize=100
11 | imgzoom=46
12 |
--------------------------------------------------------------------------------
/Camera.cpp:
--------------------------------------------------------------------------------
1 | //
2 | // Created by pidan1231239 on 18-6-13.
3 | //
4 |
5 | #include "Camera.h"
6 |
7 | namespace sky {
8 |
9 | Vector3d Camera::world2camera(const Vector3d &p_w, const SE3 &T_c_w) {
10 | return T_c_w * p_w;
11 | }
12 |
13 | Vector3d Camera::camera2world(const Vector3d &p_c, const SE3 &T_c_w) {
14 | return T_c_w.inverse() * p_c;
15 | }
16 |
17 | Vector2d Camera::camera2pixel(const Vector3d &p_c) {
18 | return Vector2d(
19 | fx * p_c(0, 0) / p_c(2, 0) + cx,
20 | fy * p_c(1, 0) / p_c(2, 0) + cy
21 | );
22 | }
23 |
24 | Vector3d Camera::pixel2camera(const Vector2d &p_p, double depth) {
25 | return Vector3d(
26 | (p_p(0, 0) - cx) * depth / fx,
27 | (p_p(1, 0) - cy) * depth / fy,
28 | depth
29 | );
30 | }
31 |
32 | Vector2d Camera::world2pixel(const Vector3d &p_w, const SE3 &T_c_w) {
33 | return camera2pixel(world2camera(p_w, T_c_w));
34 | }
35 |
36 | Vector3d Camera::pixel2world(const Vector2d &p_p, const SE3 &T_c_w, double depth) {
37 | return camera2world(pixel2camera(p_p, depth), T_c_w);
38 | }
39 |
40 | Point2f Camera::pixel2normal(const Point2d &p) const{
41 | return Point2f
42 | (
43 | (p.x - cx) / fx,
44 | (p.y - cy) / fy
45 | );
46 | }
47 |
48 |
49 | }
50 |
51 |
--------------------------------------------------------------------------------
/Camera.h:
--------------------------------------------------------------------------------
1 | //
2 | // Created by pidan1231239 on 18-6-13.
3 | //
4 |
5 | #ifndef SFM_LEARN_CAMERA_H
6 | #define SFM_LEARN_CAMERA_H
7 |
8 | #include "common_include.h"
9 | #include "opencv2/opencv.hpp"
10 |
11 | namespace sky {
12 |
13 | using namespace cv;
14 |
15 | class Camera {
16 | public:
17 | typedef shared_ptr Ptr;
18 | float fx, fy, cx, cy;
19 |
20 | Camera(float fx, float fy, float cx, float cy) :
21 | fx(fx), fy(fy), cx(cx), cy(cy) {}
22 |
23 | template
24 | void setIntrinsic(Matx intrinsic) {
25 | fx=intrinsic(0);
26 | fy=intrinsic(1);
27 | cx=intrinsic(2);
28 | cy=intrinsic(3);
29 | }
30 |
31 | //坐标转换
32 |
33 | Vector3d world2camera(const Vector3d &p_w, const SE3 &T_c_w);
34 |
35 | Vector3d camera2world(const Vector3d &p_c, const SE3 &T_c_w);
36 |
37 | Vector2d camera2pixel(const Vector3d &p_c);
38 |
39 | Vector3d pixel2camera(const Vector2d &p_p, double depth = 1);
40 |
41 | Vector3d pixel2world(const Vector2d &p_p, const SE3 &T_c_w, double depth = 1);
42 |
43 | Vector2d world2pixel(const Vector3d &p_w, const SE3 &T_c_w);
44 |
45 | Point2f pixel2normal(const Point2d &p) const;
46 |
47 | //获取参数
48 | float getFocalLength() {
49 | return (fx + fy) / 2;
50 | }
51 |
52 | cv::Point2d getPrincipalPoint() {
53 | return cv::Point2d(cx, cy);
54 | }
55 |
56 | cv::Matx getKMatxCV() {
57 | return cv::Matx(fx, 0, cx, 0, fy, cy, 0, 0, 1);
58 | }
59 |
60 | cv::Mat getKMatCV() {
61 | Mat K = (Mat_(3, 3) << fx, 0, cx, 0, fy, cy, 0, 0, 1);
62 | return K;
63 | }
64 | };
65 |
66 | }
67 |
68 |
69 | #endif //SFM_LEARN_CAMERA_H
70 |
--------------------------------------------------------------------------------
/Frame.cpp:
--------------------------------------------------------------------------------
1 | //
2 | // Created by pidan1231239 on 18-6-13.
3 | //
4 |
5 | #include "Frame.h"
6 |
7 | namespace sky {
8 | Frame::Frame()
9 | : camera(nullptr) {
10 |
11 | }
12 |
13 | Frame::~Frame() {
14 |
15 | }
16 |
17 |
18 | Vector3d Frame::getCamCenterEigen() const {
19 | return Tcw.inverse().translation();
20 | }
21 |
22 |
23 | bool Frame::isInFrame(const Vector3d &pt_world) {
24 | // cout<<"pt_world = "<world2camera(pt_world, Tcw);
26 | // cout<<"P_cam = "<world2pixel(pt_world, Tcw);
29 | // cout<<"P_pixel = "< 0 && pixel(1, 0) > 0
31 | && pixel(0, 0) < cols
32 | && pixel(1, 0) < rows;
33 | }
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/Frame.h:
--------------------------------------------------------------------------------
1 | //
2 | // Created by pidan1231239 on 18-6-13.
3 | //
4 |
5 | #ifndef SFM_LEARN_FRAME_H
6 | #define SFM_LEARN_FRAME_H
7 |
8 | #include "common_include.h"
9 | #include "Camera.h"
10 | #include
11 | #include
12 |
13 | namespace sky {
14 |
15 | class Frame {
16 | public:
17 | typedef shared_ptr Ptr;
18 | SE3 Tcw; // transform from world to camera
19 | Camera::Ptr camera; // Pinhole RGBD Camera model
20 | int cols, rows;
21 |
22 | Frame();
23 |
24 | Frame(const Camera::Ptr &camera, const Mat &image) :
25 | camera(camera), cols(image.cols), rows(image.rows) {}
26 |
27 | ~Frame();
28 |
29 |
30 | Vector3d getCamCenterEigen() const;
31 |
32 | cv::Mat getTcwMatCV(int rtype) {
33 | cv::Mat TcwCV, TcwCVR;
34 | cv::eigen2cv(Tcw.matrix(), TcwCV);
35 | TcwCV.convertTo(TcwCVR, rtype);
36 | return TcwCVR;
37 | }
38 |
39 | cv::Mat getTcw34MatCV(int rtype) {
40 | auto TcwCV = getTcwMatCV(rtype);
41 | Mat Tcw34;
42 | TcwCV(cv::Range(0, 3), cv::Range(0, 4)).convertTo(Tcw34, rtype);
43 | return Tcw34;
44 | }
45 |
46 | cv::Mat getTwcMatCV(int rtype) {
47 | cv::Mat TwcCV, TwcCVR;
48 | cv::eigen2cv(Tcw.inverse().matrix(), TwcCV);
49 | TwcCV.convertTo(TwcCVR, rtype);
50 | return TwcCVR;
51 | }
52 |
53 | template
54 | cv::Matx getAngleAxisWcMatxCV() {
55 | Sophus::AngleAxisd angleAxis(Tcw.so3().matrix());
56 | auto axis = angleAxis.angle() * angleAxis.axis();
57 | cv::Matx angleAxisCV(axis[0],axis[1],axis[2]);
58 | return angleAxisCV;
59 | };
60 |
61 | template
62 | void setTcw(Matx angleAxisAndTranslation){
63 | Tcw.so3()=SO3(angleAxisAndTranslation(0,0),
64 | angleAxisAndTranslation(0,1),
65 | angleAxisAndTranslation(0,2));
66 | Tcw.translation()=Vector3d(angleAxisAndTranslation(1,0),
67 | angleAxisAndTranslation(1,1),
68 | angleAxisAndTranslation(1,2));
69 | }
70 |
71 |
72 |
73 |
74 | /* cv::Mat getProjMatCV() {
75 | return camera->getKMatCV()*getTcw34MatCV();
76 | }*/
77 |
78 |
79 | // check if a point is in this frame
80 | bool isInFrame(const Vector3d &pt_world);
81 | };
82 |
83 | }
84 |
85 |
86 | #endif //SFM_LEARN_FRAME_H
87 |
--------------------------------------------------------------------------------
/Map.cpp:
--------------------------------------------------------------------------------
1 | //
2 | // Created by pidan1231239 on 18-6-13.
3 | //
4 |
5 | #include "Map.h"
6 | #ifdef CLOUDVIEWER_DEBUG
7 | #include
8 | #include
9 | #include
10 | #include
11 | #include
12 | #include
13 | #include
14 | #endif
15 |
16 | namespace sky {
17 |
18 | void Map::addFrame(Frame::Ptr frame) {
19 | if (frame)
20 | frames.push_back(frame);
21 | }
22 |
23 | void Map::addMapPoint(MapPoint::Ptr mapPoint) {
24 | if (mapPoint)
25 | mapPoints.push_back(mapPoint);
26 | }
27 |
28 | #ifdef CLOUDVIEWER_DEBUG
29 | void Map::visInCloudViewer() {
30 |
31 | pcl::PointCloud::Ptr cloud(new pcl::PointCloud);
32 | for (auto point:mapPoints) {
33 | pcl::PointXYZRGB pointXYZ(point->rgb[0], point->rgb[1], point->rgb[2]);
34 | pointXYZ.x = point->pos(0);
35 | pointXYZ.y = point->pos(1);
36 | pointXYZ.z = point->pos(2);
37 | cloud->push_back(pointXYZ);
38 | }
39 |
40 | pcl::visualization::PCLVisualizer viewer("Viewer");
41 | viewer.setBackgroundColor(50, 50, 50);
42 | viewer.addPointCloud(cloud, "Triangulated Point Cloud");
43 | viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE,
44 | 3,
45 | "Triangulated Point Cloud");
46 | viewer.addCoordinateSystem(1.0);
47 |
48 | int indexFrame = 0;
49 | for (auto &frame:frames) {
50 | Eigen::Matrix4f camPose;
51 | auto T_c_w = frame->Tcw.inverse().matrix();
52 | for (int i = 0; i < camPose.rows(); ++i)
53 | for (int j = 0; j < camPose.cols(); ++j)
54 | camPose(i, j) = T_c_w(i, j);
55 | viewer.addCoordinateSystem(1.0, Eigen::Affine3f(camPose), "cam" + to_string(indexFrame++));
56 | }
57 | viewer.initCameraParameters ();
58 | while (!viewer.wasStopped ()) {
59 | viewer.spin();
60 | }
61 |
62 | }
63 | #endif
64 |
65 | }
66 |
67 |
--------------------------------------------------------------------------------
/Map.h:
--------------------------------------------------------------------------------
1 | //
2 | // Created by pidan1231239 on 18-6-13.
3 | //
4 |
5 | #ifndef SFM_LEARN_MAP_H
6 | #define SFM_LEARN_MAP_H
7 |
8 | #include "MapPoint.h"
9 | #include "Frame.h"
10 |
11 | namespace sky {
12 |
13 | class Map {
14 | public:
15 | typedef shared_ptr