Removing outliers using a RadiusOutlierRemoval filter
This document demonstrates how to create and use a RadiusOutlierRemoval object that can be used to remove points from a PointCloud that do not have a given number of neighbors within a specific radius from their location.
The code
First, create a file, let’s say, radius_outlier_removal.cpp in your favorite editor, and place the following inside it:
1#include <iostream>
2#include <pcl/point_types.h>
3#include <pcl/filters/radius_outlier_removal.h>
4
5int
6 main ()
7{
8 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
9 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered (new pcl::PointCloud<pcl::PointXYZ>);
10
11 // Fill in the cloud data
12 cloud->width = 5;
13 cloud->height = 1;
14 cloud->points.resize (cloud->width * cloud->height);
15
16 for (auto& point: *cloud)
17 {
18 point.x = 1024 * rand () / (RAND_MAX + 1.0f);
19 point.y = 1024 * rand () / (RAND_MAX + 1.0f);
20 point.z = 1024 * rand () / (RAND_MAX + 1.0f);
21 }
22
23 std::cerr << "Cloud before filtering: " << std::endl;
24 for (const auto& point: *cloud)
25 std::cerr << " " << point.x << " "
26 << point.y << " "
27 << point.z << std::endl;
28 // build the filter
29 pcl::RadiusOutlierRemoval<pcl::PointXYZ> outrem;
30 outrem.setInputCloud(cloud);
31 outrem.setRadiusSearch(0.8);
32 outrem.setMinNeighborsInRadius (2);
33
34 // apply filter
35 outrem.filter (*cloud_filtered);
36
37 // display pointcloud after filtering
38 std::cerr << "Cloud after filtering: " << std::endl;
39 for (const auto& point: *cloud_filtered)
40 std::cerr << " " << point.x << " "
41 << point.y << " "
42 << point.z << std::endl;
43 return (0);
44}
The explanation
Now, let’s break down the code piece by piece.
In the following lines, we define the PointCloud structures, fill in the input cloud and display it’s contents to the screen.
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered (new pcl::PointCloud<pcl::PointXYZ>);
// Fill in the cloud data
cloud->width = 5;
cloud->height = 1;
cloud->points.resize (cloud->width * cloud->height);
for (auto& point: *cloud)
{
point.x = 1024 * rand () / (RAND_MAX + 1.0f);
point.y = 1024 * rand () / (RAND_MAX + 1.0f);
point.z = 1024 * rand () / (RAND_MAX + 1.0f);
}
std::cerr << "Cloud before filtering: " << std::endl;
for (const auto& point: *cloud)
std::cerr << " " << point.x << " "
<< point.y << " "
<< point.z << std::endl;
Then, we create the RadiusOutlierRemoval filter object, set it’s parameters and apply it to our input cloud. The radius of search is set to 0.8, and a point must have a minimum of 2 neighbors in that radius to be kept as part of the PointCloud.
pcl::RadiusOutlierRemoval<pcl::PointXYZ> outrem;
outrem.setInputCloud(cloud);
outrem.setRadiusSearch(0.8);
outrem.setMinNeighborsInRadius (2);
// apply filter
outrem.filter (*cloud_filtered);
This final block of code just displays the contents of the resulting PointCloud to the screen.
// display pointcloud after filtering
std::cerr << "Cloud after filtering: " << std::endl;
for (const auto& point: *cloud_filtered)
std::cerr << " " << point.x << " "
<< point.y << " "
<< point.z << std::endl;
Compiling and running the program
Add the following lines to your CMakeLists.txt file:
1cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
2
3project(radius_outlier_removal)
4
5find_package(PCL 1.2 REQUIRED)
6
7include_directories(${PCL_INCLUDE_DIRS})
8link_directories(${PCL_LIBRARY_DIRS})
9add_definitions(${PCL_DEFINITIONS})
10
11add_executable (radius_outlier_removal radius_outlier_removal.cpp)
12target_link_libraries (radius_outlier_removal ${PCL_LIBRARIES})
After you have made the executable, you can run it. Simply do:
$ ./conditioinal_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
Cloud after filtering:
-0.731898 0.667105 0.441304
-0.734766 0.854581 -0.0361733