Construct a convex hull polygon for a plane model
In this tutorial we will learn how to calculate a simple 2D convex hull polygon for a set of points supported by a plane.
The following video shows a demonstration of the code given below on the test dataset table_scene_mug_stereo_textured.pcd.
The code
First, download the dataset table_scene_mug_stereo_textured.pcd and save it somewhere to disk.
Then, create a file, let’s say, convex_hull_2d.cpp
in your favorite
editor, and place the following inside it:
1#include <pcl/ModelCoefficients.h>
2#include <pcl/io/pcd_io.h>
3#include <pcl/point_types.h>
4#include <pcl/sample_consensus/method_types.h>
5#include <pcl/sample_consensus/model_types.h>
6#include <pcl/filters/passthrough.h>
7#include <pcl/filters/project_inliers.h>
8#include <pcl/segmentation/sac_segmentation.h>
9#include <pcl/surface/convex_hull.h>
10
11int
12 main ()
13{
14 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>), cloud_filtered (new pcl::PointCloud<pcl::PointXYZ>), cloud_projected (new pcl::PointCloud<pcl::PointXYZ>);
15 pcl::PCDReader reader;
16 reader.read ("table_scene_mug_stereo_textured.pcd", *cloud);
17
18 // Build a filter to remove spurious NaNs and scene background
19 pcl::PassThrough<pcl::PointXYZ> pass;
20 pass.setInputCloud (cloud);
21 pass.setFilterFieldName ("z");
22 pass.setFilterLimits (0, 1.1);
23 pass.filter (*cloud_filtered);
24 std::cerr << "PointCloud after filtering has: " << cloud_filtered->size () << " data points." << std::endl;
25
26 pcl::ModelCoefficients::Ptr coefficients (new pcl::ModelCoefficients);
27 pcl::PointIndices::Ptr inliers (new pcl::PointIndices);
28 // Create the segmentation object
29 pcl::SACSegmentation<pcl::PointXYZ> seg;
30 // Optional
31 seg.setOptimizeCoefficients (true);
32 // Mandatory
33 seg.setModelType (pcl::SACMODEL_PLANE);
34 seg.setMethodType (pcl::SAC_RANSAC);
35 seg.setDistanceThreshold (0.01);
36
37 seg.setInputCloud (cloud_filtered);
38 seg.segment (*inliers, *coefficients);
39
40 // Project the model inliers
41 pcl::ProjectInliers<pcl::PointXYZ> proj;
42 proj.setModelType (pcl::SACMODEL_PLANE);
43 proj.setInputCloud (cloud_filtered);
44 proj.setIndices (inliers);
45 proj.setModelCoefficients (coefficients);
46 proj.filter (*cloud_projected);
47
48 // Create a Convex Hull representation of the projected inliers
49 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_hull (new pcl::PointCloud<pcl::PointXYZ>);
50 pcl::ConvexHull<pcl::PointXYZ> chull;
51 chull.setInputCloud (cloud_projected);
52 chull.reconstruct (*cloud_hull);
53
54 std::cerr << "Convex hull has: " << cloud_hull->size () << " data points." << std::endl;
55
56 pcl::PCDWriter writer;
57 writer.write ("table_scene_mug_stereo_textured_hull.pcd", *cloud_hull, false);
58
59 return (0);
60}
The explanation
The only interesting part is in the lines below, where the ConvexHull object gets created and the reconstruction is performed:
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_hull (new pcl::PointCloud<pcl::PointXYZ>);
pcl::ConvexHull<pcl::PointXYZ> chull;
chull.setInputCloud (cloud_projected);
chull.reconstruct (*cloud_hull);
Compiling and running the program
Add the following lines to your CMakeLists.txt file:
1cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
2
3project(convex_hull_2d)
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 (convex_hull_2d convex_hull_2d.cpp)
12target_link_libraries (convex_hull_2d ${PCL_LIBRARIES})
After you have made the executable, you can run it. Simply do:
$ ./convex_hull_2d
You will see something similar to:
PointCloud after filtering has: 139656 data points.
Convex hull has: 30 data points.