Attention

You are viewing an older version of the documentation. The latest version is 2.2.

Plane Model Segmentation

In this tutorial we will learn how to do a simple plane segmentation from a set of points to find all the points within a point cloud that support a plane model.

Note

This tutorial can be run both inside and outside a Docker* image. We assume that the pcl-oneapi-tutorial Deb package has been installed, and the user has copied the tutorial directory from /opt/intel/pcl/oneapi/tutorials/ to a user-writable directory.

  1. Prepare the environment:

    cd <path-to-oneapi-tutorials>/segmentation
    
    Copy to clipboard
  2. oneapi_passthrough.cpp should be in the directory with following content:

     1#include <pcl/oneapi/segmentation/segmentation.h>
     2#include <pcl/io/pcd_io.h>
     3#include <pcl/point_types.h>
     4
     5int main (int argc, char **argv)
     6{
     7    std::cout << "Running on device: " << dpct::get_default_queue().get_device().get_info<sycl::info::device::name>() << "\n";
     8
     9    //Read Point Cloud
    10    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_input (new pcl::PointCloud<pcl::PointXYZ> ());
    11
    12    //Load a standard PCD file from disk
    13    int result = pcl::io::loadPCDFile("test59.pcd", *cloud_input);
    14    if (result != 0)
    15    {
    16        pcl::console::print_info ("Load pcd file failed.\n");
    17        return result;
    18    }
    19
    20    //Create the oneapi_segmentation object
    21    pcl::oneapi::SACSegmentation seg;
    22
    23    //Configure oneapi_segmentation class 
    24    seg.setInputCloud(cloud_input);
    25    seg.setProbability(0.99);
    26    seg.setMaxIterations(50);
    27    seg.setDistanceThreshold(0.01);
    28    //Optional  
    29    seg.setOptimizeCoefficients(true);
    30    //Set algorithm method and model type 
    31    seg.setMethodType(pcl::oneapi::SAC_RANSAC);
    32    seg.setModelType (pcl::oneapi::SACMODEL_PLANE);
    33
    34    //Out parameter declaration for getting inliers and model coefficients  
    35    pcl::PointIndices::Ptr inliers (new pcl::PointIndices);
    36    double coeffs[4]={0,0,0,0};
    37
    38    //Getting inliers and model coefficients
    39    seg.segment(*inliers, coeffs);
    40
    41    std::cout << "input cloud size   : " << seg.getCloudSize() << std::endl;
    42    std::cout << "inliers size       : " << seg.getInliersSize() << std::endl;
    43    std::cout << "model coefficients : " << coeffs[0] << ", " << coeffs[1] << ", " << coeffs[2] << ", " << coeffs[3] << std::endl;
    44
    45    return 0;
    46}
    
    Copy to clipboard
  3. Source the Intel® oneAPI Base Toolkit environment:

    source /opt/intel/oneapi/setvars.sh
    
    Copy to clipboard
  4. (Optional) Setup proxy setting to download test data:

    export http_proxy="http://<http_proxy>:port"
    export https_proxy="http://<https_proxy>:port"
    
    Copy to clipboard
  5. Build the code:

    mkdir build && cd build
    cmake ../
    make -j
    
    Copy to clipboard
  6. Run the binary:

    ./oneapi_segmentation
    
    Copy to clipboard
  7. Expected results example:

    input cloud size   : 307200
    inliers size       : 25332
    model coefficients : -0.176599, -1.87228, -1.08408, 1
    
    Copy to clipboard

Code Explanation

Load the test data from GitHub* into a PointCloud<PointXYZ>.

    //Read Point Cloud
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_input (new pcl::PointCloud<pcl::PointXYZ> ());
Copy to clipboard

Create the oneapi_segmentation object.

    pcl::oneapi::SACSegmentation seg;
Copy to clipboard

Configure the oneapi_segmentation class.

    seg.setInputCloud(cloud_input);
    seg.setProbability(0.99);
    seg.setMaxIterations(50);
    seg.setDistanceThreshold(0.01);
    //Optional  
    seg.setOptimizeCoefficients(true);
    //Set algorithm method and model type 
    seg.setMethodType(pcl::oneapi::SAC_RANSAC);
    seg.setModelType (pcl::oneapi::SACMODEL_PLANE);
Copy to clipboard

Set to true if a coefficient refinement is required.

    seg.setOptimizeCoefficients(true);
Copy to clipboard

Set the algorithm method and model type.

    seg.setMethodType(pcl::oneapi::SAC_RANSAC);
    seg.setModelType (pcl::oneapi::SACMODEL_PLANE);
Copy to clipboard

Declare output parameters for getting inliers and model coefficients.

    pcl::PointIndices::Ptr inliers (new pcl::PointIndices);
    double coeffs[4]={0,0,0,0};
Copy to clipboard

Get inliers and model coefficients by calling the segment() API.

    seg.segment(*inliers, coeffs);
Copy to clipboard

Result (output log):

    std::cout << "input cloud size   : " << seg.getCloudSize() << std::endl;
    std::cout << "inliers size       : " << seg.getInliersSize() << std::endl;
    std::cout << "model coefficients : " << coeffs[0] << ", " << coeffs[1] << ", " << coeffs[2] << ", " << coeffs[3] << std::endl;
Copy to clipboard