1.首先必找三张尺寸大小一致的图片
# -*- coding: utf-8 -*-
"""
Use GLImageItem to display image data on rectangular planes.
In this example, the image data is sampled from a volume and the image planes
placed as if they slice through the volume.
"""
import pyqtgraph.opengl as gl
import pyqtgraph as pg
import cv2
app = pg.mkQApp("GLImageItem Example")
w = gl.GLViewWidget()
w.opts['distance'] = 200
w.show()
w.setWindowTitle('pyqtgraph example: ImageItem')
## create volume data set to slice three images from
shape = (400, 400, 4) # shape大小为此时传入图片的大小,因此传入图片的大小必须一致
def transform(file_path):
image = cv2.imread(file_path)
# 将3通道的图片转为4通道
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGBA)
return image
# 3张图片大小必须保持一致
image_path1 = "../Image/image_01.jpeg"
image_path2 = "../Image/image_02.jpeg"
image_path3 = "../Image/image_03.jpeg"
# image为ndarray类型
image1 = transform(image_path1)
image2 = transform(image_path2)
image3 = transform(image_path3)
levels = (-0.08, 0.08)
tex1 = image1 # yz plane
tex2 = image2 # xz plane
tex3 = image3 # xy plane
# Create three image items from textures, add to view
# 从纹理创建三个图像项,添加到视图
v1 = gl.GLImageItem(tex1)
v1.translate(-shape[1] / 2, -shape[2] / 2, 0)
v1.rotate(90, 0, 0, 1)
v1.rotate(-90, 0, 1, 0)
w.addItem(v1)
v2 = gl.GLImageItem(tex2)
v2.translate(-shape[0] / 2, -shape[2] / 2, 0)
v2.rotate(-90, 1, 0, 0)
w.addItem(v2)
# 第三轴Z轴
v3 = gl.GLImageItem(tex3)
v3.translate(-shape[0] / 2, -shape[1] / 2, -200) # 使Z轴在其他图片的一半的地方进行切分插入。
w.addItem(v3)
ax = gl.GLAxisItem()
w.addItem(ax)
if __name__ == '__main__':
pg.exec()
2.运行结果: