如何使用pyqt在场景中输出色彩映射?

Fri*_*ter 17 python qt matplotlib pyqt4

TL; DR:您只需阅读"更新"部分.

如何numpy.random.random((256, 256))将色彩映射输出到qt场景?

这是摘要的摘要.

更新:

以下是我想要保存到文件的色彩映射.

scaled_image = Image.fromarray(np.uint8(self.image*255))
plt.savefig("/home/test.png")
Run Code Online (Sandbox Code Playgroud)

self.image 是256x256 numpy数组,它的所有值都在-1和1之间. 在此输入图像描述

如何将此图像输出到Qt中的场景?您可以使用self.image = numpy.random.random((256, 256))与我类似的起点.你如何将你的2D numpy随机值数组作为colormap放到pyqt场景中?


更新24/02/1016

很近.这似乎有效,但规模已经反转.蓝色现在很热,红色很冷.如何切换它使它看起来像上面的图像?

    scene = QGraphicsScene(self)
    scaled_image = Image.fromarray(np.uint8(self.image*255))
    gcf().canvas.draw() # IMPORTANT!
    stringBuffer = gcf().canvas.buffer_rgba() # IMPORTANT!
    l, b, w, h = gcf().bbox.bounds
    qImage = QtGui.QImage(stringBuffer,
                  w,
                  h,
                  QtGui.QImage.Format_ARGB32)
    pixmap = QtGui.QPixmap.fromImage(qImage)
    pixmapItem = QtGui.QGraphicsPixmapItem(pixmap)
    scene.addItem(pixmapItem)
    self.graphicsView.setScene(scene)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我在2016年2月23日尝试过的

以下给我一个空白的屏幕

    scene = QGraphicsScene(self)
    scaled_image = Image.fromarray(np.uint8(self.image*255))
    pixMap = QPixmap(scaled_image)
    scene.addPixmap(pixMap)
    self.graphicsView.setScene(scene)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

下面给我一个输出到Qt场景的灰度.它为什么不是彩色的?

scene = QGraphicsScene(self)
scaled_image = Image.fromarray(np.uint8(self.image*255))
imgQ = ImageQt(scaled_image)
pixMap = QtGui.QPixmap.fromImage(imgQ)
scene.addPixmap(pixMap)
self.graphicsView.setScene(scene)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我从逆向工程的解决方案,以色彩表的问题在这里和显示图像这里.

使用来自解决方案的建议,我尝试创建QGraphicsPixmapItem第一个,然后将项目添加到场景中.官方文档有点令人困惑,所以我终于从更加清晰的pyside文档中获得了我所需要的东西.可悲的是,我得到了与上面相同的灰度.代码如下:

    scene = QGraphicsScene(self)
    scaled_image = Image.fromarray(np.uint8(self.image*255))
    imgQ = ImageQt(scaled_image)
    pixMap = QtGui.QPixmap.fromImage(imgQ)
    pixMapItem = QtGui.QGraphicsPixmapItem(pixMap)
    scene.addItem(pixMapItem)
    self.graphicsView.setScene(scene)
Run Code Online (Sandbox Code Playgroud)

我试过的其他东西(较旧):

self.image 是numpy 2D数组,它的所有值都在-1和1之间.

我按如下比例缩放并获得灰度图像.但是,我想要一个colourmap:

    scene = QGraphicsScene(self)
    scaled_image = (self.image + 1) * (255 / 2)    
    scene.addPixmap(QPixmap.fromImage(qimage2ndarray.array2qimage(scaled_image)))
    self.graphicsView.setScene(scene)

#min(self.image) = -0.64462
#max(self.image) = 1.0
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

如果我做了以下操作,我会得到我想要的色彩图,例如我之前开发的网络应用程序的下图

>>> fig = plt.figure()
>>> ax = fig.add_subplot(111)
>>> imgplot = ax.imshow(self.image)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

如何将色彩映射添加到qt场景,而不是仅仅具有灰度?

rba*_*dar 4

QPixmap支持大于255的颜色值和多种图像格式。如果不是这样,Qt 中的所有图标都将是灰度的(显然情况并非如此;))。

您可以以任何您想要的方式生成颜色图(在将其转换为 a 之前(使用 OpenCV 和 numpy)或之后QImage(使用 Qt)),将其转换为 aQPixmap 并使用QGraphicsPixmapItem(不要直接用作QPixmapa 的一部分QGraphicScene将其附加到您的QGraphicsScene.

编辑: 我误读了文档。实际上,您可以QPixmap通过使用 直接在 QGraphicScene 中使用addPixmap()。对于那个很抱歉。作为道歉,这里有一些代码供您参考。:P

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class ImageView(QGraphicsView):
  def __init__(self, pixmap=None, parent=None):
    '''
    QGraphicsView shows the image
    '''
    super(ImageView, self).__init__(parent)
    self.pixmap = pixmap


class Widget(QWidget):
  def __init__(self, parent=None):
    super(Widget, self).__init__(parent)
    layout = QVBoxLayout(self)
    pic = QPixmap('/home/-----/Pictures/7 Cat Sins - Wrath.jpg')
    grview = ImageView(pic, self)
    layout.addWidget(grview)
    self.setFixedSize(pic.size())

    scene = QGraphicsScene(self)
    scene.addPixmap(pic)

    grview.setScene(scene)
    self.show()

def main():
  app = QApplication(sys.argv)
  w = Widget()

  return sys.exit(app.exec_())

if __name__ == '__main__':
  main()
Run Code Online (Sandbox Code Playgroud)

它产生以下结果:

在此输入图像描述

图片来源:在 Google 上查找7 Cat Sins - Wrath ;)

基本上,您创建一个图像查看器,创建场景图,将像素图添加到场景图,将图像查看器的场景设置为该图,并使用该查看器在屏幕上显示像素图。

EDIT2: 好的,看来您在matplotlib. 以下是您的操作方法(取自此处,并对 进行了一些小更改gcf().canvas.buffer_rgba()):

import numpy as np
from matplotlib import use
use('AGG')
from matplotlib.pylab import *
from PySide import QtCore,QtGui
# Following are used for the generation of the image but you have your own imports so you don't need them
from matplotlib.transforms import Bbox
from matplotlib.path import Path
from matplotlib.patches import Rectangle    

# Generation of the figure (you can skip all that and use your stuff)
rect = Rectangle((-1, -1), 2, 2, facecolor="#aaaaaa")
gca().add_patch(rect)
bbox = Bbox.from_bounds(-1, -1, 2, 2)

for i in range(12):
    vertices = (np.random.random((4, 2)) - 0.5) * 6.0
    vertices = np.ma.masked_array(vertices, [[False, False], [True, True], [False, False], [False, False]])
    path = Path(vertices)
    if path.intersects_bbox(bbox):
        color = 'r'
    else:
        color = 'b'
    plot(vertices[:,0], vertices[:,1], color=color)

# The conversion and display of the plot
app = QtGui.QApplication(sys.argv)
gcf().canvas.draw() # IMPORTANT!

stringBuffer = gcf().canvas.buffer_rgba() # IMPORTANT!
l, b, w, h = gcf().bbox.bounds

qImage = QtGui.QImage(stringBuffer, 
                      w,
                      h,
                      QtGui.QImage.Format_ARGB32)

scene = QtGui.QGraphicsScene()
view = QtGui.QGraphicsView(scene)
pixmap = QtGui.QPixmap.fromImage(qImage)
pixmapItem = QtGui.QGraphicsPixmapItem(pixmap)
scene.addItem(pixmapItem)
view.show()

app.exec_()
Run Code Online (Sandbox Code Playgroud)

编辑3: 对于您的反转颜色图问题,请参阅此处(文档)此处(SO)

PS:我建议您不要使用额外的库(例如qimage2ndarray将 OpenCV 图像转换为QImages),而是自己执行此操作,看看到底需要做什么。如果您对这些材料有丰富的经验并且您只需要节省一些时间,那么这样的库就很不错。除此之外,远离他们。