PCL点云库学习笔记(1):点云读取、显示、噪声点去除,体素化下采样、法向量求解并显示

初学者笔记:

点云数据链接(不会上传,所以用了百度云):
链接:https://pan.baidu.com/s/1VTVxn3BntbAr9tGHv6L-HA
提取码:u81q

#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL)
#include <pcl/features/normal_3d.h>
#include <pcl/kdtree/kdtree.h>
#include <pcl/point_types.h>
#include <pcl/io/pcd_io.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/filters/statistical_outlier_removal.h>
#include <pcl/filters/voxel_grid.h>
#include <iostream>
#include <pcl/visualization/cloud_viewer.h>

using namespace pcl;
using namespace std;
//显示
void visualization_point(PointCloud<PointXYZ>::Ptr &sor_cloud, PointCloud<PointXYZ>::Ptr &voxel, 
	PointCloud<Normal>::Ptr &normal1, PointCloud<Normal>::Ptr &normal2, PointCloud<PointXYZ>::Ptr &final_point) {
	visualization::PCLVisualizer::Ptr viewer(new visualization::PCLVisualizer("3d viewer"));
	int v1(0), v2(0);
	viewer->createViewPort(0.0, 0.0, 0.5, 1.0, v1);
	//visualization::PointCloudColorHandlerCustom<PointXYZ>sor_cloud1(sor_cloud, 0, 255, 0);
	visualization::PointCloudColorHandlerGenericField<PointXYZ>sor_cloud1(sor_cloud, "z");
	viewer->setBackgroundColor(255, 0, 255, v1);
	viewer->addPointCloud(sor_cloud, sor_cloud1, "cloud1", v1);
	viewer->addPointCloudNormals<PointXYZ, Normal>(sor_cloud, normal1, 100, 0.05, "normal1", v1);
	viewer->setPointCloudRenderingProperties(visualization::PCL_VISUALIZER_POINT_SIZE, 5, "cloud1", v1);
	viewer->setPointCloudRenderingProperties(visualization::PCL_VISUALIZER_LINE_WIDTH, 2, "normal1", v1);

	viewer->createViewPort(0.5, 0.0, 1.0, 1.0, v2);
	viewer->setBackgroundColor(0, 0, 0, v2);
	visualization::PointCloudColorHandlerRandom<PointXYZ>voxel_color(voxel);
	viewer->addPointCloud(voxel, voxel_color, "cloud2", v2);
	viewer->addPointCloudNormals<PointXYZ, Normal>(voxel, normal2, 10, 0.05, "normal2", v2);
	viewer->setPointCloudRenderingProperties(visualization::PCL_VISUALIZER_POINT_SIZE, 5, "cloud2", v2);
	viewer->setPointCloudRenderingProperties(visualization::PCL_VISUALIZER_LINE_WIDTH, 2, "normal2", v2);
	viewer->spin();
	*final_point = *sor_cloud;

	
}
//主函数
int main(){
	PointCloud<PointXYZ>::Ptr cloud(new PointCloud<PointXYZ>);
	if (io::loadPCDFile("C:\\Users\\Administrator\\Desktop\\desk.pcd", *cloud) == -1){
		cout << "read false" << endl;
	}
	//噪声点去除
	PointCloud<PointXYZ>::Ptr sor_cloud(new PointCloud<PointXYZ>);
	StatisticalOutlierRemoval<PointXYZ> sor;
	sor.setInputCloud(cloud);
	sor.setMeanK(8);
	sor.setStddevMulThresh(1);
	sor.filter(*sor_cloud);
	// 体素化下采样
	PointCloud<PointXYZ>::Ptr voxel(new PointCloud<PointXYZ>);
	VoxelGrid<PointXYZ> vox;
	vox.setInputCloud(sor_cloud);
	vox.setLeafSize(0.01,0.01,0.01);
	vox.filter(*voxel);
	//法向量求解
	NormalEstimation<PointXYZ, Normal>ne;
	PointCloud<Normal>::Ptr normal1(new PointCloud<Normal>);
	PointCloud<Normal>::Ptr normal2(new PointCloud<Normal>);
	search::KdTree<PointXYZ>::Ptr tree(new search::KdTree<PointXYZ>);
	ne.setInputCloud(voxel);
	ne.setSearchMethod(tree);
	ne.setKSearch(5);
	ne.compute(*normal2);
	ne.setInputCloud(sor_cloud);
	ne.compute(*normal1);

	PointCloud<PointXYZ>::Ptr final_point(new PointCloud<PointXYZ>);
	visualization_point(sor_cloud, voxel, normal1, normal2,final_point);
	visualization::CloudViewer viewer1("windows");
	viewer1.showCloud(final_point);
	while (!viewer1.wasStopped())
	{
	}
	return 0;
}

程序输出结果:程序输出的结果
原始点云数据:在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值