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. How to wait for an image to be drawn in Qt / Which signal to connect to?
Forum Updated to NodeBB v4.3 + New Features

How to wait for an image to be drawn in Qt / Which signal to connect to?

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 3 Posters 1.2k Views 3 Watching
  • 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.
  • D Offline
    D Offline
    Dirk B.
    wrote on last edited by
    #1

    For a lab application I am required to project images using a projector and recording the projected images with a camera afterwards. In this case it is important to be sure that the frame is projected before we record with the camera. However, as we have to do this for a large number of images we don't want to have a long delay between subsequent frames. How do I create a function that draws an image and only returns once the frame has been drawn onto the screen?

    I have a python example code shown below.

    import PyQt5
    from PyQt5 import QtGui, QtCore, QtWidgets
    import numpy as np
    
    class ShowLiveImage(QtWidgets.QWidget):   
    def __init__(self):
        """ Create a tiny window in which a frame can be displayed using
            show_image.
        """
        super().__init__()
        self.imageLabel = QtWidgets.QLabel()
        self.layout = QtWidgets.QVBoxLayout()
        self.setLayout(self.layout)
        self.layout.addWidget(self.imageLabel)
        self.window().setMinimumSize(512,512)
        self.win = self.window()
        self.show()
    
    def show_image(self, image_array, wait_for_drawing=True):
        """
        Shows an image given as a numpy array and (should) wait for the frame to be drawn onto the monitor before returning.
        
        """
        N = image_array.shape[0]
        self.im = QtGui.QImage(image_array, N, N, QtGui.QImage.Format_Grayscale8)
        qpm = QtGui.QPixmap(self.im)
        self.imageLabel.setPixmap(qpm)
        if wait_for_drawing:
            # These do not help, what do I do here?
            self.imageLabel.repaint()
            QtGui.QGuiApplication.postEvent(self, QtCore.QEvent(QtCore.QEvent.UpdateRequest))
        QtGui.QGuiApplication.processEvents()
    

    However, when I keep updating images using this code, the frame rate is way too high, around 400 fps on my computer, which I tested in the following way:

    from PyQt5.Qt import QApplication
    import time
    app = QApplication([])
    viewer = ShowLiveImage()
    
    t0 = time.time()
    for i in range(1000):
        random_image = np.random.randint(0,255,(512,512), np.uint8)
        viewer.show_image(random_image)
    t1 = time.time()
    print(' FPS: ', 1000./(t1-t0))
    

    How to I make sure that show_image only returns when the frame has been projected?

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

      Hi and welcome to devnet,

      For a lab ? Are you asking for help of an exercice of your scholar cursus ?

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

      D 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi and welcome to devnet,

        For a lab ? Are you asking for help of an exercice of your scholar cursus ?

        D Offline
        D Offline
        Dirk B.
        wrote on last edited by
        #3

        @SGaist No, it's not a course, this is for an actual research lab. The device I'm controlling is called a spatial light modulator, and it acts as a second screen.

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

          That's the kind of details that is good to provide directly :)

          Can you give a bit more details about the hardware you have at your disposition to do that task ?

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

          D 1 Reply Last reply
          0
          • SGaistS SGaist

            That's the kind of details that is good to provide directly :)

            Can you give a bit more details about the hardware you have at your disposition to do that task ?

            D Offline
            D Offline
            Dirk B.
            wrote on last edited by
            #5

            @SGaist I'm not sure which specs you require exactly, but this is a typical device:
            https://www.meadowlark.com/small-512-512-spatial-light-modulator-p-139?mid=18#.XC1BBhAo-is

            The driver installs the device as an additional screen with a update rate of 204 Hz at 512x512 pixels. The PC I'm using is a high-end windows machine of a few years ago, with an NVIDIA GTX 780 graphics card, and an i7 intel processor, I'm not sure about the exact specs. I'm developing on my (linux mint) laptop. The patterns that I require can be generated beforehand so generating the images should not be an issue.

            Any other specs you require?

            1 Reply Last reply
            0
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi
              I was wondering when you say

              • recording the projected images with a camera afterwards

              Does it means it will take still images or record as video ?
              Also, how fast can the camera record ?

              D 1 Reply Last reply
              0
              • mrjjM mrjj

                Hi
                I was wondering when you say

                • recording the projected images with a camera afterwards

                Does it means it will take still images or record as video ?
                Also, how fast can the camera record ?

                D Offline
                D Offline
                Dirk B.
                wrote on last edited by Dirk B.
                #7

                @mrjj the camera supports up to 400 fps, but ideally I will use 205 fps to match the slm. I'm recording still images at 200 hz which is working fine art the moment.

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

                  So you would need to have your recorder synced on the VSync signal of the video card ?

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

                  D 1 Reply Last reply
                  1
                  • D Offline
                    D Offline
                    Dirk B.
                    wrote on last edited by
                    #9

                    Yes, I think that's it!

                    1 Reply Last reply
                    0
                    • SGaistS SGaist

                      So you would need to have your recorder synced on the VSync signal of the video card ?

                      D Offline
                      D Offline
                      Dirk B.
                      wrote on last edited by
                      #10

                      @SGaist any ideas how to do that exactly? I thought the repaint would perform that job.

                      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