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