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_())
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要在 `QOpenGLWidget` 中添加三维图像,你需要使用一些三维图像渲染库,比如OpenGL或者Qt3D等库。下面是一个基本的示例代码,展示如何在 `QOpenGLWidget` 中添加一个简单的三维图形: ```python from PyQt5.QtGui import QOpenGLShader, QOpenGLShaderProgram, QOpenGLTexture from PyQt5.QtWidgets import QOpenGLWidget, QApplication from PyQt5.QtCore import Qt, QCoreApplication import numpy as np import OpenGL.GL as gl class MyOpenGLWidget(QOpenGLWidget): def __init__(self, parent=None): super().__init__(parent) # 初始化三维图像 self.vertices = np.array([ [-1.0, -1.0, 1.0], [ 1.0, -1.0, 1.0], [ 1.0, 1.0, 1.0], [-1.0, 1.0, 1.0], [-1.0, -1.0, -1.0], [ 1.0, -1.0, -1.0], [ 1.0, 1.0, -1.0], [-1.0, 1.0, -1.0] ]).astype(np.float32) self.indices = np.array([ [0, 1, 2], [2, 3, 0], [1, 5, 6], [6, 2, 1], [7, 6, 5], [5, 4, 7], [4, 0, 3], [3, 7, 4], [4, 5, 1], [1, 0, 4], [3, 2, 6], [6, 7, 3] ]).astype(np.uint32) # 初始化着色器程序 self.shader_program = QOpenGLShaderProgram(self) vertex_shader = QOpenGLShader(QOpenGLShader.Vertex, self) vertex_shader.compileSourceCode(""" attribute vec3 a_position; uniform mat4 u_mvp_matrix; void main() { gl_Position = u_mvp_matrix * vec4(a_position, 1.0); } """) fragment_shader = QOpenGLShader(QOpenGLShader.Fragment, self) fragment_shader.compileSourceCode(""" void main() { gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0); } """) self.shader_program.addShader(vertex_shader) self.shader_program.addShader(fragment_shader) self.shader_program.link() # 初始化纹理 self.texture = QOpenGLTexture(QOpenGLTexture.Target2D) self.texture.setData(QCoreApplication.instance().applicationDirPath() + "/texture.jpg") # 设置背景颜色 self.setBackgroundColor(Qt.black) def setBackgroundColor(self, color): r, g, b, a = color.getRgbF() gl.glClearColor(r, g, b, a) def initializeGL(self): gl.glEnable(gl.GL_DEPTH_TEST) # 启用深度测试 def paintGL(self): gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) self.shader_program.bind() self.shader_program.enableAttributeArray("a_position") self.shader_program.setAttributeArray("a_position", self.vertices) mvp_matrix = np.eye(4, dtype=np.float32) aspect_ratio = self.width() / self.height() mvp_matrix = np.dot(mvp_matrix, self.getProjectionMatrix(aspect_ratio)) mvp_matrix = np.dot(mvp_matrix, self.getViewMatrix()) self.shader_program.setUniformValue("u_mvp_matrix", mvp_matrix) self.texture.bind() gl.glDrawElements(gl.GL_TRIANGLES, self.indices.size, gl.GL_UNSIGNED_INT, self.indices) def getProjectionMatrix(self, aspect_ratio): fov = 45.0 znear = 0.1 zfar = 100.0 projection_matrix = np.zeros((4, 4), dtype=np.float32) tan_half_fov = np.tan(np.deg2rad(fov / 2.0)) projection_matrix[0, 0] = 1.0 / (tan_half_fov * aspect_ratio) projection_matrix[1, 1] = 1.0 / tan_half_fov projection_matrix[2, 2] = -(zfar + znear) / (zfar - znear) projection_matrix[2, 3] = -2.0 * zfar * znear / (zfar - znear) projection_matrix[3, 2] = -1.0 return projection_matrix def getViewMatrix(self): eye = np.array([0.0, 0.0, 5.0]) target = np.array([0.0, 0.0, 0.0]) up = np.array([0.0, 1.0, 0.0]) view_matrix = np.zeros((4, 4), dtype=np.float32) view_matrix[:3, :3] = self.getLookAtMatrix(eye, target, up) view_matrix[:3, 3] = eye view_matrix[3, 3] = 1.0 return view_matrix def getLookAtMatrix(self, eye, target, up): forward = target - eye forward = forward / np.linalg.norm(forward) side = np.cross(forward, up) side = side / np.linalg.norm(side) up = np.cross(side, forward) up = up / np.linalg.norm(up) look_at_matrix = np.zeros((4, 4), dtype=np.float32) look_at_matrix[0, :3] = side look_at_matrix[1, :3] = up look_at_matrix[2, :3] = -forward look_at_matrix[3, 3] = 1.0 return look_at_matrix if __name__ == '__main__': app = QApplication([]) w = MyOpenGLWidget() w.show() app.exec_() ``` 该示例代码展示了如何在 `QOpenGLWidget` 中绘制一个简单的立方体,并使用着色器程序和纹理进行渲染。你可以根据需要修改代码,来绘制你自己的三维图形。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值