#include <pcl/ModelCoefficients.h>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/filters/extract_indices.h>
#include <pcl/filters/passthrough.h>
#include <pcl/features/normal_3d.h>
#include <pcl/sample_consensus/method_types.h>
#include <pcl/sample_consensus/model_types.h>
#include <pcl/segmentation/sac_segmentation.h>
#include <pcl/visualization/pcl_visualizer.h>
#pragma comment(lib,"User32.lib")
#pragma comment(lib, "gdi32.lib")
typedef pcl
::PointXYZ PointT
;
int
main(int argc
, char** argv
)
{
pcl
::PCDReader reader
;
pcl
::PassThrough
<PointT
> pass
;
pcl
::NormalEstimation
<PointT
, pcl
::Normal
> ne
;
pcl
::SACSegmentationFromNormals
<PointT
, pcl
::Normal
> seg
;
pcl
::PCDWriter writer
;
pcl
::ExtractIndices
<PointT
> extract
;
pcl
::ExtractIndices
<pcl
::Normal
> extract_normals
;
pcl
::search
::KdTree
<PointT
>::Ptr
tree(new pcl
::search
::KdTree
<PointT
>());
pcl
::PointCloud
<PointT
>::Ptr
cloud(new pcl
::PointCloud
<PointT
>);
pcl
::PointCloud
<PointT
>::Ptr
cloud_filtered(new pcl
::PointCloud
<PointT
>);
pcl
::PointCloud
<pcl
::Normal
>::Ptr
cloud_normals(new pcl
::PointCloud
<pcl
::Normal
>);
pcl
::PointCloud
<PointT
>::Ptr
cloud_filtered2(new pcl
::PointCloud
<PointT
>);
pcl
::PointCloud
<pcl
::Normal
>::Ptr
cloud_normals2(new pcl
::PointCloud
<pcl
::Normal
>);
pcl
::ModelCoefficients
::Ptr
coefficients_plane(new pcl
::ModelCoefficients
), coefficients_cylinder(new pcl
::ModelCoefficients
);
pcl
::PointIndices
::Ptr
inliers_plane(new pcl
::PointIndices
), inliers_cylinder(new pcl
::PointIndices
);
reader
.read("table_scene_mug_stereo_textured.pcd", *cloud
);
std
::cerr
<< "PointCloud has: " << cloud
->points
.size() << " data points." << std
::endl
;
pass
.setInputCloud(cloud
);
pass
.setFilterFieldName("z");
pass
.setFilterLimits(0, 1.5);
pass
.filter(*cloud_filtered
);
std
::cerr
<< "PointCloud after filtering has: " << cloud_filtered
->points
.size() << " data points." << std
::endl
;
writer
.write("直通滤波删除1.5以外的点.pcd", *cloud_filtered
, false);
ne
.setSearchMethod(tree
);
ne
.setInputCloud(cloud_filtered
);
ne
.setKSearch(50);
ne
.compute(*cloud_normals
);
writer
.write("点云法线估计.pcd", *cloud_normals
, false);
seg
.setOptimizeCoefficients(true);
seg
.setModelType(pcl
::SACMODEL_NORMAL_PLANE
);
seg
.setNormalDistanceWeight(0.1);
seg
.setMethodType(pcl
::SAC_RANSAC
);
seg
.setMaxIterations(100);
seg
.setDistanceThreshold(0.03);
seg
.setInputCloud(cloud_filtered
);
seg
.setInputNormals(cloud_normals
);
seg
.segment(*inliers_plane
, *coefficients_plane
);
std
::cerr
<< "Plane coefficients: " << *coefficients_plane
<< std
::endl
;
extract
.setInputCloud(cloud_filtered
);
extract
.setIndices(inliers_plane
);
extract
.setNegative(false);
pcl
::PointCloud
<PointT
>::Ptr
cloud_plane(new pcl
::PointCloud
<PointT
>());
extract
.filter(*cloud_plane
);
std
::cerr
<< "PointCloud representing the planar component: " << cloud_plane
->points
.size() << " data points." << std
::endl
;
writer
.write("table_scene_mug_stereo_textured_plane.pcd", *cloud_plane
, false);
extract
.setNegative(true);
extract
.filter(*cloud_filtered2
);
extract_normals
.setNegative(true);
extract_normals
.setInputCloud(cloud_normals
);
extract_normals
.setIndices(inliers_plane
);
extract_normals
.filter(*cloud_normals2
);
seg
.setOptimizeCoefficients(true);
seg
.setModelType(pcl
::SACMODEL_CYLINDER
);
seg
.setMethodType(pcl
::SAC_RANSAC
);
seg
.setNormalDistanceWeight(0.1);
seg
.setMaxIterations(10000);
seg
.setDistanceThreshold(0.05);
seg
.setRadiusLimits(0, 0.1);
seg
.setInputCloud(cloud_filtered2
);
seg
.setInputNormals(cloud_normals2
);
seg
.segment(*inliers_cylinder
, *coefficients_cylinder
);
std
::cerr
<< "Cylinder coefficients: " << *coefficients_cylinder
<< std
::endl
;
extract
.setInputCloud(cloud_filtered2
);
extract
.setIndices(inliers_cylinder
);
extract
.setNegative(false);
pcl
::PointCloud
<PointT
>::Ptr
cloud_cylinder(new pcl
::PointCloud
<PointT
>());
extract
.filter(*cloud_cylinder
);
if (cloud_cylinder
->points
.empty())
std
::cerr
<< "Can't find the cylindrical component." << std
::endl
;
else
{
std
::cerr
<< "PointCloud representing the cylindrical component: " << cloud_cylinder
->points
.size() << " data points." << std
::endl
;
writer
.write("table_scene_mug_stereo_textured_cylinder.pcd", *cloud_cylinder
, false);
}
pcl
::visualization
::PCLVisualizer
v0("segmention");
float bckgr_gray_level
= 0.0;
float txt_gray_lvl
= 1.0 - bckgr_gray_level
;
pcl
::visualization
::PointCloudColorHandlerCustom
<PointT
> cloud_in_color_h(cloud
, (int)255 * txt_gray_lvl
, (int)255 * txt_gray_lvl
,
(int)255 * txt_gray_lvl
);
v0
.addPointCloud(cloud
, cloud_in_color_h
, "cloud");
pcl
::visualization
::PCLVisualizer
viewer("segmention");
pcl
::visualization
::PointCloudColorHandlerCustom
<PointT
> cloud_tr_color_h(cloud_plane
, 0, 0, 255);
viewer
.addPointCloud(cloud_plane
, cloud_tr_color_h
, "cloud_plane");
pcl
::visualization
::PointCloudColorHandlerCustom
<PointT
> cloud_icp_color_h(cloud_cylinder
, 0, 255, 0);
viewer
.addPointCloud(cloud_cylinder
, cloud_icp_color_h
, "cloud_cylinder");
while (!viewer
.wasStopped())
{
v0
.spinOnce(100);
viewer
.spinOnce(100);
boost
::this_thread
::sleep(boost
::posix_time
::microseconds(100000));
}
return (0);
}