opengl或pyqtgraph.opengl显示三维图像

该博客展示了一个使用PyQt5和OpenGL库创建的3D视图窗口应用程序。代码中定义了一个名为`TestWindow`的类,继承自QMainWindow,它包含一个GLViewWidget用于显示3D内容。程序读取了三个文本文件中的数据,并使用这些数据生成了一个3D表面映射,通过调整颜色和比例展示了数据的三维结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import numpy as np
import pyqtgraph.opengl as gl
from PyQt5.QtWidgets import QApplication, QVBoxLayout, QMainWindow, QWidget
import sys


class TestWindow(QMainWindow):
    def __init__(self, parent=None):
        QMainWindow.__init__(self)
        # 设置窗体尺寸
        self.setGeometry(300, 300, 500, 350)
        # 添加一个按钮和一个PlotWidget

        self.D3wid = gl.GLViewWidget()
        self.D3wid.setBackgroundColor(255, 255, 255)
        self.D3wid.setCameraPosition(distance=180)
        # 将两个Widget垂直排列布局
        centralWidget = QWidget()
        main_layout = QVBoxLayout()
        main_layout.addWidget(self.D3wid)
        centralWidget.setLayout(main_layout)
        # 应用上述布局
        self.setCentralWidget(centralWidget)
        self.shape_3d_map(ax, ay, az)

    def shape_3d_map(self, x, y, z):
        x1 = x[0, :]
        y1 = y[:, 0]

        trans_z= (np.nanmax(z) + np.nanmin(z)) / 2
        trans_x= (np.nanmax(x1) + np.nanmin(x1)) / 2
        trans_y = (np.nanmax(y1) + np.nanmin(y1)) / 2
        x2 = x1 - trans_x
        y2 = y1 - trans_y 
        z1 = z - trans_z
        z2 = np.transpose(z1)
        self.D3 = gl.GLSurfacePlotItem(x2, y2, z2, shader='normalColor', color=(0.2, 0.2, 0.8, 0.8))
        z_range = np.nanmax(z) - np.nanmin(z)
        y_range = np.nanmax(y) - np.nanmin(y)
        amp_k = (y_range / 2) / z_range
        self.D3.setData(x2, y2, z=z2 * amp_k )
        self.D3wid.addItem(self.D3)


if __name__ == "__main__":

    app = QApplication(sys.argv)
    ax = np.loadtxt(r'C:\Users\DELL\Desktop\Xmetric.txt')
    ay = np.loadtxt(r'C:\Users\DELL\Desktop\Ymetric.txt')
    az = np.loadtxt(r'C:\Users\DELL\Desktop\Hmetric.txt')
    qb = TestWindow()
    qb.show()
    sys.exit(app.exec_())
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值