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. QImage to Qpixmap conversion is too slow

QImage to Qpixmap conversion is too slow

Scheduled Pinned Locked Moved Unsolved General and Desktop
21 Posts 4 Posters 5.6k 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.
  • L Offline
    L Offline
    lansing
    wrote on last edited by
    #1

    I want to display video and show its frames to the gui. I have a processor that pumps out packed RGB data, and to display them, I have a QImage that took in the data and convert them to QPixmap for display. However the QImage to QPixmap conversion is too slow for real time play speed. It played fine when the videos are around 1080p, but with 4K video, the fps just plummeted.

    QImage frame_image(data, width, height, QImage::Format_ARGB32);
    QPixmap frame_pixmap = QPixmap::fromImage(frame_image);
    

    What is a faster way to do this?

    Christian EhrlicherC 1 Reply Last reply
    0
    • L lansing

      I want to display video and show its frames to the gui. I have a processor that pumps out packed RGB data, and to display them, I have a QImage that took in the data and convert them to QPixmap for display. However the QImage to QPixmap conversion is too slow for real time play speed. It played fine when the videos are around 1080p, but with 4K video, the fps just plummeted.

      QImage frame_image(data, width, height, QImage::Format_ARGB32);
      QPixmap frame_pixmap = QPixmap::fromImage(frame_image);
      

      What is a faster way to do this?

      Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @lansing said in QImage to Qpixmap conversion is too slow:

      What is a faster way to do this?

      Use a proper backend for your task. CPU raster is definitely not made for such kind of work. Use OenGL or similar instead.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      L 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        @lansing said in QImage to Qpixmap conversion is too slow:

        What is a faster way to do this?

        Use a proper backend for your task. CPU raster is definitely not made for such kind of work. Use OenGL or similar instead.

        L Offline
        L Offline
        lansing
        wrote on last edited by
        #3

        @Christian-Ehrlicher said in QImage to Qpixmap conversion is too slow:

        @lansing said in QImage to Qpixmap conversion is too slow:

        What is a faster way to do this?

        Use a proper backend for your task. CPU raster is definitely not made for such kind of work. Use OenGL or similar instead.

        Is there any example with OpenGL? Like where do I start?

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

          Hi,

          Can you give more information about how are these images created ? Passed around ?
          Are they from a camera ?
          Are you loading them from a file ?
          Etc.

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

          L 1 Reply Last reply
          1
          • SGaistS SGaist

            Hi,

            Can you give more information about how are these images created ? Passed around ?
            Are they from a camera ?
            Are you loading them from a file ?
            Etc.

            L Offline
            L Offline
            lansing
            wrote on last edited by
            #5

            @SGaist

            They were loaded from a video file, then go into a processor where it will spit out packed RGB data frame by frame.

            I found a stackoverflow answer about passing the QImage into a QOpenGLWidget and then using its paintEvent to draw out the image, is this the way?

            https://stackoverflow.com/questions/20245865/render-qimage-with-opengl

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

              What are you using to read your video file ?
              Depending on that and the image format you could load the data directly into a texture and do the color space conversion in OpenGL rather than on the CPU side.

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

              L 1 Reply Last reply
              2
              • SGaistS SGaist

                What are you using to read your video file ?
                Depending on that and the image format you could load the data directly into a texture and do the color space conversion in OpenGL rather than on the CPU side.

                L Offline
                L Offline
                lansing
                wrote on last edited by
                #7

                @SGaist said in QImage to Qpixmap conversion is too slow:

                What are you using to read your video file ?
                Depending on that and the image format you could load the data directly into a texture and do the color space conversion in OpenGL rather than on the CPU side.

                That can be a future plan, but I want to do it one step at a time. For right now I need to know where to even start. The Qimage to QPixmap conversion is cutting my playback speed in half.

                mrjjM 1 Reply Last reply
                0
                • L lansing

                  @SGaist said in QImage to Qpixmap conversion is too slow:

                  What are you using to read your video file ?
                  Depending on that and the image format you could load the data directly into a texture and do the color space conversion in OpenGL rather than on the CPU side.

                  That can be a future plan, but I want to do it one step at a time. For right now I need to know where to even start. The Qimage to QPixmap conversion is cutting my playback speed in half.

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @lansing
                  Hi
                  well try the OpenGLWidget::paintEvent code and see if its fast enough.
                  At least you don't have the conversion to pixmap

                  L 1 Reply Last reply
                  0
                  • mrjjM mrjj

                    @lansing
                    Hi
                    well try the OpenGLWidget::paintEvent code and see if its fast enough.
                    At least you don't have the conversion to pixmap

                    L Offline
                    L Offline
                    lansing
                    wrote on last edited by lansing
                    #9

                    @mrjj

                    Hi This is what I have done so far:

                    class MyGLPainter : public QOpenGLWidget
                    {
                        Q_OBJECT
                    public:
                        MyGLPainter (QWidget *parent = nullptr);
                        void paintEvent(QPaintEvent *) override;
                        void drawImage(const QImage &image); 
                    
                    private:
                        QImage m_frameImage;
                    
                    MyGLPainter::MyGLPainter (QWidget *parent): QOpenGLWidget(parent)  
                    {
                    }
                    
                    void MyGLPainter::paintEvent(QPaintEvent *)
                    {
                        QPainter painter(this);   
                        painter.drawImage(m_frameImage.width(),m_frameImage.height(), m_frameImage);
                        painter.end();
                    }
                    
                    void MyGLPainter::drawImage(const QImage &image)
                    {
                        m_frameImage = image;
                        update();
                    }
                    

                    But when I pass the image to drawImage(), the program will just crash. Am I doing this correctly?

                    mrjjM 1 Reply Last reply
                    0
                    • L lansing

                      @mrjj

                      Hi This is what I have done so far:

                      class MyGLPainter : public QOpenGLWidget
                      {
                          Q_OBJECT
                      public:
                          MyGLPainter (QWidget *parent = nullptr);
                          void paintEvent(QPaintEvent *) override;
                          void drawImage(const QImage &image); 
                      
                      private:
                          QImage m_frameImage;
                      
                      MyGLPainter::MyGLPainter (QWidget *parent): QOpenGLWidget(parent)  
                      {
                      }
                      
                      void MyGLPainter::paintEvent(QPaintEvent *)
                      {
                          QPainter painter(this);   
                          painter.drawImage(m_frameImage.width(),m_frameImage.height(), m_frameImage);
                          painter.end();
                      }
                      
                      void MyGLPainter::drawImage(const QImage &image)
                      {
                          m_frameImage = image;
                          update();
                      }
                      

                      But when I pass the image to drawImage(), the program will just crash. Am I doing this correctly?

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @lansing
                      Hi
                      It looks ok.
                      In what lines does it crash ?

                      L 1 Reply Last reply
                      0
                      • mrjjM mrjj

                        @lansing
                        Hi
                        It looks ok.
                        In what lines does it crash ?

                        L Offline
                        L Offline
                        lansing
                        wrote on last edited by lansing
                        #11

                        @mrjj

                        At the line update(). It points to function QImage::operator=

                        1 Reply Last reply
                        0
                        • Christian EhrlicherC Online
                          Christian EhrlicherC Online
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          Do you update your image from a different thread? If so you should learn about threading: Threading Basics

                          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                          Visit the Qt Academy at https://academy.qt.io/catalog

                          L 1 Reply Last reply
                          3
                          • Christian EhrlicherC Christian Ehrlicher

                            Do you update your image from a different thread? If so you should learn about threading: Threading Basics

                            L Offline
                            L Offline
                            lansing
                            wrote on last edited by
                            #13

                            @Christian-Ehrlicher

                            HI, is this related to the crash?

                            mrjjM 1 Reply Last reply
                            0
                            • L lansing

                              @Christian-Ehrlicher

                              HI, is this related to the crash?

                              mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              @lansing

                              Hi
                              yes if you do it from another thread then very much yes.

                              L 1 Reply Last reply
                              0
                              • mrjjM mrjj

                                @lansing

                                Hi
                                yes if you do it from another thread then very much yes.

                                L Offline
                                L Offline
                                lansing
                                wrote on last edited by
                                #15

                                @mrjj

                                I don't have threading in the program.

                                mrjjM 1 Reply Last reply
                                0
                                • L lansing

                                  @mrjj

                                  I don't have threading in the program.

                                  mrjjM Offline
                                  mrjjM Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16

                                  @lansing
                                  Hi
                                  then something is wrong with the image then.
                                  Else why crash on copy.

                                  can you try with Image that you are 100% sure is valid ?

                                  L 1 Reply Last reply
                                  0
                                  • mrjjM mrjj

                                    @lansing
                                    Hi
                                    then something is wrong with the image then.
                                    Else why crash on copy.

                                    can you try with Image that you are 100% sure is valid ?

                                    L Offline
                                    L Offline
                                    lansing
                                    wrote on last edited by lansing
                                    #17

                                    @mrjj

                                    Hi, I have set the image to one in my resource folder for testing, now it has crashed at line m_frameImage = image. I have changed the image argument from const QImage & to QImage*, I don't know if that have a effect on it. But in debug, it did show my image dimension.

                                    QImage *img= new QImage(":/cross.png");
                                    myGLPainter->drawImage(img);
                                    
                                    mrjjM Christian EhrlicherC SGaistS 3 Replies Last reply
                                    0
                                    • L lansing

                                      @mrjj

                                      Hi, I have set the image to one in my resource folder for testing, now it has crashed at line m_frameImage = image. I have changed the image argument from const QImage & to QImage*, I don't know if that have a effect on it. But in debug, it did show my image dimension.

                                      QImage *img= new QImage(":/cross.png");
                                      myGLPainter->drawImage(img);
                                      
                                      mrjjM Offline
                                      mrjjM Offline
                                      mrjj
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #18

                                      @lansing
                                      and it does still crash ?

                                      1 Reply Last reply
                                      0
                                      • L Offline
                                        L Offline
                                        lansing
                                        wrote on last edited by
                                        #19

                                        Yes, it said

                                        error: Debugger encountered an exception: Exception at 0x7ff71b7e9b88, code: 0xc0000005: read access violation at: 0xffffffffffffffff, flags=0x0
                                        
                                        1 Reply Last reply
                                        0
                                        • L lansing

                                          @mrjj

                                          Hi, I have set the image to one in my resource folder for testing, now it has crashed at line m_frameImage = image. I have changed the image argument from const QImage & to QImage*, I don't know if that have a effect on it. But in debug, it did show my image dimension.

                                          QImage *img= new QImage(":/cross.png");
                                          myGLPainter->drawImage(img);
                                          
                                          Christian EhrlicherC Online
                                          Christian EhrlicherC Online
                                          Christian Ehrlicher
                                          Lifetime Qt Champion
                                          wrote on last edited by
                                          #20

                                          @lansing said in QImage to Qpixmap conversion is too slow:

                                          myGLPainter

                                          Where do you initialize this variable?

                                          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                          Visit the Qt Academy at https://academy.qt.io/catalog

                                          1 Reply Last reply
                                          1

                                          • Login

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