File: widget_pose.rst

package info (click to toggle)
opencv 2.4.9.1%2Bdfsg-1%2Bdeb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 126,800 kB
  • ctags: 62,729
  • sloc: xml: 509,055; cpp: 490,794; lisp: 23,208; python: 21,174; java: 19,317; ansic: 1,038; sh: 128; makefile: 72
file content (162 lines) | stat: -rw-r--r-- 4,116 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
.. _widget_pose:

Pose of a widget
****************

Goal
====

In this tutorial you will learn how to

.. container:: enumeratevisibleitemswithsquare

  * Add widgets to the visualization window
  * Use Affine3 to set pose of a widget
  * Rotating and translating a widget along an axis

Code
====

You can download the code from :download:`here <../../../../samples/cpp/tutorial_code/viz/widget_pose.cpp>`.

.. code-block:: cpp

    #include <opencv2/viz/vizcore.hpp>
    #include <opencv2/calib3d/calib3d.hpp>
    #include <iostream>

    using namespace cv;
    using namespace std;

    /**
     * @function main
     */
    int main()
    {
        /// Create a window
        viz::Viz3d myWindow("Coordinate Frame");

        /// Add coordinate axes
        myWindow.showWidget("Coordinate Widget", viz::WCoordinateSystem());

        /// Add line to represent (1,1,1) axis
        viz::WLine axis(Point3f(-1.0f,-1.0f,-1.0f), Point3f(1.0f,1.0f,1.0f));
        axis.setRenderingProperty(viz::LINE_WIDTH, 4.0);
        myWindow.showWidget("Line Widget", axis);

        /// Construct a cube widget
        viz::WCube cube_widget(Point3f(0.5,0.5,0.0), Point3f(0.0,0.0,-0.5), true, viz::Color::blue());
        cube_widget.setRenderingProperty(viz::LINE_WIDTH, 4.0);

        /// Display widget (update if already displayed)
        myWindow.showWidget("Cube Widget", cube_widget);

        /// Rodrigues vector
        Mat rot_vec = Mat::zeros(1,3,CV_32F);
        float translation_phase = 0.0, translation = 0.0;
        while(!myWindow.wasStopped())
        {
            /* Rotation using rodrigues */
            /// Rotate around (1,1,1)
            rot_vec.at<float>(0,0) += CV_PI * 0.01f;
            rot_vec.at<float>(0,1) += CV_PI * 0.01f;
            rot_vec.at<float>(0,2) += CV_PI * 0.01f;

            /// Shift on (1,1,1)
            translation_phase += CV_PI * 0.01f;
            translation = sin(translation_phase);

            Mat rot_mat;
            Rodrigues(rot_vec, rot_mat);

            /// Construct pose
            Affine3f pose(rot_mat, Vec3f(translation, translation, translation));

            myWindow.setWidgetPose("Cube Widget", pose);

            myWindow.spinOnce(1, true);
        }

        return 0;
    }

Explanation
===========

Here is the general structure of the program:

* Create a visualization window.

.. code-block:: cpp

    /// Create a window
    viz::Viz3d myWindow("Coordinate Frame");

* Show coordinate axes in the window using CoordinateSystemWidget.

.. code-block:: cpp

    /// Add coordinate axes
    myWindow.showWidget("Coordinate Widget", viz::WCoordinateSystem());

* Display a line representing the axis (1,1,1).

.. code-block:: cpp

    /// Add line to represent (1,1,1) axis
    viz::WLine axis(Point3f(-1.0f,-1.0f,-1.0f), Point3f(1.0f,1.0f,1.0f));
    axis.setRenderingProperty(viz::LINE_WIDTH, 4.0);
    myWindow.showWidget("Line Widget", axis);

* Construct a cube.

.. code-block:: cpp

    /// Construct a cube widget
    viz::WCube cube_widget(Point3f(0.5,0.5,0.0), Point3f(0.0,0.0,-0.5), true, viz::Color::blue());
    cube_widget.setRenderingProperty(viz::LINE_WIDTH, 4.0);
    myWindow.showWidget("Cube Widget", cube_widget);

* Create rotation matrix from rodrigues vector

.. code-block:: cpp

    /// Rotate around (1,1,1)
    rot_vec.at<float>(0,0) += CV_PI * 0.01f;
    rot_vec.at<float>(0,1) += CV_PI * 0.01f;
    rot_vec.at<float>(0,2) += CV_PI * 0.01f;

    ...

    Mat rot_mat;
    Rodrigues(rot_vec, rot_mat);

* Use Affine3f to set pose of the cube.

.. code-block:: cpp

    /// Construct pose
    Affine3f pose(rot_mat, Vec3f(translation, translation, translation));
    myWindow.setWidgetPose("Cube Widget", pose);

* Animate the rotation using wasStopped and spinOnce

.. code-block:: cpp

    while(!myWindow.wasStopped())
    {
        ...

        myWindow.spinOnce(1, true);
    }

Results
=======

Here is the result of the program.

.. raw:: html

  <div align="center">
  <iframe width="420" height="315" src="https://www.youtube.com/embed/22HKMN657U0" frameborder="0" allowfullscreen></iframe>
  </div>