Qiang Zhang
Qiang Zhang

Reputation: 952

the colormap is different using cv2.imshow and drawpixmap in PyQt5

Currently, I need to show my gray image in pseudo color. I use the opencv2 to generate a pseudo colormap. However, I find the colormap showed in cv2 and QLabel is different.

For cv2, the code is:

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()

And the result is:

enter image description here

Then, I use the same colormap to generate the pseudo color image, and display using drawpixmal, the code is:

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_())

And, the result is:

enter image description here

Why the colormap is different when I display the same image?

In addition, the cv2.COLORMAP_AUTUMN should be: enter image description here

So, the image showed by cv2 is correct, and python drawpixmap give a wrong display.

How to fix it?

Upvotes: 2

Views: 1076

Answers (1)

Qiang Zhang
Qiang Zhang

Reputation: 952

Thanks to Micka, opencv use BGR, while qt use RGB for rendering. So, the correct code should be:

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_())

Upvotes: 2

Related Questions