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
|
.. _moment_of_inertia:
Moment of inertia and eccentricity based descriptors
----------------------------------------------------
In this tutorial we will learn how to use the `pcl::MomentOfInertiaEstimation` class in order to obtain descriptors based on
eccentricity and moment of inertia. This class also allows to extract axis aligned and oriented bounding boxes of the cloud.
But keep in mind that extracted OBB is not the minimal possible bounding box.
Theoretical Primer
------------------
The idea of the feature extraction method is as follows.
First of all the covariance matrix of the point cloud is calculated and its eigen values and vectors are extracted.
You can consider that the resultant eigen vectors are normalized and always form the right-handed coordinate system
(major eigen vector represents X-axis and the minor vector represents Z-axis). On the next step the iteration process takes place.
On each iteration major eigen vector is rotated. Rotation order is always the same and is performed around the other
eigen vectors, this provides the invariance to rotation of the point cloud. Henceforth, we will refer to this rotated major vector as current axis.
.. image:: images/eigen_vectors.png
:height: 360px
For every current axis moment of inertia is calculated. Moreover, current axis is also used for eccentricity calculation.
For this reason current vector is treated as normal vector of the plane and the input cloud is projected onto it.
After that eccentricity is calculated for the obtained projection.
.. image:: images/projected_cloud.png
:height: 360px
Implemented class also provides methods for getting AABB and OBB. Oriented bounding box is computed as AABB along eigen vectors.
The code
--------
First of all you will need the point cloud for this tutorial.
`This <https://github.com/PointCloudLibrary/data/blob/master/tutorials/min_cut_segmentation_tutorial.pcd>`_ is the one presented on the screenshots.
Next what you need to do is to create a file ``moment_of_inertia.cpp`` in any editor you prefer and copy the following code inside of it:
.. literalinclude:: sources/moment_of_inertia/moment_of_inertia.cpp
:language: cpp
:linenos:
The explanation
---------------
Now let's study out what is the purpose of this code. First few lines will be omitted, as they are obvious.
.. literalinclude:: sources/moment_of_inertia/moment_of_inertia.cpp
:language: cpp
:lines: 13-15
These lines are simply loading the cloud from the .pcd file.
.. literalinclude:: sources/moment_of_inertia/moment_of_inertia.cpp
:language: cpp
:lines: 17-19
Here is the line where the instantiation of the ``pcl::MomentOfInertiaEstimation`` class takes place.
Immediately after that we set the input cloud and start the computational process, that easy.
.. literalinclude:: sources/moment_of_inertia/moment_of_inertia.cpp
:language: cpp
:lines: 21-31
This is were we declare all necessary variables needed to store descriptors and bounding boxes.
.. literalinclude:: sources/moment_of_inertia/moment_of_inertia.cpp
:language: cpp
:lines: 33-39
These lines show how to access computed descriptors and other features.
.. literalinclude:: sources/moment_of_inertia/moment_of_inertia.cpp
:language: cpp
:lines: 41-46
These lines simply create the instance of ``PCLVisualizer`` class for result visualization.
Here we also add the cloud and the AABB for visualization.
.. literalinclude:: sources/moment_of_inertia/moment_of_inertia.cpp
:language: cpp
:lines: 48-50
Visualization of the OBB is little more complex. So here we create a quaternion from the rotational matrix, set OBBs position
and pass it to the visualizer.
.. literalinclude:: sources/moment_of_inertia/moment_of_inertia.cpp
:language: cpp
:lines: 52-58
This lines are responsible for eigen vectors visualization.
.. literalinclude:: sources/moment_of_inertia/moment_of_inertia.cpp
:language: cpp
:lines: 60-98
This huge amount of code shows how to work with the oriented bounding box. Note that you need to rotate each of the vertices of the OBB.
This code does the same thing as ``PCLVisualizer::addCube ()`` method. Its only purpose is to show how to work with OBB
if you don't have such usable method as ``PCLVisualizer::addCube ()``.
Few lines that left simply launch the visualization process.
Compiling and running the program
---------------------------------
Add the following lines to your CMakeLists.txt file:
.. literalinclude:: sources/moment_of_inertia/CMakeLists.txt
:language: cmake
:linenos:
After you have made the executable, you can run it. Simply do::
$ ./moment_of_inertia lamppost.pcd
You should see something similar to this image. Here AABB is yellow, OBB is red. You can also see the eigen vectors.
.. image:: images/moment_of_inertia.png
:height: 360px
|