Python 将CV2 numpy阵列转换为QImage时如何配置颜色?

Python 将CV2 numpy阵列转换为QImage时如何配置颜色?,python,qt,opencv,numpy,pyqt,Python,Qt,Opencv,Numpy,Pyqt,该程序基于pyqt和opencv。我计划在使用QT作为GUI的同时,使用opencv读取和处理图像 当我打开一个灰色图像时,结果是正常的。但是当打开彩色图像时,它会改变图像的颜色。我猜这是因为我在将numpy数组转换为OImage类型时犯了错误,但我不知道如何更正它 # -*- coding: utf-8 -*- from PyQt4.QtGui import * from PyQt4.QtCore import * import sys import cv2 from cv2 import

该程序基于pyqt和opencv。我计划在使用QT作为GUI的同时,使用opencv读取和处理图像

当我打开一个灰色图像时,结果是正常的。但是当打开彩色图像时,它会改变图像的颜色。我猜这是因为我在将numpy数组转换为OImage类型时犯了错误,但我不知道如何更正它

# -*- coding: utf-8 -*-
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys

import cv2
from cv2 import cv

QTextCodec.setCodecForTr(QTextCodec.codecForName("utf8"))

class MainWindow(QMainWindow):
    def __init__(self,parent=None):
        super(MainWindow,self).__init__(parent)
        self.setWindowTitle("Show Image with pyqt")

        self.imageLabel=QLabel()
        self.imageLabel.setSizePolicy(QSizePolicy.Ignored,QSizePolicy.Ignored)
        self.setCentralWidget(self.imageLabel)

        self.image=QImage()

        self.createActions()
        self.createMenus()

        self.cv_img = None

    def createActions(self):
        self.fileOpenAction=QAction(QIcon(":/fileopen.png"),self.tr("Open"),self)
        self.fileOpenAction.setShortcut("Ctrl+O")
        self.fileOpenAction.setStatusTip(self.tr("Open the file"))
        self.connect(self.fileOpenAction,SIGNAL("triggered()"),self.slotOpenFile)

        self.exitAction=QAction(QIcon(":/filequit.png"),self.tr("Quit"),self)
        self.exitAction.setShortcut("Ctrl+Q")
        self.setStatusTip(self.tr("Quit"))
        self.connect(self.exitAction,SIGNAL("triggered()"),self.close)

    def createMenus(self):
        fileMenu=self.menuBar().addMenu(self.tr("File"))
        fileMenu.addAction(self.fileOpenAction)
        fileMenu.addAction(self.exitAction)


    def slotOpenFile(self):
        fileName=QFileDialog.getOpenFileName(self,self.tr("Open a file"),\
            ".",self.tr("Image File(*.png *.jpg *.jpeg *.bmp)"))

        cvfilename=fileName.toLocal8Bit().data()

        if fileName.isEmpty()==False:

            cvfilename=fileName.toLocal8Bit().data() #convert Qstring to char*
            self.cv_img = cv2.imread(cvfilename) #read image with opencv
            cv2.imshow("Show Image with Opencv",self.cv_img) #show image with opencv, this is the right result

            self.image =QImage(self.cv_img.tostring(),\
                self.cv_img.shape[0],self.cv_img.shape[1],QImage.Format_RGB888) #convert numpy array to QImage
            self.imageLabel.setPixmap(QPixmap.fromImage(self.image))
            self.resize(self.image.width(),self.image.height())

app=QApplication(sys.argv)
main=MainWindow()
main.show()
app.exec_()

您需要将图像数据从BGR转换为RGB。您还需要交换宽度和高度(见下文)——您的代码仅适用于具有相同宽度和高度的图像

self.cv_img = cv2.imread(cvfilename)

if self.cv_img != None:
    # Notice the dimensions.
    height, width, bytesPerComponent = cv_img.shape
    bytesPerLine = bytesPerComponent * width;

    cv2.imshow("Show Image with Opencv", self.cv_img)

    # Convert to RGB for QImage.
    cv2.cvtColor(self.cv_img, cv.CV_BGR2RGB, self.cv_img)

    self.image = QImage(self.cv_img.data, width, height, bytesPerLine, QImage.Format_RGB888)

我知道这是一个非常古老的线程,但对我来说cv2.cvtColor工作非常慢。 我发现了另一个功能,可以帮助那些未来需要帮助的人(我花了很长时间才找到这个补救方法):


希望您会发现它很有用

下面的代码为openCV3.x版本更新了颜色过滤器。代码示例来自

导入cv2
从cv2导入CV
......
self.cv_img=cv2.imread(cvfilename)
如果self.cv\u img!=无:
#注意尺寸。
高度、宽度、bytesPerComponent=cv_img.shape
bytesPerLine=bytesPerComponent*宽度;
imshow(“使用Opencv显示图像”,self.cv\u img)
#转换为QImage的RGB。
cv2.cvt颜色(self.cv\u img、cv2.COlOR\u BGR2RGB、self.cv\u img)
self.image=QImage(self.cv_img.data、宽度、高度、字节行、QImage.Format_RGB888)

按照您的建议进行修改后,代码可以很好地显示PNG和BMP图像,但对JPG图像有一种偏移。你知道为什么吗?谢谢。好的,修好了。您需要bytesPerLine值(我想它总是3)。对不起,但是使用“self.image=QImage(self.cv\u img.data,width,height,3,QImage.Format\u RGB888)”时,它将不再显示图像:(哦,对不起,基本上应该是“bytesPerLine=3*width”(固定)。
def cv2_to_qimage(cv_img):

    height, width, bytesPerComponent = cv_img.shape
    bgra = np.zeros([height, width, 4], dtype=np.uint8)
    bgra[:, :, 0:3] = cv_img
    return QtGui.QImage(bgra.data, width, height, QtGui.QImage.Format_RGB32)
import cv2
from cv2 import CV

...<snippet>...

self.cv_img = cv2.imread(cvfilename)

if self.cv_img != None:
    # Notice the dimensions.
    height, width, bytesPerComponent = cv_img.shape
    bytesPerLine = bytesPerComponent * width;

    cv2.imshow("Show Image with Opencv", self.cv_img)

    # Convert to RGB for QImage.
    cv2.cvtColor(self.cv_img, cv2.COlOR_BGR2RGB, self.cv_img)

    self.image = QImage(self.cv_img.data, width, height, bytesPerLine, QImage.Format_RGB888)