博客新址: http://blog.xuezhisd.top
邮箱:xuezhisd@126.com
PCL系列
- PCL系列——读入PCD格式文件操作
- PCL系列——将点云数据写入PCD格式文件
- PCL系列——拼接两个点云
- PCL系列——从深度图像(RangeImage)中提取NARF关键点
- PCL系列——如何可视化深度图像
- PCL系列——如何使用迭代最近点法(ICP)配准
- PCL系列——如何逐渐地配准一对点云
- PCL系列——三维重构之泊松重构
- PCL系列——三维重构之贪婪三角投影算法
- PCL系列——三维重构之移动立方体算法
说明
通过本教程,我们将会学会:
- 如果通过贪婪三角投影算法进行三维点云重构。
- 程序支持两种文件格式:
*.pcd
和*.ply
。 - 程序先读取点云文件;然后计算法向量,并将法向量和点云坐标放在一起;接着使用贪婪三角投影算法进行重构,最后显示结果。
操作
- 在VS2010 中新建一个文件
recon_greedyProjection.cpp
,然后将下面的代码复制到文件中。 - 参照之前的文章,配置项目的属性。设置包含目录和库目录和附加依赖项。
#include <pcl/point_types.h>
#include <pcl/io/pcd_io.h>
#include <pcl/io/ply_io.h>
#include <pcl/kdtree/kdtree_flann.h>
#include <pcl/features/normal_3d.h>
#include <pcl/surface/gp3.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <boost/thread/thread.hpp>
#include <fstream>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
int main (int argc, char** argv)
{
char tmpStr[100];
strcpy(tmpStr,argv[1]);
char* pext = strrchr(tmpStr, '.');
std::string extply("ply");
std::string extpcd("pcd");
if(pext){
*pext='\0';
pext++;
}
std::string ext(pext);
if (!((ext == extply)||(ext == extpcd))){
std::cout << "文件格式不支持!" << std::endl;
std::cout << "支持文件格式:*.pcd和*.ply!" << std::endl;
return(-1);
}
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>) ;
if (ext == extply){
if (pcl::io::loadPLYFile(argv[1] , *cloud) == -1){
PCL_ERROR("Could not read ply file!\n") ;
return -1;
}
}
else{
if (pcl::io::loadPCDFile(argv[1] , *cloud) == -1){
PCL_ERROR("Could not read pcd file!\n") ;
return -1;
}
}
pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal> n;
pcl::PointCloud<pcl::Normal>::Ptr normals (new pcl::PointCloud<pcl::Normal>);
pcl::search::KdTree<pcl::PointXYZ>::Ptr tree (new pcl::search::KdTree<pcl::PointXYZ>);
tree->setInputCloud (cloud);
n.setInputCloud (cloud);
n.setSearchMethod (tree);
n.setKSearch (20);
n.compute (*normals);
pcl::PointCloud<pcl::PointNormal>::Ptr cloud_with_normals (new pcl::PointCloud<pcl::PointNormal>);
pcl::concatenateFields (*cloud, *normals, *cloud_with_normals);
pcl::search::KdTree<pcl::PointNormal>::Ptr tree2 (new pcl::search::KdTree<pcl::PointNormal>);
tree2->setInputCloud (cloud_with_normals);
pcl::GreedyProjectionTriangulation<pcl::PointNormal> gp3;
pcl::PolygonMesh triangles;
gp3.setSearchRadius (1.5f);
gp3.setMu (2.5f);
gp3.setMaximumNearestNeighbors (100);
gp3.setMaximumSurfaceAngle(M_PI/4);
gp3.setMinimumAngle(M_PI/18);
gp3.setMaximumAngle(2*M_PI/3);
gp3.setNormalConsistency(false);
gp3.setInputCloud(cloud_with_normals);
gp3.setSearchMethod(tree2);
gp3.reconstruct (triangles);
pcl::io::savePLYFile("result.ply", triangles);
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer (new pcl::visualization::PCLVisualizer ("3D Viewer"));
viewer->setBackgroundColor (0, 0, 0);
viewer->addPolygonMesh(triangles,"my");
viewer->addCoordinateSystem (1.0);
viewer->initCameraParameters ();
while (!viewer->wasStopped ()){
viewer->spinOnce (100);
boost::this_thread::sleep (boost::posix_time::microseconds (100000));
}
return (0);
}
- 重新生成项目。
- 到改项目的Debug目录下,按住Shift,同时点击鼠标右键,在当前窗口打开CMD窗口。
- 在命令行中输入
recon_greedyProjection.exe bunny.points.ply
,执行程序。得到如下图所示的结果。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)