How to implement planar model segmentation in PCL

Source: Internet
Author: User
Tags rand split
how to achieve planar model segmentation in PCL [GO]

First, in the PCL (Point Cloud Learning) China-assisted release book [1], the 14th chapter of the CD-ROM is provided in the Example 1 folder, open a code file named Planar_segmentation.cpp.
Explanatory notes
Below we parse the file key statement opened above, the following statement is the header file contains the statement, the included header file declares the sample consistency class used in the instance and the related model parameter definition.

#include  ///Random parameter estimation method header file
#include   //Model definition header file
#include  //header file for class based on sample consistency segmentation

For more information on the implementation of other sample consistency models and robust stochastic parameter estimation methods, please refer to sample consistency.

Fill point cloud data, set point cloud width is 15, height is 1, that is disorderly point cloud
cloud.width  =;
Cloud.height = 1;
Cloud.points.resize (Cloud.width * cloud.height);
Generates data, fills the x, y coordinates of a point cloud with random numbers, but all on a plane with Z 1 for
(size_t i = 0; i < cloud.points.size (); ++i)
  {
    cloud.points[i].x = 1024x768 * RAND ()/(Rand_max + 1.0f);
    CLOUD.POINTS[I].Y = 1024x768 * RAND ()/(Rand_max + 1.0f);
    Cloud.points[i].z = 1.0;
  }
Set several outliers, that is, to reset the z-values of several points, so that they deviate from the Z-1 plane
cloud.points[0].z = 2.0;
Cloud.points[3].z = -2.0;
CLOUD.POINTS[6].Z = 4.0;
Print out the coordinate values of points in the point cloud on the standard output to facilitate the segmented reference
std::cerr << Point cloud Data: << cloud.points.size () << Points "<< Std::endl;
for (size_t i = 0; i < cloud.points.size (); ++i)
std::cerr << "    " << cloud.points[i].x << " 
<< cloud.points[i].y <<" 
<< cloud.points[i].z << Std::endl;

Create point cloud Data then print the coordinate values of these points on the screen, in order to achieve the purpose of this experiment, manually add several outliers in the data, and set their z value is not 1, that is, the point is not on the plane of the z=1 as an outlier, the point on the z=1 plane as an insider point.

The model factor object coefficients and the point Index collection object inliers that are stored inside points are created when you create the split.
pcl::modelcoefficients::P tr coefficients (new pcl::modelcoefficients);
PCL::P ointindices::P tr inliers (new PCL::P ointindices);
Create a Segmented object
pcl::sacsegmentation seg;
Optional configuration, set model coefficients need to optimize
seg.setoptimizecoefficients (true);
Must be configured to set the model type of the split, the random parameter estimation method used, the distance threshold, the input point cloud
seg.setmodeltype (pcl::sacmodel_plane);
Seg.setmethodtype (PCL::SAC_RANSAC);
Seg.setdistancethreshold (0.01);
Seg.setinputcloud (cloud.makeshared ());
The partition implementation is triggered, and the coefficients of the inliers and storage plane model of the partition result are stored coefficients
seg.segment (*inliers, *coefficients);

Create a random sample consistency segmentation object, set the model type and the random sampling consistency method type, and set the "distance threshold", the distance threshold determines what the point is considered to be an insider, the distance threshold represents a point to the maximum distance of the estimated model, in this case, using the Ransac method (pcl::sac_ RANSAC) as a robust estimation method of choice, the distance threshold is 0.01 meters, that is, as long as the point to the z=1 plane distance is less than the threshold value of the point is regarded as an inner point, and larger than the threshold value is considered as the outside point.

Prints out the estimated plane model parameters.
std::cerr << "Model coefficients:" << coefficients->values[0] << "
<values[1" <& Lt "
<values[2" << "<values[3" 
<< Std::endl;

This code is used to print out the parameters (in the form) of the estimated plane model, please refer to sample consistency for details.
Compile and run the program
Use the CMakeLists.txt file provided with the CD-ROM to create the project file in the CMake and generate the corresponding executable file. After the executable file is generated, type the following command in CMD:

->planar_segmentation.exe

After running, you will see the results shown in Figure 1, the data to begin printing is a manually added point cloud data, not all in the Z-1 plane, through the processing of the segmented object, extract all the inner points, that is, filter out the point set Z is not equal to 1.

The following is the source code, so as to avoid looking for, in fact, there are online.

#include "stdafx.h" #include <iostream> #include <pcl/ModelCoefficients.h> #include <pcl/io/pcd_io.h > #include <pcl/point_types.h> #include <pcl/sample_consensus/method_types.h> #include <pcl/sample
    _consensus/model_types.h> #include <pcl/segmentation/sac_segmentation.h> int main (int argc, char** argv) {

    PCL::P OINTCLOUD&LT;PCL::P ointxyz> Cloud;
    Fill in the cloud data cloud.width = 15;
    Cloud.height = 1;

    Cloud.points.resize (Cloud.width * cloud.height); Generate the data for (size_t i = 0; i < cloud.points.size (); ++i) {cloud.points[i].x = 1024x768 * Ran
        D ()/(Rand_max + 1.0f);
        CLOUD.POINTS[I].Y = 1024x768 * RAND ()/(Rand_max + 1.0f);
    CLOUD.POINTS[I].Z = 1.0;
    }//Set a few outliers cloud.points[0].z = 2.0;
    Cloud.points[3].z =-2.0;

    CLOUD.POINTS[6].Z = 4.0; Std::cerr << "Point cloud Data:" << cloud.points.size () << "points" << Std::endl;  for (size_t i = 0; i < cloud.points.size (); ++i) Std::cerr << "" << cloud.points[i].x <<

    "<< cloud.points[i].y <<" << cloud.points[i].z << Std::endl;
    Pcl::modelcoefficients::P TR coefficients (new pcl::modelcoefficients);
    PCL::P ointindices::P tr inliers (new PCL::P ointindices);
    Create the Segmentation object PCL::SACSEGMENTATION&LT;PCL::P ointxyz> seg;
    Optional seg.setoptimizecoefficients (TRUE);
    Mandatory Seg.setmodeltype (Pcl::sacmodel_plane);
    Seg.setmethodtype (PCL::SAC_RANSAC);

    Seg.setdistancethreshold (0.01);
    Seg.setinputcloud (cloud.makeshared ());

    Seg.segment (*inliers, *coefficients); if (inliers->indices.size () = = 0) {pcl_error ("Could not estimate a planar model for the given dataset.")
        );
    Return (-1); } std::cerr << "Model coefficients:" << coefficients->values[0] << "<< Coefficients->values[1] <<" "<< coefficients->values[2] <<"

    "<< coefficients->values[3] << Std::endl;
    Std::cerr << "Model inliers:" << inliers->indices.size () << Std::endl;    for (size_t i = 0; i < inliers->indices.size (); ++i) Std::cerr << inliers->indices[i] << " "<< cloud.points[inliers->indices[i]].x <<" "<< Cloud.points[inliers->indices[i]].y &

    lt;< "" << cloud.points[inliers->indices[i]].z << Std::endl;
return (0); }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.