Downsampling point clouds with PCL

PCL (Point Cloud Library) offers two ways that I know of to downsample a point cloud to a more manageable size.

The first uses pcl::VoxelGrid, and the second uses pcl::octree::OctreePointCloudVoxelCentroid.

They are both simple to use:


using Point = pcl::PointXYZ;
using Cloud = pcl::PointCloud<Point>;

float leaf = 1.f;
pcl::VoxelGrid<Point> vg;
vg.setLeafSize(leaf, leaf, leaf);
Cloud::Ptr output(new Cloud);

Here we create the filtering object, give it an input cloud to work on, give it the only parameter it requires, `leaf` being the voxel dimension, and filter it to a new output cloud.

The size of the resultant output cloud is dependent on the leaf size.


float leaf = 1.f;
pcl::octree::OctreePointCloudVoxelCentroid octree(leaf);
pcl::octree::OctreePointCloud::AlignedPointTVector centroids;

Cloud::Ptr output(new Cloud);
output->points.assign(centroids.begin(), centroids.end());
output->width = uint32_t(centroids.size());
output->height = 1;
output->is_dense = true;

Here we create an octree initialised with it’s voxel dimension, give it an input cloud to work with, and get back a vector of the centroids of all possible voxels. This vector can then be used to construct a new cloud.


In both cases the size of the output cloud is unknown beforehand. It’s dependent on the choice made for the voxel leaf dimension. Only after downsampling can you query the cloud for it’s size.

An iterative method is needed if you want to end up with a given size: You can perform one of the downsampling methods, and based on the resultant output size, adjust leaf up or down and do it again. Stop iterating when the output cloud size converges to within a tolerance of a required size or percentage of the input.

I do this for both methods in my hobby project of pcl-tools.


Building PCL with Qt support

Point Cloud Library (PCL) offers pre-built binaries but these don’t include Qt support. For this you need to build PCL from source. Building PCL from source with Qt support is quite an undertaking as there are many dependencies. This post documents the steps I took to build it.

You need to decide from the outset whether you want a x86 or x64 build of PCL because every dependency must be of the same type. So it influences your choice of download and build of boost, Qt, flann, etc.

Your choice of Qt is important here. The x86 or x64 version of Qt will build the same type of Qt .dlls irrespective of what VS compiler you set it to use. Use the same architecture throughout.

Choice of version of Qt and VTK
You need to go with one of the following version combinations.
VS 2013 + Qt 5.5 (or below) + VTK 6.3 (or below)
VS 2015 + Qt 5.6 (and up) + VTK 7.0 (and up)
This is because of a deprecated feature (QtWebKit) in Qt 5.6 which impacts on what can be built. The installers of Qt have version 5.6.2 as the first one that supports VS 2015.

Combinations of dependencies found to work:
PCL 1.8.0
Qt 5.6.2 x64
Boost 1.59 x64
Eigen 3.2.10
Flann 1.8.4
VTK 7.0.0
CMake 3.6.2 x64
Visual Studio 2015 Community

Boost is installed with a pre-compiled binary, which installs for example like this.


Qt is also installed with the pre-compiled binary


Eigen is installed by simply copying files


CMake-gui is built with a self-executable, choosing the option to add CMake to $path. If you’re unfamiliar with CMake (as I was) then it works like this: Place the downloaded source in one directory. Make another directory for where the build files will be created and where the build will take place. Open CMake and specify these two directories appropriately. Press Configure. You’re asked what generator to use. Choose Visual Studio 2015 x64 to create Visual Studio .sln and .vcxproj files configured for an x64 build. Continue refining the option entries until pressing Configure gives no errors, then press Generate. You then have a Visual Studio solution which can be opened and built using VS.

Flann is configure with CMake and built with Visual Studio. x64 compiler flags. Used default CMake options. The INSTALL project in the VS solution is then built to install the libraries to Program Files. I manually moved files to separate the Release and Debug version of the .dlls and .libs. I think this was necessary for PCL 1.7, but not sure for 1.8

--Program Files\

VTK is configured with CMake. Enable the Qt options. Then build using VS, both the Release and Debug congurations, but don’t install yet. Building debug and release libraries of VTK presents difficulties which need to be handled carefully. We’re going to make use of the build directory still.

PCL is configured with CMake. For FLANN library I used flann_cpp_s because of advice here. I got CMake errors where not all boost libraries were found. I had to add an entry for the BOOST_LIBDIR directory. For VTK_DIR point to the VTK build root directory. Open Visual Studio as Administrator. Select all pcl projects, right click Properties > Debugging > Environment and set to


Do this for both Release and Debug congurations. In this way when the compile needs the appropriate VTK library the path will be pointing at the appropriate build library depending on the active configuration.
Build both Release and Debug congurations.
A build error:

C:\Program Files\flann\include\flann/util/serialization.h(18): error C2228: left of '.serialize' must have class/struct/union;

solution here.
Another build error:

error C2440: Conversions between enumeration and floating point values are no longer allowed

Solution: Modify this:




and build the INSTALL project.

--Program Files\

Reopen the VTK.sln in Visual Studio as Administrator. Build the INSTALL project to install both Release and Debug configuration but with manually creating Debug and Release for bin/ and lib/ subdirectories and moving files to create following structure.

--Program Files

The reason for doing the install at all is simply convenience. It makes it intuitive to find the .dlls and .libs again, and the build directory can be removed.

Dependency Walker will now show for any of the PCL .dlls that their dependent VTK .dlls are not available. But that’s OK. The PCL .dlls have _release or _debug in their filenames, so you can change %PATH% to point to c:\Program Files\PCL\bin. The VTK .dlls unfortunately are not so well named, so they need to be copied side-by-side with your application’s .exe.