C++ PCL 1.8.1 RANSAC 直线拟合:代码示例和可视化
#include
void ransacLineFitting(const pcl::PointCloudpcl::PointXYZ::Ptr& cloud) { pcl::PointCloudpcl::PointXYZ::Ptr cloud_filtered(new pcl::PointCloudpcl::PointXYZ); pcl::PointCloudpcl::PointXYZ::Ptr cloud_inliers(new pcl::PointCloudpcl::PointXYZ); pcl::PointCloudpcl::PointXYZ::Ptr cloud_outliers(new pcl::PointCloudpcl::PointXYZ);
// Create the segmentation object
pcl::SampleConsensusModelLine<pcl::PointXYZ>::Ptr model(new pcl::SampleConsensusModelLine<pcl::PointXYZ>(cloud));
pcl::RandomSampleConsensus<pcl::PointXYZ> ransac(model);
ransac.setDistanceThreshold(0.01);
ransac.computeModel();
ransac.getInliers(*cloud_inliers);
ransac.getOutliers(*cloud_outliers);
pcl::visualization::PCLVisualizer viewer("RANSAC Line Fitting");
viewer.setBackgroundColor(0, 0, 0);
viewer.addPointCloud<pcl::PointXYZ>(cloud, "cloud");
viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR, 1.0, 1.0, 1.0, "cloud");
viewer.addPointCloud<pcl::PointXYZ>(cloud_inliers, "inliers");
viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR, 1.0, 0.0, 0.0, "inliers");
viewer.addPointCloud<pcl::PointXYZ>(cloud_outliers, "outliers");
viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR, 0.0, 1.0, 0.0, "outliers");
viewer.spin();
pcl::io::savePLYFileASCII("D:\\DIANYUNWENJIANJIA\\ransac拟合直线_ply.ply", *cloud_inliers);
}
int main() { pcl::PointCloudpcl::PointXYZ::Ptr cloud(new pcl::PointCloudpcl::PointXYZ);
// Load point cloud data from file
if (pcl::io::loadPLYFile<pcl::PointXYZ>("your_point_cloud.ply", *cloud) == -1)
{
std::cerr << "Failed to load point cloud data." << std::endl;
return -1;
}
ransacLineFitting(cloud);
return 0;
}
原文地址: https://www.cveoy.top/t/topic/p1il 著作权归作者所有。请勿转载和采集!