#include <iostream>
#include <pcl/point_types.h>
#include <pcl/io/ply_io.h>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#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>
void pcd2ply();
void ply2pcd();
int possionSurfaceReconstructio();
int main(int argc, char** argv){
possionSurfaceReconstructio();
return (0);
}
//possion表面重建
int possionSurfaceReconstructio() {
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
if (pcl::io::loadPCDFile("C:\\Users\\XuHui\\Desktop\\China_dragon.pcd", *cloud) == -1) {
PCL_ERROR("Could not read pcd file!\n");
return -1;
}
pcl::PointCloud<pcl::PointNormal>::Ptr cloud_with_normals(new pcl::PointCloud<pcl::PointNormal>);
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::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.setSearchMethod(tree2);
pn.setInputCloud(cloud_with_normals);
pn.setConfidence(false);//设置置信标志,为true时,使用法线向量长度作为置信度信息,false则需要对法线进行归一化处理
pn.setManifold(false);//设置流行标志,如果设置为true,则对多边形进行细分三角话时添加重心,设置false则不添加
pn.setOutputPolygons(false);//设置是否输出为多边形
pn.setIsoDivide(8);
pn.setSamplesPerNode(3);//设置每个八叉树节点上最少采样点数目
pcl::PolygonMesh mesh;
pn.performReconstruction(mesh);
pcl::io::savePLYFile("C:\\Users\\XuHui\\Desktop\\reconstruction.ply", mesh);
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer("3Dviewer"));
viewer->setBackgroundColor(0, 0, 0);
viewer->addPolygonMesh(mesh, "my");
viewer->initCameraParameters();
while (!viewer->wasStopped()) {
viewer->spinOnce(100);
boost::this_thread::sleep(boost::posix_time::microseconds(100000));
}
return 0;
}
void pcd2ply() {
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
// 读取点云
if (pcl::io::loadPCDFile<pcl::PointXYZ>("C:\\Users\\XuHui\\Desktop\\22_12_05_16_43 - Cloud.pcd", *cloud) == -1)
{
PCL_ERROR("输入文件不正确");
return;
}
pcl::PLYWriter writer;
writer.write("D:\\result\\Ply.ply", *cloud);
}
void ply2pcd() {
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
//加载文件
if (pcl::io::loadPLYFile<pcl::PointXYZ>("Ply.ply", *cloud) == -1)
{
PCL_ERROR("输入文件不正确");
return;
}
// 输出点的个数
std::cout << "point number: " << cloud->width * cloud->height << std::endl;
pcl::PCDWriter writer;
writer.write("Pcd.pcd", *cloud);
}
possion表面重建
于 2022-12-09 09:15:29 首次发布