PCL series
- PCL Series--read in the PCD format file operation
- PCL Series--writing point cloud data to a PCD format file
- PCL Series-Stitching two point clouds
- PCL Series--extracting Narf key points from depth image (Rangeimage)
- PCL Series--how to visualize depth images
- PCL Series--How to use the iterative nearest point method (ICP) Registration
- PCL Series--How to gradually register a pair of point clouds
- PCL Series--Poisson reconstruction of three-dimensional reconstruction
- PCL Series--three-dimensional reconstruction of greedy triangular projection algorithm
- PCL Series--three-dimensional reconstruction of moving cube algorithm
Description
Through this tutorial, we will learn to:
- If the three-dimensional point cloud reconstruction is done by moving the cube algorithm.
- The program supports two file formats:
*.pcd
and *.ply
.
- The program reads the point cloud file First, then calculates the normal vector and puts the normal vector and the point cloud coordinates together, then uses the moving cube algorithm to reconstruct, finally displays the result.
Operation
- Create a new file in VS2010
recon_marchingCubes.cpp
, and then copy the following code to the file.
- Refer to the previous article to configure the properties of the item. Sets the Include directory and Library directory and additional dependencies.
#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/marching_cubes_hoppe.h>#include <pcl/surface/marching_cubes_rbf.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>intMain (intargcChar* * argv) {//Determine file format Chartmpstr[ -];strcpy(tmpstr,argv[1]);Char* Pext =STRRCHR(Tmpstr,'. ');STD::stringExtply ("Ply");STD::stringEXTPCD ("PCD");if(Pext) {*pext=' + '; pext++; }STD::stringExt (pext);//If file format is not supported, exit the program if(! (ext = = extply) | | (ext = = EXTPCD))) {STD::cout<<"file format not supported!"<<STD:: Endl;STD::cout<<"Supported file formats: *.PCD and *.ply! "<<STD:: Endl;return(-1); }//Select input mode according to file formatPCL::P OINTCLOUD<PCL::P ointxyz>::P TR Cloud (NewPCL::P OINTCLOUD<PCL::P ointxyz>);//Create point cloud object pointer to store input 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; } }//Estimation method vectorPCL::NORMALESTIMATION<PCL::P ointxyz, pcl::normal> N; PCL::P ointcloud<pcl::normal>::P tr normals (NewPCL::P ointcloud<pcl::normal>); PCL::SEARCH::KDTREE<PCL::P ointxyz>::P tr Tree (NewPCL::SEARCH::KDTREE<PCL::P ointxyz>); Tree->setinputcloud (Cloud); N.setinputcloud (Cloud); N.setsearchmethod (tree); N.setksearch ( -); N.compute (*normals);//Calculate normals, results stored in normals //* normals cannot contain both the normal vector of the point and the curvature of the surface //Put the point cloud and the normals togetherPCL::P OINTCLOUD<PCL::P ointnormal>::P tr cloud_with_normals (NewPCL::P OINTCLOUD<PCL::P ointnormal>); Pcl::concatenatefields (*cloud, *normals, *cloud_with_normals);//* cloud_with_normals = cloud + normals //Create search TreePCL::SEARCH::KDTREE<PCL::P ointnormal>::P tr Tree2 (NewPCL::SEARCH::KDTREE<PCL::P ointnormal>); Tree2->setinputcloud (cloud_with_normals);//Initialize the Marchingcubes object and set the parametersPCL::MARCHINGCUBES<PCL::P ointnormal> *MC; MC =NewPCL::MARCHINGCUBESHOPPE<PCL::P ointnormal> ();/ * if (HOPPE_OR_RBF = = 0) mc = new PCL::MARCHINGCUBESHOPPE<PCL::P ointnormal> (); else {mc = new PCL::MARCHINGCUBESRBF<PCL::P ointnormal> (); (REINTERPRET_CAST<PCL::MARCHINGCUBESRBF<PCL::P ointnormal>*> (MC))->setoffsurfacedisplacement (off _surface_displacement); } */ //Create a multi-deformed mesh for storing resultsPCL::P Olygonmesh Mesh;//Set parameters for the Marchingcubes objectMc->setisolevel (0.0f); Mc->setgridresolution ( -, -, -); Mc->setpercentageextendgrid (0.0f);//Set Search methodMc->setinputcloud (cloud_with_normals);//Perform refactoring, save results in meshMc->reconstruct (mesh);//Save grid DiagramPcl::io::saveplyfile ("Result.ply", mesh);//Show result graphBoost::shared_ptr<pcl::visualization::P clvisualizer> Viewer (NewPcl::visualization::P Clvisualizer ("3D Viewer")); Viewer->setbackgroundcolor (0,0,0);//Set BackgroundViewer->addpolygonmesh (Mesh,"My");//Set the displayed gridViewer->addcoordinatesystem (1.0);//Set coordinate systemViewer->initcameraparameters (); while(!viewer->wasstopped ()) {Viewer->spinonce ( -); Boost::this_thread::sleep (boost::p osix_time::microseconds (100000)); }return(0);}
- Rebuild the project.
- To change the project's debug directory, hold down SHIFT while you click the right mouse button to open the Cmd window in the current window.
- Enter in the command line
recon_marchingCubes.exe bunny.points.ply
to execute the program. Get the result as shown.
PCL Series--three-dimensional reconstruction of moving cube algorithm