gpt4 book ai didi

python - 如何使用 pyqt 在场景中输出颜色图?

转载 作者:IT老高 更新时间:2023-10-28 21:14:14 30 4
gpt4 key购买 nike

TL;DR:您只需要阅读“更新”部分。

How do you output numpy.random.random((256, 256)) as a colormap to a qt scene?

这是摘要的摘要。

更新:

以下内容为我提供了我想要保存到文件的颜色图。

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

self.image 是 256x256 的 numpy 数组,它的所有值都在 -1 和 1 之间。 enter image description here

如何在 Qt 中将此图像输出到场景中?你可以使用 self.image = numpy.random.random((256, 256)) 有一个和我类似的起点。如何将 2D numpy 随机值数组作为颜色图放到 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)

enter image description here

我在 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)

enter image description here

下面给我一个灰度输出到 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)

enter image description here

我从颜色图问题的解决方案进行逆向工程 here并显示图像 here .

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

    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)

我尝试过的其他事情(旧):

self.image 是 numpy 二维数组,所有值都在 -1 和 1 之间。

我按如下缩放并得到灰度图像。但是,我想要一个颜色图:

    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

enter image description here

如果我改为执行以下操作,我会得到我想要的颜色图,例如我之前开发的 Web 应用程序的下图中

>>> fig = plt.figure()
>>> ax = fig.add_subplot(111)
>>> imgplot = ax.imshow(self.image)

enter image description here

如何将颜色图添加到 qt 场景,而不仅仅是灰度?

最佳答案

QPixmap 支持大于 255 和 multiple image formats 的颜色值.如果不是这样,Qt 中的所有图标都会被灰度化(显然不是这样;))。

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

编辑:我误读了文档。您实际上可以通过使用 addPixmap() 在 QGraphicScene 中直接使用 QPixmap。对于那个很抱歉。作为道歉,这里有一些代码给你。 :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()

它产生以下结果:

enter image description here

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

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

EDIT2:好的,看来您在实际集成 matplotlib 时遇到了问题。以下是您的操作方法(取自 here,对 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_()

编辑 3:对于您的反转颜色图问题,请参阅 here (documentation)here (SO) .

PS:我建议您自己做,而不是使用诸如 qimage2ndarray 之类的额外库来将 OpenCV 图像转换为 QImage,以查看究竟需要什么完毕。如果您对 Material 有丰富的经验并且您只需要节省一些时间,那么这样的库就很整洁。除此之外 - 远离他们。

关于python - 如何使用 pyqt 在场景中输出颜色图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35445584/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com