python - pyqtgraph ImageView和彩色图像

标签 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 图像,但我得到了第二个空窗口(我假设它来自 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/

相关文章:

python - 如何增加 Paramiko SSH 客户端上的向后滚动缓冲区?

python - 从 SFTP 文件读取 CSV/Excel 文件,使用 Pandas 对这些文件进行一些更改,然后保存回来

python - 山魈模板变量不替代

python - 元素的 numpy 条件格式

python - 使用pyqtgraph绘图而不显示

python - PySide/PyQtGraph 访问主 Qt 事件线程

python - Lisp vs Python——静态编译

python - 在 python/pandas 中部分基于自身计算列

python - PyQtGraph:为什么对象总是在左下角