Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
O
opencv
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
opencv
Commits
2969ec02
Commit
2969ec02
authored
Sep 07, 2013
by
Ozan Tonkal
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
transformations tutorial rst
parent
5307339f
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
234 additions
and
6 deletions
+234
-6
camera_view_point.png
doc/tutorials/viz/images/camera_view_point.png
+0
-0
global_view_point.png
doc/tutorials/viz/images/global_view_point.png
+0
-0
red_triangle.png
doc/tutorials/viz/images/red_triangle.png
+0
-0
transformations.rst
doc/tutorials/viz/transformations.rst
+202
-0
bunny.ply
samples/cpp/tutorial_code/viz/bunny.ply
+0
-0
transformations.cpp
samples/cpp/tutorial_code/viz/transformations.cpp
+32
-6
No files found.
doc/tutorials/viz/images/camera_view_point.png
0 → 100644
View file @
2969ec02
18 KB
doc/tutorials/viz/images/global_view_point.png
0 → 100644
View file @
2969ec02
13.4 KB
doc/tutorials/viz/images/red_triangle.png
0 → 100644
View file @
2969ec02
10.2 KB
doc/tutorials/viz/transformations.rst
0 → 100644
View file @
2969ec02
.. _transformations:
Transformations
***************
Goal
====
In this tutorial you will learn how to
.. container:: enumeratevisibleitemswithsquare
* How to use makeTransformToGlobal to compute pose
* How to use makeCameraPose and Viz3d::setViewerPose
* How to visualize camera position by axes and by viewing frustum
Code
====
You can download the code from `here <../../../../samples/cpp/tutorial_code/viz/transformations.cpp>`_.
.. code-block:: cpp
#include <opencv2/viz.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 < 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;
cloud *= 5.0f;
return cloud;
}
/**
* @function main
*/
int main(int argn, char **argv)
{
if (argn < 2)
{
cout << "Usage: " << endl << "./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::CoordinateSystemWidget());
/// 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_point, 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::CloudWidget 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::CameraPositionWidget cpw(0.5); // Coordinate axes
viz::CameraPositionWidget 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 of camera
if (camera_pov)
myWindow.setViewerPose(cam_pose);
/// Start event loop.
myWindow.spin();
return 0;
}
Explanation
===========
Here is the general structure of the program:
* Create a visualization window.
.. code-block:: cpp
/// Create a window
viz::Viz3d myWindow("Transformations");
* Get camera pose from camera position, camera focal point and y direction.
.. code-block:: cpp
/// 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_point, cam_y_dir);
* Obtain transform matrix knowing the axes of camera coordinate system.
.. code-block:: cpp
/// 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 from bunny.ply file
.. code-block:: cpp
/// Create a cloud widget.
Mat bunny_cloud = cvcloud_load();
viz::CloudWidget cloud_widget(bunny_cloud, viz::Color::green());
* Given the pose in camera coordinate system, estimate the global pose.
.. code-block:: cpp
/// 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;
* If the view point is set to be global, visualize camera coordinate frame and viewing frustum.
.. code-block:: cpp
/// Visualize camera frame
if (!camera_pov)
{
viz::CameraPositionWidget cpw(0.5); // Coordinate axes
viz::CameraPositionWidget cpw_frustum(Vec2f(0.889484, 0.523599)); // Camera frustum
myWindow.showWidget("CPW", cpw, cam_pose);
myWindow.showWidget("CPW_FRUSTUM", cpw_frustum, cam_pose);
}
* Visualize the cloud widget with the estimated global pose
.. code-block:: cpp
/// Visualize widget
myWindow.showWidget("bunny", cloud_widget, cloud_pose_global);
* If the view point is set to be camera's, set viewer pose to **cam_pose**.
.. code-block:: cpp
/// Set the viewer pose to that of camera
if (camera_pov)
myWindow.setViewerPose(cam_pose);
Results
=======
#. Here is the result from the camera point of view.
.. image:: images/camera_view_point.png
:alt: Camera Viewpoint
:align: center
#. Here is the result from global point of view.
.. image:: images/global_view_point.png
:alt: Global Viewpoint
:align: center
samples/cpp/tutorial_code/viz/bunny.ply
0 → 100644
View file @
2969ec02
This diff is collapsed.
Click to expand it.
samples/cpp/tutorial_code/viz/transformations.cpp
View file @
2969ec02
...
...
@@ -6,6 +6,7 @@
#include <opencv2/viz.hpp>
#include <iostream>
#include <fstream>
using
namespace
cv
;
using
namespace
std
;
...
...
@@ -22,10 +23,32 @@ void help()
<<
"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
<<
"./
coordinate_frame
[ G | C ]"
<<
endl
<<
"./
transformations
[ G | C ]"
<<
endl
<<
endl
;
}
/**
* @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
;
cloud
*=
5.0
f
;
return
cloud
;
}
/**
* @function main
*/
...
...
@@ -48,20 +71,23 @@ int main(int argn, char **argv)
myWindow
.
showWidget
(
"Coordinate Widget"
,
viz
::
CoordinateSystemWidget
());
/// Let's assume camera has the following properties
Point3f
cam_pos
(
3.0
f
,
3.0
f
,
3.0
f
),
cam_focal_point
(
3.0
f
,
3.0
f
,
2.0
f
),
cam_y_dir
(
-
1.0
,
0.0
,
0.0
);
Point3f
cam_pos
(
3.0
f
,
3.0
f
,
3.0
f
),
cam_focal_point
(
3.0
f
,
3.0
f
,
2.0
f
),
cam_y_dir
(
-
1.0
f
,
0.0
f
,
0.0
f
);
/// We can get the pose of the cam using makeCameraPose
Affine3f
cam_pose
=
viz
::
makeCameraPose
(
cam_pos
,
cam_focal_point
,
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.0
f
,
-
1.0
f
,
0.0
f
),
Vec3f
(
-
1.0
f
,
0.0
f
,
0.0
f
),
Vec3f
(
0.0
f
,
0.0
f
,
-
1.0
f
),
cam_pos
);
/// Create a cloud widget.
viz
::
SphereWidget
sphere_widget
(
Point3f
(
0.0
,
0.0
,
0.0
),
0.5
,
10
,
viz
::
Color
::
red
());
Mat
bunny_cloud
=
cvcloud_load
();
viz
::
CloudWidget
cloud_widget
(
bunny_cloud
,
viz
::
Color
::
green
());
/// Pose of the widget in camera frame
Affine3f
sphere
_pose
=
Affine3f
().
translate
(
Vec3f
(
0.0
f
,
0.0
f
,
3.0
f
));
Affine3f
cloud
_pose
=
Affine3f
().
translate
(
Vec3f
(
0.0
f
,
0.0
f
,
3.0
f
));
/// Pose of the widget in global frame
Affine3f
sphere_pose_global
=
transform
*
sphere
_pose
;
Affine3f
cloud_pose_global
=
transform
*
cloud
_pose
;
/// Visualize camera frame
if
(
!
camera_pov
)
...
...
@@ -73,7 +99,7 @@ int main(int argn, char **argv)
}
/// Visualize widget
myWindow
.
showWidget
(
"
sphere"
,
sphere_widget
,
sphere
_pose_global
);
myWindow
.
showWidget
(
"
bunny"
,
cloud_widget
,
cloud
_pose_global
);
/// Set the viewer pose to that of camera
if
(
camera_pov
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment