recon2v.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include <opencv2/sfm.hpp>
  2. #include <opencv2/core.hpp>
  3. #include <opencv2/viz.hpp>
  4. #include <iostream>
  5. #include <fstream>
  6. #include <string>
  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 two view reconstruction capabilities in the \n"
  14. << " OpenCV Structure From Motion (SFM) module.\n"
  15. << " It uses the following data from the VGG datasets at ...\n"
  16. << " Usage:\n"
  17. << " reconv2_pts.txt \n "
  18. << " where the first line has the number of points and each subsequent \n"
  19. << " line has entries for matched points as: \n"
  20. << " x1 y1 x2 y2 \n"
  21. << "------------------------------------------------------------------\n\n"
  22. << endl;
  23. }
  24. int main(int argc, char** argv)
  25. {
  26. // Do projective reconstruction
  27. bool is_projective = true;
  28. // Read 2D points from text file
  29. Mat_<double> x1, x2;
  30. int npts;
  31. if (argc < 2) {
  32. help();
  33. exit(0);
  34. } else {
  35. ifstream myfile(argv[1]);
  36. if (!myfile.is_open()) {
  37. cout << "Unable to read file: " << argv[1] << endl;
  38. exit(0);
  39. } else {
  40. string line;
  41. // Read number of points
  42. getline(myfile, line);
  43. npts = (int) atof(line.c_str());
  44. x1 = Mat_<double>(2, npts);
  45. x2 = Mat_<double>(2, npts);
  46. // Read the point coordinates
  47. for (int i = 0; i < npts; ++i) {
  48. getline(myfile, line);
  49. stringstream s(line);
  50. string cord;
  51. s >> cord;
  52. x1(0, i) = atof(cord.c_str());
  53. s >> cord;
  54. x1(1, i) = atof(cord.c_str());
  55. s >> cord;
  56. x2(0, i) = atof(cord.c_str());
  57. s >> cord;
  58. x2(1, i) = atof(cord.c_str());
  59. }
  60. myfile.close();
  61. }
  62. }
  63. // Call the reconstruction function
  64. std::vector < Mat_<double> > points2d;
  65. points2d.push_back(x1);
  66. points2d.push_back(x2);
  67. Matx33d K_estimated;
  68. Mat_<double> points3d_estimated;
  69. std::vector < cv::Mat > Ps_estimated;
  70. reconstruct(points2d, Ps_estimated, points3d_estimated, K_estimated, is_projective);
  71. // Print output
  72. cout << endl;
  73. cout << "Projection Matrix of View 1: " << endl;
  74. cout << "============================ " << endl;
  75. cout << Ps_estimated[0] << endl << endl;
  76. cout << "Projection Matrix of View 2: " << endl;
  77. cout << "============================ " << endl;
  78. cout << Ps_estimated[1] << endl << endl;
  79. // Display 3D points using VIZ module
  80. // Create the pointcloud
  81. std::vector<cv::Vec3f> point_cloud;
  82. for (int i = 0; i < npts; ++i) {
  83. cv::Vec3f point3d((float) points3d_estimated(0, i),
  84. (float) points3d_estimated(1, i),
  85. (float) points3d_estimated(2, i));
  86. point_cloud.push_back(point3d);
  87. }
  88. // Create a 3D window
  89. viz::Viz3d myWindow("Coordinate Frame");
  90. /// Add coordinate axes
  91. myWindow.showWidget("Coordinate Widget", viz::WCoordinateSystem());
  92. viz::WCloud cloud_widget(point_cloud, viz::Color::green());
  93. myWindow.showWidget("cloud", cloud_widget);
  94. myWindow.spin();
  95. return 0;
  96. }