Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. setPixmap behaves strange

setPixmap behaves strange

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 413 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.
  • H Offline
    H Offline
    hernancrespo89
    wrote on last edited by hernancrespo89
    #1

    Hi everyoe I am new with Qt,
    I have been trying to show this image on GUI image. I check that wheter that image is valid or not using cv2.imshow and it is valid result
    But when i try to show image on GUI, it seems strange as you see.

    Note: Size of image is not always constat but its width is around 60 and its height is around 160.

    here is my code:

    import cv2
    import numpy as np
    
    from PyQt5.QtCore import QThread, QTimer
    from PyQt5.QtWidgets import QLabel, QWidget, QPushButton, QVBoxLayout, QApplication, QHBoxLayout, QMessageBox,  QMainWindow
    from PyQt5.QtGui import QPixmap, QImage
    
    class Camera:
    
        def __init__(self, cam_num):
            self.cap = cv2.VideoCapture(cam_num)
            self.cam_num = cam_num
    
        def open(self, width=640, height=480, fps=30):
            self.cap.set(3, width)  # set width 
            self.cap.set(4, height)  # set height
            return self.cap.isOpened()
    
        def read(self):
            roi2 = np.zeros((480, 640, 3), dtype="uint8")
            image2 = np.zeros((170, 70, 3), dtype="uint8")
            image = cv2.imread('image.jpg')
            cv2.imshow('image', image)
            image2[0:image.shape[0], 0: image.shape[1]] = image[0: image.shape[0], 0:image.shape[1]]  # PROBLEM IS HERE
            # image.shape = 159,66,3
            roi2[250:250 + image.shape[0] - 62, 50:50 + image.shape[1]] = image[0: image.shape[0] - 62, 0:image.shape[1]]
    
            return roi2, image2
    
        def close_camera(self):
            self.cap.release()
    
    
    class UI_Window(QWidget):
    
        def __init__(self, cam_num):
            super().__init__()
            self.cam_num = cam_num
            self.cam_num.open()
            # Create a timer.
            self.timer = QTimer()
            self.timer.timeout.connect(self.nextFrameSlot)
            self.timer.start(1000 / 24)
            # Create a layout.
            layout = QHBoxLayout()
            # Add a button
            button_layout = QHBoxLayout()
            btnCamera = QPushButton("Find Defects and Plot Histogram")
            #btnCamera.clicked.connect(self.cam_num.findcnt)
            button_layout.addWidget(btnCamera)
            layout.addLayout(button_layout)
            button2_layout = QHBoxLayout()
            btn2 = QPushButton("Acquire Frame")
            btn2.clicked.connect(self.take_photo)
            button2_layout.addWidget(btn2)
            layout.addLayout(button2_layout)
            self.label = QLabel()
            self.label.setFixedSize(640, 640)
            layout.addWidget(self.label)
            self.label2 = QLabel()
            self.label2.setFixedSize(640, 640)
            self.label2.setGeometry(0, 50, 200, 200)
    
            layout.addWidget(self.label2)
            self.setLayout(layout)
            self.setWindowTitle("First GUI with QT")
    
        def nextFrameSlot(self):
    
            frame,frame2 = self.cam_num.read()
    
            if frame is not None:
                image = QImage(frame, frame.shape[1], frame.shape[0], QImage.Format_RGB888) #	The image is stored using a 24-bit RGB format (8-8-8).
                self.pixmap = QPixmap.fromImage(image)
    
            if frame2 is not None:
                image2 = QImage(frame2, frame2.shape[1], frame2.shape[0], QImage.Format_RGB888) #	The image is stored using a 24-bit RGB format (8-8-8).
                self.pixmap2 = QPixmap.fromImage(image2)
    
        def take_photo(self):
            self.label.setPixmap(self.pixmap)
            self.label2.setPixmap(self.pixmap2)
    
    
    if __name__ == '__main__': 
    
        camera = Camera(1)  #
    
        app = QApplication([])
        start_window = UI_Window(camera)
        start_window.show()
    
        app.exit(app.exec_()) 
        camera.close_camera()
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #4

      QLabel can adapt itself on the size of the image.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      H 1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi,

        Why are you setting such fixed size if you know your images are going to be dynamic ?

        As for your "problem is here line", what exactly is image.jpg ? What size is it ? Because you already have a problem there since you might be trying to fit an image of a different size in a numpy array.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        H 1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          Why are you setting such fixed size if you know your images are going to be dynamic ?

          As for your "problem is here line", what exactly is image.jpg ? What size is it ? Because you already have a problem there since you might be trying to fit an image of a different size in a numpy array.

          H Offline
          H Offline
          hernancrespo89
          wrote on last edited by
          #3

          @SGaist Thanks for reply. I set fixed size as maximum size of image .
          Shape of image.jpg is 640x480x3. So I set size of label as 640x640.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #4

            QLabel can adapt itself on the size of the image.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            H 1 Reply Last reply
            0
            • SGaistS SGaist

              QLabel can adapt itself on the size of the image.

              H Offline
              H Offline
              hernancrespo89
              wrote on last edited by
              #5

              @SGaist
              I didn't know that. Thanks a lot

              1 Reply Last reply
              0

              • Login

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