PCL 1.8.1: 使用 SACSegmentation 在 Y 轴上拟合直线
#include\x3ciostream\x3e\n#include\x3cpcl/io/ply_io.h\x3e\n#include\x3cpcl/point_types.h\x3e\n#include\x3cpcl/ModelCoefficients.h\x3e\n#include\x3cpcl/filters/extract_indices.h\x3e\n#include\x3cpcl/filters/voxel_grid.h\x3e\n#include\x3cpcl/features/normal_3d.h\x3e\n#include\x3cpcl/sample_consensus/method_types.h\x3e\n#include\x3cpcl/sample_consensus/model_types.h\x3e\n#include\x3cpcl/segmentation/sac_segmentation.h\x3e\n#include\x3cpcl/visualization/pcl_visualizer.h\x3e\n\nint main()\n{\n\t// 读取点云数据\n\tpcl::PointCloud\x3cpcl::PointXYZ\x3e::Ptr cloud(new pcl::PointCloud\x3cpcl::PointXYZ\x3e);\n\tpcl::PLYReader reader;\n\treader.read("D:\DIANYUNWENJIANJIA\newkruskal2_ply.ply", *cloud);\n\n\t// 创建霍夫变换对象\n\tpcl::ModelCoefficients::Ptr coefficients(new pcl::ModelCoefficients);\n\tpcl::PointIndices::Ptr inliers(new pcl::PointIndices);\n\tpcl::SACSegmentation\x3cpcl::PointXYZ\x3e seg;\n\tseg.setOptimizeCoefficients(true);\n\tseg.setModelType(pcl::SACMODEL_LINE_Y);\n\tseg.setAxis(Eigen::Vector3f(0, 1, 0));\n\tseg.setMethodType(pcl::SAC_RANSAC);\n\tseg.setMaxIterations(1000);\n\tseg.setDistanceThreshold(0.04);\n\n\t// 执行霍夫变换\n\tseg.setInputCloud(cloud);\n\tseg.segment(*inliers, *coefficients);\n\n\t// 提取直线内点\n\tpcl::ExtractIndices\x3cpcl::PointXYZ\x3e extract;\n\textract.setInputCloud(cloud);\n\textract.setIndices(inliers);\n\textract.setNegative(false);\n\tpcl::PointCloud\x3cpcl::PointXYZ\x3e::Ptr line(new pcl::PointCloud\x3cpcl::PointXYZ\x3e);\n\textract.filter(*line);\n\n\t// 可视化结果\n\tpcl::visualization::PCLVisualizer viewer("3D Viewer");\n\tviewer.setBackgroundColor(0, 0, 0);\n\tviewer.addPointCloud\x3cpcl::PointXYZ\x3e(cloud, "cloud");\n\tviewer.addPointCloud\x3cpcl::PointXYZ\x3e(line, "line");\n\tviewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "cloud");\n\tviewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR, 0, 0, 1, "line");\n\tviewer.addCoordinateSystem(1.0);\n\tviewer.initCameraParameters();\n\tviewer.spin();\n\n\t// 保存结果\n\tpcl::PLYWriter writer;\n\twriter.write("D:\DIANYUNWENJIANJIA\拟合中心线_ply.ply", *line);\n\n\treturn 0;\n
原文地址: https://www.cveoy.top/t/topic/p1gF 著作权归作者所有。请勿转载和采集!