scene_reconstruction.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #include <opencv2/sfm.hpp>
  2. #include <opencv2/viz.hpp>
  3. #include <opencv2/calib3d.hpp>
  4. #include <opencv2/core.hpp>
  5. #include <iostream>
  6. #include <fstream>
  7. using namespace std;
  8. using namespace cv;
  9. using namespace cv::sfm;
  10. static void help() {
  11. cout
  12. << "\n------------------------------------------------------------------------------------\n"
  13. << " This program shows the multiview reconstruction capabilities in the \n"
  14. << " OpenCV Structure From Motion (SFM) module.\n"
  15. << " It reconstruct a scene from a set of 2D images \n"
  16. << " Usage:\n"
  17. << " example_sfm_scene_reconstruction <path_to_file> <f> <cx> <cy>\n"
  18. << " where: path_to_file is the file absolute path into your system which contains\n"
  19. << " the list of images to use for reconstruction. \n"
  20. << " f is the focal length in pixels. \n"
  21. << " cx is the image principal point x coordinates in pixels. \n"
  22. << " cy is the image principal point y coordinates in pixels. \n"
  23. << "------------------------------------------------------------------------------------\n\n"
  24. << endl;
  25. }
  26. static int getdir(const string _filename, vector<String> &files)
  27. {
  28. ifstream myfile(_filename.c_str());
  29. if (!myfile.is_open()) {
  30. cout << "Unable to read file: " << _filename << endl;
  31. exit(0);
  32. } else {;
  33. size_t found = _filename.find_last_of("/\\");
  34. string line_str, path_to_file = _filename.substr(0, found);
  35. while ( getline(myfile, line_str) )
  36. files.push_back(path_to_file+string("/")+line_str);
  37. }
  38. return 1;
  39. }
  40. int main(int argc, char* argv[])
  41. {
  42. // Read input parameters
  43. if ( argc != 5 )
  44. {
  45. help();
  46. exit(0);
  47. }
  48. // Parse the image paths
  49. vector<String> images_paths;
  50. getdir( argv[1], images_paths );
  51. // Build intrinsics
  52. float f = atof(argv[2]),
  53. cx = atof(argv[3]), cy = atof(argv[4]);
  54. Matx33d K = Matx33d( f, 0, cx,
  55. 0, f, cy,
  56. 0, 0, 1);
  57. /// Reconstruct the scene using the 2d images
  58. bool is_projective = true;
  59. vector<Mat> Rs_est, ts_est, points3d_estimated;
  60. reconstruct(images_paths, Rs_est, ts_est, K, points3d_estimated, is_projective);
  61. // Print output
  62. cout << "\n----------------------------\n" << endl;
  63. cout << "Reconstruction: " << endl;
  64. cout << "============================" << endl;
  65. cout << "Estimated 3D points: " << points3d_estimated.size() << endl;
  66. cout << "Estimated cameras: " << Rs_est.size() << endl;
  67. cout << "Refined intrinsics: " << endl << K << endl << endl;
  68. cout << "3D Visualization: " << endl;
  69. cout << "============================" << endl;
  70. /// Create 3D windows
  71. viz::Viz3d window("Coordinate Frame");
  72. window.setWindowSize(Size(500,500));
  73. window.setWindowPosition(Point(150,150));
  74. window.setBackgroundColor(); // black by default
  75. // Create the pointcloud
  76. cout << "Recovering points ... ";
  77. // recover estimated points3d
  78. vector<Vec3f> point_cloud_est;
  79. for (int i = 0; i < points3d_estimated.size(); ++i)
  80. point_cloud_est.push_back(Vec3f(points3d_estimated[i]));
  81. cout << "[DONE]" << endl;
  82. /// Recovering cameras
  83. cout << "Recovering cameras ... ";
  84. vector<Affine3d> path;
  85. for (size_t i = 0; i < Rs_est.size(); ++i)
  86. path.push_back(Affine3d(Rs_est[i],ts_est[i]));
  87. cout << "[DONE]" << endl;
  88. /// Add the pointcloud
  89. if ( point_cloud_est.size() > 0 )
  90. {
  91. cout << "Rendering points ... ";
  92. viz::WCloud cloud_widget(point_cloud_est, viz::Color::green());
  93. window.showWidget("point_cloud", cloud_widget);
  94. cout << "[DONE]" << endl;
  95. }
  96. else
  97. {
  98. cout << "Cannot render points: Empty pointcloud" << endl;
  99. }
  100. /// Add cameras
  101. if ( path.size() > 0 )
  102. {
  103. cout << "Rendering Cameras ... ";
  104. window.showWidget("cameras_frames_and_lines", viz::WTrajectory(path, viz::WTrajectory::BOTH, 0.1, viz::Color::green()));
  105. window.showWidget("cameras_frustums", viz::WTrajectoryFrustums(path, K, 0.1, viz::Color::yellow()));
  106. window.setViewerPose(path[0]);
  107. cout << "[DONE]" << endl;
  108. }
  109. else
  110. {
  111. cout << "Cannot render the cameras: Empty path" << endl;
  112. }
  113. /// Wait for key 'q' to close the window
  114. cout << endl << "Press 'q' to close each windows ... " << endl;
  115. window.spin();
  116. return 0;
  117. }