1. <i id='LX3ku'><tr id='LX3ku'><dt id='LX3ku'><q id='LX3ku'><span id='LX3ku'><b id='LX3ku'><form id='LX3ku'><ins id='LX3ku'></ins><ul id='LX3ku'></ul><sub id='LX3ku'></sub></form><legend id='LX3ku'></legend><bdo id='LX3ku'><pre id='LX3ku'><center id='LX3ku'></center></pre></bdo></b><th id='LX3ku'></th></span></q></dt></tr></i><div id='LX3ku'><tfoot id='LX3ku'></tfoot><dl id='LX3ku'><fieldset id='LX3ku'></fieldset></dl></div>
      <bdo id='LX3ku'></bdo><ul id='LX3ku'></ul>
  2. <tfoot id='LX3ku'></tfoot>

    <small id='LX3ku'></small><noframes id='LX3ku'>

  3. <legend id='LX3ku'><style id='LX3ku'><dir id='LX3ku'><q id='LX3ku'></q></dir></style></legend>

    1. 如何使用 opencv 向 PYQT 显示图像

      时间:2023-08-05
        <tbody id='K1LjF'></tbody>

      • <bdo id='K1LjF'></bdo><ul id='K1LjF'></ul>

        <legend id='K1LjF'><style id='K1LjF'><dir id='K1LjF'><q id='K1LjF'></q></dir></style></legend>
        <i id='K1LjF'><tr id='K1LjF'><dt id='K1LjF'><q id='K1LjF'><span id='K1LjF'><b id='K1LjF'><form id='K1LjF'><ins id='K1LjF'></ins><ul id='K1LjF'></ul><sub id='K1LjF'></sub></form><legend id='K1LjF'></legend><bdo id='K1LjF'><pre id='K1LjF'><center id='K1LjF'></center></pre></bdo></b><th id='K1LjF'></th></span></q></dt></tr></i><div id='K1LjF'><tfoot id='K1LjF'></tfoot><dl id='K1LjF'><fieldset id='K1LjF'></fieldset></dl></div>

            1. <small id='K1LjF'></small><noframes id='K1LjF'>

              <tfoot id='K1LjF'></tfoot>

                本文介绍了如何使用 opencv 向 PYQT 显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                您好,我是 python 和 opencv 的新手

                我想问,如何在 qlabel (pyqt) 中显示我的图像,我想将 qlabel 转换为灰度.

                I want to ask, how to show my image in qlabel (pyqt) and i want to convert qlabel to grayscale.

                import sys
                from PyQt5.uic import loadUi
                from PyQt5.QtWidgets import QMainWindow, QApplication, QFileDialog
                import cv2
                from PyQt5 import QtCore, QtGui, QtWidgets
                from PyQt5.QtGui import *
                from PyQt5.QtCore import *
                
                class UIProgram(QMainWindow):
                def __init__(self):
                    super(UIProgram,self).__init__()
                    loadUi("Backpro2.ui",self)
                    #self.image=None
                    self.trainLoadImgBtn.clicked.connect(self.loadClicked)
                    self.image = QImage()
                @pyqtSlot()
                def loadClicked(self):
                    fname,filter=QFileDialog.getOpenFileName(self,'Open File','D:\',"Image Files(*.jpg)")
                    if fname:
                        self.loadImage(fname)
                    else:
                        print('invalid image')
                
                def loadImage(self,fname):
                    self.image=cv2.imread(fname,cv2.IMREAD_COLOR)
                    self.displayImage()
                
                def displayImage(self):
                    qformat =QImage.Format_Indexed8
                
                    if len(self.image.shape)==3:
                        if(self.image.shape[2])==4:
                            qformat=QImage.Format_RGBA8888
                        else:
                            qformat=QImage.Format_RGB888
                        img=QtGui.QImage(self.image.data,self.image.shape[1],self.image[0],QtGui.QImage.Format_RGB888)
                
                        img = img.rgbSwapped()
                        self.trainOpenImg.setPixmap(QPixmap.fromImage(img))
                        self.trainOpenImgn.setAlignment(QtCore.Qt.AlignHCenter|QtCore.Qt.AlignVCenter)
                
                
                
                if __name__ == "__main__":
                
                app=QApplication(sys.argv)
                window=UIProgram()
                window.setWindowTitle('Test')
                window.show()
                sys.exit(app.exec_())
                

                当我点击加载图片按钮时它崩溃了,图片无法在 qlabel 中显示还有这个错误

                it crashed when i click load image button, the image can't show in qlabel and this error

                进程以退出代码 -1073740791 (0xC0000409) 结束

                Process finished with exit code -1073740791 (0xC0000409)

                推荐答案

                有2个错误:

                • 您必须将 bytesPerLine 传递给 QImage
                • 设置对齐方式时,trainOpenImg 中有n"个更多.
                • You have to pass the bytesPerLine to the QImage
                • You have an "n" of more in trainOpenImg when you set the alignment.
                def displayImage(self):
                    qformat =QImage.Format_Indexed8
                    if len(self.image.shape)==3:
                        if self.image.shape[2] ==4:
                            qformat=QImage.Format_RGBA8888
                        else:
                            qformat=QImage.Format_RGB888
                        img = QtGui.QImage(self.image.data,
                            self.image.shape[1],
                            self.image.shape[0], 
                            self.image.strides[0], # <--- +++
                            qformat)
                        img = img.rgbSwapped()
                        self.trainOpenImg.setPixmap(QPixmap.fromImage(img))
                        self.trainOpenImg.setAlignment(QtCore.Qt.AlignCenter)
                

                另一方面,IDE 在处理某些类型的错误时会遇到问题,并且只能启动代码,因此在这些情况下,建议在 CMD 或终端中运行它,因为它们会为您提供更多信息,例如在这种情况下,错误消息是:

                On the other hand IDEs have problems handling certain types of errors and only launch a code, so in those cases it is advisable to run it in the CMD or terminal as they give you more information, for example in this case the error message was :

                Traceback (most recent call last):
                  File "test.py", line 20, in loadClicked
                    self.loadImage(fname)
                  File "test.py", line 26, in loadImage
                    self.displayImage()
                  File "test.py", line 36, in displayImage
                    img=QtGui.QImage(self.image.data,self.image.shape[1],self.image[0],QtGui.QImage.Format_RGB888)
                TypeError: arguments did not match any overloaded call:
                  QImage(): too many arguments
                  QImage(QSize, QImage.Format): argument 1 has unexpected type 'memoryview'
                  QImage(int, int, QImage.Format): argument 1 has unexpected type 'memoryview'
                  QImage(bytes, int, int, QImage.Format): argument 3 has unexpected type 'numpy.ndarray'
                  QImage(sip.voidptr, int, int, QImage.Format): argument 3 has unexpected type 'numpy.ndarray'
                  QImage(bytes, int, int, int, QImage.Format): argument 3 has unexpected type 'numpy.ndarray'
                  QImage(sip.voidptr, int, int, int, QImage.Format): argument 3 has unexpected type 'numpy.ndarray'
                  QImage(List[str]): argument 1 has unexpected type 'memoryview'
                  QImage(str, format: str = None): argument 1 has unexpected type 'memoryview'
                  QImage(QImage): argument 1 has unexpected type 'memoryview'
                  QImage(Any): too many arguments
                Aborted (core dumped)
                

                这篇关于如何使用 opencv 向 PYQT 显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:Qt Designer和PyQt程序中verticalLayout的大小不同 下一篇:使用 pdf 路径更新 QWebEngineView

                相关文章

                最新文章

                  <tfoot id='RrfBV'></tfoot>

                    • <bdo id='RrfBV'></bdo><ul id='RrfBV'></ul>
                  1. <small id='RrfBV'></small><noframes id='RrfBV'>

                  2. <legend id='RrfBV'><style id='RrfBV'><dir id='RrfBV'><q id='RrfBV'></q></dir></style></legend>
                    <i id='RrfBV'><tr id='RrfBV'><dt id='RrfBV'><q id='RrfBV'><span id='RrfBV'><b id='RrfBV'><form id='RrfBV'><ins id='RrfBV'></ins><ul id='RrfBV'></ul><sub id='RrfBV'></sub></form><legend id='RrfBV'></legend><bdo id='RrfBV'><pre id='RrfBV'><center id='RrfBV'></center></pre></bdo></b><th id='RrfBV'></th></span></q></dt></tr></i><div id='RrfBV'><tfoot id='RrfBV'></tfoot><dl id='RrfBV'><fieldset id='RrfBV'></fieldset></dl></div>