2013-09-07 23:26:57 +08:00
|
|
|
/**
|
|
|
|
* @file transformations.cpp
|
|
|
|
* @brief Visualizing cloud in different positions, coordinate frames, camera frustums
|
|
|
|
* @author Ozan Cagri Tonkal
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <opencv2/viz.hpp>
|
|
|
|
#include <iostream>
|
2013-09-08 00:23:26 +08:00
|
|
|
#include <fstream>
|
2013-09-07 23:26:57 +08:00
|
|
|
|
|
|
|
using namespace cv;
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @function help
|
|
|
|
* @brief Display instructions to use this tutorial program
|
|
|
|
*/
|
|
|
|
void help()
|
|
|
|
{
|
|
|
|
cout
|
|
|
|
<< "--------------------------------------------------------------------------" << endl
|
|
|
|
<< "This program shows how to use makeTransformToGlobal() to compute required pose,"
|
|
|
|
<< "how to use makeCameraPose and Viz3d::setViewerPose. You can observe the scene "
|
|
|
|
<< "from camera point of view (C) or global point of view (G)" << endl
|
|
|
|
<< "Usage:" << endl
|
2013-09-08 00:23:26 +08:00
|
|
|
<< "./transformations [ G | C ]" << endl
|
2013-09-07 23:26:57 +08:00
|
|
|
<< endl;
|
|
|
|
}
|
|
|
|
|
2013-09-08 00:23:26 +08:00
|
|
|
/**
|
|
|
|
* @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 < 12; ++i)
|
|
|
|
getline(ifs, str);
|
|
|
|
|
|
|
|
Point3f* data = cloud.ptr<cv::Point3f>();
|
|
|
|
float dummy1, dummy2;
|
|
|
|
for(size_t i = 0; i < 1889; ++i)
|
|
|
|
ifs >> data[i].x >> data[i].y >> data[i].z >> dummy1 >> dummy2;
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-09-08 00:23:26 +08:00
|
|
|
cloud *= 5.0f;
|
|
|
|
return cloud;
|
|
|
|
}
|
|
|
|
|
2013-09-07 23:26:57 +08:00
|
|
|
/**
|
|
|
|
* @function main
|
|
|
|
*/
|
|
|
|
int main(int argn, char **argv)
|
|
|
|
{
|
|
|
|
help();
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-09-07 23:26:57 +08:00
|
|
|
if (argn < 2)
|
|
|
|
{
|
|
|
|
cout << "Missing arguments." << endl;
|
|
|
|
return 1;
|
|
|
|
}
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-09-07 23:26:57 +08:00
|
|
|
bool camera_pov = (argv[1][0] == 'C');
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-09-07 23:26:57 +08:00
|
|
|
/// Create a window
|
|
|
|
viz::Viz3d myWindow("Coordinate Frame");
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-09-07 23:26:57 +08:00
|
|
|
/// Add coordinate axes
|
2013-09-15 22:26:53 +08:00
|
|
|
myWindow.showWidget("Coordinate Widget", viz::WCoordinateSystem());
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-09-07 23:26:57 +08:00
|
|
|
/// Let's assume camera has the following properties
|
2016-06-27 16:01:06 +08:00
|
|
|
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);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-09-07 23:26:57 +08:00
|
|
|
/// We can get the pose of the cam using makeCameraPose
|
|
|
|
Affine3f cam_pose = viz::makeCameraPose(cam_pos, cam_focal_point, cam_y_dir);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-09-07 23:26:57 +08:00
|
|
|
/// 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);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-09-07 23:26:57 +08:00
|
|
|
/// Create a cloud widget.
|
2013-09-08 00:23:26 +08:00
|
|
|
Mat bunny_cloud = cvcloud_load();
|
2013-09-15 22:26:53 +08:00
|
|
|
viz::WCloud cloud_widget(bunny_cloud, viz::Color::green());
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-09-07 23:26:57 +08:00
|
|
|
/// Pose of the widget in camera frame
|
2013-09-08 00:23:26 +08:00
|
|
|
Affine3f cloud_pose = Affine3f().translate(Vec3f(0.0f,0.0f,3.0f));
|
2013-09-07 23:26:57 +08:00
|
|
|
/// Pose of the widget in global frame
|
2013-09-08 00:23:26 +08:00
|
|
|
Affine3f cloud_pose_global = transform * cloud_pose;
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-09-07 23:26:57 +08:00
|
|
|
/// Visualize camera frame
|
|
|
|
if (!camera_pov)
|
|
|
|
{
|
2013-09-15 22:26:53 +08:00
|
|
|
viz::WCameraPosition cpw(0.5); // Coordinate axes
|
|
|
|
viz::WCameraPosition cpw_frustum(Vec2f(0.889484, 0.523599)); // Camera frustum
|
2013-09-07 23:26:57 +08:00
|
|
|
myWindow.showWidget("CPW", cpw, cam_pose);
|
|
|
|
myWindow.showWidget("CPW_FRUSTUM", cpw_frustum, cam_pose);
|
|
|
|
}
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-09-07 23:26:57 +08:00
|
|
|
/// Visualize widget
|
2013-09-08 00:23:26 +08:00
|
|
|
myWindow.showWidget("bunny", cloud_widget, cloud_pose_global);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-09-07 23:26:57 +08:00
|
|
|
/// Set the viewer pose to that of camera
|
|
|
|
if (camera_pov)
|
|
|
|
myWindow.setViewerPose(cam_pose);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-09-07 23:26:57 +08:00
|
|
|
/// Start event loop.
|
|
|
|
myWindow.spin();
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-09-07 23:26:57 +08:00
|
|
|
return 0;
|
|
|
|
}
|