本文方法来自:PYQT5内嵌外部exe程序(win7)_pyqt5嵌入外部窗口_这杯可乐有点甜的博客-CSDN博客
open3d在绘制点云等图形时,通常需要创建一个窗口。本文实现了将open3d创建的窗口显示在Qt窗口内,以便于后续通过Qt控件和槽函数调用open3d强大的绘图和处理功能。
运行结果如下图所示:
实现过程:
1.首先,导入需要的库:
import sys
import open3d as o3d
from PyQt5.QtWidgets import QApplication,QMainWindow,QWidget
from PyQt5.QtGui import QWindow
from PyQt5.QtCore import QTimer
import win32gui
其中win32gui为Pywin32库。
2.设计界面:
用Qt Designer绘制界面,如图:
保存并编译.ui文件,得到对应的.py文件。
3.嵌入窗口:
新建一个python文件,继承刚刚所写的界面的类。
创建一个open3d的Visualizer类的实例vis,并创建窗口:
class MainWindow(QMainWindow):
def __init__(self,parent=None):
super(MainWindow, self).__init__(parent)
self.ui = Ui_display_test.Ui_MainWindow()
self.ui.setupUi(self)
self.vis = o3d.visualization.Visualizer()
self.vis.create_window()
添加断点,或者使用vis.run()使窗口保持显示,在Visual Studio安装目录的Community\Common7\Tools找到并打开Spy++,按Ctrl+F,将“查找程序工具”拖动到打开的open3d窗口上,如图:
得到窗体类名为GLFW30。
通过Pywin32和PyQt5.QtGui 找到这个类对应的窗口,并获得对应WinId:
self.winid = win32gui.FindWindow('GLFW30',None)
self.sub_window = QWindow.fromWinId(self.winid)
创建WindowContainer,将窗口放入其中,在将WindowContainer添加到Qt布局中:
self.displayer = QWidget.createWindowContainer(self.sub_window)
self.ui.grid_display.addWidget(self.displayer)
此时open3d窗口已经嵌入Qt窗口中。
4.加载点云,测试效果:
根据open3d文档,o3d.visualization.draw_geometries([pcd])方法可以用以下代码替代实现:
def custom_draw_geometry(pcd):
# The following code achieves the same effect as:
# o3d.visualization.draw_geometries([pcd])
vis = o3d.visualization.Visualizer()
vis.create_window()
vis.add_geometry(pcd)
vis.run()
vis.destroy_window()
因此,添加函数:
def draw_test(self):
pcd = o3d.io.read_point_cloud(r'../material/bun000.pcd') #点云路径
self.vis.add_geometry(pcd)
self.vis.run()
并在析构函数添加
self.vis.destroy_window()
5.修改为非阻塞显示方式:
上面的代码虽然能正常显示点云,但是由于vis.run()是阻塞方式运行的,所以在窗口关闭时destroy_window()并不能被执行,此时会导致GLFW一直报错,程序无法停止运行。
在open3d文档中,有:
代码的下一部分是本教程的核心。update_geometry通知vis相关的几何图形已经更新。最后,可视化器通过调用poll_events和update_renderer渲染一个新帧。在任何for循环迭代之后,destroy_window将关闭窗口。
将vis.run()替换为:
while True:
self.vis.poll_events()
self.vis.update_renderer()
实现的效果与vis.run()相同。
添加一个定时器,设置定时器为20毫秒,将这两行代码写到定时器信号对应的槽函数中:
def __init__:
…………
self.clock = QTimer(self)
self.clock.timeout.connect(self.draw_update)
self.clock.start(20)
…………
def draw_update(self):
self.vis.poll_events()
self.vis.update_renderer()
就实现了每20ms刷新一次显示,同时不阻塞程序运行。
完整代码:
display_test.ui:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>828</width>
<height>608</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<layout class="QHBoxLayout" name="horizontalLayout" stretch="3">
<item>
<layout class="QGridLayout" name="grid_display">
<property name="sizeConstraint">
<enum>QLayout::SetDefaultConstraint</enum>
</property>
</layout>
</item>
</layout>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>828</width>
<height>26</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
Ui_display_test.py,由display_test.ui编译生成:文章来源:https://www.toymoban.com/news/detail-461173.html
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'd:\personal programs\gradesign\CSDN文章用\display_test.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# 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_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(828, 608)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.grid_display = QtWidgets.QGridLayout()
self.grid_display.setSizeConstraint(QtWidgets.QLayout.SetDefaultConstraint)
self.grid_display.setObjectName("grid_display")
self.horizontalLayout.addLayout(self.grid_display)
self.horizontalLayout.setStretch(0, 3)
self.gridLayout.addLayout(self.horizontalLayout, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 828, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
displaytest.py:文章来源地址https://www.toymoban.com/news/detail-461173.html
import sys
import open3d as o3d
from PyQt5.QtWidgets import QApplication,QMainWindow,QWidget
from PyQt5.QtGui import QWindow
from PyQt5.QtCore import QTimer
import win32gui
import Ui_display_test
class MainWindow(QMainWindow):
def __init__(self,parent=None):
super(MainWindow, self).__init__(parent)
self.ui = Ui_display_test.Ui_MainWindow()
self.ui.setupUi(self)
self.vis = o3d.visualization.Visualizer()
self.vis.create_window(visible=False) #visible=False窗口不显示,避免启动时一闪而过
self.winid = win32gui.FindWindow('GLFW30',None)
self.sub_window = QWindow.fromWinId(self.winid)
self.displayer = QWidget.createWindowContainer(self.sub_window)
self.ui.grid_display.addWidget(self.displayer)
self.clock = QTimer(self)
self.clock.timeout.connect(self.draw_update)
self.clock.start(20)
self.draw_test()
def draw_test(self):
pcd = o3d.io.read_point_cloud(r'../material/bun000.pcd') #点云路径
self.vis.add_geometry(pcd)
self.vis.update_geometry(pcd)
def draw_update(self):
self.vis.poll_events()
self.vis.update_renderer()
def __del__(self):
#self.clock.stop() #这一行其实并不需要
self.vis.destroy_window()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
到了这里,关于在PyQt5窗口中嵌入open3d窗口显示点云图形的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!