transformations.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /**
  2. * @file transformations.cpp
  3. * @brief Visualizing cloud in different positions, coordinate frames, camera frustums
  4. * @author Ozan Cagri Tonkal
  5. */
  6. #include <opencv2/viz.hpp>
  7. #include <iostream>
  8. #include <fstream>
  9. using namespace cv;
  10. using namespace std;
  11. /**
  12. * @function help
  13. * @brief Display instructions to use this tutorial program
  14. */
  15. static void help()
  16. {
  17. cout
  18. << "--------------------------------------------------------------------------" << endl
  19. << "This program shows how to use makeTransformToGlobal() to compute required pose,"
  20. << "how to use makeCameraPose and Viz3d::setViewerPose. You can observe the scene "
  21. << "from camera point of view (C) or global point of view (G)" << endl
  22. << "Usage:" << endl
  23. << "./transformations [ G | C ]" << endl
  24. << endl;
  25. }
  26. /**
  27. * @function cvcloud_load
  28. * @brief load bunny.ply
  29. */
  30. static Mat cvcloud_load()
  31. {
  32. Mat cloud(1, 1889, CV_32FC3);
  33. ifstream ifs("bunny.ply");
  34. string str;
  35. for(size_t i = 0; i < 12; ++i)
  36. getline(ifs, str);
  37. Point3f* data = cloud.ptr<cv::Point3f>();
  38. float dummy1, dummy2;
  39. for(size_t i = 0; i < 1889; ++i)
  40. ifs >> data[i].x >> data[i].y >> data[i].z >> dummy1 >> dummy2;
  41. cloud *= 5.0f;
  42. return cloud;
  43. }
  44. /**
  45. * @function main
  46. */
  47. int main(int argn, char **argv)
  48. {
  49. help();
  50. if (argn < 2)
  51. {
  52. cout << "Missing arguments." << endl;
  53. return 1;
  54. }
  55. bool camera_pov = (argv[1][0] == 'C');
  56. /// Create a window
  57. viz::Viz3d myWindow("Coordinate Frame");
  58. /// Add coordinate axes
  59. myWindow.showWidget("Coordinate Widget", viz::WCoordinateSystem());
  60. /// Let's assume camera has the following properties
  61. Vec3f cam_pos(3.0f,3.0f,3.0f), cam_focal_point(3.0f,3.0f,2.0f), cam_y_dir(-1.0f,0.0f,0.0f);
  62. /// We can get the pose of the cam using makeCameraPose
  63. Affine3f cam_pose = viz::makeCameraPose(cam_pos, cam_focal_point, cam_y_dir);
  64. /// We can get the transformation matrix from camera coordinate system to global using
  65. /// - makeTransformToGlobal. We need the axes of the camera
  66. Affine3f transform = viz::makeTransformToGlobal(Vec3f(0.0f,-1.0f,0.0f), Vec3f(-1.0f,0.0f,0.0f), Vec3f(0.0f,0.0f,-1.0f), cam_pos);
  67. /// Create a cloud widget.
  68. Mat bunny_cloud = cvcloud_load();
  69. viz::WCloud cloud_widget(bunny_cloud, viz::Color::green());
  70. /// Pose of the widget in camera frame
  71. Affine3f cloud_pose = Affine3f().translate(Vec3f(0.0f,0.0f,3.0f));
  72. /// Pose of the widget in global frame
  73. Affine3f cloud_pose_global = transform * cloud_pose;
  74. /// Visualize camera frame
  75. if (!camera_pov)
  76. {
  77. viz::WCameraPosition cpw(0.5); // Coordinate axes
  78. viz::WCameraPosition cpw_frustum(Vec2f(0.889484, 0.523599)); // Camera frustum
  79. myWindow.showWidget("CPW", cpw, cam_pose);
  80. myWindow.showWidget("CPW_FRUSTUM", cpw_frustum, cam_pose);
  81. }
  82. /// Visualize widget
  83. myWindow.showWidget("bunny", cloud_widget, cloud_pose_global);
  84. /// Set the viewer pose to that of camera
  85. if (camera_pov)
  86. myWindow.setViewerPose(cam_pose);
  87. /// Start event loop.
  88. myWindow.spin();
  89. return 0;
  90. }