gpt4 book ai didi

python - pyqtgraph ImageView和彩色图像

转载 作者:行者123 更新时间:2023-12-01 09:22:15 26 4
gpt4 key购买 nike

我正在尝试在 pyqtgraph 的 Dock 中的 ImageView() (或类似的)中显示 RGB numpy 数组。

总体思路是这样的代码:

import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
import numpy as np

from pyqtgraph.dockarea import Dock, DockArea

class SimDock(Dock):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

#self.im1 = pg.image()
self.im1 = pg.ImageView()

self.im1.setImage(np.random.normal(size=(100, 100, 3)))
self.addWidget(self.im1, row=0, col=0)

self.im1.ui.histogram.hide()
self.im1.ui.menuBtn.hide()
self.im1.ui.roiBtn.hide()


app = QtGui.QApplication([])
win = QtGui.QMainWindow()
area = DockArea()
win.setCentralWidget(area)
win.resize(1500, 800)
win.setWindowTitle('pyqtgraph example: dockarea')

simdock = SimDock("Similar Images", size=(500, 500))
area.addDock(simdock, 'right')

win.show()

# Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
app.instance().exec_()

但是,当我运行上面的代码时,我得到:

ValueError: could not broadcast input array from shape (100,100,3) into shape (100,100)

当我将 self.im1 切换为 pg.image 而不是 pg.ImageView 时,Dock 中会显示 RGB 图像,但我得到了第二个空窗口(我假设它来自 pg.image())。

Based on this question ,ImageView 可以接受(M,N,3)RGB 数据,但我似乎无法让它在没有弹出第二个窗口的情况下在小部件中显示 RGB 图像。

最佳答案

嗯,我找到了一种看起来合理的方法。 There is a post这建议子类化 pg.ImageView 以便自动添加颜色查找表。所以,最后我有:

class ColorImageView(pg.ImageView):
"""
Wrapper around the ImageView to create a color lookup
table automatically as there seem to be issues with displaying
color images through pg.ImageView.
"""

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.lut = None

def updateImage(self, autoHistogramRange=True):
super().updateImage(autoHistogramRange)
self.getImageItem().setLookupTable(self.lut)

然后我的代码中的调用变为 self.im1 = ColorImageView()

这对我有用,而且看起来相当简单。

关于python - pyqtgraph ImageView和彩色图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50722238/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com