《PCL》获取点云边界

两个限制条件:

est.setAngleThreshold(M_PI_2);    //角度:
est.setKSearch(atoi(argv[3]));  //搜索点:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

int main(int argc, char **argv)
{
	pcl::PointCloud::Ptr cloud(new pcl::PointCloud);
	
	if (pcl::io::loadPCDFile(argv[1], *cloud) == -1)
	{
		PCL_ERROR("COULD NOT READ FILE mid.pcd \n");
		return (-1);
	}

	std::cout << "points sieze is:" << cloud->size() << std::endl;
	pcl::PointCloud::Ptr normals(new pcl::PointCloud);
	pcl::PointCloud boundaries;
	pcl::BoundaryEstimation est;
	pcl::search::KdTree::Ptr tree(new pcl::search::KdTree());
	
	//创建一个快速k近邻查询,查询的时候若该点在点云中,则第一个近邻点是其本身
	pcl::KdTreeFLANN kdtree;  
	kdtree.setInputCloud(cloud);
	int k =2;
	float everagedistance =0;
	for (int i =0; i < cloud->size()/2;i++)
	{
		//std::cout << "cloud->size()/2" << cloud->points[i] << std::endl;
		vector nnh;
		vector squaredistance;
		//  pcl::PointXYZ p;
		//   p = cloud->points[i];
		kdtree.nearestKSearch(cloud->points[i], k, nnh, squaredistance);
		/*std::cout << "查询点位: " << cloud->points[i] << std::endl;
		std::cout << "近邻为: " << nnh[0] << "  " << nnh[1] << std::endl;
		std::cout << "近邻为: " << cloud->points[nnh[0]] << "  " << cloud->points[nnh[1]] << std::endl;
*/
		everagedistance += sqrt(squaredistance[1]);
		//   cout<size()/2);
	cout<<"everage distance is : "< normEst;  //其中pcl::PointXYZ表示输入类型数据,pcl::Normal表示输出类型,且pcl::Normal前三项是法向,最后一项是曲率
	normEst.setInputCloud(cloud);
	normEst.setSearchMethod(tree);
	// normEst.setRadiusSearch(2);  //法向估计的半径
	normEst.setKSearch(9);  //法向估计的点数
	normEst.compute(*normals);
	cout << "normal size is " << normals->size() << endl;

	//normal_est.setViewPoint(0,0,0); //这个应该会使法向一致
	est.setInputCloud(cloud);
	est.setInputNormals(normals);/*M_PI_2 */
	est.setAngleThreshold(M_PI_2);   ///在这里 由于构造函数已经对其进行了初始化 为Π/2 ,必须这样 使用 M_PI/2  M_PI_2  
	est.setSearchMethod(tree);
	est.setKSearch(atoi(argv[3]));  //一般这里的数值越高,最终边界识别的精度越好 20
	//  est.setRadiusSearch(everagedistance);  //搜索半径
	est.compute(boundaries);

	//  pcl::PointCloud boundPoints;
	pcl::PointCloud::Ptr boundPoints(new pcl::PointCloud);
	pcl::PointCloud noBoundPoints;
	int countBoundaries = 0;
	for (int i = 0; isize(); i++){
		uint8_t x = (boundaries.points[i].boundary_point);
		int a = static_cast(x); //该函数的功能是强制类型转换
		if (a == 1)
		{
			//  boundPoints.push_back(cloud->points[i]);
			(*boundPoints).push_back(cloud->points[i]);
			countBoundaries++;
		}
		else
			noBoundPoints.push_back(cloud->points[i]);

	}
	std::cout << "boudary size is:" << countBoundaries << std::endl;
	//  pcl::io::savePCDFileASCII("boudary.pcd",boundPoints);

	pcl::io::savePCDFileASCII("boudary.pcd", *boundPoints);
	pcl::io::savePCDFileASCII("NoBoundpoints.pcd", noBoundPoints);
	pcl::visualization::CloudViewer viewer("test");
	viewer.showCloud(boundPoints);
	while (!viewer.wasStopped())
	{
	}
	return 0;
}



你可能感兴趣的:(《PCL》获取点云边界)