Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. QImage does not show image correctly as OpenCV does
Forum Updated to NodeBB v4.3 + New Features

QImage does not show image correctly as OpenCV does

Scheduled Pinned Locked Moved Unsolved Qt for Python
3 Posts 2 Posters 810 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    feiyuhuahuo
    wrote on 16 Dec 2022, 06:07 last edited by
    #1

    I'm use the following code to do test.

    class PP(QMainWindow):
        def __init__(self):
            super().__init__()
            loader = QUiLoader()
            self.main_ui = loader.load('test.ui')
            self.setCentralWidget(self.main_ui)
    
            img = cv2.imread('2222.png', cv2.IMREAD_GRAYSCALE)
            cv2.imshow('aa', img)
            cv2.waitKey()
    
            height, width = img.shape
            print('shape:', img.shape, 'dtype:', img.dtype)
            qimg = QImage(img.astype('uint8').data, width, height, QImage.Format_Grayscale8)
    
            self.main_ui.label.setPixmap(QPixmap(qimg))
            self.show()
    
    
    if __name__ == '__main__':
        app = QApplication()
        ui = PP()
        app.exec()
    

    When the image is '2222.png', the widget shows the same as OpenCV does. Th left window is called by OpenCV.
    屏幕截图 2022-12-16 135317.png
    But if I change '2222.png' to another image, the widget shows weirdly. I want the widget (the right window) to show the same as OpenCV does.
    屏幕截图 2022-12-16 135232.png
    The two image are both greyscale image and are both uint8 dtype. But why is the difference?

    J 1 Reply Last reply 16 Dec 2022, 07:07
    0
    • F feiyuhuahuo
      16 Dec 2022, 06:07

      I'm use the following code to do test.

      class PP(QMainWindow):
          def __init__(self):
              super().__init__()
              loader = QUiLoader()
              self.main_ui = loader.load('test.ui')
              self.setCentralWidget(self.main_ui)
      
              img = cv2.imread('2222.png', cv2.IMREAD_GRAYSCALE)
              cv2.imshow('aa', img)
              cv2.waitKey()
      
              height, width = img.shape
              print('shape:', img.shape, 'dtype:', img.dtype)
              qimg = QImage(img.astype('uint8').data, width, height, QImage.Format_Grayscale8)
      
              self.main_ui.label.setPixmap(QPixmap(qimg))
              self.show()
      
      
      if __name__ == '__main__':
          app = QApplication()
          ui = PP()
          app.exec()
      

      When the image is '2222.png', the widget shows the same as OpenCV does. Th left window is called by OpenCV.
      屏幕截图 2022-12-16 135317.png
      But if I change '2222.png' to another image, the widget shows weirdly. I want the widget (the right window) to show the same as OpenCV does.
      屏幕截图 2022-12-16 135232.png
      The two image are both greyscale image and are both uint8 dtype. But why is the difference?

      J Offline
      J Offline
      J.Hilk
      Moderators
      wrote on 16 Dec 2022, 07:07 last edited by
      #2

      @feiyuhuahuo
      I would say you have some "chunk" data in your file, that is causing an offset in the bytes -> the result is the shifted image. That chunkdata is probably the header. Did you simply change the ending/file extension of your image ?
      Seems like opencv does some error corrections., but my guess is your image is not format conform.


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      F 1 Reply Last reply 19 Dec 2022, 01:43
      0
      • J J.Hilk
        16 Dec 2022, 07:07

        @feiyuhuahuo
        I would say you have some "chunk" data in your file, that is causing an offset in the bytes -> the result is the shifted image. That chunkdata is probably the header. Did you simply change the ending/file extension of your image ?
        Seems like opencv does some error corrections., but my guess is your image is not format conform.

        F Offline
        F Offline
        feiyuhuahuo
        wrote on 19 Dec 2022, 01:43 last edited by
        #3

        @J-Hilk Hi,the two images are saved by OpenCV.Write() function, and I did not simply change the ending/file extension.
        I tried another initialization method of QImage.
        543ef26d-40de-48fb-abe7-d7eb96ef568c-image.png
        I modified the code to:
        qimg = QImage(img.astype('uint8').data, width, height, width*1, QImage.Format_Grayscale8)
        And now the image shows correctly.
        3e25c7ef-1942-4f75-bb54-f2396730cb70-image.png
        Can this exclude that the data of my image is error?

        1 Reply Last reply
        0

        1/3

        16 Dec 2022, 06:07

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved