Explanation
- Create a visualization window.
- Get camera pose from the camera position, camera focal point and y direction.
- Obtain transform matrix Knowing the axes of the camera coordinate system.
- Create a cloud widget from bunny.ply file
- Given the pose in camera coordinate system, estimate the global pose.
- If The view point was set to be global, visualize camera coordinate frame and viewing frustum.
- Visualize the cloud widget with the estimated global pose
- If The view point was set to being camera ' s, set viewer pose to cam_pose.
code
#include <opencv2/viz/vizcore.hpp> #include <iostream> #include <fstream>using namespace cv;using Namespace std;/** * @function cvcloud_load * @brief load bunny.ply */mat cvcloud_load () {Mat Cloud (1, 1889, CV_32FC3); Ifstream ifs ("bunny.ply"); String str; for (size_t i = 0; i <; ++i) getline (IFS, str); point3f* data = CLOUD.PTR<CV::P oint3f> (); float dummy1, dummy2; for (size_t i = 0; i < 1889; ++i) ifs >> data[i].x >> data[i].y >> data[i].z >> dummy1 &G T;> Dummy2; Cloud *= 5.0f; return cloud;} /** * @function main */int main (int argn, char **argv) {if (Argn < 2) {cout << "Usage:" << E Ndl << "./transformations [G | C] "<< Endl; return 1; } bool Camera_pov = (argv[1][0] = = ' C '); Create a window viz::viz3d mywindow ("Coordinate Frame"); ADD coordinate axes mywindow.showwidget ("Coordinate Widget", Viz::wcoordinatesystem ()); Let's assume camera has the following properties 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); We can get the pose of the cam using makecamerapose affine3f cam_pose = Viz::makecamerapose (Cam_pos, Cam_focal_poin T, Cam_y_dir); We can get the transformation matrix from camera coordinate system to global using//-Maketransformtoglobal. We need the axes of the camera 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); Create a cloud widget. Mat Bunny_cloud = Cvcloud_load (); Viz::wcloud Cloud_widget (Bunny_cloud, Viz::color::green ()); Pose of the widget in camera frame affine3f cloud_pose = affine3f (). Translate (vec3f (0.0f,0.0f,3.0f)); Pose of the widget in global frame affine3f Cloud_pose_global = transform * cloud_pose; Visualize camera frame if (!camera_pov) {Viz::wcameraposItion CPW (0.5); Coordinate axes viz::wcameraposition cpw_frustum (vec2f (0.889484, 0.523599)); Camera frustum mywindow.showwidget ("CPW", CPW, Cam_pose); Mywindow.showwidget ("Cpw_frustum", Cpw_frustum, Cam_pose); }///Visualize widget Mywindow.showwidget ("Bunny", Cloud_widget, Cloud_pose_global); Set the viewer pose to that camera if (Camera_pov) mywindow.setviewerpose (cam_pose); Start event Loop. Mywindow.spin (); return 0;}
OpenCV tutorials--transformations