Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved Using a label to display large images in QT GUI

    General and Desktop
    opencv qlabel image display opengl pixmap
    3
    5
    1197
    Loading More Posts
    • 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.
    • R
      rtavakko last edited by rtavakko

      Hi guys,

      My application does live processing of images using OpenCV and shows the result on an output display / monitor. I'm using a 'label' for this, first converting my OpenCV matrix to a QImage and then I use that QImage to create a pixmap for the label. It works ok for small labels but if the label is covering the entire size of say a 1920 x 1080 display, setting the pixmap slows everything down by a factor of 6 which is inhumane (it goes from processing time of ~10 mS to ~60 mS).

      Can anyone else confirm that this is the case?

      Is there a faster way of doing this using another type of widget perhaps? OpenGL?

      This is what I'm doing at the moment (videoFrameOut is my OpenCV matrix):

      image = QImage((uchar*)(videoFrameOut.data),videoFrameOut.cols,videoFrameOut.rows, QImage::Format_RGBA8888);
      
      ui -> oLabel -> setAlignment(Qt::AlignCenter);
      ui -> oLabel -> setPixmap(QPixmap::fromImage(image.scaled(QSize(ui->oLabel->width(),ui->oLabel->height()), Qt::KeepAspectRatio, Qt::FastTransformation)));
      
      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

        Something is not completely clear, are you always resizing to a smaller version of the image for displaying ?

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

        R 1 Reply Last reply Reply Quote 2
        • Chris Kawa
          Chris Kawa Moderators last edited by Chris Kawa

          The first line is nice because it just creates Qt interface over the foreign data without any copies. Unfortunately the rest of it is not as nice.
          I'd say the performance is killed by the amount of copies and resizing you're doing: image.scaled does scaling and creates new buffer, QPixmap::fromImage does another copy, setPixmap is another (potential) copy and then setting that pixmap on a label causes it to re-layout itself and do all sorts of bookkeeping.

          Without going into hardware acceleration (OpenGL or Vulkan) the low hanging fruit is avoiding all those copies.
          Instead of a label inherit a plain QWidget, override paintEvent and draw the image directly with QPainter::drawImage. It will do the scaling and draw on the fly without all those expensive copies.

          1 Reply Last reply Reply Quote 5
          • R
            rtavakko @SGaist last edited by

            @sgaist I have a few small-sized labels of ~130x130 pixels which display a scaled down image (usually 1280x720 or 1920x1080). The bigger label which covers an entire display is what is causing the performance drop. Not knowing whats going on under the hood I would have imagined that downsizing to the smaller labels would be the bottleneck but its the other way round.

            @Chris-Kawa That makes sense, I'll try that.

            There doesn't seem to be any way to set the pixmap from raw data similar to what I'm doing with the QImage, at least not with a uchar array outside of a file, is that right?

            1 Reply Last reply Reply Quote 0
            • R
              rtavakko last edited by rtavakko

              An update on this. It works perfectly and its a very low-cost solution. My processing time remains almost the same (~10mS) using this method since the painter does the scaling for you when it draws.

              Thanks for your help guys.

              1 Reply Last reply Reply Quote 1
              • First post
                Last post