在c++MFC下用PCL显示操作点云文件 MFC对话框显示操作PCL点云
原文作者:aircraft
原文地址:https://www.cnblogs.com/DOMLX/p/13115873.html
第一步 下载PCL库 我的版本是1.8.1的
你都要MFC下跑PCL了 你不会不知道怎么下载吧 ( 不会吧 不会吧 不会有人真的不知道怎么下载吧。。。)
第二步 新建一个MFC对话框程序(这个不要人教的把 )
打开VS2017 新建项目-MFC应用程序-基于对话框
第三步 配置PCL
点开属性管理器 debugx64下新建一个属性页命名PCL_ALLINONE (因为我是debug x64 你们32就32咯)
然后右键打开其属性
在包含目录编辑 将自己的PCL库包含 注意找你们自己PCL的路径 这个要是来问我 我不锤爆你们的狗头!!!!
库目录包含
在到链接器-》输入-》附加依赖项包含自己PCL库的lib文件名字
(注意!!!如果你们库跟我一样可以直接用我的) 如果不一样 那么到相应的lib库文件位置 打开cmd 然后用命令行将文件名都输入到一个文件保存就行了 好像是 dir \b >>1.txt (不太记得了 然后复制里面lib结尾的名字粘贴就行了)
vtknetcdf_c++-gd.lib
pcl_common_debug.lib
pcl_features_debug.lib
pcl_filters_debug.lib
pcl_io_ply_debug.lib
pcl_io_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
libboost_atomic-vc141-mt-gd-1_64.lib
libboost_bzip2-vc141-mt-gd-1_64.lib
libboost_chrono-vc141-mt-gd-1_64.lib
libboost_container-vc141-mt-gd-1_64.lib
libboost_context-vc141-mt-gd-1_64.lib
libboost_coroutine-vc141-mt-gd-1_64.lib
libboost_date_time-vc141-mt-gd-1_64.lib
libboost_exception-vc141-mt-gd-1_64.lib
libboost_fiber-vc141-mt-gd-1_64.lib
libboost_filesystem-vc141-mt-gd-1_64.lib
libboost_graph-vc141-mt-gd-1_64.lib
libboost_graph_parallel-vc141-mt-gd-1_64.lib
libboost_iostreams-vc141-mt-gd-1_64.lib
libboost_locale-vc141-mt-gd-1_64.lib
libboost_log-vc141-mt-gd-1_64.lib
libboost_log_setup-vc141-mt-gd-1_64.lib
libboost_math_c99-vc141-mt-gd-1_64.lib
libboost_math_c99f-vc141-mt-gd-1_64.lib
libboost_math_c99l-vc141-mt-gd-1_64.lib
libboost_math_tr1-vc141-mt-gd-1_64.lib
libboost_math_tr1f-vc141-mt-gd-1_64.lib
libboost_math_tr1l-vc141-mt-gd-1_64.lib
libboost_mpi-vc141-mt-gd-1_64.lib
libboost_numpy3-vc141-mt-gd-1_64.lib
libboost_numpy-vc141-mt-gd-1_64.lib
libboost_prg_exec_monitor-vc141-mt-gd-1_64.lib
libboost_program_options-vc141-mt-gd-1_64.lib
libboost_python3-vc141-mt-gd-1_64.lib
libboost_python-vc141-mt-gd-1_64.lib
libboost_random-vc141-mt-gd-1_64.lib
libboost_regex-vc141-mt-gd-1_64.lib
libboost_serialization-vc141-mt-gd-1_64.lib
libboost_signals-vc141-mt-gd-1_64.lib
libboost_system-vc141-mt-gd-1_64.lib
libboost_test_exec_monitor-vc141-mt-gd-1_64.lib
libboost_thread-vc141-mt-gd-1_64.lib
libboost_timer-vc141-mt-gd-1_64.lib
libboost_type_erasure-vc141-mt-gd-1_64.lib
libboost_unit_test_framework-vc141-mt-gd-1_64.lib
libboost_wave-vc141-mt-gd-1_64.lib
libboost_wserialization-vc141-mt-gd-1_64.lib
libboost_zlib-vc141-mt-gd-1_64.lib
flann-gd.lib
flann_cpp-gd.lib
flann_cpp_s-gd.lib
flann_s-gd.lib
qhull_d.lib
qhullcpp_d.lib
qhullstatic_d.lib
qhullstatic_r_d.lib
qhull_p_d.lib
qhull_r_d.lib
vtkalglib-8.0-gd.lib
vtkChartsCore-8.0-gd.lib
vtkCommonColor-8.0-gd.lib
vtkCommonComputationalGeometry-8.0-gd.lib
vtkCommonCore-8.0-gd.lib
vtkCommonDataModel-8.0-gd.lib
vtkCommonExecutionModel-8.0-gd.lib
vtkCommonMath-8.0-gd.lib
vtkCommonMisc-8.0-gd.lib
vtkCommonSystem-8.0-gd.lib
vtkCommonTransforms-8.0-gd.lib
vtkDICOMParser-8.0-gd.lib
vtkDomainsChemistry-8.0-gd.lib
vtkexoIIc-8.0-gd.lib
vtkexpat-8.0-gd.lib
vtkFiltersAMR-8.0-gd.lib
vtkFiltersCore-8.0-gd.lib
vtkFiltersExtraction-8.0-gd.lib
vtkFiltersFlowPaths-8.0-gd.lib
vtkFiltersGeneral-8.0-gd.lib
vtkFiltersGeneric-8.0-gd.lib
vtkFiltersGeometry-8.0-gd.lib
vtkFiltersHybrid-8.0-gd.lib
vtkFiltersHyperTree-8.0-gd.lib
vtkFiltersImaging-8.0-gd.lib
vtkFiltersModeling-8.0-gd.lib
vtkFiltersParallel-8.0-gd.lib
vtkFiltersParallelImaging-8.0-gd.lib
vtkFiltersPoints-8.0-gd.lib
vtkFiltersProgrammable-8.0-gd.lib
vtkFiltersSelection-8.0-gd.lib
vtkFiltersSMP-8.0-gd.lib
vtkFiltersSources-8.0-gd.lib
vtkFiltersStatistics-8.0-gd.lib
vtkFiltersTexture-8.0-gd.lib
vtkFiltersTopology-8.0-gd.lib
vtkFiltersVerdict-8.0-gd.lib
vtkfreetype-8.0-gd.lib
vtkGeovisCore-8.0-gd.lib
vtkgl2ps-8.0-gd.lib
vtkhdf5-8.0-gd.lib
vtkhdf5_hl-8.0-gd.lib
vtkImagingColor-8.0-gd.lib
vtkImagingCore-8.0-gd.lib
vtkImagingFourier-8.0-gd.lib
vtkImagingGeneral-8.0-gd.lib
vtkImagingHybrid-8.0-gd.lib
vtkImagingMath-8.0-gd.lib
vtkImagingMorphological-8.0-gd.lib
vtkImagingSources-8.0-gd.lib
vtkImagingStatistics-8.0-gd.lib
vtkImagingStencil-8.0-gd.lib
vtkInfovisCore-8.0-gd.lib
vtkInfovisLayout-8.0-gd.lib
vtkInteractionImage-8.0-gd.lib
vtkInteractionStyle-8.0-gd.lib
vtkInteractionWidgets-8.0-gd.lib
vtkIOAMR-8.0-gd.lib
vtkIOCore-8.0-gd.lib
vtkIOEnSight-8.0-gd.lib
vtkIOExodus-8.0-gd.lib
vtkIOExport-8.0-gd.lib
vtkIOExportOpenGL-8.0-gd.lib
vtkIOGeometry-8.0-gd.lib
vtkIOImage-8.0-gd.lib
vtkIOImport-8.0-gd.lib
vtkIOInfovis-8.0-gd.lib
vtkIOLegacy-8.0-gd.lib
vtkIOLSDyna-8.0-gd.lib
vtkIOMINC-8.0-gd.lib
vtkIOMovie-8.0-gd.lib
vtkIONetCDF-8.0-gd.lib
vtkIOParallel-8.0-gd.lib
vtkIOParallelXML-8.0-gd.lib
vtkIOPLY-8.0-gd.lib
vtkIOSQL-8.0-gd.lib
vtkIOTecplotTable-8.0-gd.lib
vtkIOVideo-8.0-gd.lib
vtkIOXML-8.0-gd.lib
vtkIOXMLParser-8.0-gd.lib
vtkjpeg-8.0-gd.lib
vtkjsoncpp-8.0-gd.lib
vtklibharu-8.0-gd.lib
vtklibxml2-8.0-gd.lib
vtklz4-8.0-gd.lib
vtkmetaio-8.0-gd.lib
vtkNetCDF-8.0-gd.lib
vtkoggtheora-8.0-gd.lib
vtkParallelCore-8.0-gd.lib
vtkpng-8.0-gd.lib
vtkproj4-8.0-gd.lib
vtkRenderingAnnotation-8.0-gd.lib
vtkRenderingContext2D-8.0-gd.lib
vtkRenderingContextOpenGL-8.0-gd.lib
vtkRenderingCore-8.0-gd.lib
vtkRenderingFreeType-8.0-gd.lib
vtkRenderingGL2PS-8.0-gd.lib
vtkRenderingImage-8.0-gd.lib
vtkRenderingLabel-8.0-gd.lib
vtkRenderingLIC-8.0-gd.lib
vtkRenderingLOD-8.0-gd.lib
vtkRenderingOpenGL-8.0-gd.lib
vtkRenderingVolume-8.0-gd.lib
vtkRenderingVolumeOpenGL-8.0-gd.lib
vtksqlite-8.0-gd.lib
vtksys-8.0-gd.lib
vtktiff-8.0-gd.lib
vtkverdict-8.0-gd.lib
vtkViewsContext2D-8.0-gd.lib
vtkViewsCore-8.0-gd.lib
vtkViewsInfovis-8.0-gd.lib
vtkzlib-8.0-gd.lib
opengl32.lib
配置完毕 以后其他mfc就不用重复配置 将这个配置文件PCL_ALLINONG复制添加就行了
第四步 编辑代码
编辑对话框 添加一个pictrue_control控件 改ID为IDC_PCDVIEW 添加一个按钮改名为openFILE
双击openFile按钮进入按钮事件编辑 粘贴代码:
CString strFile = _T("");
CFileDialog dlgFile(TRUE, NULL, NULL, OFN_HIDEREADONLY, _T("Describe Files (*.pcd)|*.pcd|All Files (*.*)|*.*||"), NULL);
if (dlgFile.DoModal())
{
strFile = dlgFile.GetPathName();
//Cstring 转string
CString theCStr;
std::string STDStr(CW2A(strFile.GetString()));
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new
pcl::PointCloud<pcl::PointXYZ>);
if (pcl::io::loadPCDFile<pcl::PointXYZ>(STDStr, *cloud) == -1)//*打开点云文件
{
AfxMessageBox(_T("读入点云数据失败"));
}
m_viewer->removeAllPointClouds();//将前一次点云移除
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> single_color(cloud, 0, 255, 0);
m_viewer->addPointCloud<pcl::PointXYZ >(cloud, single_color, "sample cloud");
}
代码就是打开文件选取PCD点云 然后显示
在 某某某dlg.h下添加头文件
#include <pcl/console/parse.h>
#include <pcl/io/io.h>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/common/io.h>
#include <pcl/visualization/pcl_visualizer.h>
//vtk
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL); // VTK was built with vtkRenderingOpenGL2
VTK_MODULE_INIT(vtkInteractionStyle);
using namespace pcl;
如果上面这个头文件会报错试试下面这套 不报错就用上面的 (因为我的MFC有时候用第一套会报错)
#undef min //新加代码// #undef max #include <pcl/visualization/cloud_viewer.h> #include <iostream> #include <pcl/io/io.h> #include <pcl/io/pcd_io.h> #include <pcl/console/parse.h> #include <pcl/io/io.h> #include <pcl/io/pcd_io.h> #include <pcl/point_types.h> #include <pcl/common/io.h> #include <pcl/visualization/pcl_visualizer.h> //vtk #include <vtkRenderWindow.h> #include <vtkRenderWindowInteractor.h> #include "vtkAutoInit.h" VTK_MODULE_INIT(vtkRenderingOpenGL); // VTK was built with vtkRenderingOpenGL2 VTK_MODULE_INIT(vtkInteractionStyle); using namespace pcl;
并且在这个dlg.h的文件类里添加:
private:
boost::shared_ptr<pcl::visualization::PCLVisualizer> m_viewer;//要共享指针类型的,要不然,显示窗口会跳出MFC界面
vtkRenderWindow* m_win; //vtk渲染的窗口句柄
vtkRenderWindowInteractor* m_iren;//vtk交互的对象
然后在dlg.cpp文件的OnInitDialog()初始化函数下添加: (函数里的直接复制过去就行了 毕竟我工程名字乱打的)
BOOL CMFCApplication3Dlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// 设置此对话框的图标。 当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
// TODO: 在此添加额外的初始化代码
//pcl显示窗口初始化
m_viewer.reset(new pcl::visualization::PCLVisualizer("viewer", false));//初始化viewer对象
m_viewer->addCoordinateSystem(); //设置对应的坐标系
m_viewer->setBackgroundColor(0, 0, 0);//设置背景颜色
m_viewer->initCameraParameters();//初始化相机的参数
m_win = m_viewer->getRenderWindow();//将view中的渲染窗口的句柄传递给vtk window
m_iren = vtkRenderWindowInteractor::New(); //初始化vtkwindow交互的对象
m_viewer->resetCamera();//使点云显示在屏幕中间,并绕中心操作
CRect rect;
GetClientRect(&rect);//实时获取MFC窗口大小
m_win->SetSize(rect.right - rect.left, rect.bottom - rect.top);//根据当前窗口的大小设置vtk 窗口的大小
CWnd *viewer_pcWnd;
viewer_pcWnd = this->GetDlgItem(IDC_PCDVIEW);//获取对应的wnd
m_win->SetParentId(viewer_pcWnd->m_hWnd);//设置vtk窗口的句柄
m_iren->SetRenderWindow(m_win);//将vtk交互对象与vtk window绑定
m_viewer->createInteractor();
m_win->Render();//开始渲染
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
如果这页的开头附近有下面这个 直接注释掉 不然就会报错 error C2661: “pcl::PointCloud::operator new”: 没有重载函数接受 3 个参数
//#ifdef _DEBUG //#define new DEBUG_NEW //#endif
最后结果
运行点击打开选了一个bunny.pcd打开显示这样 就这么简单搞定了 根本不需要网上的其他什么MFC教程 又要cmake编译啊 又要单文档得 (TMen都是呆子)
(bunny.pcd文件不要找我拿 你都要显示点云了 一个点云文件没有?那你心里可能没有点B数啊)
如果你们编译运行错误 自己复制错误去百度啊 难道来问我 难道我知道?
若有兴趣交流分享技术,可关注本人公众号,里面会不定期的分享各种编程教程,和共享源码,诸如研究分享关于c/c++,python,前端,后端,opencv,halcon,opengl,机器学习深度学习之类有关于基础编程,图像处理和机器视觉开发的知识