1 Transformations {#tutorial_transformations} 2 =============== 3 4 Goal 5 ---- 6 7 In this tutorial you will learn how to 8 9 - How to use makeTransformToGlobal to compute pose 10 - How to use makeCameraPose and Viz3d::setViewerPose 11 - How to visualize camera position by axes and by viewing frustum 12 13 Code 14 ---- 15 16 You can download the code from [here ](https://github.com/Itseez/opencv/tree/master/samples/cpp/tutorial_code/viz/transformations.cpp). 17 @include samples/cpp/tutorial_code/viz/transformations.cpp 18 19 Explanation 20 ----------- 21 22 Here is the general structure of the program: 23 24 - Create a visualization window. 25 @code{.cpp} 26 /// Create a window 27 viz::Viz3d myWindow("Transformations"); 28 @endcode 29 - Get camera pose from camera position, camera focal point and y direction. 30 @code{.cpp} 31 /// Let's assume camera has the following properties 32 Point3f 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); 33 34 /// We can get the pose of the cam using makeCameraPose 35 Affine3f cam_pose = viz::makeCameraPose(cam_pos, cam_focal_point, cam_y_dir); 36 @endcode 37 - Obtain transform matrix knowing the axes of camera coordinate system. 38 @code{.cpp} 39 /// We can get the transformation matrix from camera coordinate system to global using 40 /// - makeTransformToGlobal. We need the axes of the camera 41 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); 42 @endcode 43 - Create a cloud widget from bunny.ply file 44 @code{.cpp} 45 /// Create a cloud widget. 46 Mat bunny_cloud = cvcloud_load(); 47 viz::WCloud cloud_widget(bunny_cloud, viz::Color::green()); 48 @endcode 49 - Given the pose in camera coordinate system, estimate the global pose. 50 @code{.cpp} 51 /// Pose of the widget in camera frame 52 Affine3f cloud_pose = Affine3f().translate(Vec3f(0.0f,0.0f,3.0f)); 53 /// Pose of the widget in global frame 54 Affine3f cloud_pose_global = transform * cloud_pose; 55 @endcode 56 - If the view point is set to be global, visualize camera coordinate frame and viewing frustum. 57 @code{.cpp} 58 /// Visualize camera frame 59 if (!camera_pov) 60 { 61 viz::WCameraPosition cpw(0.5); // Coordinate axes 62 viz::WCameraPosition cpw_frustum(Vec2f(0.889484, 0.523599)); // Camera frustum 63 myWindow.showWidget("CPW", cpw, cam_pose); 64 myWindow.showWidget("CPW_FRUSTUM", cpw_frustum, cam_pose); 65 } 66 @endcode 67 - Visualize the cloud widget with the estimated global pose 68 @code{.cpp} 69 /// Visualize widget 70 myWindow.showWidget("bunny", cloud_widget, cloud_pose_global); 71 @endcode 72 - If the view point is set to be camera's, set viewer pose to **cam_pose**. 73 @code{.cpp} 74 /// Set the viewer pose to that of camera 75 if (camera_pov) 76 myWindow.setViewerPose(cam_pose); 77 @endcode 78 79 Results 80 ------- 81 82 -# Here is the result from the camera point of view. 83 84 ![](images/camera_view_point.png) 85 86 -# Here is the result from global point of view. 87 88 ![](images/global_view_point.png) 89