Plane Model Segmentation#

In this tutorial, we will explore the process of simple plane segmentation, extracting points within a point cloud that contribute to a plane model.

Note

This tutorial is applicable for execution both within inside and outside a Docker image. It assumes that the pcl-oneapi-tutorials Deb package is 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
    
  2. oneapi_segmentation.cpp should be in the directory with following content:

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

    source /opt/intel/oneapi/setvars.sh
    
  4. (Optional) Set up proxy setting to download test data:

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

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

    ./oneapi_segmentation
    
  7. Expected results example:

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

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> ());

Create the oneapi_segmentation object.

pcl::oneapi::SACSegmentation seg;

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);

Set to true if a coefficient refinement is required.

seg.setOptimizeCoefficients(true);

Set the algorithm method and model type.

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

Declare output parameters for getting inliers and model coefficients.

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

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

seg.segment(*inliers, coeffs);

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;