PCL1.8.1+Qt5.9.1+VS2015 jointly compile and display 3D images

Due to project requirements, it is necessary to display 3D images in Qt UI. PCL is an open source library, including algorithms, matrix calculations, and VTK 3D display, but it is not used with Qt, so it is not very convenient to use. The compiled VTK is not available on the official website. With Qt, you need to manually cmake yourself

1) Download qt-opensource-windows-x86-5.9.1.exe from the Qt official website and install it;

2) Download VTK-8.0.1.zip and VTKData-8.0.1.zip from the VTK official website, first decompress VTK-8.0.1.zip, then decompress VTKData-8.0.1.zip, and the decompression folder is VTK-8.0 .1

3) Download and install cmake-3.8.2-win64-x64, open cmake-gui.exe

4) Where is the source code in cmake-gui.exe is the previously unzipped folder VTK-8.0.1, "where to build the binaries" is the path of the compilation result after cmake compilation,

Click "Configure", and select the generated platform, the drop-down list generally does not need to be selected, it will search for the compiler by default, which is "Visual Studio 14 2015", which is the win32 compilation platform, click Finish

5) "BUILD_SHARED_LIBS" is the static link library lib to be created, which needs to be checked; "CMAKE_INSTALL_PREFIX" is the compilation project, and the header file, lib file, and dll file are extracted to the corresponding path; "VTK_Group_Qt" is required to be used with Qt , need to be checked; then click "Configure"

6) " Found unsuitable Qt version "5.9.1"  " is displayed in step 5) , you need to change "VTK_QT_VERSION" from 4 to 5, and then click "Configure"

7) After there is no problem in step 6), click "Generate" to generate the sln project, and then click "Open Project";

8) After opening the project, the lib and dll of Debug are currently generated, select "ALL_BUILD" right-click to generate, and wait for the project to be compiled completely

9) After selecting "INSTALL", all header files, lib and dll files will be copied to the path corresponding to "CMAKE_INSTALL_PREFIX" in step 5);

10) Remember to copy the QVTKWidgetPlugin.lib and QVTKWidgetPlugin.dll files compiled by the original project to lib and bin in the corresponding path of "CMAKE_INSTALL_PREFIX";

11) Replace the installed VTK with the compiled VTK, that is, replace the VTK of 3rdParty in the PCL installation path, and then configure the static link library lib of VS2015, and add the dll under bin to the environment variable path.

Next, create a new Qt ui project. For example, the project name is QtGuiApplication, open QtGuiApplication.ui, select the Widget control, the control name is qvtkWidget, and the control is upgraded to "QVTKWidget"

In the QtGuiApplication.h file

#pragma once

//这部分注意,需要添加
#ifndef INITIAL_OPENGL
#define INITIAL_OPENGL
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2)
VTK_MODULE_INIT(vtkInteractionStyle)
VTK_MODULE_INIT(vtkRenderingContextOpenGL2)
VTK_MODULE_INIT(vtkRenderingFreeType)
#endif

#include <QtWidgets/QMainWindow>
#include "ui_QtGuiApplication1.h"
#include <pcl/visualization/cloud_viewer.h>
//#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <iostream>
class QtGuiApplication1 : public QMainWindow
{
Q_OBJECT


public:
QtGuiApplication1(QWidget *parent = Q_NULLPTR);
//Initialize the vtk component
void initialVtkWidget();
void getRandom();
private:
Ui::QtGuiApplication1Class ui;
//point cloud data storage
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud;
pcl::visualization::PCLVisualizer::Ptr viewer;
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ>::Ptr cloud_color_handler;
std::string CloudId;
private slots:
void on_LoadBtn_clicked();
};


in QtGuiApplication.cpp

#include "QtGuiApplication1.h"
#include <QFileDialog>
#include <vtkRenderWindow.h>
QtGuiApplication1::QtGuiApplication1(QWidget *parent): QMainWindow(parent)
{
ui.setupUi (this);
CloudId = "cloud";
initialVtkWidget();
}


void QtGuiApplication1::initialVtkWidget()
{
cloud.reset(new pcl::PointCloud<pcl::PointXYZ>);
viewer.reset(new pcl::visualization::PCLVisualizer("viewer", false));
cloud_color_handler.reset(new pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ>(cloud, 0, 255, 0));
viewer->addPointCloud(cloud, *cloud_color_handler, CloudId);


ui.qvtkWidget-> SetRenderWindow (viewer-> getRenderWindow ());
viewer-> setupInteractor (ui.qvtkWidget-> GetInteractor (), ui.qvtkWidget-> GetRenderWindow ());
ui.qvtkWidget->update();
}


//Generate random numbers
void QtGuiApplication1::getRandom()
{
cloud->width = 100;
cloud->height = 1;
cloud->points.resize(cloud->width*cloud->height);
//Generate data, fill the X, Y coordinates of the point cloud with random numbers
for (int i=0;i<cloud->points.size();i++)
{
cloud->points[i].x = 1024 * rand() / (RAND_MAX + 1.0f);
cloud->points[i].y = 1024 * rand() / (RAND_MAX + 1.0f);
cloud->points[i].z = 1024 * rand() / (RAND_MAX + 1.0f);
}
}


void QtGuiApplication1::on_LoadBtn_clicked()
{
//Only open PCD files
QString fileName = QFileDialog::getOpenFileName(this, tr("Open PointCloud"), ".", tr("Open PCD files(*.pcd)"));
if (!fileName.isEmpty())
{
std::string file_name = fileName.toStdString();
//sensor_msgs::PointCloud2 cloud2;
pcl::PCLPointCloud2 cloud2;
//pcl::PointCloud<Eigen::MatrixXf> 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 == 1)
{
pcl::PCDReader reader;
int ok = reader.read<pcl::PointXYZ>(fileName.toStdString(), *cloud);
}
else
{
return;
}


//Set the background color of the window, you can set the RGB color arbitrarily, here is set to black
viewer->setBackgroundColor(0, 0, 0);
//Used to change the size of the displayed point cloud, you can use this method to control the display method of the point cloud in the viewport,
bool res=  viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, CloudId);
//If you have point cloud data, that is CloudId, you should remove this point cloud ID
if(viewer->contains(CloudId)) viewer->removePointCloud(CloudId);
res = viewer->addPointCloud<pcl::PointXYZ>(cloud, *cloud_color_handler, CloudId);


//viewer->updatePointCloud<pcl::PointXYZ>(cloud,*cloud_color_handler, CloudId) 
//Update the existing CloudId
//res = viewer->updatePointCloud<pcl::PointXYZ>(cloud,*cloud_color_handler, CloudId);


//The CloudID behind addPointCloud should be different
// getRandom();
//res = viewer->addPointCloud<pcl::PointXYZ>(cloud, *cloud_color_handler, CloudId+'0');
viewer->addCoordinateSystem(1.0);
viewer->resetCamera();
ui.qvtkWidget->update();
}
}





Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324126169&siteId=291194637