mayavi pyqt 实例

目录

安装:

示例代码:

生成3d检测框:

显示立方体 两个窗口


安装:

pip install vtk
pip install mayavi
pip install PyQt5
 

pip install pyqt5 mayavi traits traitsui

示例代码:

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget, QPushButton
from traits.api import HasTraits, Instance
from traitsui.api import View, Item
from tvtk.pyface.scene_editor import SceneEditor
from mayavi.tools.mlab_scene_model import MlabSceneModel
from mayavi.core.ui.mayavi_scene import MayaviScene

class Visualization(HasTraits):
    scene = Instance(MlabSceneModel, ())

    def __init__(self):
        HasTraits.__init__(self)

    # the layout of the dialog created
    view = View(Item('scene', editor=SceneEditor(scene_class=MayaviScene),
                     height=250, width=300, show_label=False), )


class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        self.setWindowTitle('Mayavi embedded in PyQt5')

        # Create the main widget and layout
        main_widget = QWidget()
        self.setCentralWidget(main_widget)
        layout = QVBoxLayout(main_widget)

        # Create the Mayavi visualization
        self.visualization = Visualization()
        self.mayavi_scene = self.visualization.edit_traits(parent=main_widget, kind='subpanel').control
        layout.addWidget(self.mayavi_scene)

        # Create a button and connect its event
        self.button = QPushButton('Generate 3D Mesh')
        layout.addWidget(self.button)
        self.button.clicked.connect(self.button_event)

    def button_event(self):
        # Create the data.
        from numpy import pi, sin, cos, mgrid
        dphi, dtheta = pi / 250.0, pi / 250.0
        [phi, theta] = mgrid[0:pi + dphi * 1.5:dphi, 0:2 * pi + dtheta * 1.5:dtheta]
        m0 = 4; m1 = 3; m2 = 2; m3 = 3; m4 = 6; m5 = 2; m6 = 6; m7 = 4;
        r = sin(m0 * phi) ** m1 + cos(m2 * phi) ** m3 + sin(m4 * theta) ** m5 + cos(m6 * theta) ** m7
        x = r * sin(phi) * cos(theta)
        y = r * cos(phi)
        z = r * sin(phi) * sin(theta)

        # View it.
        self.visualization.scene.mlab.mesh(x, y, z)

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

生成3d检测框:

import sys
import numpy as np
from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget, QPushButton
from traits.api import HasTraits, Instance
from traitsui.api import View, Item
from tvtk.pyface.scene_editor import SceneEditor
from mayavi.tools.mlab_scene_model import MlabSceneModel
from mayavi.core.ui.mayavi_scene import MayaviScene
from mayavi import mlab

class Visualization(HasTraits):
    scene = Instance(MlabSceneModel, ())

    def __init__(self):
        HasTraits.__init__(self)

    # the layout of the dialog created
    view = View(Item('scene', editor=SceneEditor(scene_class=MayaviScene),
                     height=600, width=800, show_label=False), )

    def draw_3d_box(self, center, size, orientation):
        c, s = np.cos(orientation), np.sin(orientation)
        R = np.array([
            [c, -s, 0],
            [s, c, 0],
            [0, 0, 1]
        ])

        l, w, h = size / 2.0
        corners = np.array([
            [ l,  w,  h],
            [ l, -w,  h],
            [-l, -w,  h],
            [-l,  w,  h],
            [ l,  w, -h],
            [ l, -w, -h],
            [-l, -w, -h],
            [-l,  w, -h]
        ])

        corners = np.dot(corners, R.T) + center

        edges = [
            [0, 1], [1, 2], [2, 3], [3, 0],
            [4, 5], [5, 6], [6, 7], [7, 4],
            [0, 4], [1, 5], [2, 6], [3, 7]
        ]

        for edge in edges:
            self.scene.mlab.plot3d(
                [corners[edge[0], 0], corners[edge[1], 0]],
                [corners[edge[0], 1], corners[edge[1], 1]],
                [corners[edge[0], 2], corners[edge[1], 2]],
                color=(1, 0, 0), tube_radius=None, line_width=1, figure=self.scene.mayavi_scene
            )

class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        self.setWindowTitle('Mayavi embedded in PyQt5')

        # Create the main widget and layout
        main_widget = QWidget()
        self.setCentralWidget(main_widget)
        layout = QVBoxLayout(main_widget)

        # Create the Mayavi visualization
        self.visualization = Visualization()
        self.mayavi_scene = self.visualization.edit_traits(parent=main_widget, kind='subpanel').control
        layout.addWidget(self.mayavi_scene)

        # Create a button and connect its event
        self.button = QPushButton('Generate 3D Detection Box')
        layout.addWidget(self.button)
        self.button.clicked.connect(self.button_event)

    def button_event(self):
        # Example data for a 3D box
        center = np.array([0, 0, 0])
        size = np.array([2, 1, 1])
        orientation = np.pi / 4  # 45 degrees

        # Draw the 3D box
        self.visualization.draw_3d_box(center, size, orientation)

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

显示立方体 两个窗口

import sys
import numpy as np
from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget, QPushButton
from PyQt5.QtCore import QTimer
from traits.api import HasTraits, Instance
from traitsui.api import View, Item
from mayavi.tools.mlab_scene_model import MlabSceneModel
from mayavi.core.ui.api import MlabSceneModel, SceneEditor
from mayavi import mlab

class Visualization(HasTraits):
    scene = Instance(MlabSceneModel, ())

    def __init__(self):
        HasTraits.__init__(self)
        self.figure = mlab.figure(bgcolor=(1, 1, 1), size=(800, 600))

    # Layout of the dialog created
    view = View(Item('scene', editor=SceneEditor(scene_class=MlabSceneModel),
                     height=600, width=800, show_label=False), resizable=True)

    def draw_3d_box(self, center, size, orientation):
        # Define the rotation matrix
        c, s = np.cos(orientation), np.sin(orientation)
        R = np.array([
            [c, -s, 0],
            [s, c, 0],
            [0, 0, 1]
        ])

        # Half dimensions
        l, w, h = size / 2.0
        corners = np.array([
            [ l,  w,  h],
            [ l, -w,  h],
            [-l, -w,  h],
            [-l,  w,  h],
            [ l,  w, -h],
            [ l, -w, -h],
            [-l, -w, -h],
            [-l,  w, -h]
        ])

        # Rotate and translate corners
        corners = np.dot(corners, R.T) + center

        # Define edges of the box
        edges = [
            (0, 1), (1, 2), (2, 3), (3, 0),
            (4, 5), (5, 6), (6, 7), (7, 4),
            (0, 4), (1, 5), (2, 6), (3, 7)
        ]

        # Plot edges
        for edge in edges:
            mlab.plot3d([corners[edge[0]][0], corners[edge[1]][0]],
                        [corners[edge[0]][1], corners[edge[1]][1]],
                        [corners[edge[0]][2], corners[edge[1]][2]],
                        color=(1, 0, 0), tube_radius=0.01, figure=self.figure)

class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setWindowTitle('Moving 3D Box with Mayavi and PyQt5')

        # Main widget and layout
        main_widget = QWidget()
        self.setCentralWidget(main_widget)
        layout = QVBoxLayout(main_widget)

        # Create the Mayavi visualization
        self.visualization = Visualization()
        self.ui = self.visualization.edit_traits(parent=main_widget, kind='subpanel').control
        layout.addWidget(self.ui)

        # Timer for moving the box
        self.timer = QTimer(self)
        self.timer.timeout.connect(self.update_position)
        self.timer.start(100)  # Update every 100 ms

        # Box parameters
        self.box_center = np.array([0, 0, 0])
        self.box_size = np.array([1, 1, 1])
        self.orientation = 0  # No rotation initially

    def update_position(self):
        self.visualization.figure.scene.disable_render = True  # For performance
        self.box_center[0] += 0.1  # Move along X
        mlab.clf()  # Clear the figure
        self.visualization.draw_3d_box(self.box_center, self.box_size, self.orientation)
        self.visualization.figure.scene.disable_render = False  # Re-enable rendering

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

  • 10
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Mayavi是一个基于Python的科学可视化工具,而PyQt是Python的一个GUI库,可以用于创建交互式用户界面。将MayaviPyQt结合起来,可以创建交互式的科学可视化应用程序。 下面是一些步骤,可以将MayaviPyQt结合起来: 1. 安装所需的库:MayaviPyQt。 2. 在PyQt中创建一个QWidget对象,作为Mayavi视图的容器。 3. 在Mayavi中创建一个场景,将数据添加到场景中,并创建一个可视化模块。 4. 将Mayavi的可视化模块添加到PyQt的QWidget对象中。 5. 将PyQt的QWidget对象添加到主窗口中,并启动应用程序。 这是一个简单的示例代码,演示了MayaviPyQt结合的基本思路: ```python from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout from mayavi import mlab from mayavi.core.ui.api import SceneEditor, MlabSceneModel from traits.api import HasTraits, Instance from traitsui.api import View, Item class MayaviQWidget(QWidget): def __init__(self, parent=None): QWidget.__init__(self, parent) self.layout = QVBoxLayout(self) self.scene = MlabSceneModel() self.view = SceneEditor(scene=self.scene) self.ui = self.view.edit_traits(parent=self, kind='subpanel').control self.layout.addWidget(self.ui) class MyDialog(HasTraits): scene = Instance(MlabSceneModel, ()) view = View(Item('scene', editor=SceneEditor(), height=250, width=300, show_label=False)) def main(): app = QApplication.instance() if not app: app = QApplication([]) w = QWidget() layout = QVBoxLayout() mayavi_widget = MayaviQWidget(w) layout.addWidget(mayavi_widget) w.setLayout(layout) w.show() m = MyDialog() m.edit_traits(parent=w, kind='subpanel') app.exec_() if __name__ == '__main__': main() ``` 在这个例子中,Mayavi的场景被添加到了PyQt的QWidget对象中,并且可以与PyQt的其他控件一起在窗口中显示。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI算法网奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值