【问题标题】:pyqtgraph ImageView and color imagespyqtgraph ImageView 和彩色图像
【发布时间】:2018-11-16 05:57:02
【问题描述】:

我正在尝试在 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 图像。

【问题讨论】:

  • 使用self.im1.setImage(np.random.normal(size=(100, 100, 3)).T)
  • 这不是我要找的...如果您在该尺寸的 3D 图像上执行.T,它将为您提供尺寸为 (3, 100, 100) 的图像然后随着时间的推移被解释为图像。 (然后在底部显示一个滑块,图像仍然是灰度)

标签: python pyqtgraph


【解决方案1】:

嗯,我找到了一种看起来很合理的方法。 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()

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

【讨论】:

  • 非常好的解决方案,我花了几个小时试图解决这个问题,最后只通过一个通道显示灰度图像,但这立即为我解决了问题。谢谢!
猜你喜欢
  • 1970-01-01
  • 2021-10-06
  • 1970-01-01
  • 1970-01-01
  • 2022-10-15
  • 2020-06-01
  • 1970-01-01
  • 2015-09-26
  • 1970-01-01
相关资源
最近更新 更多