PCL 基于最小割的点云分割
最小割(min-cut)算法能够对给定的输入点云进行二值分割。在已知目标中心及其半径的情况下该算法会将点云划分为两个集合前景点和背景点即属于目标的点与不属于目标的点。输入点云是三辆小汽车:在这个程序里我们只需要修改30~32行物体的中心坐标(x,y,z).根据up测试坐标系如下图所示X轴(红色):范围大概是65~75Y轴(绿色):范围大概是-22~-15Z轴(蓝色):范围大概是0~2有了这个坐标系之后就很容易看出来代码里给的中心坐标( 68.97,-18.55,0.57)是中间那辆小汽车。对应的分割结果如下图所示(白色是被分割出来的目标/前景红色是背景色)可以修改中心坐标分割出左边或者右边的小汽车如下图所示:输入min_cut_segmentation_tutorial.pcd输出没有保存输出结果运行编译后双击运行视频演示https://www.bilibili.com/video/BV1VPKf6TETe/代码#include iostream #include vector #include pcl/io/pcd_io.h #include pcl/point_types.h #include pcl/visualization/cloud_viewer.h #include pcl/filters/filter_indices.h // for pcl::removeNaNFromPointCloud #include pcl/segmentation/min_cut_segmentation.h int main () { //加载指定的点云min_cut_segmentation_tutorial.pcd pcl::PointCloud pcl::PointXYZ::Ptr cloud (new pcl::PointCloud pcl::PointXYZ); if ( pcl::io::loadPCDFile pcl::PointXYZ (min_cut_segmentation_tutorial.pcd, *cloud) -1 ) { std::cout Cloud reading failed. std::endl; return (-1); } pcl::IndicesPtr indices (new std::vector int); pcl::removeNaNFromPointCloud(*cloud, *indices); pcl::MinCutSegmentationpcl::PointXYZ seg; seg.setInputCloud (cloud); seg.setIndices (indices); pcl::PointCloudpcl::PointXYZ::Ptr foreground_points(new pcl::PointCloudpcl::PointXYZ ()); pcl::PointXYZ point; //修改这里改变分割结果别的不需要修改 point.x 68.97; point.y -18.55; point.z 0.57; foreground_points-points.push_back(point); seg.setForegroundPoints (foreground_points); seg.setSigma (0.25); seg.setRadius (3.0433856); seg.setNumberOfNeighbours (14); seg.setSourceWeight (0.8); std::vector pcl::PointIndices clusters; seg.extract (clusters); std::cout Maximum flow is seg.getMaxFlow () std::endl; pcl::PointCloud pcl::PointXYZRGB::Ptr colored_cloud seg.getColoredCloud (); pcl::visualization::CloudViewer viewer (Cluster viewer); viewer.showCloud(colored_cloud); while (!viewer.wasStopped ()) { } return (0); }参考Min-Cut Based Segmentation — Point Cloud Library 0.0 documentation