Ubuntu下QT Creator界面显示pcl点云数据

之前的博客里有介绍如何在windows下在QT Creator界面显示点云数据,见如下链接QTcreator界面上显示pcl点云数据_jiugeshao的专栏-CSDN博客

这里再介绍下Ubuntu下的配置。

1. 使用apt-get install libpcl-dev,安装完毕后,vtk和eigen都会安装上了

 2.可在/usr下看到安装的一些版本:

 

2. 用QTCreator打开项目,在里面加入界面显示pcl点云得到功能,这个项目的式样可见之前博客

 Ubuntu下Qt Creator配置opencv_jiugeshao的专栏-CSDN博客

3.在.pro文件中加入如下配置项

INCLUDEPATH += /usr/include/eigen3

INCLUDEPATH += /usr/include/vtk-7.1
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.10
LIBS += /usr/lib/x86_64-linux-gnu/libpcl_*.so

界面上增加一个Widget控件

 

右击该控件,选择Promoted widgets选项

如下设置,可参考我之前博客QTcreator界面上显示pcl点云数据_jiugeshao的专栏-CSDN博客

4.添加代码,mainwindow.h中代码如下:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <vtkRenderWindow.h>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/visualization/pcl_visualizer.h>


namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void slot1();

private:
    Ui::MainWindow *ui;
    pcl::visualization::PCLVisualizer::Ptr viewer;
};

#endif // MAINWINDOW_H

 mainwindow.cpp中代码如下:

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <opencv2/core/core.hpp>
#include <opencv2/opencv.hpp>

#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
VTK_MODULE_INIT(vtkRenderingFreeType);

#include "vtkConeSource.h"
#include "vtkConeSource.h"
#include "vtkCommand.h"
#include "vtkCamera.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkTransform.h"
#include "vtkInteractorStyleTrackballCamera.h"
#include <vtkSmartPointer.h>
#include <vtkDoubleArray.h>
#include <vtkCellArray.h>
#include <vtkPolyData.h>
#include <vtkDataSet.h>
#include <vtkLookupTable.h>
#include <vtkPointData.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkProperty.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkArcSource.h>
#include <vtkAppendPolyData.h>
#include <vtkScalarBarActor.h>
#include <vtkKdTree.h>
#include <vtkLODActor.h>
#include <vtkMath.h>
#include <vtkWindowToImageFilter.h>
#include <vtkBMPWriter.h>

#include <boost/thread/thread.hpp>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/io/io.h>
#include <pcl/io/pcd_io.h>//pcd 读写类相关的头文件。
#include <pcl/PCLPointCloud2.h>
#include <pcl/visualization/range_image_visualizer.h>
#include <pcl/visualization/pcl_visualizer.h>




using namespace pcl;
using namespace cv;
using namespace pcl::io;
using namespace std;

using pcl::visualization::PointCloudColorHandlerGenericField;
using pcl::visualization::PointCloudColorHandlerCustom;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    viewer.reset(new pcl::visualization::PCLVisualizer("viewer", false));
        ui->guiwidget->SetRenderWindow(viewer->getRenderWindow());
        viewer->setupInteractor(ui->guiwidget->GetInteractor(), ui->guiwidget->GetRenderWindow());
        ui->guiwidget->update();
}

void MainWindow::slot1()
{
   ui->textEdit->setText("hello world!");
   Mat img = imread("/home/icecreamshao/108.bmp");
   imshow("image", img);
   waitKey(0);

   Mat temp;
   cvtColor(img, temp, CV_BGR2RGB);
   QImage Qtemp = QImage((const unsigned char*)(temp.data), temp.cols, temp.rows, temp.step, QImage::Format_RGB888);
   ui->label->setPixmap(QPixmap::fromImage(Qtemp));
   ui->label->resize(Qtemp.size());
   ui->label->show();

   pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
   //第一种读入方法较多场合如此
   char strfilepath[256] = "/home/icecreamshao/rabbit.pcd";
   if (-1 == pcl::io::loadPCDFile(strfilepath, *cloud))
   {
       cout << "error input!" << endl;
       return;
   }

   std::cout << "read completed" << std::endl;
   cout << cloud->points.size() << endl;
   viewer->addPointCloud(cloud);


  // boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer("PointsCloud Recognition"));
  // viewer->addPointCloud(cloud, "model_cloud");
   //viewer->addCoordinateSystem(1.0);
   //viewer->initCameraParameters();
 //  while (!viewer->wasStopped())
  // {
  //     viewer->spinOnce(100);
   //    boost::this_thread::sleep(boost::posix_time::microseconds(100000));
   //}

      //正方体点云
      // pcl::visualization::CloudViewer viewer("Cloud Viewer");
     //  pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>());
     // for(int i=0; i<20; i++)
     // {
     //     for(int j=0; j<20; j++)
     //     {
     //         for(int k = 0; k<20; k++)
      //        {
       //           cloud->push_back(pcl::PointXYZ((i-10)/1.0f, (j-10)/1.0f, (k-10)/1.0f));
       //       }
      //    }
     // }

    //  viewer.showCloud(cloud);

     // while(!viewer.wasStopped());


}

MainWindow::~MainWindow()
{
    delete ui;
}

5.运行出现报错

#error PCL requires c++14 or above

 解决方式是在pro中修改支持的c++版本到14,

CONFIG += c++14

 修改完毕后test_QT_GUI2.pro中的内容如下:

#-------------------------------------------------
#
# Project created by QtCreator 2021-09-26T21:43:41
#
#-------------------------------------------------

QT       += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = test_QT_GUI2
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

CONFIG += c++14

SOURCES += \
        main.cpp \
        mainwindow.cpp

HEADERS += \
        mainwindow.h

FORMS += \
        mainwindow.ui

INCLUDEPATH += /usr/local/include \
               /usr/local/include/opencv \
               /usr/local/include/opencv2

LIBS += /usr/local/lib/libopencv_highgui.so \
        /usr/local/lib/libopencv_core.so    \
        /usr/local/lib/libopencv_imgproc.so \
        /usr/local/lib/libopencv_imgcodecs.so

INCLUDEPATH += /usr/include/eigen3

INCLUDEPATH += /usr/include/vtk-7.1
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.10
LIBS += /usr/lib/x86_64-linux-gnu/libpcl_*.so

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

运行程序,运行效果如下图,界面上show出了经典的兔子3D点云图。

 

 

 

 

 

Supongo que te gusta

Origin blog.csdn.net/jiugeshao/article/details/120593684
Recomendado
Clasificación