VisualStudio2019配置pcl1.9.1
PCL1.9.1下载与安装
1.下载
链接: https://github.com/PointCloudLibrary/pcl/releases.
根据自己的编译器环境选择下载win64或者winX32的版本,下载
PCL-1.9.1-AllInOne-msvc2017-win64.exe
pcl-1.9.1-pdb-msvc2017-win64.zip
两个文件。
2.安装
安装步骤很简单,傻瓜式安装,点击“PCL-1.9.0-AllInOne-msvc2017-win64.exe”进行安装:
建议:选:“Add PCL to the system PATH for all users”。选择第三方库都选上。
安装到快结束的时候会弹出安装OpenNI的提示,建议安装到你自定义的第三方库的目录下(不是硬性要求安装目录,这样只是方便之后配置环境方便);
由于我之前安装过OpenNI的库,所以我弹出的选项是“修复"不是“安装”。
解压“pcl-1.9.0-pdb-msvc2019-win64.zip”,将解压得到的文件夹中的内容添加到你的PCL安装目录C:\Program Files\PCL 1.9.1\bin中。
安装到此结束。
VS2019配置PCL1.9.0
1.系统环境变量
安装的时候选了“Add PCL to the system PATH for all users”并且没有报错的,理论上来说可以跳过这一步。
手动添加:
此电脑—>属性—>高级系统属性—>高级—>环境变量
配置环境变量
找到path,双击。
添加以上8个,由于我之前装了OpenNI2,所以OpenNI2的路径不在PCL1.9.1的目录下,目录对应就会。
可能需要重启
2.配置相关目录
打开VS2019新建一个空项目。
输入名字,选择生成文件夹,然后选择“空项目”,直接确定就可以了。
编译环境是X64,Debug版本。
为方便其他工程使用PCL,创建pcl属性文件:
属性管理器—>Debug|X64中添加新建属性表“pcl”(名字自己定)。
双击新建的pcl属性表
通用属性—>VC++目录—>包含目录中添加7个目录(注意OpenNI库安装在PCL1.8的目录中的,注意检查自己的目录是否正确):
添加lib目录,在通用属性—>VC++目录—>库目录中添加如下6个目录:
在C/C++—>预处理器—>预处理器定义中添加:
_CRT_SECURE_NO_WARNINGS
_SCL_SECURE_NO_WARNINGS
_SILENCE_FPOS_SEEKPOS_DEPRECATION_WARNING
将C/C++—>所有选项—>SDL检查改为否。
最后,也是最麻烦的一个:在通用属性—>链接器—>输入—>附加的依赖项中添加对应的.“lib”文件,主要是添加PCL的“.lib”文件和第三方库中VTK的“.lib”文件。pcl的lib文件带debug,pcl的文件带gd。
pcl_common_debug.lib
pcl_features_debug.lib
pcl_filters_debug.lib
pcl_io_debug.lib
pcl_io_ply_debug.lib
pcl_kdtree_debug.lib
pcl_keypoints_debug.lib
pcl_ml_debug.lib
pcl_octree_debug.lib
pcl_outofcore_debug.lib
pcl_people_debug.lib
pcl_recognition_debug.lib
pcl_registration_debug.lib
pcl_sample_consensus_debug.lib
pcl_search_debug.lib
pcl_segmentation_debug.lib
pcl_stereo_debug.lib
pcl_surface_debug.lib
pcl_tracking_debug.lib
pcl_visualization_debug.lib
vtkalglib-8.1-gd.lib
vtkChartsCore-8.1-gd.lib
vtkCommonColor-8.1-gd.lib
vtkCommonComputationalGeometry-8.1-gd.lib
vtkCommonCore-8.1-gd.lib
vtkCommonDataModel-8.1-gd.lib
vtkCommonExecutionModel-8.1-gd.lib
vtkCommonMath-8.1-gd.lib
vtkCommonMisc-8.1-gd.lib
vtkCommonSystem-8.1-gd.lib
vtkCommonTransforms-8.1-gd.lib
vtkDICOMParser-8.1-gd.lib
vtkDomainsChemistry-8.1-gd.lib
vtkexoIIc-8.1-gd.lib
vtkexpat-8.1-gd.lib
vtkFiltersAMR-8.1-gd.lib
vtkFiltersCore-8.1-gd.lib
vtkFiltersExtraction-8.1-gd.lib
vtkFiltersFlowPaths-8.1-gd.lib
vtkFiltersGeneral-8.1-gd.lib
vtkFiltersGeneric-8.1-gd.lib
vtkFiltersGeometry-8.1-gd.lib
vtkFiltersHybrid-8.1-gd.lib
vtkFiltersHyperTree-8.1-gd.lib
vtkFiltersImaging-8.1-gd.lib
vtkFiltersModeling-8.1-gd.lib
vtkFiltersParallel-8.1-gd.lib
vtkFiltersParallelImaging-8.1-gd.lib
vtkFiltersPoints-8.1-gd.lib
vtkFiltersProgrammable-8.1-gd.lib
vtkFiltersSelection-8.1-gd.lib
vtkFiltersSMP-8.1-gd.lib
vtkFiltersSources-8.1-gd.lib
vtkFiltersStatistics-8.1-gd.lib
vtkFiltersTexture-8.1-gd.lib
vtkFiltersTopology-8.1-gd.lib
vtkFiltersVerdict-8.1-gd.lib
vtkfreetype-8.1-gd.lib
vtkGeovisCore-8.1-gd.lib
vtkgl2ps-8.1-gd.lib
vtkhdf5-8.1-gd.lib
vtkhdf5_hl-8.1-gd.lib
vtkImagingColor-8.1-gd.lib
vtkImagingCore-8.1-gd.lib
vtkImagingFourier-8.1-gd.lib
vtkImagingGeneral-8.1-gd.lib
vtkImagingHybrid-8.1-gd.lib
vtkImagingMath-8.1-gd.lib
vtkImagingMorphological-8.1-gd.lib
vtkImagingSources-8.1-gd.lib
vtkImagingStatistics-8.1-gd.lib
vtkImagingStencil-8.1-gd.lib
vtkInfovisCore-8.1-gd.lib
vtkInfovisLayout-8.1-gd.lib
vtkInteractionImage-8.1-gd.lib
vtkInteractionStyle-8.1-gd.lib
vtkInteractionWidgets-8.1-gd.lib
vtkIOAMR-8.1-gd.lib
vtkIOCore-8.1-gd.lib
vtkIOEnSight-8.1-gd.lib
vtkIOExodus-8.1-gd.lib
vtkIOExport-8.1-gd.lib
vtkIOExportOpenGL-8.1-gd.lib
vtkIOGeometry-8.1-gd.lib
vtkIOImage-8.1-gd.lib
vtkIOImport-8.1-gd.lib
vtkIOInfovis-8.1-gd.lib
vtkIOLegacy-8.1-gd.lib
vtkIOLSDyna-8.1-gd.lib
vtkIOMINC-8.1-gd.lib
vtkIOMovie-8.1-gd.lib
vtkIONetCDF-8.1-gd.lib
vtkIOParallel-8.1-gd.lib
vtkIOParallelXML-8.1-gd.lib
vtkIOPLY-8.1-gd.lib
vtkIOSQL-8.1-gd.lib
vtkIOTecplotTable-8.1-gd.lib
vtkIOVideo-8.1-gd.lib
vtkIOXML-8.1-gd.lib
vtkIOXMLParser-8.1-gd.lib
vtkjpeg-8.1-gd.lib
vtkjsoncpp-8.1-gd.lib
vtklibharu-8.1-gd.lib
vtklibxml2-8.1-gd.lib
vtklz4-8.1-gd.lib
vtkmetaio-8.1-gd.lib
vtkNetCDF-8.1-gd.lib
vtknetcdfcpp-8.1-gd.lib
vtkoggtheora-8.1-gd.lib
vtkParallelCore-8.1-gd.lib
vtkpng-8.1-gd.lib
vtkproj4-8.1-gd.lib
vtkRenderingAnnotation-8.1-gd.lib
vtkRenderingContext2D-8.1-gd.lib
vtkRenderingContextOpenGL-8.1-gd.lib
vtkRenderingCore-8.1-gd.lib
vtkRenderingFreeType-8.1-gd.lib
vtkRenderingGL2PS-8.1-gd.lib
vtkRenderingImage-8.1-gd.lib
vtkRenderingLabel-8.1-gd.lib
vtkRenderingLIC-8.1-gd.lib
vtkRenderingLOD-8.1-gd.lib
vtkRenderingOpenGL-8.1-gd.lib
vtkRenderingVolume-8.1-gd.lib
vtkRenderingVolumeOpenGL-8.1-gd.lib
vtksqlite-8.1-gd.lib
vtksys-8.1-gd.lib
vtktiff-8.1-gd.lib
vtkverdict-8.1-gd.lib
vtkViewsContext2D-8.1-gd.lib
vtkViewsCore-8.1-gd.lib
vtkViewsInfovis-8.1-gd.lib
vtkzlib-8.1-gd.lib
检查你的lib文件和我给出的静态链接库版本是否一样。
注意VTK的版本,在路径:C:\Program Files\PCL 1.9.1\3rdParty\VTK\lib中查看VTK版本。
如果是Release版本pcl的lib文件有不同,vtk的lib文件名不带gd:
pcl_common_release.lib
pcl_features_release.lib
pcl_filters_release.lib
pcl_io_ply_release.lib
pcl_io_release.lib
pcl_kdtree_release.lib
pcl_keypoints_release.lib
pcl_ml_release.lib
pcl_octree_release.lib
pcl_outofcore_release.lib
pcl_people_release.lib
pcl_recognition_release.lib
pcl_registration_release.lib
pcl_sample_consensus_release.lib
pcl_search_release.lib
pcl_segmentation_release.lib
pcl_stereo_release.lib
pcl_surface_release.lib
pcl_tracking_release.lib
pcl_visualization_release.lib
vtkalglib-8.1.lib
vtkChartsCore-8.1.lib
vtkCommonColor-8.1.lib
vtkCommonComputationalGeometry-8.1.lib
vtkCommonCore-8.1.lib
vtkCommonDataModel-8.1.lib
vtkCommonExecutionModel-8.1.lib
vtkCommonMath-8.1.lib
vtkCommonMisc-8.1.lib
vtkCommonSystem-8.1.lib
vtkCommonTransforms-8.1.lib
vtkDICOMParser-8.1.lib
vtkDomainsChemistry-8.1.lib
vtkexoIIc-8.1.lib
vtkexpat-8.1.lib
vtkFiltersAMR-8.1.lib
vtkFiltersCore-8.1.lib
vtkFiltersExtraction-8.1.lib
vtkFiltersFlowPaths-8.1.lib
vtkFiltersGeneral-8.1.lib
vtkFiltersGeneric-8.1.lib
vtkFiltersGeometry-8.1.lib
vtkFiltersHybrid-8.1.lib
vtkFiltersHyperTree-8.1.lib
vtkFiltersImaging-8.1.lib
vtkFiltersModeling-8.1.lib
vtkFiltersParallel-8.1.lib
vtkFiltersParallelImaging-8.1.lib
vtkFiltersPoints-8.1.lib
vtkFiltersProgrammable-8.1.lib
vtkFiltersSelection-8.1.lib
vtkFiltersSMP-8.1.lib
vtkFiltersSources-8.1.lib
vtkFiltersStatistics-8.1.lib
vtkFiltersTexture-8.1.lib
vtkFiltersTopology-8.1.lib
vtkFiltersVerdict-8.1.lib
vtkfreetype-8.1.lib
vtkGeovisCore-8.1.lib
vtkgl2ps-8.1.lib
vtkhdf5-8.1.lib
vtkhdf5_hl-8.1.lib
vtkImagingColor-8.1.lib
vtkImagingCore-8.1.lib
vtkImagingFourier-8.1.lib
vtkImagingGeneral-8.1.lib
vtkImagingHybrid-8.1.lib
vtkImagingMath-8.1.lib
vtkImagingMorphological-8.1.lib
vtkImagingSources-8.1.lib
vtkImagingStatistics-8.1.lib
vtkImagingStencil-8.1.lib
vtkInfovisCore-8.1.lib
vtkInfovisLayout-8.1.lib
vtkInteractionImage-8.1.lib
vtkInteractionStyle-8.1.lib
vtkInteractionWidgets-8.1.lib
vtkIOAMR-8.1.lib
vtkIOCore-8.1.lib
vtkIOEnSight-8.1.lib
vtkIOExodus-8.1.lib
vtkIOExport-8.1.lib
vtkIOExportOpenGL-8.1.lib
vtkIOGeometry-8.1.lib
vtkIOImage-8.1.lib
vtkIOImport-8.1.lib
vtkIOInfovis-8.1.lib
vtkIOLegacy-8.1.lib
vtkIOLSDyna-8.1.lib
vtkIOMINC-8.1.lib
vtkIOMovie-8.1.lib
vtkIONetCDF-8.1.lib
vtkIOParallel-8.1.lib
vtkIOParallelXML-8.1.lib
vtkIOPLY-8.1.lib
vtkIOSQL-8.1.lib
vtkIOTecplotTable-8.1.lib
vtkIOVideo-8.1.lib
vtkIOXML-8.1.lib
vtkIOXMLParser-8.1.lib
vtkjpeg-8.1.lib
vtkjsoncpp-8.1.lib
vtklibharu-8.1.lib
vtklibxml2-8.1.lib
vtklz4-8.1.lib
vtkmetaio-8.1.lib
vtkNetCDF-8.1.lib
vtknetcdfcpp-8.1.lib
vtkoggtheora-8.1.lib
vtkParallelCore-8.1.lib
vtkpng-8.1.lib
vtkproj4-8.1.lib
vtkRenderingAnnotation-8.1.lib
vtkRenderingContext2D-8.1.lib
vtkRenderingContextOpenGL-8.1.lib
vtkRenderingCore-8.1.lib
vtkRenderingFreeType-8.1.lib
vtkRenderingGL2PS-8.1.lib
vtkRenderingImage-8.1.lib
vtkRenderingLabel-8.1.lib
vtkRenderingLIC-8.1.lib
vtkRenderingLOD-8.1.lib
vtkRenderingOpenGL-8.1.lib
vtkRenderingVolume-8.1.lib
vtkRenderingVolumeOpenGL-8.1.lib
vtksqlite-8.1.lib
vtksys-8.1.lib
vtktiff-8.1.lib
vtkverdict-8.1.lib
vtkViewsContext2D-8.1.lib
vtkViewsCore-8.1.lib
vtkViewsInfovis-8.1.lib
vtkzlib-8.1.lib
配置基本结束。
例程:RANSAC平面检测(或者PCL Tutorial里找个简单例程)
PCL Tutorial 平面分割相关:http://www.pointclouds.org/documentation/tutorials/planar_segmentation.php#planar-segmentation
新建一个main.cpp,代码如下:
//ComputeBuildingNormals
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/filters/statistical_outlier_removal.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/filters/voxel_grid.h>
#include <pcl/surface/mls.h>
#include <pcl/features/normal_3d.h>
#include <pcl/surface/gp3.h>
#include <pcl/ModelCoefficients.h>//模型系数头文件
#include <pcl/sample_consensus/model_types.h>
#include <pcl/segmentation/sac_segmentation.h>//基于采样一致性分割的类的头文件
#include <pcl/filters/extract_indices.h>
//pcl::ModelCoefficients::Ptr cofficients (new pcl::ModelCoefficients);//模型系数
//pcl::PointIndices::Ptr inliers(new pcl::PointIndices);
using namespace std;
typedef pcl::PointXYZ PointT;
void SegBuilding(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_in, string filename);//定义一个函数,对输入点云进行分割
int
main(int argc, char** argv)
{
/*if (argc < 2) {
cout << "Error!" << endl;
return -1;
}*/
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_in(new pcl::PointCloud<pcl::PointXYZ>);
pcl::io::loadPCDFile("20191022Cloud.pcd", *cloud_in);
std::cout << "cloud_in: " << cloud_in->size() << endl;
std::cout << "fileds: " << pcl::getFieldsList(*cloud_in) << std::endl;
// 对点云重采样
pcl::search::KdTree<PointT>::Ptr treeSampling(new pcl::search::KdTree<PointT>);
pcl::PointCloud<PointT> mls_point;
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_smoothed;
pcl::MovingLeastSquares<PointT, PointT> mls;
mls.setComputeNormals(false);
mls.setInputCloud(cloud_in);
mls.setPolynomialOrder(2);
mls.setPolynomialFit(false);
mls.setSearchMethod(treeSampling);
mls.setSearchRadius(0.05);
mls.process(mls_point);
// 输出重采样结果
cloud_smoothed = mls_point.makeShared();
ostringstream oss;
oss << "Smoothed_" << "20191022Cloud.pcd";
std::cout << oss.str() << " |size: " << cloud_smoothed->size() << std::endl;
std::cout << oss.str() << " |fields: " << pcl::getFieldsList(mls_point) << std::endl;
//save cloud_smoothed
pcl::io::savePCDFileASCII(oss.str(), *cloud_smoothed);
// 法线估计
pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal> normalEstimation;
normalEstimation.setInputCloud(cloud_smoothed);
pcl::search::KdTree<pcl::PointXYZ>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZ>);
normalEstimation.setSearchMethod(tree);
pcl::PointCloud<pcl::Normal>::Ptr normals(new pcl::PointCloud<pcl::Normal>);
normalEstimation.setKSearch(10);
normalEstimation.compute(*normals);
std::cout << "normals: " << normals->size() << "\n" << "normals fields: " << pcl::getFieldsList(*normals) << std::endl;
oss.str("");
oss << "NormalsOnly_" << "20191022Cloud.pcd";
pcl::io::savePCDFileASCII(oss.str(), *normals);
//可视化
pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer);
int v4(0);
viewer->setBackgroundColor(0, 0, 0, v4);
// viewer->addPolylineFromPolygonMesh(triangles,"GPTriangle");
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> color2(cloud_smoothed, 0, 200, 0);
//平面分割
SegBuilding(cloud_smoothed, "20191022Cloud.pcd");//
cout << "Final cloud_smoothed: " << cloud_smoothed->size() << endl;
//保存最后的剩下的点云
oss.str("");
oss << "Remain_" << "20191022Cloud.pcd";
pcl::io::savePCDFileASCII(oss.str(), *cloud_smoothed);
cout << oss.str() << " Saved!" << endl;
// viewer->addText("cloud_filtered_out",0,0,"cloud_filtered_out",v4);
//viewer->addCoordinateSystem();
viewer->spin();
return 0;
}
void SegBuilding(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_in, string filename) {
cout << "\nSegBuilding\n------------------------------------------------------" << endl;
pcl::PCDWriter writer;
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_p(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_f(new pcl::PointCloud<pcl::PointXYZ>);
pcl::ModelCoefficients::Ptr coefficients(new pcl::ModelCoefficients());
pcl::PointIndices::Ptr inliers(new pcl::PointIndices()); //创建一个PointIndices结构体指针
// 创建分割对象
pcl::SACSegmentation<pcl::PointXYZ> seg;
// 可选
seg.setOptimizeCoefficients(true); //设置对估计的模型做优化处理
// 必选
seg.setModelType(pcl::SACMODEL_PLANE);//设置分割模型类别
seg.setMethodType(pcl::SAC_RANSAC);//设置使用那个随机参数估计方法
seg.setMaxIterations(1000);//迭代次数
seg.setDistanceThreshold(0.045);//设置是否为模型内点的距离阈值
// 创建滤波器对象
pcl::ExtractIndices<pcl::PointXYZ> extract;
int i = 0, nr_points = (int)cloud_in->points.size();
cout << "All Point: " << cloud_in->size() << "\n--------------------" << endl;
double PointNumber = cloud_in->size();
// 当还多于30%原始点云数据时
while (cloud_in->points.size() > 0.3 * nr_points) {
// 从余下的点云中分割最大平面组成部分
seg.setInputCloud(cloud_in);
seg.segment(*inliers, *coefficients);
if (inliers->indices.size() == 0) {
std::cerr << "Could not estimate a planar model for the given dataset." << std::endl;
break;
}
// 分离内层
extract.setInputCloud(cloud_in);
extract.setIndices(inliers);
extract.setNegative(false);
extract.filter(*cloud_p);//将分割点云根据inliers提取到cloud_p中
PointNumber = PointNumber - cloud_p->size();
std::cerr << "after cloud_filtered: " << PointNumber << std::endl;//剩余的点云
//保存
std::cerr << "PointCloud representing the planar component: " << cloud_p->width * cloud_p->height
<< " data points." << std::endl;
std::stringstream ss;
ss << "Plane_" << i << "_" << filename; //对每一次的提取都进行了文件保存
writer.write<pcl::PointXYZ>(ss.str(), *cloud_p, false);
cout << ss.str() << " saved!" << endl;
std::cerr << "----------------------------------" << std::endl;
// 再次创建滤波器对象
extract.setNegative(true);//提取外层
extract.filter(*cloud_f);//将外层的提取结果保存到cloud_f
cloud_in.swap(cloud_f);//经cloud_filtered与cloud_f交换
i++;
}
}
注意输入点云文件名字及位置。
运行结果:输出点云的平面检测结构,并保存点云平面
3.可能的报错
3.1…无法解析的外部符号…
1>vtkCommonCore-8.0-gd.lib(vtkWin32OutputWindow.obj) : error LNK2001: 无法解析的外部符号 __imp_MessageBoxA
1>vtkCommonCore-8.0-gd.lib(vtkWin32OutputWindow.obj) : error LNK2019: 无法解析的外部符号 __imp_GetStockObject,该符号在函数 "protected: static int __cdecl vtkWin32OutputWindow::Initialize(void)" (?Initialize@vtkWin32OutputWindow@@KAHXZ) 中被引用
...
在最顶端添加:
#pragma comment(lib, "User32.lib")
#pragma comment(lib, "gdi32.lib")
3.2遇到“XXX被声明为已否决”
直接双击属性表的根目录Debug|X64
然后更改SDL检查为否:
3.3检测到XXX的不匹配项:值…
解决方法:
1.首先检查你属性表中:链接器—输入—附加依耐项中配置的.lib文件是不是你所用的debug/release版本;
2.如果第一步没错,尝试以下步骤:
直接双击属性表的根目录Release|X64(根据你使用的是Debug还是Release)。找到C++->代码生成->运行库,更改为其他几个试一下。
3.4第三方库flann报错:
添加头文件#include <pcl/registration/icp.h>编译的时候可能会遇到如下报错:
1>...\flann\algorithms\dist.h(523): error C3861: “pop_t”: 找不到标识符
解决方法:
(注由于要对第三方库的头文件进行修改,建议先备份一下原头文件,其路径...\PCL 1.9.0\3rdParty\FLANN\include\flann\algorithms\dist.h)
开始解决:
双击该行输出直接打开dist.h头文件;
将第503行的typedef unsigned long long pop_t;移动到第480行前面;