繁体   English   中英

原始图像显示在qt与python

[英]raw image display in qt with python

我正在使用python程序从科学相机中获取图像。 这部分没关系,我可以在一个数组中得到16位图像。 问题来自于我想在qt窗口中显示图像(我正在使用QGraphicsWindow),图像的显示方式非常奇怪。 为了显示图像,我将2d数组转换为pixmap,然后将其显示出来。 我尝试了不同的东西,但是获得了以下代码的最佳结果:

def array2Pixmap(arr):
arr_uint8 = arr.view(dtype=numpy.uint8)
im8 = Image.fromarray(arr_uint8)
imQt = QtGui.QImage(ImageQt.ImageQt(im8))
pix = QtGui.QPixmap.fromImage(imQt)
return pix

得出以下结果: 在此输入图像描述

还有这个:

def array2Pixmap(arr):
arr_uint8 = arr.astype(numpy.uint8)
im8 = Image.fromarray(arr_uint8)
imQt = QtGui.QImage(ImageQt.ImageQt(im8))
pix = QtGui.QPixmap.fromImage(imQt)
return pix

这给出了完全相同的捕获条件(相机曝光时间,光照强度等): 在此输入图像描述

所以现在我正在寻找一种以正确的方式显示图像的方法。 你知道我做错了什么吗?

谢谢

编辑

这是arr的例子。 命令print(arr)返回

[[100  94  94 ...  97  98  98]
[ 97 100  98 ...  98 101  99]
[100  95  98 ... 104  98 102]
...
[ 98  98  98 ...  96  98 100]
[ 94 100 102 ...  92  98 104]
[ 97  90  96 ...  96  97 100]]

并返回一个print(type(arr))

<class 'numpy.ndarray'>

编辑

好的,我有一些消息。 我改变了我的代码,以便现在转换为8位数组ID,如下所示:

arr = numpy.around(arr*(2^8-1)/(2^16-1))
arr_uint8 = arr.astype(numpy.uint8)

如果我使用matplotlib.pyplot.imshow(arr, cmap='gray')显示图像,它可以工作,图像在编辑器中显示如下:

在此输入图像描述

但当我将其转换为QPixmap时,结果与之前相同。

奇怪的是,当我使用arr_uint8 = arr.view(dtype=numpy.uint8)转换为8位时,结果是一个2048 * 4096而不是2048 * 2048的数组。 我不明白为什么......

所以,虽然你没有在问题中说出来,但我假设你的图像格式是16位灰度。

在这里查看格式类型: https : //doc.qt.io/Qt-5/qimage.html#Format-enum ,这是不受支持的格式,因此您必须将其更改为可以显示的内容。

RGB64格式允许每种颜色16位,这对于您拥有的值足够分辨率:

from PySide import QtGui, QPixmap

def array_to_pixmap(arr):
    """Returns a QPixmap from a 16 bit greyscale image `arr`."""

    # create a local variable arr which is 64 bit so we can left shift it
    # without overflowing the 16 bit original array
    arr = arr.astype(np.int64)

    # pack the 16 bit values of arr into the red, green, and blue channels
    rgb = arr << 48 | arr << 32 | arr << 16 | 0xffff
    im = QtGui.QImage(rgb, rgb.shape[0], rgb.shape[1], QtGui.QImage.Format_RGBA64)
    return QtGui.QPixmap.fromImage(im)

我没有测试过这个,但它应该给你足够的信息继续。

我找到了解决方案。 实际上,@ user545424的解决方案不起作用,因为我使用的是PyQt5,并且不支持图像格式Format_RGBA64。 我试图安装PySide2,但它没有用,所以经过一些研究,我发现这篇文章: 将16位灰度转换为QImage答案中提出的解决方案完美无缺。 这是我用来显示16位图像的代码:

from PyQt5 import QtGui
import numpy as np

def array2Pixmap(img):
    img8 = (img/256.0).astype(np.uint8) 
    img8 = ((img8 - img8.min()) / (img8.ptp() / 255.0)).astype(np.uint8)
    img = QtGui.QImage(img8.repeat(4), 2048, 2048, QtGui.QImage.Format_RGB32)

    pix = QtGui.QPixmap(img.scaledToWidth(img.width()*2))
    return pix

这段代码有效,我有一个很好的图像,但现在我必须处理32位图像2048 * 2048像素,所以一段时间后执行速度变慢。 我会试着找出原因。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM