原文作者: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;
並且在這個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
}
最後結果
執行點選開啟選了一個bunny.pcd開啟顯示這樣 就這麼簡單搞定了 根本不需要網上的其他什麼MFC教程 又要cmake編譯啊 又要單文件得 (TMen都是呆子)
(bunny.pcd檔案不要找我拿 你都要顯示點雲了 一個點雲檔案沒有?那你心裡可能沒有點B數啊)
如果你們編譯執行錯誤 自己複製錯誤去百度啊 難道來問我 難道我知道?
若有興趣交流分享技術,可關注本人公眾號,裡面會不定期的分享各種程式設計教程,和共享原始碼,諸如研究分享關於c/c++,python,前端,後端,opencv,halcon,opengl,機器學習深度學習之類有關於基礎程式設計,影像處理和機器視覺開發的知識