PyQt显示点云

PyQt+open3d显示点云

纯代码方式实现:
view_open3d.py

import sys
import open3d as o3d
import numpy as np
import pyqtgraph.opengl as gl
from PyQt5.QtWidgets import QApplication, QVBoxLayout, QPushButton, QWidget, QFileDialog
from pyqtgraph.opengl import GLViewWidget


class PyQtGraphicDemo(QWidget):
    def __init__(self, parent=None):
        super(PyQtGraphicDemo, self).__init__(parent)

        self.resize(800, 600)      
        # 点云显示控件
        self.graphicsView = GLViewWidget(self)
        # 按钮
        self.pushButton = QPushButton(self)
        self.pushButton.setText("PushButton")
        self.pushButton.clicked.connect(self.showCloud)
        # 布局
        self.verticalLayout = QVBoxLayout(self)
        self.verticalLayout.addWidget(self.graphicsView)
        self.verticalLayout.addWidget(self.pushButton)
        self.setLayout(self.verticalLayout)

    def showCloud(self):
        fileName, filetype = QFileDialog.getOpenFileName(self, "请选择点云:", '.', "cloud Files(*pcd *ply)")
        if fileName != '':          
            pcd = o3d.io.read_point_cloud(fileName) #读取点云           
            np_points = np.asarray(pcd.points)  #获取Numpy数组          
            plot = gl.GLScatterPlotItem() #创建显示对象            
            plot.setData(pos=np_points, color=(1, 1, 1, 1), size=0.001, pxMode=False) #设置显示数据
            self.graphicsView.addItem(plot) #显示点云


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = PyQtGraphicDemo()
    window.show()
    sys.exit(app.exec_())

借助Qt creator编辑ui界面:
widget.ui(在Qt creator中编辑)


<ui version="4.0">
 <class>Formclass>
 <widget class="QWidget" name="Form">
  <property name="geometry">
   <rect>
    <x>0x>
    <y>0y>
    <width>800width>
    <height>600height>
   rect>
  property>
  <property name="windowTitle">
   <string>Formstring>
  property>
  <widget class="QPushButton" name="pushButton">
   <property name="geometry">
    <rect>
     <x>340x>
     <y>550y>
     <width>93width>
     <height>28height>
    rect>
   property>
   <property name="text">
    <string>打开点云string>
   property>
  widget>
 widget>
 <resources/>
 <connections/>
ui>

上述文件可以编译生成python代码如下:
Ui_widget.py

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'e:\BaiduNetdiskDownload\document_202206\vscode_project\.vscode\python\pyqt\open3d\widget.ui'
#
# Created by: PyQt5 UI code generator 5.15.7
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(800, 600)
        self.pushButton = QtWidgets.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(340, 550, 93, 28))
        self.pushButton.setObjectName("pushButton")

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.pushButton.setText(_translate("Form", "打开点云"))

main.py

import sys
import open3d as o3d
import numpy as np
import pyqtgraph.opengl as gl
from PyQt5.QtWidgets import QApplication, QVBoxLayout, QWidget, QFileDialog
from pyqtgraph.opengl import GLViewWidget
from Ui_widget import Ui_Form


class PyQtGraphicDemo(QWidget, Ui_Form):
    def __init__(self, parent=None):
        super(PyQtGraphicDemo, self).__init__(parent)
        self.setupUi(self)     
        self.graphicsView = GLViewWidget(self)
        self.pushButton.clicked.connect(self.showCloud)
        self.verticalLayout = QVBoxLayout(self)
        self.verticalLayout.addWidget(self.graphicsView)
        self.verticalLayout.addWidget(self.pushButton)
        self.setLayout(self.verticalLayout)

    def showCloud(self):
        fileName, filetype = QFileDialog.getOpenFileName(self, "请选择点云:", '.', "cloud Files(*pcd *ply)")
        if fileName != '':          
            pcd = o3d.io.read_point_cloud(fileName) #读取点云           
            np_points = np.asarray(pcd.points)  #获取Numpy数组          
            plot = gl.GLScatterPlotItem() #创建显示对象            
            plot.setData(pos=np_points, color=(1, 1, 1, 1), size=0.001, pxMode=False) #设置显示数据
            self.graphicsView.addItem(plot) #显示点云


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = PyQtGraphicDemo()
    window.show()
    sys.exit(app.exec_())

PyQt+pcl显示点云

这里只给出纯代码实现方式,另一种方式同理。

import sys
import pcl
import numpy as np
import pyqtgraph.opengl as gl
from PyQt5.QtWidgets import QApplication, QVBoxLayout, QPushButton, QWidget, QFileDialog
from pyqtgraph.opengl import GLViewWidget


class PyQtGraphicDemo(QWidget):
    def __init__(self, parent=None):
        super(PyQtGraphicDemo, self).__init__(parent)

        self.resize(800, 600)       
        #显示控件
        self.graphicsView = GLViewWidget(self)
        #按钮
        self.pushButton = QPushButton(self)
        self.pushButton.setText("PushButton")
        self.pushButton.clicked.connect(self.showCloud)
        #布局
        self.verticalLayout = QVBoxLayout(self)
        self.verticalLayout.addWidget(self.graphicsView)
        self.verticalLayout.addWidget(self.pushButton)
        self.setLayout(self.verticalLayout)

    def showCloud(self):
        fileName, filetype = QFileDialog.getOpenFileName(self, "请选择点云:", '.', "cloud Files(*pcd *ply)")
        if fileName != '':           
            pcd = pcl.load(fileName) #读取点云           
            np_points = np.array(pcd.to_array()) #获取Numpy数组           
            plot = gl.GLScatterPlotItem()  #创建显示对象           
            plot.setData(pos=np_points, color=(1, 1, 1, 1), size=0.001, pxMode=False)  #设置显示数据            
            self.graphicsView.addItem(plot) # 显示点云


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = PyQtGraphicDemo()
    window.show()
    sys.exit(app.exec_())

PyQt显示点云_第1张图片

你可能感兴趣的:(#,PyQt,pyqt,python)