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 MayaviSceneclass Visualization(HasTraits):scene = Instance(MlabSceneModel, ())def __init__(self):HasTraits.__init__(self)# the layout of the dialog createdview = 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 layoutmain_widget = QWidget()self.setCentralWidget(main_widget)layout = QVBoxLayout(main_widget)# Create the Mayavi visualizationself.visualization = Visualization()self.mayavi_scene = self.visualization.edit_traits(parent=main_widget, kind='subpanel').controllayout.addWidget(self.mayavi_scene)# Create a button and connect its eventself.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, mgriddphi, 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) ** m7x = 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 mlabclass Visualization(HasTraits):scene = Instance(MlabSceneModel, ())def __init__(self):HasTraits.__init__(self)# the layout of the dialog createdview = 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.0corners = 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) + centeredges = [[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 layoutmain_widget = QWidget()self.setCentralWidget(main_widget)layout = QVBoxLayout(main_widget)# Create the Mayavi visualizationself.visualization = Visualization()self.mayavi_scene = self.visualization.edit_traits(parent=main_widget, kind='subpanel').controllayout.addWidget(self.mayavi_scene)# Create a button and connect its eventself.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 boxcenter = np.array([0, 0, 0])size = np.array([2, 1, 1])orientation = np.pi / 4  # 45 degrees# Draw the 3D boxself.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 mlabclass 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 createdview = 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 matrixc, s = np.cos(orientation), np.sin(orientation)R = np.array([[c, -s, 0],[s, c, 0],[0, 0, 1]])# Half dimensionsl, w, h = size / 2.0corners = 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 cornerscorners = np.dot(corners, R.T) + center# Define edges of the boxedges = [(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 edgesfor 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 layoutmain_widget = QWidget()self.setCentralWidget(main_widget)layout = QVBoxLayout(main_widget)# Create the Mayavi visualizationself.visualization = Visualization()self.ui = self.visualization.edit_traits(parent=main_widget, kind='subpanel').controllayout.addWidget(self.ui)# Timer for moving the boxself.timer = QTimer(self)self.timer.timeout.connect(self.update_position)self.timer.start(100)  # Update every 100 ms# Box parametersself.box_center = np.array([0, 0, 0])self.box_size = np.array([1, 1, 1])self.orientation = 0  # No rotation initiallydef update_position(self):self.visualization.figure.scene.disable_render = True  # For performanceself.box_center[0] += 0.1  # Move along Xmlab.clf()  # Clear the figureself.visualization.draw_3d_box(self.box_center, self.box_size, self.orientation)self.visualization.figure.scene.disable_render = False  # Re-enable renderingif __name__ == '__main__':app = QApplication(sys.argv)window = MainWindow()window.show()sys.exit(app.exec_())

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/33081.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

如何在Java中实现缓存机制?

如何在Java中实现缓存机制? 大家好,我是免费搭建查券返利机器人省钱赚佣金就用微赚淘客系统3.0的小编,也是冬天不穿秋裤,天冷也要风度的程序猿!今天我们将深入探讨在Java应用程序中如何实现高效的缓存机制。缓存是提高…

温湿度采集及OLED显示

目录 软件I2C和硬件I2C每隔2秒钟采集一次温湿度数据,显示到OLED上,同时通过串口发送到上位机的“串口助手”软件 软件I2C和硬件I2C "I2C"代表Inter-Integrated Circuit,是一种用于在数字电路之间进行通信的串行通信协议。软件I2C和…

使用Testcontainers进行Java集成测试

在现代软件开发中,集成测试是确保应用程序与其依赖项(如数据库、消息队列等)正确交互的关键步骤。Testcontainers是一个支持JUnit测试的Java库,它提供了一个简单而强大的方式来创建和管理测试所需的任何Docker容器。本文将详细介绍…

【PythonWeb开发】Flask请求中传递参数到视图函数的方法总结。

在Flask中&#xff0c;传入参数主要有两种常见的方式&#xff0c;即通过GET请求和POST请求。 一、GET请求传递参数 &#xff08;1&#xff09;URL路径中获取 这种类型的参数通常称为路径参数或路由参数&#xff0c;它们是URL路径的一部分&#xff0c;通过尖括号<parameter…

CV每日论文--2024.6.21

1、An Image is Worth More Than 16x16 Patches: Exploring Transformers on Individual Pixels 中文标题&#xff1a;一张图像的价值超过 16x16 的补丁&#xff1a;在单个像素上探索 Transformers 简介&#xff1a;这项工作并非介绍一种新的方法&#xff0c;而是呈现了一项有…

@ControllerAdvice:你可以没用过,但是不能不了解

1.概述 最近在梳理Spring MVC相关扩展点时发现了ControllerAdvice这个注解&#xff0c;用于定义全局的异常处理、数据绑定、数据预处理等功能。通过使用 ControllerAdvice&#xff0c;可以将一些与控制器相关的通用逻辑提取到单独的类中进行集中管理&#xff0c;从而减少代码重…

WinForm 2048

WinForm 2048 是一个基于 Windows 窗体应用程序&#xff08;WinForms&#xff09;实现的经典益智游戏——2048。这个游戏通过简单的滑动或点击操作&#xff0c;将相同数字的方块合并&#xff0c;以生成更大的数字方块&#xff0c;最终目标是创造出一个数字为 2048 的方块。 游…

电商爬虫API的定制开发:满足个性化需求的解决方案

一、引言 随着电子商务的蓬勃发展&#xff0c;电商数据成为了企业决策的重要依据。然而&#xff0c;电商数据的获取并非易事&#xff0c;特别是对于拥有个性化需求的企业来说&#xff0c;更是面临诸多挑战。为了满足这些个性化需求&#xff0c;电商爬虫API的定制开发成为了解决…

【杂记-浅谈IPv6地址】

IPv6地址 一、IPv6地址概述二、IPv6地址结构三、IPv6地址分类四、IPv6地址配置五、IPv6的应用场景 一、IPv6地址概述 IPv6&#xff0c;Internet Protocol version 6&#xff0c;是互联网协议的第六版&#xff0c;旨在克服IPv4地址耗尽的挑战&#xff0c;并为互联网的未来发展提…

Apache Tomcat 10.1.25 新版本发布 java 应用服务器

Tomcat 是一个小型的轻量级应用服务器&#xff0c;在中小型系统和并发访问用户不是很多的场合下被普遍使用&#xff0c;是开发和调试 JSP 程序的首选。对于一个初学者来说&#xff0c;可以这样认为&#xff0c;当在一台机器上配置好 Apache 服务器&#xff0c;可利用它响应对 H…

uniapp 使用uview 插件

看创建项目版本vue2 、 vue3 Button 按钮 | uView 2.0 - 全面兼容 nvue 的 uni-app 生态框架 - uni-app UI 框架 1. npm install uview-ui2.0.36 2. // main.js&#xff0c;注意要在use方法之后执行 import uView from uview-ui Vue.use(uView) // 如此配置即可 uni.$u.c…

服务治理入门

服务治理的生命周期 在微服务架构中&#xff0c;服务治理是确保服务正常运行和高效协作的关键。服务治理的生命周期包括以下五个阶段&#xff1a;服务注册、服务发现、服务续约/心跳、服务被动剔除和服务主动剔除。 服务注册 服务提供者在启动时&#xff0c;需要将其服务信…

Prometheus的infratest、UAT、PRE、PRD分别代表什么

Prometheus的infratest、UAT、PRE、PRD分别代表什么 在Prometheus监控系统中,infratest、UAT、PRE和PRD通常指的是不同阶段的测试环境,分别对应基础设施测试(Infrastructure Test)、用户验收测试(User Acceptance Test)、预生产环境(Pre-production)和生产环境(Produ…

构建RISC-V工具链:基本步骤

在这一节内容中&#xff0c;我们将介绍如何构建一个64位的RISC-V工具链。在这个过程中&#xff0c;我们将编译默认的RISC-V工具链&#xff0c;而不修改指令集。 1. 安装必要的软件包 首先&#xff0c;需要安装一些必要的软件包。在终端中运行以下命令&#xff1a; sudo apt-g…

vue3-cropperjs图片裁剪工具-用户上传图片截取-(含预览视频)

效果图 上传图片弹窗预览 对于这个上传图片样式可以参考 官方原代码 官网传送入口 Upload 上传 | Element Plus (element-plus.org) <template><el-uploadclass"upload-demo"dragaction"https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6…

免费分享一套SpringBoot+Vue电影院售票管理系统【论文+源码+SQL脚本】,帅呆了~~

大家好&#xff0c;我是java1234_小锋老师&#xff0c;看到一个不错的SpringBootVue电影院售票管理系统&#xff0c;分享下哈。 项目视频演示 【免费】SpringBootVue电影院售票管理系统 Java毕业设计_哔哩哔哩_bilibili【免费】SpringBootVue电影院售票管理系统 Java毕业设计…

DriverManager.getConnection用法总结

DriverManager.getConnection用法总结 大家好&#xff0c;我是免费搭建查券返利机器人省钱赚佣金就用微赚淘客系统3.0的小编&#xff0c;也是冬天不穿秋裤&#xff0c;天冷也要风度的程序猿&#xff01; 在Java编程中&#xff0c;DriverManager.getConnection是一个用于建立与…

《Windows API每日一练》5.5 插入符号

当你向程序中输入文本时&#xff0c;通常会有下划线、竖线或方框指示你输入的下一个字符将出现在屏幕上的位置。你也许认为这是“光标”&#xff0c;但在编写Windows程序时&#xff0c;你必须避免这种习惯。在Windows中&#xff0c;它被称为“插入符号”&#xff08;caret&…

市政道路乙级资质申报的筹备与执行

一、筹备阶段 1. 政策研读与自我评估 详细了解资质标准&#xff1a;仔细阅读最新的资质申报指南和相关法规&#xff0c;明确乙级资质的具体要求&#xff0c;包括企业资本、技术人员配置、过往业绩等。自我评估&#xff1a;对照资质标准&#xff0c;对企业现状进行全面评估&am…

河南省乙级建筑设计资质标准案例分析

河南省乙级建筑设计资质标准案例分析 虽然我没有具体的河南省乙级建筑设计资质的详细案例分析&#xff0c;但我可以根据一般性的资质标准和流程&#xff0c;构建一个简化的案例分析框架&#xff0c;帮助理解乙级建筑设计资质的获取和应用。 案例背景&#xff1a; 假设“华豫…