# RoPs (Rotational Projection Statistics) feature

In this tutorial we will learn how to use the pcl::ROPSEstimation class in order to extract points features. The feature extraction method implemented in this class was proposed by Yulan Guo, Ferdous Sohel, Mohammed Bennamoun, Min Lu and Jianwei Wanalso in their article “Rotational Projection Statistics for 3D Local Surface Description and Object Recognition”

# Theoretical Primer

The idea of the feature extraction method is as follows. Having a mesh and a set of points for which feature must be computed we perform some simple steps. First of all for a given point of interest the local surface is cropped. Local surface consists of the points and triangles that are within the given support radius. For the given local surface LRF (Local Reference Frame) is computed. LRF is simply a triplet of vectors, the comprehensive information about how these vectors are computed you can find in the article. What is really important is that using these vectors we can provide the invariance to the rotation of the cloud. To do that, we simply translate points of the local surface in such way that point of interest became the origin, after that we rotate local surface so that the LRF vectors were aligned with the Ox, Oy and Oz axes. Having this done, we then start the feature extraction. For every axis Ox, Oy and Oz the following steps are performed, we will refer to these axes as current axis:

• local surface is rotated around the current axis by a given angle;
• points of the rotated local surface are projected onto three planes XY, XZ and YZ;
• for each projection distribution matrix is built, this matrix simply shows how much points fall onto each bin. Number of bins represents the matrix dimension and is the parameter of the algorithm, as well as the support radius;
• for each distribution matrix central moments are calculated: M11, M12, M21, M22, E. Here E is the Shannon entropy;
• calculated values are then concatenated to form the sub-feature.

We iterate through these steps several times. Number of iterations depends on the given number of rotations. Sub-features for different axes are concatenated to form the final RoPS descriptor.

# The code

For this tutorial we will use the model from the Queen’s Dataset. You can choose any other point cloud, but in order to make the code work you will need to use the triangulation algorithm in order to obtain polygons. You can find the proposed model here:

• points - contains the point cloud
• `indices - contains indices of the points for which RoPs must be computed
• `triangles - contains the polygons

Next what you need to do is to create a file `rops_feature.cpp` in any editor you prefer and copy the following code inside of it:

 ``` 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``` ```#include #include int main (int argc, char** argv) { if (argc != 4) return (-1); pcl::PointCloud::Ptr cloud (new pcl::PointCloud ()); if (pcl::io::loadPCDFile (argv[1], *cloud) == -1) return (-1); pcl::PointIndicesPtr indices (new pcl::PointIndices); std::ifstream indices_file; indices_file.open (argv[2], std::ifstream::in); for (std::string line; std::getline (indices_file, line);) { std::istringstream in (line); unsigned int index = 0; in >> index; indices->indices.push_back (index - 1); } indices_file.close (); std::vector triangles; std::ifstream triangles_file; triangles_file.open (argv[3], std::ifstream::in); for (std::string line; std::getline (triangles_file, line);) { pcl::Vertices triangle; std::istringstream in (line); unsigned int vertex = 0; in >> vertex; triangle.vertices.push_back (vertex - 1); in >> vertex; triangle.vertices.push_back (vertex - 1); in >> vertex; triangle.vertices.push_back (vertex - 1); triangles.push_back (triangle); } float support_radius = 0.0285f; unsigned int number_of_partition_bins = 5; unsigned int number_of_rotations = 3; pcl::search::KdTree::Ptr search_method (new pcl::search::KdTree); search_method->setInputCloud (cloud); pcl::ROPSEstimation > feature_estimator; feature_estimator.setSearchMethod (search_method); feature_estimator.setSearchSurface (cloud); feature_estimator.setInputCloud (cloud); feature_estimator.setIndices (indices); feature_estimator.setTriangles (triangles); feature_estimator.setRadiusSearch (support_radius); feature_estimator.setNumberOfPartitionBins (number_of_partition_bins); feature_estimator.setNumberOfRotations (number_of_rotations); feature_estimator.setSupportRadius (support_radius); pcl::PointCloud >::Ptr histograms (new pcl::PointCloud > ()); feature_estimator.compute (*histograms); return (0); } ```

# The explanation

Now let’s study out what is the purpose of this code.

```  pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ> ());
if (pcl::io::loadPCDFile (argv[1], *cloud) == -1)
return (-1);
```

```  pcl::PointIndicesPtr indices (new pcl::PointIndices);
std::ifstream indices_file;
indices_file.open (argv[2], std::ifstream::in);
for (std::string line; std::getline (indices_file, line);)
{
std::istringstream in (line);
unsigned int index = 0;
in >> index;
indices->indices.push_back (index - 1);
}
indices_file.close ();
```

Here the indices of points for which RoPS feature must be computed are loaded. You can comment it and compute features for every single point in the cloud. if you want.

```  std::vector <pcl::Vertices> triangles;
std::ifstream triangles_file;
triangles_file.open (argv[3], std::ifstream::in);
for (std::string line; std::getline (triangles_file, line);)
{
pcl::Vertices triangle;
std::istringstream in (line);
unsigned int vertex = 0;
in >> vertex;
triangle.vertices.push_back (vertex - 1);
in >> vertex;
triangle.vertices.push_back (vertex - 1);
in >> vertex;
triangle.vertices.push_back (vertex - 1);
triangles.push_back (triangle);
}
```

These lines are loading the information about the polygons. You can replace them with the code for the triangulation if you have only the point cloud instead of the mesh.

```  float support_radius = 0.0285f;
unsigned int number_of_partition_bins = 5;
unsigned int number_of_rotations = 3;
```

These code defines important algorithm parameters: support radius for local surface cropping, number of partition bins used to form the distribution matrix and the number of rotations. The last parameter affects the length of the descriptor.

```  pcl::search::KdTree<pcl::PointXYZ>::Ptr search_method (new pcl::search::KdTree<pcl::PointXYZ>);
search_method->setInputCloud (cloud);
```

These lines set up the search method that will be used by the algorithm.

```  pcl::ROPSEstimation <pcl::PointXYZ, pcl::Histogram <135> > feature_estimator;
feature_estimator.setSearchMethod (search_method);
feature_estimator.setSearchSurface (cloud);
feature_estimator.setInputCloud (cloud);
feature_estimator.setIndices (indices);
feature_estimator.setTriangles (triangles);
feature_estimator.setNumberOfPartitionBins (number_of_partition_bins);
feature_estimator.setNumberOfRotations (number_of_rotations);
```

Here is the place where the instantiation of the `pcl::ROPSEstimation` class takes place. It has two parameters:

• PointInT - type of the input points;
• PointOutT - type of the output points.

Immediately after that we set the input all the necessary data needed for the feature computation.

```  pcl::PointCloud<pcl::Histogram <135> >::Ptr histograms (new pcl::PointCloud <pcl::Histogram <135> > ());
feature_estimator.compute (*histograms);
```

Here is the place where the computational process is launched.

# Compiling and running the program

 ``` 1 2 3 4 5 6 7 8 9 10 11 12``` ```cmake_minimum_required(VERSION 2.8 FATAL_ERROR) project(rops_feature) find_package(PCL 1.8 REQUIRED) include_directories(\${PCL_INCLUDE_DIRS}) link_directories(\${PCL_LIBRARY_DIRS}) add_definitions(\${PCL_DEFINITIONS}) add_executable (rops_feature rops_feature.cpp) target_link_libraries (rops_feature \${PCL_LIBRARIES}) ```
```\$ ./rops_feature points.pcd indices.txt triangles.txt