利用RanSac找到点云中所有的平面

如题,这例直接上代码。具体可以看代码中的注释。

void get_plane(PointCloud<PointXYZ>::Ptr cloud, vector<vector<float>> &Coffis, vector <PointIndices> &clusters,int threshold){

	while (cloud->points.size() >= threshold){

		//创建分割对象 -- 检测平面参数
		//pcl::search::KdTree<pcl::PointXYZ>::Ptr search(new pcl::search::KdTree<pcl::PointXYZ>);
		ModelCoefficients::Ptr coefficients(new ModelCoefficients); //存储输出的模型的系数
		PointIndices::Ptr inliers(new PointIndices); //存储内点,使用的点
		SACSegmentation<PointXYZ> seg;
		//可选设置
		seg.setOptimizeCoefficients(true);
		//必须设置
		seg.setModelType(SACMODEL_PLANE); //设置模型类型,检测平面
		seg.setMethodType(SAC_RANSAC);      //设置方法【聚类或随机样本一致性】
		seg.setMaxIterations(1000);
		seg.setDistanceThreshold(0.01);
		//seg.setSamplesMaxDist(0.1, search);
		seg.setInputCloud(cloud);
		seg.segment(*inliers, *coefficients);    //分割操作
		//search->setInputCloud(cloud);
		if (inliers->indices.size() == 0){
			PCL_ERROR("Could not estimate a planar model for the given dataset.");
			return;
		}
		if (inliers->indices.size()<1000){
			break;
		}
		//平面参数
		vector<float> tmp;
		tmp.push_back(coefficients->values[0]);
		tmp.push_back(coefficients->values[1]);
		tmp.push_back(coefficients->values[2]);
		tmp.push_back(coefficients->values[3]);
		Coffis.push_back(tmp);
		std::cerr << "Model coefficients: " << coefficients->values[0] << " " << coefficients->values[1] << " "
			<< coefficients->values[2] << " " << coefficients->values[3] << std::endl;

		//提取平面
		pcl::ExtractIndices<pcl::PointXYZ> extract;
		extract.setInputCloud(cloud);
		extract.setIndices(inliers);
		pcl::PointCloud<pcl::PointXYZ>::Ptr output(new pcl::PointCloud<pcl::PointXYZ>);
		extract.filter(*output);//提取对于索引的点云 内点
		std::cerr << "output point size : " << output->points.size() << std::endl;
		clusters.push_back(*inliers);

		// 移去平面局内点,提取剩余点云
		pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_other(new pcl::PointCloud<pcl::PointXYZ>);
		// *cloud_other = *cloud - *output;
		extract.setNegative(true);
		extract.filter(*cloud_other);
		std::cerr << "other point size : " << cloud_other->points.size() << std::endl;
		cloud = cloud_other;
		//D3_view(output, cloud_other, false);
	}
}

 

  • 2
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值