pyqtgraph ImageView和彩色图像

时间:2018-06-06 13:54:47

标签: python pyqtgraph

我试图在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图像,但我会得到第二个空窗口(我假设它来自{ {1}})。

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

1 个答案:

答案 0 :(得分:0)

好吧,我找到了一种方法,这似乎是合理的。 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()

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