opengl或pyqtgraph.opengl显示三维图像

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_())
    原文作者:老衲要学习
    原文地址: https://blog.csdn.net/weixin_46180132/article/details/120159944
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞