Reading Point Cloud data from PCD files

In this tutorial we will learn how to read point cloud data from a PCD file.

The code

First, create a file called, let’s say, pcd_read.cpp in your favorite editor, and place the following code inside it:

 1#include <iostream>
 2#include <pcl/io/pcd_io.h>
 3#include <pcl/point_types.h>
 4
 5int
 6main ()
 7{
 8  pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
 9
10  if (pcl::io::loadPCDFile<pcl::PointXYZ> ("test_pcd.pcd", *cloud) == -1) //* load the file
11  {
12    PCL_ERROR ("Couldn't read file test_pcd.pcd \n");
13    return (-1);
14  }
15  std::cout << "Loaded "
16            << cloud->width * cloud->height
17            << " data points from test_pcd.pcd with the following fields: "
18            << std::endl;
19  for (const auto& point: *cloud)
20    std::cout << "    " << point.x
21              << " "    << point.y
22              << " "    << point.z << std::endl;
23
24  return (0);
25}

The explanation

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

  pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);

creates a PointCloud<PointXYZ> boost shared pointer and initializes it.

  if (pcl::io::loadPCDFile<pcl::PointXYZ> ("test_pcd.pcd", *cloud) == -1) //* load the file
  {
    PCL_ERROR ("Couldn't read file test_pcd.pcd \n");
    return (-1);
  }

loads the PointCloud data from disk (we assume that test_pcd.pcd has already been created from the previous tutorial) into the binary blob.

Alternatively, you can read a PCLPointCloud2 blob (available only in PCL 1.x). Due to the dynamic nature of point clouds, we prefer to read them as binary blobs, and then convert to the actual representation that we want to use.

pcl::PCLPointCloud2 cloud_blob;
pcl::io::loadPCDFile ("test_pcd.pcd", cloud_blob);
pcl::fromPCLPointCloud2 (cloud_blob, *cloud); //* convert from pcl/PCLPointCloud2 to pcl::PointCloud<T>

reads and converts the binary blob into the templated PointCloud format, here using pcl::PointXYZ as the underlying point type.

Finally:

  for (const auto& point: *cloud)
    std::cout << "    " << point.x
              << " "    << point.y
              << " "    << point.z << std::endl;

is used to show the data that was loaded from file.

Compiling and running the program

Add the following lines to your CMakeLists.txt file:

 1cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
 2
 3project(pcd_read)
 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 (pcd_read pcd_read.cpp)
12target_link_libraries (pcd_read ${PCL_LIBRARIES})

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

$ ./pcd_read

You will see something similar to:

Loaded 5 data points from test_pcd.pcd with the following fields: x y z
  0.35222 -0.15188 -0.1064
  -0.39741 -0.47311 0.2926
  -0.7319 0.6671 0.4413
  -0.73477 0.85458 -0.036173
  -0.4607 -0.27747 -0.91676

Note that if the file test_pcd.pcd does not exist (either it hasn’t been created or it has been erased), you should get an error message such as:

Couldn't read file test_pcd.pcd