如何使用Opencv在Pyqt中显示图像

时间:2019-06-22 12:57:54

标签: python-3.x opencv pyqt5

我正在尝试创建一个显示图像的GUI。我不知道如何在Pyqt标签中显示图像。

这是我的OpenCV简单代码:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from dialog import Ui_MainWindow
import cv2 as cv2


class MyApp(QMainWindow):
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.filename = 'lin-manuel-miranda.png'
        self.setup_ui()
        # Events
        self.ui.pushButton.clicked.connect(self.insertText)

    def insertText(self):
        t = self.ui.label.text()
        self.ui.lineEdit.setText(t)

    def setup_ui(self):
        img = cv2.imread(self.filename)
        self.ui.image = QLabel()
        if img is None:
            self.image_label.setText("Cannot load the input image.")
        else:
            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            img_ = QImage(img.data, img.shape[1], img.shape[0], QImage.Format_RGB888)
            self.ui.image.setPixmap(QPixmap.fromImage(img_))


if __name__ == '__main__':
    app = QApplication(sys.argv)
    myapp = MyApp()
    myapp.show()
    app.exec_()

我正在使用Opencv 4.1.0版和python 3.7.3版

0 个答案:

没有答案