Skip to content

Commit

Permalink
Merge pull request #702 from VictorLamoine/timoHackel
Browse files Browse the repository at this point in the history
Model outlier removal contribution
  • Loading branch information
taketwo committed Jun 15, 2014
2 parents 9dac3ec + 5206f4f commit 2e20bc8
Show file tree
Hide file tree
Showing 10 changed files with 864 additions and 0 deletions.
14 changes: 14 additions & 0 deletions doc/tutorials/content/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,20 @@ Segmentation
.. |se_10| image:: images/progressive_morphological_filter.png
:height: 100px

* :ref:`model_outlier_removal`

======= ======
|se_11| Title: **Model outlier removal**

Author: *Timo Häckel*

Compatibility: >= PCL 1.7.2

This tutorial describes how to extract points from a point cloud using SAC models
======= ======

.. |se_11| image:: images/pcl_logo.png
:height: 75px

.. _surface_tutorial:

Expand Down
81 changes: 81 additions & 0 deletions doc/tutorials/content/model_outlier_removal.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
.. _model_outlier_removal:

Filtering a PointCloud using ModelOutlierRemoval
------------------------------------------------

This tutorial demonstrates how to extract parametric models for example for planes or spheres
out of a PointCloud by using SAC_Models with known coefficients.
If you don't know the models coefficients take a look at the :ref:`random_sample_consensus` tutorial.

The code
--------

First, create a file, let's call it ``model_outlier_removal.cpp``, in your favorite
editor, and place the following inside it:

.. literalinclude:: sources/model_outlier_removal/model_outlier_removal.cpp
:language: cpp
:linenos:

The explanation
---------------

Now, let's break down the code piece by piece.

In the following lines, we define the PointClouds structures, fill in noise, random points
on a plane as well as random points on a sphere and display its content to screen.

.. literalinclude:: sources/model_outlier_removal/model_outlier_removal.cpp
:language: cpp
:lines: 7-45

Finally we extract the sphere using ModelOutlierRemoval.

.. literalinclude:: sources/model_outlier_removal/model_outlier_removal.cpp
:language: cpp
:lines: 50-61

Compiling and running the program
---------------------------------

Add the following lines to your CMakeLists.txt file:

.. literalinclude:: sources/model_outlier_removal/CMakeLists.txt
:language: cmake
:linenos:


After you have made the executable, you can run it. Simply do::

$ ./model_outlier_removal

You will see something similar to::

Cloud before filtering:
0.352222 -0.151883 -0.106395
-0.397406 -0.473106 0.292602
-0.731898 0.667105 0.441304
-0.734766 0.854581 -0.0361733
-0.4607 -0.277468 -0.916762
-0.82 -0.341666 0.4592
-0.728589 0.667873 0.152
-0.3134 -0.873043 -0.3736
0.62553 0.590779 0.5096
-0.54048 0.823588 -0.172
-0.707627 0.424576 0.5648
-0.83153 0.523556 0.1856
-0.513903 -0.719464 0.4672
0.291534 0.692393 0.66
0.258758 0.654505 -0.7104
Sphere after filtering:
-0.82 -0.341666 0.4592
-0.728589 0.667873 0.152
-0.3134 -0.873043 -0.3736
0.62553 0.590779 0.5096
-0.54048 0.823588 -0.172
-0.707627 0.424576 0.5648
-0.83153 0.523556 0.1856
-0.513903 -0.719464 0.4672
0.291534 0.692393 0.66
0.258758 0.654505 -0.7104

12 changes: 12 additions & 0 deletions doc/tutorials/content/sources/model_outlier_removal/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)

project(model_outlier_removal)

find_package(PCL 1.7 REQUIRED)

include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})

add_executable (model_outlier_removal model_outlier_removal.cpp)
target_link_libraries (model_outlier_removal ${PCL_LIBRARIES})
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include <iostream>
#include <pcl/point_types.h>
#include <pcl/filters/model_outlier_removal.h>

int
main ()
{
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_sphere_filtered (new pcl::PointCloud<pcl::PointXYZ>);

// 1. Generate cloud data
int noise_size = 5;
int sphere_data_size = 10;
cloud->width = noise_size + sphere_data_size;
cloud->height = 1;
cloud->points.resize (cloud->width * cloud->height);
// 1.1 Add noise
for (size_t i = 0; i < noise_size; ++i)
{
cloud->points[i].x = 1024 * rand () / (RAND_MAX + 1.0f);
cloud->points[i].y = 1024 * rand () / (RAND_MAX + 1.0f);
cloud->points[i].z = 1024 * rand () / (RAND_MAX + 1.0f);
}
// 1.2 Add sphere:
double rand_x1 = 1;
double rand_x2 = 1;
for (size_t i = noise_size; i < noise_size + sphere_data_size; ++i)
{
// See: http://mathworld.wolfram.com/SpherePointPicking.html
while (pow (rand_x1, 2) + pow (rand_x2, 2) >= 1)
{
rand_x1 = (rand () % 100) / (50.0f) - 1;
rand_x2 = (rand () % 100) / (50.0f) - 1;
}
double pre_calc = sqrt (1 - pow (rand_x1, 2) - pow (rand_x2, 2));
cloud->points[i].x = 2 * rand_x1 * pre_calc;
cloud->points[i].y = 2 * rand_x2 * pre_calc;
cloud->points[i].z = 1 - 2 * (pow (rand_x1, 2) + pow (rand_x2, 2));
rand_x1 = 1;
rand_x2 = 1;
}

std::cerr << "Cloud before filtering: " << std::endl;
for (size_t i = 0; i < cloud->points.size (); ++i)
std::cout << " " << cloud->points[i].x << " " << cloud->points[i].y << " " << cloud->points[i].z << std::endl;

// 2. filter sphere:
// 2.1 generate model:
// modelparameter for this sphere:
// position.x: 0, position.y: 0, position.z:0, radius: 1
pcl::ModelCoefficients sphere_coeff;
sphere_coeff.values.resize (4);
sphere_coeff.values[0] = 0;
sphere_coeff.values[1] = 0;
sphere_coeff.values[2] = 0;
sphere_coeff.values[3] = 1;

pcl::ModelOutlierRemoval<pcl::PointXYZ> sphere_filter;
sphere_filter.setModelCoefficients (sphere_coeff);
sphere_filter.setThreshold (0.05);
sphere_filter.setModelType (pcl::SACMODEL_SPHERE);
sphere_filter.setInputCloud (cloud);
sphere_filter.filter (*cloud_sphere_filtered);

std::cerr << "Sphere after filtering: " << std::endl;
for (size_t i = 0; i < cloud_sphere_filtered->points.size (); ++i)
std::cout << " " << cloud_sphere_filtered->points[i].x << " " << cloud_sphere_filtered->points[i].y << " " << cloud_sphere_filtered->points[i].z
<< std::endl;

return (0);
}
3 changes: 3 additions & 0 deletions filters/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ if(build)
src/grid_minimum.cpp
src/morphological_filter.cpp
src/local_maximum.cpp
src/model_outlier_removal.cpp
)

set(incs
Expand Down Expand Up @@ -77,6 +78,7 @@ if(build)
"include/pcl/${SUBSYS_NAME}/grid_minimum.h"
"include/pcl/${SUBSYS_NAME}/morphological_filter.h"
"include/pcl/${SUBSYS_NAME}/local_maximum.h"
"include/pcl/${SUBSYS_NAME}/model_outlier_removal.h"
)

set(impl_incs
Expand Down Expand Up @@ -112,6 +114,7 @@ if(build)
"include/pcl/${SUBSYS_NAME}/impl/grid_minimum.hpp"
"include/pcl/${SUBSYS_NAME}/impl/morphological_filter.hpp"
"include/pcl/${SUBSYS_NAME}/impl/local_maximum.hpp"
"include/pcl/${SUBSYS_NAME}/impl/model_outlier_removal.hpp"
)

set(LIB_NAME "pcl_${SUBSYS_NAME}")
Expand Down
Loading

0 comments on commit 2e20bc8

Please sign in to comment.