在本教程中,我们将学习如何使用pcl::MinCutSegmentation类
中实现的基于最小切的分割算法。该算法对给定的输入云进行二进制分割。根据对象中心及其半径,该算法将点云划分为两组:前景点和背景点(属于对象的点和不属于对象的点)。
源码:
创建 min_cut_segmentation.cpp 文件
1#include
2#include
3#include
4#include
5#include
6#include // for pcl::removeNaNFromPointCloud
7#include
8
9int main ()
10{
11 pcl::PointCloud ::Ptr cloud (new pcl::PointCloud );
12 if ( pcl::io::loadPCDFile ("min_cut_segmentation_tutorial.pcd", *cloud) == -1 )
13 {
14 std::cout << "Cloud reading failed." << std::endl;
15 return (-1);
16 }
17
18 pcl::IndicesPtr indices (new std::vector );
19 pcl::removeNaNFromPointCloud(*cloud, *indices);
20
21 pcl::MinCutSegmentation seg;
22 seg.setInputCloud (cloud);
23 seg.setIndices (indices);
24
25 pcl::PointCloud::Ptr foreground_points(new pcl::PointCloud ());
26 pcl::PointXYZ point;
27 point.x = 68.97;
28 point.y = -18.55;
29 point.z = 0.57;
30 foreground_points->points.push_back(point);
31 seg.setForegroundPoints (foreground_points);
32
33 seg.setSigma (0.25);
34 seg.setRadius (3.0433856);
35 seg.setNumberOfNeighbours (14);
36 seg.setSourceWeight (0.8);
37
38 std::vector clusters;
39 seg.extract (clusters);
40
41 std::cout << "Maximum flow is " << seg.getMaxFlow () << std::endl;
42
43 pcl::PointCloud ::Ptr colored_cloud = seg.getColoredCloud ();
44 pcl::visualization::CloudViewer viewer ("Cluster viewer");
45 viewer.showCloud(colored_cloud);
46 while (!viewer.wasStopped ())
47 {
48 }
49
50 return (0);
51}
说明:
1、相关头文件
1#include
2#include
3#include
4#include
5#include
6#include // for pcl::removeNaNFromPointCloud
7#include
2、从 .pcd 文件加载点云。
pcl::PointCloud ::Ptr cloud (new pcl::PointCloud );
if ( pcl::io::loadPCDFile ("min_cut_segmentation_tutorial.pcd", *cloud) == -1 )
{
std::cout << "Cloud reading failed." << std::endl;
return (-1);
}
3、表明pcl::MinCutSegmentation
类可以使用索引。这里,只选择有效点进行分割。
pcl::IndicesPtr indices (new std::vector );
pcl::removeNaNFromPointCloud(*cloud, *indices);
4、pcl::MinCutSegmentation
类实例化。模板类只有一个参数 - PointT - 说明将使用哪种类型的点。
pcl::MinCutSegmentation seg;
5、提供了必须分割的点云和索引。
seg.setInputCloud (cloud);
seg.setIndices (indices);
6、算法需要已知为对象中心的点。这些行提供了它。
pcl::PointCloud::Ptr foreground_points(new pcl::PointCloud ());
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);
7、设置平滑成本计算所需的和对象半径。
seg.setSigma (0.25);
seg.setRadius (3.0433856);
8、表示在构造图时要找到多少邻居。设置的邻居越多,它包含的边数就越多。
seg.setNumberOfNeighbours (14);
9、设置前景惩罚
seg.setSourceWeight (0.8);
10、启动算法,分割后的簇将包含结果
std::vector clusters;
seg.extract (clusters);
11、访问在图形切割期间计算的流量值
std::cout << "Maximum flow is " << seg.getMaxFlow () << std::endl;
12、创建 CloudViewer 类的实例以实现结果可视化。
pcl::PointCloud ::Ptr colored_cloud = seg.getColoredCloud ();
pcl::visualization::CloudViewer viewer ("Cluster viewer");
viewer.showCloud(colored_cloud);
while (!viewer.wasStopped ())
{
}
编译和运行
1、在 CMakeLists.txt 文件中添加以下代码行:
1cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
2
3project(min_cut_segmentation)
4
5find_package(PCL 1.5 REQUIRED)
6
7include_directories(${PCL_INCLUDE_DIRS})
8link_directories(${PCL_LIBRARY_DIRS})
9add_definitions(${PCL_DEFINITIONS})
10
11add_executable (min_cut_segmentation min_cut_segmentation.cpp)
12target_link_libraries (min_cut_segmentation ${PCL_LIBRARIES})
2、运行
$ ./min_cut_segmentation
3、输出