OpenCV  3.0.0-rc1
Open Source Computer Vision
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Transformations

Goal

In this tutorial you will learn how to

Code

You can download the code from here.

1 
7 #include <opencv2/viz.hpp>
8 #include <iostream>
9 #include <fstream>
10 
11 using namespace cv;
12 using namespace std;
13 
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 
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 
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 
68  viz::Viz3d myWindow("Coordinate Frame");
69 
71  myWindow.showWidget("Coordinate Widget", viz::WCoordinateSystem());
72 
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 
77  Affine3f cam_pose = viz::makeCameraPose(cam_pos, cam_focal_point, cam_y_dir);
78 
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 
84  Mat bunny_cloud = cvcloud_load();
85  viz::WCloud cloud_widget(bunny_cloud, viz::Color::green());
86 
88  Affine3f cloud_pose = Affine3f().translate(Vec3f(0.0f,0.0f,3.0f));
90  Affine3f cloud_pose_global = transform * cloud_pose;
91 
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 
102  myWindow.showWidget("bunny", cloud_widget, cloud_pose_global);
103 
105  if (camera_pov)
106  myWindow.setViewerPose(cam_pose);
107 
109  myWindow.spin();
110 
111  return 0;
112 }
This 3D Widget represents camera position in a scene by its axes or viewing frustum. :
Definition: widgets.hpp:534
Template class for 3D points specified by its coordinates x, y and z.
Definition: types.hpp:218
Clouds.
Definition: widgets.hpp:671
static Color green()
Vec< float, 3 > Vec3f
Definition: matx.hpp:376
Affine3d makeTransformToGlobal(const Vec3d &axis_x, const Vec3d &axis_y, const Vec3d &axis_z, const Vec3d &origin=Vec3d::all(0))
Takes coordinate frame data and builds transform to global coordinate frame.
Affine3d makeCameraPose(const Vec3d &position, const Vec3d &focal_point, const Vec3d &y_dir)
Constructs camera pose from position, focal_point and up_vector (see gluLookAt() for more infromation...
Compond widgets.
Definition: widgets.hpp:504
The Viz3d class represents a 3D visualizer window. This class is implicitly shared. :
Definition: viz3d.hpp:67
Affine3< float > Affine3f
Definition: affine.hpp:142
#define CV_32FC3
Definition: cvdef.h:148
for i
Definition: modelConvert.m:63
Vec< float, 2 > Vec2f
Definition: matx.hpp:375
Definition: gr_skig.hpp:67
void transform(InputArray src, OutputArray dst, InputArray m)
Performs the matrix transformation of every array element.
int main(int argc, const char *argv[])
Definition: facerec_demo.cpp:67
Affine transform.
Definition: affine.hpp:61

Explanation

Here is the general structure of the program:

Results

  1. Here is the result from the camera point of view.

    camera_view_point.png
  2. Here is the result from global point of view.

    global_view_point.png