三维点云学习(1)实现三维点云物体的区域增长算法c++

一、采用的数据集

shapeNet数据集

二、运行环境

vs2017、pcl1.8.0

三、完整代码如下

// growth.cpp : 定义控制台应用程序的入口点。
//

//#include "stdafx.h"


#include <iostream> //标准c++库输入输出相关头文件
#include <vector>
#include <pcl/point_types.h> //pcl中支持的点类型头文件
#include <pcl/io/pcd_io.h> //pcd读写相关头文件
#include <pcl/search/search.h>
#include <pcl/search/kdtree.h> //包含kdtree头文件
#include <pcl/features/normal_3d.h>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/filters/passthrough.h>
#include <pcl/segmentation/region_growing.h>

int main(int argc, char** argv)
{
	DWORD t1, t2;
	t1 = GetTickCount();//以上两句和最后return 0 之上的为计时函数。

	// 定义点云格式,具体见下章
	//typedef pcl::PointXYZ PointT;
	//点云的类型
	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
	//打开点云
	if (pcl::io::loadPCDFile <pcl::PointXYZ>("G:/VS_TEST/数据集/guitar_03467517/1abe78447898821e93f0194265a9746c.pcd", *cloud) == -1)//改成想要输入的点云名称...*cloud就是把输入的点云记录到变量指针cloud中。
	{
		std::cout << "Cloud reading failed." << std::endl;
		return (-1);
	}
	//设置搜索的方式或者说是结构
	pcl::search::Search<pcl::PointXYZ>::Ptr tree = boost::shared_ptr<pcl::search::Search<pcl::PointXYZ> >(new pcl::search::KdTree<pcl::PointXYZ>);
	//求法线
	pcl::PointCloud <pcl::Normal>::Ptr normals(new pcl::PointCloud <pcl::Normal>);
	pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal> normal_estimator;
	normal_estimator.setSearchMethod(tree); 
	normal_estimator.setInputCloud(cloud); //设置要搜索的点云,建立kdtree
	normal_estimator.setKSearch(50);// 设置拟合时采用的点数
	normal_estimator.compute(*normals);
	//直通滤波在Z轴的0到1米之间
	pcl::IndicesPtr indices(new std::vector <int>);
	pcl::PassThrough<pcl::PointXYZ> pass;
	pass.setInputCloud(cloud);
	pass.setFilterFieldName("z");
	pass.setFilterLimits(0.0, 1.0);
	pass.filter(*indices);
	//聚类对象<点,法线>
	pcl::RegionGrowing<pcl::PointXYZ, pcl::Normal> reg; //首先建立reg寄存器(区域曾长的对象)
	reg.setMinClusterSize(10);  //最小的聚类的点数(小于这参数的平面被忽略不计)
	reg.setMaxClusterSize(1000000);  //最大的(一般随便设置)
	reg.setSearchMethod(tree);    //搜索方式(采用的默认是K—d树法)
	reg.setNumberOfNeighbours(15); //设置搜索的邻域点的个数,周围多少个点决定这是一个平面(决定容错率,设置大时有倾斜也可接受,设置小时检测到的平面会很小)
	reg.setInputCloud(cloud);         //输入点
	//reg.setIndices (indices);
	reg.setInputNormals(normals);     //输入的法线
	reg.setSmoothnessThreshold(3.0 / 180.0 * M_PI);  //设置平滑度(设置两个法线在多大夹角内可当做是共面的)
	reg.setCurvatureThreshold(1.0);     //设置曲率的阈值
	//最后也是一个弯曲的阈值,这个决定了比当前考察的点和平均的法线角度,决定是否还有继续探索下去的必要。
	//(也就是假设每个点都是平稳弯曲的,那么normal的夹角都很小,但是时间长了偏移的就大了,这个参数就是限制这个用的)

	std::vector <pcl::PointIndices> clusters;
	reg.extract(clusters);
	//把结果输出到一个簇里面,这个簇会自动把每个平面分成一个vector,可以打印下来看看
	std::cout << "Number of clusters is equal to " << clusters.size() << std::endl;
	std::cout << "First cluster has " << clusters[0].indices.size() << " points." << endl;
	std::cout << "These are the indices of the points of the initial" <<
		std::endl << "cloud that belong to the first cluster:" << std::endl;

	//int counter = 0;
	//while (counter < clusters[0].indices.size())
	//{
	//	std::cout << clusters[0].indices[counter] << ", ";
	//	counter++;
	//	if (counter % 10 == 0)
	//		std::cout << std::endl;
	//}
	//std::cout << std::endl;

	//可视化聚类的结果
	pcl::PointCloud <pcl::PointXYZRGB>::Ptr colored_cloud = reg.getColoredCloud();
	pcl::visualization::CloudViewer viewer("Cluster viewer");
	viewer.showCloud(colored_cloud);
	while (!viewer.wasStopped())
	{
	}
	pcl::PCDWriter writer;//将点云写入磁盘
	writer.write("G:/VS_TEST/ConsoleApplication1/1abe78447898821e93f0194265a9746c.pcd", *colored_cloud, false);//改成想要输出的点云名称

	t2 = GetTickCount();  //从这句到return 0之间的两句为计时函数
	printf("Use Time:%f\n", (t2 - t1)*1.0 / 1000);

	system("pause");//windows命令行窗口暂停
	return (0);
}

四、实验结果

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值