初学者笔记:
点云数据链接(不会上传,所以用了百度云):
链接: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;
}
程序输出结果:
原始点云数据: