PyQt界面上显示2D图及3D点云

这部分讲解在PyQt界面上显示2D图及3D点云(vtk显示、PCL数据转化为vtk数据显示)

配置环境见前面博客PyQt5实现2D和3D及深度学习平台Demo(一)

一. PyQt界面显示2D图

1.界面上创建show image按钮及QLabel(界面上其它控件可以忽视),这里的按钮如何绑定槽也参见我的博客

PyQt界面上显示2D图及3D点云_第1张图片

2. 所绑定的按钮槽里的函数代码如下:(这里Pcharm如何添加Qt Designer、PyUI命令都略过,参见上面链接博客)

 def showImage(self):
        image = cv.imread("D:\\cat003.jpg")
        dstimage = cv.cvtColor(image,cv.COLOR_BGR2RGB);
        Qtemp = QImage(dstimage[:], dstimage.shape[1], dstimage.shape[0], dstimage.shape[1]*3, QImage.Format_RGB888)
        pixmap_Qtemp = QPixmap.fromImage(Qtemp)
        #pixmap_Qtemp = QPixmap.fromImage(Qtemp).scaled(self.label.width(), self.label.height())
        self.label.setPixmap(pixmap_Qtemp)
        self.label.resize(Qtemp.size())

运行程序,显示如下:

PyQt界面上显示2D图及3D点云_第2张图片

二. pyqt5界面显示vtk数据

界面设计如下,有三个控件:Widget、Form layout(添加到Widget控件中、Push Button)

PyQt界面上显示2D图及3D点云_第3张图片

3.通过PyUIC命令生成py文件,这里我的取名是testvtkgui.py

4.编写测试py文件,我取名为testvtkgui.py文件,文件代码如下:

from PyQt5.QtWidgets import QApplication, QMainWindow
import sys
import vtk
from PyQt5 import QtCore, QtGui, QtWidgets
from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
from testvtk import Ui_MainWindow
import pcl.pcl_visualization


class Mywindow(QtWidgets.QMainWindow, Ui_MainWindow):

    def __init__(self):
        super(Mywindow, self).__init__()
        self.setupUi(self)
        self.setWindowTitle('test_gui')

        self.frame = QtWidgets.QFrame()
        self.vtkWidget = QVTKRenderWindowInteractor(self.frame)
        self.formLayout.addWidget(self.vtkWidget)

        self.ren = vtk.vtkRenderer()
        self.vtkWidget.GetRenderWindow().AddRenderer(self.ren)
        self.iren = self.vtkWidget.GetRenderWindow().GetInteractor()

        # Create source
        source = vtk.vtkConeSource()
        source.SetCenter(0, 0, 0)
        source.SetRadius(0.1)

        source1 = vtk.vtkSphereSource()
        source1.SetCenter(0, 0, 0)
        source1.SetRadius(0.3)

        # Create a mapper
        mapper = vtk.vtkPolyDataMapper()
        mapper.SetInputConnection(source.GetOutputPort())

        mapper1 = vtk.vtkPolyDataMapper()
        mapper1.SetInputConnection(source1.GetOutputPort())

        # Create an actor
        actor = vtk.vtkActor()
        actor.SetMapper(mapper)

        actor1 = vtk.vtkActor()
        actor1.SetMapper(mapper1)

        self.ren.AddActor(actor)
        self.ren.AddActor(actor1)

        self.ren.ResetCamera()

        #self.frame.setLayout(self.formLayout)
        #self.setCentralWidget(self.frame)

        self.show()
        self.iren.Initialize()


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    window = Mywindow()
    window.show()
    sys.exit(app.exec_())


run该测试文件testvtkgui.py文件,界面显示如下:

PyQt界面上显示2D图及3D点云_第4张图片PyQt界面上显示2D图及3D点云_第5张图片

三. Python-pcl点云数据转化为vtk数据,并在pyqt5界面显示

利用第二部分生成的testvtk.py文件,创建新的测试py文件,这里我取名为testvtkgui-pcl.py,代码理内容如下:

from PyQt5.QtWidgets import QApplication, QMainWindow
import sys
import vtk
from PyQt5 import QtCore, QtGui, QtWidgets
from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
from testvtk import Ui_MainWindow
import pcl.pcl_visualization


class Mywindow(QtWidgets.QMainWindow, Ui_MainWindow):

    def __init__(self):
        super(Mywindow, self).__init__()
        self.setupUi(self)
        self.setWindowTitle('test_gui')

        self.frame = QtWidgets.QFrame()
        self.vtkWidget = QVTKRenderWindowInteractor(self.frame)
        self.formLayout.addWidget(self.vtkWidget)

        self.ren = vtk.vtkRenderer()
        self.vtkWidget.GetRenderWindow().AddRenderer(self.ren)
        self.iren = self.vtkWidget.GetRenderWindow().GetInteractor()

        # Create source
        fileName = "D:\\rabbit.pcd";  # 注意更换自己的pcd点云文件
        cloud = pcl.load_XYZRGB(fileName)

        poins = vtk.vtkPoints()
        for i in range(cloud.size):
          dp = cloud[i]
          poins.InsertNextPoint(dp[0], dp[1], dp[2])

        polydata = vtk.vtkPolyData()
        polydata.SetPoints(poins)

        glyphFilter = vtk.vtkVertexGlyphFilter()
        glyphFilter.SetInputData(polydata)
        glyphFilter.Update()

        dataMapper = vtk.vtkPolyDataMapper()
        dataMapper.SetInputConnection(glyphFilter.GetOutputPort())

        # Create an actor
        actor = vtk.vtkActor()
        actor.SetMapper(dataMapper)

        actor1 = vtk.vtkActor()
        actor1.SetMapper(dataMapper)

        self.ren.AddActor(actor)
        self.ren.AddActor(actor1)

        self.ren.ResetCamera()

        #self.frame.setLayout(self.formLayout)
        #self.setCentralWidget(self.frame)

        self.show()
        self.iren.Initialize()


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    window = Mywindow()
    window.show()
    sys.exit(app.exec_())


运行,结果如下:

PyQt界面上显示2D图及3D点云_第6张图片PyQt界面上显示2D图及3D点云_第7张图片

测试文件可以从如上链接博客中获取(2D图或者3D点云图)

下面3D部分先放一放,着重整下深度学习,下面博客会先做一些准备工作,为目标检测、目标分类、语义分割整理一个图像库,后面的标注、训练等就拿这些做demo

你可能感兴趣的:(PCL,pyqt5,GUI,vtk,python-pcl)