test_tutorial3.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. #include "test_precomp.hpp"
  5. namespace opencv_test { namespace {
  6. /**
  7. * @function main
  8. */
  9. static void tutorial3(bool camera_pov)
  10. {
  11. /// Create a window
  12. viz::Viz3d myWindow("Coordinate Frame");
  13. /// Add coordinate axes
  14. myWindow.showWidget("Coordinate Widget", viz::WCoordinateSystem());
  15. /// Let's assume camera has the following properties
  16. Point3d cam_origin(3.0, 3.0, 3.0), cam_focal_point(3.0, 3.0, 2.0), cam_y_dir(-1.0, 0.0, 0.0);
  17. /// We can get the pose of the cam using makeCameraPose
  18. Affine3d camera_pose = viz::makeCameraPose(cam_origin, cam_focal_point, cam_y_dir);
  19. /// We can get the transformation matrix from camera coordinate system to global using
  20. /// - makeTransformToGlobal. We need the axes of the camera
  21. Affine3d transform = viz::makeTransformToGlobal(Vec3d(0.0, -1.0, 0.0), Vec3d(-1.0, 0.0, 0.0), Vec3d(0.0, 0.0, -1.0), cam_origin);
  22. /// Create a cloud widget.
  23. Mat dragon_cloud = viz::readCloud(get_dragon_ply_file_path());
  24. viz::WCloud cloud_widget(dragon_cloud, viz::Color::green());
  25. /// Pose of the widget in camera frame
  26. Affine3d cloud_pose = Affine3d().rotate(Vec3d(0.0, CV_PI/2, 0.0)).rotate(Vec3d(0.0, 0.0, CV_PI)).translate(Vec3d(0.0, 0.0, 3.0));
  27. /// Pose of the widget in global frame
  28. Affine3d cloud_pose_global = transform * cloud_pose;
  29. /// Visualize camera frame
  30. myWindow.showWidget("CPW_FRUSTUM", viz::WCameraPosition(Vec2f(0.889484f, 0.523599f)), camera_pose);
  31. if (!camera_pov)
  32. myWindow.showWidget("CPW", viz::WCameraPosition(0.5), camera_pose);
  33. /// Visualize widget
  34. myWindow.showWidget("bunny", cloud_widget, cloud_pose_global);
  35. /// Set the viewer pose to that of camera
  36. if (camera_pov)
  37. myWindow.setViewerPose(camera_pose);
  38. /// Start event loop.
  39. myWindow.spinOnce(500, true);
  40. }
  41. TEST(Viz, tutorial3_global_view)
  42. {
  43. tutorial3(false);
  44. }
  45. TEST(Viz, tutorial3_camera_view)
  46. {
  47. tutorial3(true);
  48. }
  49. }} // namespace