1、泊松重建
#include "stdafx.h"
#include <iostream>
#include <string>
#include <pcl\io\pcd_io.h>
#include <pcl\io\ply_io.h>
#include <pcl\point_types.h>
#include <pcl\kdtree\kdtree_flann.h>
#include <pcl\features\normal_3d.h>
#include <pcl\features\normal_3d_omp.h>
#include <pcl\surface\poisson.h>
#include <pcl\surface\gp3.h>
#include <pcl\visualization\cloud_viewer.h>
#include <pcl\visualization\pcl_visualizer.h>
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::Poisson<pcl::PointNormal> pn;
pn.setDegree(2); //设置参数degree[1,5],值越大越精细,耗时越久。
pn.setDepth(8); //树的最大深度,求解2^d x 2^d x 2^d立方体元。 // 由于八叉树自适应采样密度,指定值仅为最大深度。
pn.setIsoDivide(8);
pn.setSamplesPerNode(10); //设置落入一个八叉树结点中的样本点的最小数量。无噪声,[1.0-5.0],有噪声[15.-20.]平滑
pn.setScale(1.25); //设置用于重构的立方体直径和样本边界立方体直径的比率。
pn.setSolverDivide(8); //设置求解线性方程组的Gauss-Seidel迭代方法的深度
pn.setConfidence(false);
pn.setManifold(false); //是否添加多边形的重心,当多边形三角化时。
pn.setOutputPolygons(false); //是否输出多边形网格(而不是三角化移动立方体的结果)
pn.setSearchMethod(tree2);
pn.setInputCloud(cloud_with_normals);
pcl::PolygonMesh triangles;
pn.performReconstruction(triangles);
2、贪婪三角化
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.makeShared());
n.setInputCloud(cloud.makeShared());
n.setSearchMethod(tree);
n.setKSearch(p1);
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::PolygonMesh triangles; //存储最终三角化的网络模型
pcl::GreedyProjectionTriangulation<pcl::PointNormal> gp3; // 定义三角化对象
gp3.setSearchRadius(p2 * resolution); //设置连接点之间的最大距离(最大边长)用于确定k近邻的球半径【默认值 0】
gp3.setMu(p3); 设置样本点搜索其近邻点的最远距离为2.5倍(典型值2.5-3),这样使得算法自适应点云密度的变化 设置最近邻距离的乘子,以得到每个点的最终搜索半径【默认值 0】
gp3.setMaximumNearestNeighbors(p4); //设置搜索的最近邻点的最大数量 //设置样本点最多可搜索的邻域个数,典型值是50-100
gp3.setMaximumSurfaceAngle(M_PI*p5); // 45 degrees(pi)最大平面角
gp3.setMinimumAngle(M_PI/18); // 设置三角化后得到的三角形内角的最小的角度为10°
gp3.setMaximumAngle(2*M_PI/3); // 设置三角化后得到的三角形内角的最大角度为120°
gp3.setNormalConsistency(false); //设置该参数为true保证法线朝向一致,设置为false的话不会进行法线一致性检查
gp3.setInputCloud(cloud_with_normals);
gp3.setSearchMethod(tree2);
gp3.reconstruct(triangles);
3、贪婪投影三角化
#include <pcl/point_types.h> //PCL中所有点类型定义的头文件
#include <pcl/io/pcd_io.h> //打开关闭pcd文件的类定义的头文件
#include <pcl/kdtree/kdtree_flann.h> //kdtree搜索对象的类定义的头文件
#include <pcl/features/normal_3d.h> //法向量特征估计相关类定义的头文件
#include <pcl/surface/gp3.h> //贪婪投影三角化算法类定义的头文件
#include <pcl/visualization/pcl_visualizer.h>
#include <boost/thread/thread.hpp>
int
main(int argc, char** argv)
{
// 将一个xyz点类型的pcd文件打开并存储到对象PointCloud中
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PCLPointCloud2 cloud_blob;
pcl::io::loadPCDFile("table_scene_lms400_downsampled.pcd", cloud_blob); //加载table_scene_lms400_downsampled.pcd文件
pcl::fromPCLPointCloud2(cloud_blob, *cloud); //*数据最终存储在cloud中
/**********由于例子中用到的pcd文件只有XYZ坐标,所以我们把它加载到对象
PointCloud< PointXYZ>中,上面代码打开pcd文件,并将点云存储到cloud指针对象中。 **********/
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>); //定义kd树指针
tree->setInputCloud(cloud); //用cloud构建tree对象
n.setInputCloud(cloud); //为法线估计对象设置输入点云
n.setSearchMethod(tree); //设置搜索方法
n.setKSearch(20); //设置k搜索的k值为20
n.compute(*normals); //估计法线存储结果到normals中
/***********由于本例中使用的三角化算法输入必须为有向点云,
所以需要使用PCL中的法线估计方法预先估计出数据中每个点的法线,
上面的代码就完成该预处理。*********/
// Concatenate the XYZ and normal fields*
pcl::PointCloud<pcl::PointNormal>::Ptr cloud_with_normals(new pcl::PointCloud<pcl::PointNormal>);
pcl::concatenateFields(*cloud, *normals, *cloud_with_normals); //连接字段,cloud_with_normals存储有向点云
//* cloud_with_normals = cloud + normals
/*************由于XYZ坐标字段和法线字段需要在相同PointCloud对象中,
所以创建一个新的PointNormal类型的点云来存储坐标字段和法线连接后的点云。***********/
// Create search tree*
pcl::search::KdTree<pcl::PointNormal>::Ptr tree2(new pcl::search::KdTree<pcl::PointNormal>);//定义搜索树对象
tree2->setInputCloud(cloud_with_normals); //利用点云构建搜索树
// Initialize objects
pcl::GreedyProjectionTriangulation<pcl::PointNormal> gp3; //定义三角化对象
pcl::PolygonMesh triangles; //存储最终三角化的网格模型
/******以上代码是对三角化对象相关变量进行定义。******/
gp3.setSearchRadius(0.025); //设置连接点之间的最大距离(即为三角形最大边长)为0.025
//设置各参数特征值
gp3.setMu(2.5); //设置被样本点搜索其邻近点的最远距离为2.5,为了适应点云密度的变化
gp3.setMaximumNearestNeighbors(100); //设置样本点可搜索的邻域个数为100
gp3.setMaximumSurfaceAngle(M_PI / 4); //设置某点法线方向偏离样本点法线方向的最大角度为45度
gp3.setMinimumAngle(M_PI / 18); //设置三角化后得到三角形内角最小角度为10度
gp3.setMaximumAngle(2 * M_PI / 3); //设置三角化后得到三角形内角最大角度为120度
gp3.setNormalConsistency(false); //设置该参数保证法线朝向一致
// Get result
gp3.setInputCloud(cloud_with_normals);//设置输入点云为有向点云cloud_with_normals
gp3.setSearchMethod(tree2); //设置搜索方式为tree2
gp3.reconstruct(triangles); //重建提取三角化
// std::cout << triangles;
// 附加顶点信息
std::vector<int> parts = gp3.getPartIDs();
std::vector<int> states = gp3.getPointStates();
/*********对每个点来说,ID字段中中含有连接组件和该点本身的“状态”
(例如 gp3.FREE, gp3.BOUNDARY或 gp3.COMPLETED)可以被检索到。**********/
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));
}
// Finish
return (0);
}
4、泊松重建
//点的类型的头文件
#include <pcl/point_types.h>
//点云文件IO(pcd文件和ply文件)
#include <pcl/io/pcd_io.h>
#include <pcl/io/ply_io.h>
//kd树
#include <pcl/kdtree/kdtree_flann.h>
//特征提取
#include <pcl/features/normal_3d_omp.h>
#include <pcl/features/normal_3d.h>
//重构
#include <pcl/surface/gp3.h>
#include <pcl/surface/poisson.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::PointXYZRGB > ::Ptr cloud(new pcl::PointCloud<pcl::PointXYZRGB>); //创建点云对象指针,用于存储输入
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::PointXYZRGB, pcl::Normal> n;//法线估计对象
pcl::PointCloud<pcl::Normal>::Ptr normals(new pcl::PointCloud<pcl::Normal>);//存储估计的法线的指针
pcl::search::KdTree<pcl::PointXYZRGB>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZRGB>);
tree->setInputCloud(cloud);
n.setInputCloud(cloud);
n.setSearchMethod(tree);
n.setKSearch(20);
n.compute(*normals); //计算法线,结果存储在normals中
//将点云和法线放到一起
pcl::PointCloud<pcl::PointXYZRGBNormal>::Ptr cloud_with_normals(new pcl::PointCloud<pcl::PointXYZRGBNormal>);
pcl::concatenateFields(*cloud, *normals, *cloud_with_normals);
//创建搜索树
pcl::search::KdTree<pcl::PointXYZRGBNormal>::Ptr tree2(new pcl::search::KdTree<pcl::PointXYZRGBNormal>);
tree2->setInputCloud(cloud_with_normals);
//创建Poisson对象,并设置参数
pcl::Poisson<pcl::PointXYZRGBNormal> pn;
pn.setConfidence(true); //是否使用法向量的大小作为置信信息。如果false,所有法向量均归一化。
pn.setDegree(2); //设置参数degree[1,5],值越大越精细,耗时越久。
pn.setDepth(8); //树的最大深度,求解2^d x 2^d x 2^d立方体元。由于八叉树自适应采样密度,指定值仅为最大深度。
pn.setIsoDivide(8); //用于提取ISO等值面的算法的深度
pn.setManifold(true); //是否添加多边形的重心,当多边形三角化时。 设置流行标志,如果设置为true,则对多边形进行细分三角话时添加重心,设置false则不添加
pn.setOutputPolygons(false); //是否输出多边形网格(而不是三角化移动立方体的结果)
pn.setSamplesPerNode(3.0); //设置落入一个八叉树结点中的样本点的最小数量。无噪声,[1.0-5.0],有噪声[15.-20.]平滑
pn.setScale(1.25); //设置用于重构的立方体直径和样本边界立方体直径的比率。
pn.setSolverDivide(8); //设置求解线性方程组的Gauss-Seidel迭代方法的深度
//pn.setIndices();
//设置搜索方法和输入点云
pn.setSearchMethod(tree2);
pn.setInputCloud(cloud_with_normals);
//创建多变形网格,用于存储结果
pcl::PolygonMesh mesh;
//执行重构
pn.performReconstruction(mesh);
//保存网格图
pcl::io::savePLYFile("result.ply", mesh);
// 显示结果图
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer("3D viewer"));
viewer->setBackgroundColor(0, 0, 0);
viewer->addPolygonMesh(mesh, "my");
viewer->addCoordinateSystem(50.0);
viewer->initCameraParameters();
while (!viewer->wasStopped()){
viewer->spinOnce(100);
boost::this_thread::sleep(boost::posix_time::microseconds(100000));
}
return 0;
}
5、三维数据格式的转化
### ply转stl ####
#include <pcl/io/vtk_lib_io.h>
#include <vtkPLYReader.h>
#include <vtkSTLWriter.h>
int main(int argc, char** argv)
{
vtkSmartPointer<vtkPLYReader> reader = vtkSmartPointer<vtkPLYReader>::New();
reader->SetFileName("rabbit.ply");
reader->Update();
vtkSmartPointer<vtkPolyData> polyData = vtkSmartPointer<vtkPolyData>::New();
polyData = reader->GetOutput();
polyData->GetNumberOfPoints();
vtkSmartPointer<vtkSTLWriter> writer = vtkSmartPointer<vtkSTLWriter>::New();
writer->SetInputData(polyData);
writer->SetFileName("rabbit.stl");
writer->Write();
return 0;
}
### pcd转ply #######
#include <iostream>
#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>
using namespace std;
int main(int argc, char** argv)
{
// Load input file
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
pcl::io::loadPCDFile("rabbit.pcd", *cloud);
// Normal estimation*
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(10);
n.compute(*normals);
//* normals should not contain the point normals + surface curvatures
// Concatenate the XYZ and normal fields*
pcl::PointCloud<pcl::PointNormal>::Ptr cloud_with_normals(new pcl::PointCloud<pcl::PointNormal>);
pcl::concatenateFields(*cloud, *normals, *cloud_with_normals);
//* cloud_with_normals = cloud + normals
// Create search tree*
pcl::search::KdTree<pcl::PointNormal>::Ptr tree2(new pcl::search::KdTree<pcl::PointNormal>);
tree2->setInputCloud(cloud_with_normals);
// Initialize objects
pcl::GreedyProjectionTriangulation<pcl::PointNormal> gp3;
pcl::PolygonMesh triangles;
// Set the maximum distance between connected points (maximum edge length)
gp3.setSearchRadius(1);
// Set typical values for the parameters
gp3.setMu(2.5);
gp3.setMaximumNearestNeighbors(100);
gp3.setMaximumSurfaceAngle(M_PI / 4); // 45 degrees
gp3.setMinimumAngle(M_PI / 18); // 10 degrees
gp3.setMaximumAngle(2 * M_PI / 3); // 120 degrees
gp3.setNormalConsistency(false);
// Get result
gp3.setInputCloud(cloud_with_normals);
gp3.setSearchMethod(tree2);
gp3.reconstruct(triangles);
pcl::io::savePLYFile("rabbit.ply", triangles);
return 0;
}
### pcd转ply (三角网格化) 第一种 #######
#include <iostream>
#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>
using namespace std;
int main(int argc, char** argv)
{
// Load input file
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
pcl::io::loadPCDFile("rabbit.pcd", *cloud);
// Normal estimation*
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(10);
n.compute(*normals);
//* normals should not contain the point normals + surface curvatures
// Concatenate the XYZ and normal fields*
pcl::PointCloud<pcl::PointNormal>::Ptr cloud_with_normals(new pcl::PointCloud<pcl::PointNormal>);
pcl::concatenateFields(*cloud, *normals, *cloud_with_normals);
//* cloud_with_normals = cloud + normals
// Create search tree*
pcl::search::KdTree<pcl::PointNormal>::Ptr tree2(new pcl::search::KdTree<pcl::PointNormal>);
tree2->setInputCloud(cloud_with_normals);
// Initialize objects
pcl::GreedyProjectionTriangulation<pcl::PointNormal> gp3;
pcl::PolygonMesh triangles;
// Set the maximum distance between connected points (maximum edge length)
gp3.setSearchRadius(1);
// Set typical values for the parameters
gp3.setMu(2.5);
gp3.setMaximumNearestNeighbors(100);
gp3.setMaximumSurfaceAngle(M_PI / 4); // 45 degrees
gp3.setMinimumAngle(M_PI / 18); // 10 degrees
gp3.setMaximumAngle(2 * M_PI / 3); // 120 degrees
gp3.setNormalConsistency(false);
// Get result
gp3.setInputCloud(cloud_with_normals);
gp3.setSearchMethod(tree2);
gp3.reconstruct(triangles);
pcl::io::savePLYFile("rabbit.ply", triangles);
return 0;
}
### pcd转ply 第二种 #######
#include <pcl/io/pcd_io.h>
#include <pcl/io/ply_io.h>
#include<pcl/PCLPointCloud2.h>
#include<iostream>
#include<string>
using namespace pcl;
using namespace pcl::io;
using namespace std;
int PCDtoPLYconvertor(string & input_filename, string& output_filename)
{
pcl::PCLPointCloud2 cloud;
if (loadPCDFile(input_filename, cloud) < 0)
{
cout << "Error: cannot load the PCD file!!!" << endl;
return -1;
}
PLYWriter writer;
writer.write(output_filename, cloud, Eigen::Vector4f::Zero(), Eigen::Quaternionf::Identity(), true, true);
return 0;
}
int main()
{
string input_filename = "InputFile.pcd";
string output_filename = "OotputFile.ply";
PCDtoPLYconvertor(input_filename, output_filename);
return 0;
}
### ply转pcd ###
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/io/ply_io.h>
int main(int argc, char** argv)
{
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
pcl::io::loadPLYFile("rabbit.ply", *cloud);
pcl::io::savePCDFileBinary("rabbit.pcd", *cloud);
return 0;
}
6、a-shape 曲面重构(凹包算法扩展) 设置不同的数值可以达到不同的效果
#include <pcl/io/pcd_io.h>
#include <pcl/io/ply_io.h>
#include <pcl/io/obj_io.h>
#include <pcl/point_types.h>
#include <pcl/surface/concave_hull.h>
#include <pcl/visualization/pcl_visualizer.h>
using namespace std;
int
main(int argc, char** argv)
{
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
pcl::io::loadPCDFile<pcl::PointXYZ>("data//bunny.pcd", *cloud);
pcl::PointCloud<pcl::PointXYZ>::Ptr surface_hull(new pcl::PointCloud<pcl::PointXYZ>);
pcl::ConcaveHull<pcl::PointXYZ> cavehull;
cavehull.setInputCloud(cloud);
cavehull.setAlpha(0.003);
vector<pcl::Vertices> polygons;
cavehull.reconstruct(*surface_hull, polygons);// 重建面要素到点云
pcl::PolygonMesh mesh;
cavehull.reconstruct(mesh);// 重建面要素到mesh
pcl::io::saveOBJFile("object_mesh.obj", mesh);
cerr << "Concave hull has: " << surface_hull->points.size()
<< " data points." << endl;
pcl::PCDWriter writer;
writer.write("hull.pcd", *surface_hull, false);
// 可视化
pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("hull"));
viewer->setWindowName("alshape曲面重构");
viewer->addPolygonMesh<pcl::PointXYZ>(surface_hull, polygons, "polyline");
viewer->spin();
return (0);
}