python - PyQt5中使用cv2.imshow和drawpixmap的颜色图是不同的

标签 python opencv pyqt colormap

目前,我需要以伪彩色显示我的灰色图像。我使用 opencv2 生成伪色彩图。但是,我发现 cv2 和 QLabel 中显示的颜色图不同。

对于 cv2,代码为:

import numpy as np
import cv2

img = np.zeros((256,256),dtype=np.uint8)
img[0:128,0:128] = 255
img[128:255,128:255] = 128
disImg = cv2.applyColorMap(img, cv2.COLORMAP_AUTUMN)
cv2.imshow('test',disImg)
cv2.waitKey()

结果是:

enter image description here

然后,我使用相同的颜色图生成伪彩色图像,并使用drawpixmal进行显示,代码为:

import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import numpy as np
import cv2
class MyLabel(QLabel):
    def __init__(self):
        super(MyLabel, self).__init__()
        img = np.zeros((256,256),dtype=np.uint8)
        img[0:128,0:128] = 255
        img[128:255,128:255] = 128
        disImg = cv2.applyColorMap(img, cv2.COLORMAP_AUTUMN)
        QImg = QImage(disImg.data, disImg.shape[1], disImg.shape[0], disImg.strides[0], QImage.Format_RGB888)
        self.qimg = QImg

    def paintEvent(self, QPaintEvent):
        super(MyLabel, self).paintEvent(QPaintEvent)

        pos = QPoint(0, 0)
        source = QRect(0, 0, 256,256)

        painter = QPainter(self)
        painter.drawPixmap(pos, QPixmap.fromImage(self.qimg), source)

class Window(QWidget):
    def __init__(self):
        super(Window, self).__init__()
        layout = QHBoxLayout(self)
        self.resize(300,300)
        self.label = MyLabel()
        layout.addWidget(self.label)


if __name__ == '__main__':

    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

结果是:

enter image description here

为什么显示相同的图像时颜色图不同?

此外,cv2.COLORMAP_AUTUMN 应该是: enter image description here

所以,cv2显示的图像是正确的,而python drawpixmap显示错误。

如何解决?

最佳答案

感谢 Micka,opencv 使用 BGR,而 qt 使用 RGB 进行渲染。所以,正确的代码应该是:

import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import numpy as np
import cv2
class MyLabel(QLabel):
    def __init__(self):
        super(MyLabel, self).__init__()
        img = np.zeros((256,256),dtype=np.uint8)
        img[0:128,0:128] = 255
        img[128:255,128:255] = 128
        disImg = cv2.applyColorMap(img, cv2.COLORMAP_AUTUMN)
        b = disImg[:,:,0]
        g = disImg[:,:,1]
        r = disImg[:,:,2]

        img = np.zeros((256,256,3),dtype=np.uint8)
        img[:,:,0] = r
        img[:,:,1] = g
        img[:,:,2] = b
        disImg = img


        QImg = QImage(disImg.data, disImg.shape[1], disImg.shape[0], disImg.strides[0], QImage.Format_RGB888)
        self.qimg = QImg

    def paintEvent(self, QPaintEvent):
        super(MyLabel, self).paintEvent(QPaintEvent)

        pos = QPoint(0, 0)
        source = QRect(0, 0, 256,256)

        painter = QPainter(self)
        painter.drawPixmap(pos, QPixmap.fromImage(self.qimg), source)

class Window(QWidget):
    def __init__(self):
        super(Window, self).__init__()
        layout = QHBoxLayout(self)
        self.resize(300,300)
        self.label = MyLabel()
        layout.addWidget(self.label)


if __name__ == '__main__':

    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

关于python - PyQt5中使用cv2.imshow和drawpixmap的颜色图是不同的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50623735/

相关文章:

python - 如何使用 Pyqt5 显示重叠的 QWidget?

python - 如何多次动态调用命令的子命令?

python - Autobahn websocket 服务器的 ConnectionRequest.headers 中的所有 http header key 均以小写形式出现

python - OpenCV HoughCircles 偶尔会返回 [0. 0. 0.]

python - 从辅助线程重定向标准输出(使用函数而不是类进行多线程?)

python - QMessageBox 的功能测试...为什么不起作用?

python - 在 Python 中执行 *nix 二进制文件

python - 从两个可变长度字符串数组返回相似度矩阵(scipy 选项?)

c++ - GDB 在 ROS 中调试 OpenCV

Python 和 OpenCV - 在某些点获取视频的持续时间