将CV2 numpy数组转换为QImage时如何配置颜色? [英] How to configure color when convert CV2 numpy array to QImage?

查看:614
本文介绍了将CV2 numpy数组转换为QImage时如何配置颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The program is based on pyqt and opencv. I plan to read and process image with opencv while using QT as GUI.

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

when I open a gray image, The result is OK. But it will change the color of image when opening a color image. I guess it's because I made mistake when I convert numpy array to OImage type, but I can't figure out how to correct it.

# -*- 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.您还需要交换宽度和高度(请参见下文)-您的代码仅适用于宽度和高度相同的图像.

You need to convert the image data from BGR to RGB. You also need to swap width and height (see below) -- your code only works for images with same width and height.

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 numpy数组转换为QImage时如何配置颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆