How to show image to PYQT with opencv

人走茶凉 提交于 2019-12-02 05:59:48

It has 2 errors:

  • 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)

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)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!