VS2015+PCL1.81+Qt5.11.2配置VTK8.00完整方案

五迷三道 提交于 2020-02-10 17:47:53

第一次写博文,条理可能不太清楚。最近想要用Qt5制作一个GUI界面,用来显示pcl点云数据。在配置VTK的过程中碰到了许多坑,在此与大家分享,希望对大家有所帮助。

1.前期介绍

软件信息:

Visual Studio 2015_64
pcl 1.8.1
Qt 5.11.2
VTK 8.00
CMake 3.10

四者的关系:
VS不用多说,集成开发环境;pcl为点云处理的库,提供了对点云处理的函数;Qt为跨品台的GUI制作工具;Qt中没有很好显示点云的控件,需要用CMake将vtk编译为QVTKWidget控件,以供Qt调用。
接下来的任务:
在这里我假设读者现在VS中已经配置好了pcl,Qt,接下来着重介绍vtk的编译安装过程。
安装vtk有两种方式,一是用CMake自己编译vtk,也就是我接下来要讲的方法,比较麻烦;另一种是下载别人编译好的vtk(一定要注意版本,版本差别太大可能用不了),使用我编译好的vtk直接替换掉PCL1.8.1\3rdParty\VTK就行了,这种方法没有试过,不保证成功。
这里附下参考博文和我编译好的vtk网盘连接:
https://blog.csdn.net/HB_Programmer/article/details/92836303
链接:https://pan.baidu.com/s/1KgquVbbtUY7jkps11mtGbA 提取码:r3xg

2.CMake安装

一定要注意CMake的版本,开始我下载了较新的cmake-3.16.0,但是在之后编译vtk的过程中会出现检测不出VS2015_64位的情况,只能检测出VS2015,所以也建议安装较旧的版本。
官网下载比较慢,这里附下cmake-3.10的网盘链接链接:
https://pan.baidu.com/s/11B4u2fT3DmSX15m_Hgz8Eg 提取码:txql
安装过程可以直接参考博文,亲测有效:
https://blog.csdn.net/u011231598/article/details/80338941
在这里插入图片描述

3.CMake编译vtk

从官网下载对应的vtk安装包https://gitlab.kitware.com/vtk/vtk/tree/v8.0.0,将其解压至PCL1.8.1\3rdParty下,并清空或备份该路径下VTK文件
在这里插入图片描述
在vtk-v-8.00下新建build文件
打开CMake,参考如下方式添加路径
在这里插入图片描述
点击Add Entry,添加CMAKE_DEBUG_POSTFIX,类型为“STRING”,其值设置为"-gd"
在这里插入图片描述
点击configure,选择VS 2015 Win64。如果只有VS 2015,建议安装更低版本的CMake重试一下
在这里插入图片描述
点击configure,运行结果如下:
在这里插入图片描述
1.勾选“BUILD_SHARED_LIBS”和“VTK_GROUP_QT”
2.去掉BUILD_EXAMPLES和BUILD_TEST
3.VTK_RENDERING_BACKEND选为OpenGL
4.将“CMAKE_INSTALL_PREFIX”设置为之前我们清空后的VTK目录

点击configure在这里插入图片描述
将VTK_QT_VERSION设为5
点击configure,如果还报错,没有红色警告,仔细检查
在这里插入图片描述
发现VTK_QT_VERSION还是4,将其改为5
点击configure,继续报错
在这里插入图片描述
将Qt5_Core_DIR修改为上图所示
继续configure,如果未发现错误,点击gnenrate
在这里插入图片描述
CMake编译完毕
使用VS编译Cmake编译后的VTK

4.VS编译CMake编译后的vtk

用管理员身份运行VS2015,打开vtk-v8.0.0->build文件下的VTK.sln文件
在这里插入图片描述

在这里我们只生成vtk的Release版本,将解决方案配置为Release X64,
在这里插入图片描述
右击ALL_BUILD,重新生成,大概需要等30分钟
右击INSTALL,生成,大概需要等5分钟
生成之后便可以看到PCL1.8.1\3rdParty\VTK文件夹下出现了以下目录:
在这里插入图片描述
将PCL1.8.1\3rdParty\VTK\plugins\designer里的QVTkWidgetPlugin.dll放到然后将QVTKWidgetPlugin.dll放到Qt5.11.2\msvc2015_64\plugins\designer中,单独运行Qt Designer,可以看见QtVTKWidget控件:
在这里插入图片描述
生成完毕!!
未解决的问题:
目前我vs新建工程时可以正常使用,但是用Qt Creator打开Qt Designer时则显示不出qvtkwidged,希望高人指点。

5.VS配合QVTKWidget显示点云数据

1.新建一个Qt GUI Application
双击.ui文件进入Qt Designer,将QVTKWidget空间拖至中间,并建立菜单,如图所示:
在这里插入图片描述
2.依次点击【项目】、【属性】
在【VC++】的包含目录中,按以下方式添加:
在这里插入图片描述
在库目录中,按以下方式添加
在这里插入图片描述
点击【链接器】、【输入】,在附加依赖项中添加
在这里插入图片描述
直接复制以下内容即可:

pcl_tracking_release.lib
pcl_stereo_release.lib
pcl_people_release.lib
pcl_surface_release.lib
pcl_segmentation_release.lib
pcl_outofcore_release.lib
pcl_keypoints_release.lib
pcl_recognition_release.lib
pcl_visualization_release.lib
pcl_ml_release.lib
pcl_registration_release.lib
pcl_features_release.lib
pcl_io_release.lib
pcl_io_ply_release.lib
pcl_filters_release.lib
pcl_search_release.lib
pcl_sample_consensus_release.lib
pcl_octree_release.lib
pcl_kdtree_release.lib
pcl_common_release.lib
libboost_atomic-vc140-mt-1_64.lib
libboost_atomic-vc140-mt-gd-1_64.lib
libboost_bzip2-vc140-mt-1_64.lib
libboost_bzip2-vc140-mt-gd-1_64.lib
libboost_chrono-vc140-mt-1_64.lib
libboost_chrono-vc140-mt-gd-1_64.lib
libboost_container-vc140-mt-1_64.lib
libboost_container-vc140-mt-gd-1_64.lib
libboost_context-vc140-mt-1_64.lib
libboost_context-vc140-mt-gd-1_64.lib
libboost_coroutine-vc140-mt-1_64.lib
libboost_coroutine-vc140-mt-gd-1_64.lib
libboost_date_time-vc140-mt-1_64.lib
libboost_date_time-vc140-mt-gd-1_64.lib
libboost_exception-vc140-mt-1_64.lib
libboost_exception-vc140-mt-gd-1_64.lib
libboost_fiber-vc140-mt-1_64.lib
libboost_fiber-vc140-mt-gd-1_64.lib
libboost_filesystem-vc140-mt-1_64.lib
libboost_filesystem-vc140-mt-gd-1_64.lib
libboost_graph_parallel-vc140-mt-1_64.lib
libboost_graph_parallel-vc140-mt-gd-1_64.lib
libboost_graph-vc140-mt-1_64.lib
libboost_graph-vc140-mt-gd-1_64.lib
libboost_iostreams-vc140-mt-1_64.lib
libboost_iostreams-vc140-mt-gd-1_64.lib
libboost_locale-vc140-mt-1_64.lib
libboost_locale-vc140-mt-gd-1_64.lib
libboost_log_setup-vc140-mt-1_64.lib
libboost_log_setup-vc140-mt-gd-1_64.lib
libboost_log-vc140-mt-1_64.lib
libboost_log-vc140-mt-gd-1_64.lib
libboost_math_c99f-vc140-mt-1_64.lib
libboost_math_c99f-vc140-mt-gd-1_64.lib
libboost_math_c99l-vc140-mt-1_64.lib
libboost_math_c99l-vc140-mt-gd-1_64.lib
libboost_math_c99-vc140-mt-1_64.lib
libboost_math_c99-vc140-mt-gd-1_64.lib
libboost_math_tr1f-vc140-mt-1_64.lib
libboost_math_tr1f-vc140-mt-gd-1_64.lib
libboost_math_tr1l-vc140-mt-1_64.lib
libboost_math_tr1l-vc140-mt-gd-1_64.lib
libboost_math_tr1-vc140-mt-1_64.lib
libboost_math_tr1-vc140-mt-gd-1_64.lib
libboost_mpi-vc140-mt-1_64.lib
libboost_mpi-vc140-mt-gd-1_64.lib
libboost_numpy3-vc140-mt-1_64.lib
libboost_numpy3-vc140-mt-gd-1_64.lib
libboost_numpy-vc140-mt-1_64.lib
libboost_numpy-vc140-mt-gd-1_64.lib
libboost_prg_exec_monitor-vc140-mt-1_64.lib
libboost_prg_exec_monitor-vc140-mt-gd-1_64.lib
libboost_program_options-vc140-mt-1_64.lib
libboost_program_options-vc140-mt-gd-1_64.lib
libboost_python3-vc140-mt-1_64.lib
libboost_python3-vc140-mt-gd-1_64.lib
libboost_python-vc140-mt-1_64.lib
libboost_python-vc140-mt-gd-1_64.lib
libboost_random-vc140-mt-1_64.lib
libboost_random-vc140-mt-gd-1_64.lib
libboost_regex-vc140-mt-1_64.lib
libboost_regex-vc140-mt-gd-1_64.lib
libboost_serialization-vc140-mt-1_64.lib
libboost_serialization-vc140-mt-gd-1_64.lib
libboost_signals-vc140-mt-1_64.lib
libboost_signals-vc140-mt-gd-1_64.lib
libboost_system-vc140-mt-1_64.lib
libboost_system-vc140-mt-gd-1_64.lib
libboost_test_exec_monitor-vc140-mt-1_64.lib
libboost_test_exec_monitor-vc140-mt-gd-1_64.lib
libboost_thread-vc140-mt-1_64.lib
libboost_thread-vc140-mt-gd-1_64.lib
libboost_timer-vc140-mt-1_64.lib
libboost_timer-vc140-mt-gd-1_64.lib
libboost_type_erasure-vc140-mt-1_64.lib
libboost_type_erasure-vc140-mt-gd-1_64.lib
libboost_unit_test_framework-vc140-mt-1_64.lib
libboost_unit_test_framework-vc140-mt-gd-1_64.lib
libboost_wave-vc140-mt-1_64.lib
libboost_wave-vc140-mt-gd-1_64.lib
libboost_wserialization-vc140-mt-1_64.lib
libboost_wserialization-vc140-mt-gd-1_64.lib
libboost_zlib-vc140-mt-1_64.lib
libboost_zlib-vc140-mt-gd-1_64.lib
flann.lib
flann_cpp.lib
flann_cpp_s.lib
flann_cpp_s-gd.lib
flann_cpp-gd.lib
flann_s.lib
flann_s-gd.lib
flann-gd.lib
qhull.lib
qhull_d.lib
qhull_p.lib
qhull_p_d.lib
qhull_r.lib
qhull_r_d.lib
qhullcpp.lib
qhullcpp_d.lib
qhullstatic.lib
qhullstatic_d.lib
qhullstatic_r.lib
qhullstatic_r_d.lib
vtkalglib-8.0.lib
vtkChartsCore-8.0.lib
vtkCommonColor-8.0.lib
vtkCommonComputationalGeometry-8.0.lib
vtkCommonCore-8.0.lib
vtkCommonDataModel-8.0.lib
vtkCommonExecutionModel-8.0.lib
vtkCommonMath-8.0.lib
vtkCommonMisc-8.0.lib
vtkCommonSystem-8.0.lib
vtkCommonTransforms-8.0.lib
vtkDICOMParser-8.0.lib
vtkDomainsChemistry-8.0.lib
vtkexoIIc-8.0.lib
vtkexpat-8.0.lib
vtkFiltersAMR-8.0.lib
vtkFiltersCore-8.0.lib
vtkFiltersExtraction-8.0.lib
vtkFiltersFlowPaths-8.0.lib
vtkFiltersGeneral-8.0.lib
vtkFiltersGeneric-8.0.lib
vtkFiltersGeometry-8.0.lib
vtkFiltersHybrid-8.0.lib
vtkFiltersHyperTree-8.0.lib
vtkFiltersImaging-8.0.lib
vtkFiltersModeling-8.0.lib
vtkFiltersParallel-8.0.lib
vtkFiltersParallelImaging-8.0.lib
vtkFiltersPoints-8.0.lib
vtkFiltersProgrammable-8.0.lib
vtkFiltersSelection-8.0.lib
vtkFiltersSMP-8.0.lib
vtkFiltersSources-8.0.lib
vtkFiltersStatistics-8.0.lib
vtkFiltersTexture-8.0.lib
vtkFiltersTopology-8.0.lib
vtkFiltersVerdict-8.0.lib
vtkfreetype-8.0.lib
vtkGeovisCore-8.0.lib
vtkgl2ps-8.0.lib
vtkGUISupportQt-8.0.lib
vtkGUISupportQtOpenGL-8.0.lib
vtkGUISupportQtSQL-8.0.lib
vtkhdf5_hl-8.0.lib
vtkhdf5-8.0.lib
vtkImagingColor-8.0.lib
vtkImagingCore-8.0.lib
vtkImagingFourier-8.0.lib
vtkImagingGeneral-8.0.lib
vtkImagingHybrid-8.0.lib
vtkImagingMath-8.0.lib
vtkImagingMorphological-8.0.lib
vtkImagingSources-8.0.lib
vtkImagingStatistics-8.0.lib
vtkImagingStencil-8.0.lib
vtkInfovisCore-8.0.lib
vtkInfovisLayout-8.0.lib
vtkInteractionImage-8.0.lib
vtkInteractionStyle-8.0.lib
vtkInteractionWidgets-8.0.lib
vtkIOAMR-8.0.lib
vtkIOCore-8.0.lib
vtkIOEnSight-8.0.lib
vtkIOExodus-8.0.lib
vtkIOExport-8.0.lib
vtkIOExportOpenGL-8.0.lib
vtkIOGeometry-8.0.lib
vtkIOImage-8.0.lib
vtkIOImport-8.0.lib
vtkIOInfovis-8.0.lib
vtkIOLegacy-8.0.lib
vtkIOLSDyna-8.0.lib
vtkIOMINC-8.0.lib
vtkIOMovie-8.0.lib
vtkIONetCDF-8.0.lib
vtkIOParallel-8.0.lib
vtkIOParallelXML-8.0.lib
vtkIOPLY-8.0.lib
vtkIOSQL-8.0.lib
vtkIOTecplotTable-8.0.lib
vtkIOVideo-8.0.lib
vtkIOXML-8.0.lib
vtkIOXMLParser-8.0.lib
vtkjpeg-8.0.lib
vtkjsoncpp-8.0.lib
vtklibharu-8.0.lib
vtklibxml2-8.0.lib
vtklz4-8.0.lib
vtkmetaio-8.0.lib
vtknetcdf_c++.lib
vtkNetCDF-8.0.lib
vtkoggtheora-8.0.lib
vtkParallelCore-8.0.lib
vtkpng-8.0.lib
vtkproj4-8.0.lib
vtkRenderingAnnotation-8.0.lib
vtkRenderingContext2D-8.0.lib
vtkRenderingContextOpenGL-8.0.lib
vtkRenderingCore-8.0.lib
vtkRenderingFreeType-8.0.lib
vtkRenderingGL2PS-8.0.lib
vtkRenderingImage-8.0.lib
vtkRenderingLabel-8.0.lib
vtkRenderingLIC-8.0.lib
vtkRenderingLOD-8.0.lib
vtkRenderingOpenGL-8.0.lib
vtkRenderingQt-8.0.lib
vtkRenderingVolume-8.0.lib
vtkRenderingVolumeOpenGL-8.0.lib
vtksqlite-8.0.lib
vtksys-8.0.lib
vtktiff-8.0.lib
vtkverdict-8.0.lib
vtkViewsContext2D-8.0.lib
vtkViewsCore-8.0.lib
vtkViewsInfovis-8.0.lib
vtkViewsQt-8.0.lib
vtkzlib-8.0.lib

3.程序编写
QtPcl.h

#pragma once

#include <QtWidgets/QMainWindow>
#include "ui_QtPcl.h" 

#ifndef
INITIAL_OPENGL
#define INITIAL_OPENGL
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL)
VTK_MODULE_INIT(vtkInteractionStyle)
#endif
 
#include <QFileDialog>
#include <vtkRenderWindow.h>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/visualization/pcl_visualizer.h>
 
class QtPcl : public QMainWindow
{
   Q_OBJECT 
   
public:
    QtPcl(QWidget *parent = Q_NULLPTR);
 
private:
    Ui::QtPclClass ui;
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud;
    boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer;
    void initialVtkWidget();

    private slots :
        void onOpen();
};

QtPcl.cpp

#include "QtPcl.h"

QtPcl::QtPcl(QWidget *parent)
    :QMainWindow(parent)
{
    ui.setupUi(this);
    initialVtkWidget();
    connect(ui.actionOpen,SIGNAL(triggered()), this, SLOT(onOpen()));
} 

void QtPcl::initialVtkWidget()
{ 
      cloud.reset(new pcl::PointCloud<pcl::PointXYZ>);
      viewer.reset(new pcl::visualization::PCLVisualizer("viewer", false));
      viewer->addPointCloud(cloud,"cloud");     
      ui.qvtkWidget->SetRenderWindow(viewer->getRenderWindow());
      viewer->setupInteractor(ui.qvtkWidget->GetInteractor(),ui.qvtkWidget->GetRenderWindow());     
      ui.qvtkWidget->update();  
} 
void QtPcl::onOpen()
{
    QString fileName = QFileDialog::getOpenFileName(this, "Open PointCloud", ".", "Open PCD files(*.pcd)");
    if (!fileName.isEmpty()){
        std::string file_name = fileName.toStdString();
        pcl::PCLPointCloud2 cloud2;
        Eigen::Vector4f origin;
        Eigen::Quaternionf orientation;
        int pcd_version;
        int data_type;
        unsigned int data_idx;
        int offset = 0;
        pcl::PCDReader rd;
        rd.readHeader(file_name,cloud2, origin, orientation, pcd_version, data_type, data_idx);
        if (data_type == 0)
        {
            pcl::io::loadPCDFile(fileName.toStdString(),*cloud);
        }    
        else if (data_type == 2)
        {           
            pcl::PCDReader reader;
            reader.read<pcl::PointXYZ>(fileName.toStdString(),*cloud);
        }
        viewer->updatePointCloud(cloud,"cloud");
        viewer->resetCamera();
        ui.qvtkWidget->update();
    }
}

4.运行实例

解决方案配置为Release X64,因为我们之前把vtk编译成了release版本
在这里插入图片描述
编译运行后,直接在界面上点击File->Open,选择准备好的.pcd点云文件,即可显示点云数据
在这里插入图片描述
如有纰漏,希望批评指正。

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!