Qt 调用 Pcl库,在Widget中显示点云
最终效果展示
本人环境:Ubuntu18.04 / Qt 5.14.2 / PCL 1.8
下面介绍具体原理和实现步骤。
原理
其实核心就是这一行代码
将数据从pclviewer 传输到 QVTKWidget。
具体实现
1、.ui 设计
插入一个QWidget,然后右键QWidget => 提升为 => QVTKWidget (注意QVTKWidget.h区分大小写!!)
再新建四个horizontalSlider,分别命名为horizontalSlider_Red,horizontalSlider_Green,horizontalSlider_Blue,horizontalSlider_Size。用于控制点云的颜色和大小。
2、代码
.pro
QT += core gui
greaterThan(QT_MAJOR_VERSION,4): QT += widgets
CONFIG += c++11
# 主要需要修改的部分在下方,注意库的地址!
# -------------------------------------------------------
INCLUDEPATH +=/usr/include/eigen3 #注意路径可能不一样
INCLUDEPATH +=/usr/include/vtk-6.3
LIBS +=/usr/lib/x86_64-linux-gnu/libvtk*.so #注意路径可能不一样
INCLUDEPATH +=/usr/include/boost #注意路径可能不一样
LIBS +=/usr/lib/x86_64-linux-gnu/libboost_*.so
INCLUDEPATH +=/usr/include/pcl-1.8 #注意路径可能不一样
LIBS +=/usr/lib/x86_64-linux-gnu/libpcl_*.so
# -------------------------------------------------------
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += \
main.cpp \
qtpclviewer.cpp
HEADERS += \
qtpclviewer.h
FORMS += \
qtpclviewer.ui
# Default rules for deployment.
qnx: target.path =/tmp/$${TARGET}/bin
else: unix:!android: target.path =/opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
qtpclviewer.h
#ifndef QTPCLVIEWER_H#define QTPCLVIEWER_H#include<QMainWindow>#include"ui_qtpclviewer.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>//QT_BEGIN_NAMESPACE//namespace Ui { class QtPclViewer; }//QT_END_NAMESPACEclassQtPclViewer:public QMainWindow
{
Q_OBJECT
public:QtPclViewer(QWidget *parent =nullptr);~QtPclViewer();private:
Ui::QtPclViewer *ui;// Ui::QtPclViewerClass ui;
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud;
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer;voidinitialVtkWidget();protected:unsigned red;unsigned green;unsigned blue;double size;private slots :voidonOpen();voidrgbSliderReleased();voidpSliderValueChangeed(int value);voidredSliderValueChangeed(int value);voidgreenSliderValueChangeed(int value);voidblueSliderValueChangeed(int value);};#endif// QTPCLVIEWER_H
main.cpp
#include"qtpclviewer.h"#include<QApplication>intmain(int argc,char*argv[]){
QApplication a(argc, argv);
QtPclViewer w;
w.show();return a.exec();}
qtpclviewer.cpp
#include"qtpclviewer.h"#include"ui_qtpclviewer.h"
QtPclViewer::QtPclViewer(QWidget *parent):QMainWindow(parent),ui(new Ui::QtPclViewer){
ui->setupUi(this);initialVtkWidget();onOpen();// connect(ui.actionOpen, SIGNAL(triggered()), this, SLOT(onOpen()));connect(ui->horizontalSlider_Size,SIGNAL(valueChanged(int)),this,SLOT(pSliderValueChangeed(int)));//改变大小connect(ui->horizontalSlider_Red,SIGNAL(valueChanged(int)),this,SLOT(redSliderValueChangeed(int)));//颜色改变connect(ui->horizontalSlider_Green,SIGNAL(valueChanged(int)),this,SLOT(greenSliderValueChangeed(int)));connect(ui->horizontalSlider_Blue,SIGNAL(valueChanged(int)),this,SLOT(blueSliderValueChangeed(int)));connect(ui->horizontalSlider_Red,SIGNAL(sliderReleased()),this,SLOT(rgbSliderReleased()));//slider滑动connect(ui->horizontalSlider_Green,SIGNAL(sliderReleased()),this,SLOT(rgbSliderReleased()));connect(ui->horizontalSlider_Blue,SIGNAL(sliderReleased()),this,SLOT(rgbSliderReleased()));}
QtPclViewer::~QtPclViewer(){delete ui;}void QtPclViewer::initialVtkWidget(){//初始化颜色及大小信息
red =255;
green =255;
blue =255;
size =1.0;//点云加载
cloud.reset(new pcl::PointCloud<pcl::PointXYZ>);
viewer.reset(new pcl::visualization::PCLVisualizer("viewer",false));//viewer->addPointCloud(cloud, "cloud");
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ>single_color(cloud, red, green, blue);//自定义点云颜色
viewer->addPointCloud<pcl::PointXYZ>(cloud, single_color,"cloud");
viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, size,"cloud");//设置点云单个点的大小
ui->qvtkWidget->SetRenderWindow(viewer->getRenderWindow());
viewer->setupInteractor(ui->qvtkWidget->GetInteractor(), ui->qvtkWidget->GetRenderWindow());
ui->qvtkWidget->update();}void QtPclViewer::onOpen(){
QString fileName = QFileDialog::getOpenFileName(this,"Open PointCloud",".","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;unsignedint 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);}elseif(data_type ==2){
pcl::PCDReader reader;
reader.read<pcl::PointXYZ>(fileName.toStdString(),*cloud);}// pcl::io::loadPCDFile("1_over.pcd",*cloud);qDebug("cloud->points.size: %ld", cloud->points.size());//重新加载点云//viewer->updatePointCloud(cloud, "cloud");
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ>single_color(cloud, red, green, blue);//自定义点云颜色
viewer->updatePointCloud<pcl::PointXYZ>(cloud, single_color,"cloud");
viewer->resetCamera();
ui->qvtkWidget->update();}}void QtPclViewer::rgbSliderReleased(){//更新点云颜色
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ>single_color(cloud, red, green, blue);
viewer->updatePointCloud<pcl::PointXYZ>(cloud, single_color,"cloud");
ui->qvtkWidget->update();}void QtPclViewer::redSliderValueChangeed(int value){
red= value;}void QtPclViewer::greenSliderValueChangeed(int value){
green= value;}void QtPclViewer::blueSliderValueChangeed(int value){
blue= value;}void QtPclViewer::pSliderValueChangeed(int value){
size=double(10+value)/10;
viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, size,"cloud");//设置点云单个点的大小
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ>single_color(cloud, red, green, blue);
viewer->updatePointCloud<pcl::PointXYZ>(cloud,single_color,"cloud");
ui->qvtkWidget->update();}
版权归原作者 FCC_421 所有, 如有侵权,请联系我们删除。