pyqtgraph ImageView 和彩色图像

pyqtgraph ImageView and color images

我正在尝试在 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 图像,但我得到第二个空的 window(这我假设来自 pg.image()).

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

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

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().

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