Attention

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

Using SampleConsensusInitialAligment (SCIA) to Initial Align Two Point Clouds

In this tutorial, we will learn how to initial align two point clouds, using SCIA, provided with the transformation 4x4 matrix.

Note

This tutorial is applicable for execution for 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>/sample_consensus_initial_alignment
    
    Copy to clipboard
  2. oneapi_scia.cpp should be in the directory with following content:

     1#include <pcl/oneapi/registration/ia_ransac.h>
     2#include <pcl/io/pcd_io.h>
     3#include <pcl/point_types.h>
     4#include <pcl/point_cloud.h>
     5
     6
     7using namespace pcl;
     8using namespace pcl::io;
     9
    10int main (int argc, char** argv)
    11{
    12  std::cout << "Running on device: " << dpct::get_default_queue().get_device().get_info<sycl::info::device::name>() << "\n";
    13
    14  // Read Point Cloud
    15  PointCloud<PointXYZ> cloud_source, cloud_target;
    16  PointCloud<FPFHSignature33> FPFH_cloud_source, FPFH_cloud_target;
    17
    18  if (loadPCDFile<PointXYZ>("../../data/scia_source.pcd", cloud_source) < 0)
    19  {
    20    std::cerr << "Failed to read alignment source point cloud, please check scia_source.pcd" << std::endl;
    21    return (-1);
    22  }
    23  if (loadPCDFile<PointXYZ>("../../data/scia_target.pcd", cloud_target) < 0)
    24  {
    25    std::cerr << "Failed to read alignment target point cloud, please check scia_target.pcd" << std::endl;
    26    return (-1);
    27  }
    28  if (loadPCDFile<FPFHSignature33>("../../data/scia_source_fpfh33.pcd", FPFH_cloud_source) < 0)
    29  {
    30    std::cerr << "Failed to read FPFH feature cloud of alignment source point cloud, please check scia_source_fpfh33.pcd" << std::endl;
    31    return (-1);
    32  }
    33  if (loadPCDFile<FPFHSignature33>("../../data/scia_target_fpfh33.pcd", FPFH_cloud_target) < 0)
    34  {
    35    std::cerr << "Failed to read FPFH feature cloud of alignment target point cloud, please check scia_target_fpfh33.pcd" << std::endl;
    36    return (-1);
    37  }
    38
    39  // GPU calculate
    40  pcl::oneapi::SampleConsensusInitialAlignment<PointXYZ, PointXYZ, FPFHSignature33> scia;
    41  scia.setInputSource(cloud_source.makeShared());
    42  scia.setInputTarget(cloud_target.makeShared());
    43  scia.setSourceFeatures(FPFH_cloud_source.makeShared());
    44  scia.setTargetFeatures(FPFH_cloud_target.makeShared());
    45
    46  constexpr float SACdismin = 0.02f;
    47  constexpr int SCANum = 20;
    48  constexpr int SCAradomn = 100;
    49  scia.setMinSampleDistance(SACdismin);
    50  scia.setNumberOfSamples(SCANum);
    51  scia.setCorrespondenceRandomness(SCAradomn);
    52
    53  PointCloud<PointXYZ> cloud_result;
    54  Eigen::Matrix4f sac_trans;
    55  scia.align(cloud_result);
    56  sac_trans = scia.getFinalTransformation();
    57
    58  // print log
    59  std::cout << "[oneapi SCIA] Transformation Matrix 4x4 = " << std::endl << sac_trans << std::endl;
    60}
    
    Copy to clipboard
  3. Source the Intel® oneAPI Base Toolkit environment:

    source /opt/intel/oneapi/setvars.sh
    
    Copy to clipboard
  4. Build the code:

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

    ./oneapi_scia
    
    Copy to clipboard
  6. Expected results example:

    [oneapi SCIA] Transformation Matrix 4x4 =
     0.268113 -0.734448 -0.623458  0.289958
     0.748389 -0.248733  0.614853 -0.208532
    -0.606653  -0.63144  0.482965  0.229585
            0         0         0         1
    
    Copy to clipboard

Code Explanation

Load source/target/fpfh_source/fpfh_target PCD file

  if (loadPCDFile<PointXYZ>("../../data/scia_source.pcd", cloud_source) < 0)
  {
    std::cerr << "Failed to read alignment source point cloud, please check scia_source.pcd" << std::endl;
    return (-1);
  }
  if (loadPCDFile<PointXYZ>("../../data/scia_target.pcd", cloud_target) < 0)
  {
    std::cerr << "Failed to read alignment target point cloud, please check scia_target.pcd" << std::endl;
    return (-1);
  }
Copy to clipboard

Load the example source/target/fpfh_source/fpfh_target PCD into a PointCloud<PointXYZ/FPFHSignature33>.

  if (loadPCDFile<FPFHSignature33>("../../data/scia_source_fpfh33.pcd", FPFH_cloud_source) < 0)
  {
    std::cerr << "Failed to read FPFH feature cloud of alignment source point cloud, please check scia_source_fpfh33.pcd" << std::endl;
    return (-1);
  }
  if (loadPCDFile<FPFHSignature33>("../../data/scia_target_fpfh33.pcd", FPFH_cloud_target) < 0)
  {
    std::cerr << "Failed to read FPFH feature cloud of alignment target point cloud, please check scia_target_fpfh33.pcd" << std::endl;
    return (-1);
  }
Copy to clipboard

Start to compute the model.

  // GPU calculate
  pcl::oneapi::SampleConsensusInitialAlignment<PointXYZ, PointXYZ, FPFHSignature33> scia;
  scia.setInputSource(cloud_source.makeShared());
  scia.setInputTarget(cloud_target.makeShared());
  scia.setSourceFeatures(FPFH_cloud_source.makeShared());
  scia.setTargetFeatures(FPFH_cloud_target.makeShared());

  constexpr float SACdismin = 0.02f;
  constexpr int SCANum = 20;
  constexpr int SCAradomn = 100;
  scia.setMinSampleDistance(SACdismin);
  scia.setNumberOfSamples(SCANum);
  scia.setCorrespondenceRandomness(SCAradomn);

  PointCloud<PointXYZ> cloud_result;
  Eigen::Matrix4f sac_trans;
  scia.align(cloud_result);
  sac_trans = scia.getFinalTransformation();
Copy to clipboard

Result (output log).

  // print log
  std::cout << "[oneapi SCIA] Transformation Matrix 4x4 = " << std::endl << sac_trans << std::endl;
Copy to clipboard