PCL:RANSAC算法拟合直线的两种实现方式

pcl利用ransac实现直线拟合的方法

pcl::SampleConsensusModelLine

#include <pcl/io/pcd_io.h>
#include <pcl/sample_consensus/ransac.h>
#include <pcl/sample_consensus/sac_model_line.h>
#include <pcl/visualization/pcl_visualizer.h>

using namespace std;

//------------------------------- 加载点云 -------------------------------
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
if (pcl::io::loadPCDFile("straightLine.pcd", *cloud) < 0)
{
	PCL_ERROR("\a点云文件不存在!\n");
	system("pause");
	return -1;
}
cout << "->加载点云个数:" << cloud->points.size() << endl;
//========================================================================

//----------------------------- 空间直线拟合 -----------------------------
pcl::SampleConsensusModelLine<pcl::PointXYZ>::Ptr model_line(new pcl::SampleConsensusModelLine<pcl::PointXYZ>(cloud));	//指定拟合点云与几何模型
pcl::RandomSampleConsensus<pcl::PointXYZ> ransac(model_line);	//创建随机采样一致性对象
ransac.setDistanceThreshold(0.01);	//内点到模型的最大距离
ransac.setMaxIterations(1000);		//最大迭代次数
ransac.computeModel();				//执行RANSAC空间直线拟合

vector<int> inliers;				//存储内点索引的向量
ransac.getInliers(inliers);			//提取内点对应的索引

/// 根据索引提取内点
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_line(new pcl::PointCloud<pcl::PointXYZ>);
pcl::copyPointCloud<pcl::PointXYZ>(*cloud, inliers, *cloud_line);

/// 模型参数
Eigen::VectorXf coefficient;
ransac.getModelCoefficients(coefficient);
cout << "直线点向式方程为:\n"
	<< "   (x - " << coefficient[0] << ") / " << coefficient[3]
	<< " = (y - " << coefficient[1] << ") / " << coefficient[4]
	<< " = (z - " << coefficient[2] << ") / " << coefficient[5];
//========================================================================

	

pcl::SACSegmentation

 1#include <iostream>
 2#include <pcl/ModelCoefficients.h>
 3#include <pcl/io/pcd_io.h>
 4#include <pcl/point_types.h>
 5#include <pcl/sample_consensus/method_types.h>
 6#include <pcl/sample_consensus/model_types.h>
 7#include <pcl/segmentation/sac_segmentation.h>
 8
 9int
10 main ()
11{
12  pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
13
14  // Fill in the cloud data
15  cloud->width  = 15;
16  cloud->height = 1;
17  cloud->points.resize (cloud->width * cloud->height);
18
19  // Generate the data
20  for (auto& point: *cloud)
21  {
22    point.x = 1024 * rand () / (RAND_MAX + 1.0f);
23    point.y = 1024 * rand () / (RAND_MAX + 1.0f);
24    point.z = 1.0;
25  }
26
27  // Set a few outliers
28  (*cloud)[0].z = 2.0;
29  (*cloud)[3].z = -2.0;
30  (*cloud)[6].z = 4.0;
31
32  std::cerr << "Point cloud data: " << cloud->size () << " points" << std::endl;
33  for (const auto& point: *cloud)
34    std::cerr << "    " << point.x << " "
35                        << point.y << " "
36                        << point.z << std::endl;
37
38  pcl::ModelCoefficients::Ptr coefficients (new pcl::ModelCoefficients);
39  pcl::PointIndices::Ptr inliers (new pcl::PointIndices);
40  // Create the segmentation object
41  pcl::SACSegmentation<pcl::PointXYZ> seg;
42  // Optional
43  seg.setOptimizeCoefficients (true);
44  // Mandatory
45  seg.setModelType (pcl::SACMODEL_LINE);
46  seg.setMethodType (pcl::SAC_RANSAC);
47  seg.setDistanceThreshold (0.01);
48
49  seg.setInputCloud (cloud);
50  seg.segment (*inliers, *coefficients);
51
52  if (inliers->indices.size () == 0)
53  {
54    PCL_ERROR ("Could not estimate a line model for the given dataset.\n");
55    return (-1);
56  }
57
58 return (0);
59}

  • 5
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
PCL(Point Cloud Library)是一个用于点云处理的开源库,其中包含了很多常用的点云算法,包括RANSAC算法RANSACRandom Sample Consensus)是一种基于随机采样的拟合算法,能够从含有噪声数据的点云中提取出具有代表性的模型。 在PCL中,使用RANSAC算法拟合曲线的步骤如下: 1. 定义需要拟合的曲线模型,例如直线、圆、椭圆等。 2. 从点云中随机选择一定数量的点作为样本,计算出符合模型的参数。 3. 对于剩余的点,计算它们到模型的距离,如果距离小于一定的阈值,则认为它们属于该模型。 4. 统计属于该模型的点的数量,如果数量大于一定的阈值,则认为该模型有效。 5. 重复上述步骤若干次,最终得到拟合效果最好的模型。 下面是一个使用RANSAC算法拟合直线的示例代码: ```c++ #include <pcl/ModelCoefficients.h> #include <pcl/point_types.h> #include <pcl/sample_consensus/method_types.h> #include <pcl/sample_consensus/model_types.h> #include <pcl/segmentation/sac_segmentation.h> pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>); // 读取点云数据 pcl::ModelCoefficients::Ptr coefficients(new pcl::ModelCoefficients); pcl::PointIndices::Ptr inliers(new pcl::PointIndices); pcl::SACSegmentation<pcl::PointXYZ> seg; seg.setOptimizeCoefficients(true); seg.setModelType(pcl::SACMODEL_LINE); seg.setMethodType(pcl::SAC_RANSAC); seg.setMaxIterations(1000); seg.setDistanceThreshold(0.01); seg.setInputCloud(cloud); seg.segment(*inliers, *coefficients); if (inliers->indices.size() == 0) { std::cout << "Failed to estimate a planar model for the given dataset." << std::endl; } else { std::cout << "Model coefficients: " << coefficients->values[0] << " " << coefficients->values[1] << " " << coefficients->values[2] << std::endl; } ``` 其中,`pcl::SACMODEL_LINE`表示拟合直线模型,`pcl::SAC_RANSAC`表示使用RANSAC算法进行拟合。`setMaxIterations`和`setDistanceThreshold`分别表示最大迭代次数和点到直线距离的阈值。最终得到的模型系数存储在`coefficients`中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ywfwyht

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值