python - 在 PyQt5 中使用 cv2.imshow 和 drawpixmap 的颜色图是不同的
问题描述
目前,我需要以伪彩色显示我的灰色图像。我使用 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()
结果是:
然后,我使用相同的颜色图生成伪彩色图像,并使用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_())
而且,结果是:
为什么当我显示相同的图像时颜色图不同?
因此,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_())
推荐阅读
- java - 运行我的程序时导致服务器断开连接的原因是什么?
- reactjs - 通过 PayPal 余额付款选项在 Sandbox 帐户中不可用
- java - 捕获 IIOException 的问题已经捕获
- vba - PowerPoint 2019 for Windows 宏错误:编译错误:标签未定义
- pointers - 当我想使用 llvm api 将数组 ref 传递给函数时该怎么办?
- r - R xaringan 构造页脚以自动包含标题/作者
- javascript - 有没有办法使用 HTML 和 CSS 在 SVG 路径上制作滚动选取框?
- python - 如何将模型最后一层的输出添加到我创建的附加数组中?
- javascript - 在 Firebase 错误上使用电话号码进行身份验证:“无法加载外部 reCAPTCHA 依赖项!”
- git - 如何恢复以前的文件和更改