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. Live camera image using OpenCV showing in Qlabel crashes due to out of memory error

Live camera image using OpenCV showing in Qlabel crashes due to out of memory error

Scheduled Pinned Locked Moved Solved General and Desktop
c++ qtguiopencvqimageqpixmap
9 Posts 5 Posters 2.4k 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.
  • I Offline
    I Offline
    Imran Hassan
    wrote on 13 Aug 2020, 15:00 last edited by
    #1

    Hi all,

    I am using QT Qpixmap to show the image captured from the camera using the OpenCV frame. I am doing following steps

    1. Capture image using OpenCV
    2. Convert the OpenCV image to QImage
    3. Convert QImage to Qpixmap
    4. Show it on Qlabel

    The problem I am facing is the memory start increasing at great speed and after few time the application crashed with out of memory

    I have gone through the code several time to check any object being created again and again.

    Using QT 5 with MinGW 7

    Any help will be appreciated.

    P 1 Reply Last reply 13 Aug 2020, 16:31
    1
    • I Offline
      I Offline
      Imran Hassan
      wrote on 14 Aug 2020, 07:53 last edited by
      #9

      Thank you all guys. Your suggestions and expert opinion helped a lot with solving the problem. QT forum is always so helpful.

      After trying many things here is the conclusion and code is now working perfectly fine

      I removed the tic toc part

      time(&start);
      timer = double(getTickCount());
      tic();
      It was working but crashing then just to make sure that QImage is not NULL I removed the img = QImage(); // releasing memory

      with

      if(!img.isNull())
      img = QImage();
      Its working perfectly fine now.

      1 Reply Last reply
      2
      • I Imran Hassan
        13 Aug 2020, 15:00

        Hi all,

        I am using QT Qpixmap to show the image captured from the camera using the OpenCV frame. I am doing following steps

        1. Capture image using OpenCV
        2. Convert the OpenCV image to QImage
        3. Convert QImage to Qpixmap
        4. Show it on Qlabel

        The problem I am facing is the memory start increasing at great speed and after few time the application crashed with out of memory

        I have gone through the code several time to check any object being created again and again.

        Using QT 5 with MinGW 7

        Any help will be appreciated.

        P Offline
        P Offline
        Pablo J. Rogina
        wrote on 13 Aug 2020, 16:31 last edited by Pablo J. Rogina
        #2

        @Imran-Hassan said in Live camera image using OpenCV showing in Qlabel crashes due to out of memory error:

        The problem I am facing is the memory start increasing at great speed and after few time the application crashed with out of memory

        Not that I'll discover something special, but clearly you're leaking memory...

        You're not stated the OS you're running, but from using MinGW I guess it's some Windows variant. If you could use Linux somehow, you could use Valgrind to help spot those memory issues

        I have gone through the code several time to check any object being created again and again.

        You may want to share some code snippets in any case

        Upvote the answer(s) that helped you solve the issue
        Use "Topic Tools" button to mark your post as Solved
        Add screenshots via postimage.org
        Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        2
        • I Offline
          I Offline
          Imran Hassan
          wrote on 13 Aug 2020, 17:27 last edited by Imran Hassan
          #3

          I am using windows so I can not know where memory is being leaked.

          I am using thread. Here is the code

          void MainWindow::mainfucntion(){
          std::thread producer_t(&MainWindow::RunDefaultCamera,this);

          for(;;){

                  time(&start);
                  timer = double(getTickCount());
                  tic();
          
                  if(!bufferQueue.empty()){
                    lock_guard<std::mutex> lock(fmutex);
                      readFrame = bufferQueue.front();
                      qDebug() << "1 : " << bufferQueue.size();
                      bufferQueue.pop_front();
                      qDebug() << "2 : " << bufferQueue.size();
                  }
                  else{
                      if(keepRunning == true)
                      {
                          if(threadEnable==false)
                          {
                             std::thread producer_t(&MainWindow::RunDefaultCamera,this);
                          }
                          continue;
                      }
                      else{
                          producer_t.join();
                          return -1;
                      }
                  }
          

          // cap >> readFrame;
          cv::resize(readFrame, readFrame, size);

          img = QImage((uchar*) readFrame.data, readFrame.cols, readFrame.rows, readFrame.step, QImage::Format_BGR888);
          image = QPixmap::fromImage(img);
          // QPixmap image = QPixmap(img);

                  ui->lblDisplayVideo->setPixmap(image);
          

          }

          Thread function is here

          void RunDefaultCamera()
          {
          while(capture.isOpened())
          {

                  qDebug() << "thread is running";
                  capture >> ImageMat;
                  bufferQueue.push_back(ImageMat);
                  
                  if(!ImageMat.empty())
                  {
                      frameCounter++;
          
                      lock_guard<std::mutex> lock(fmutex);
                      if (int(bufferQueue.size()) >= bufferSize)
                      {
                          bufferQueue.clear();
                      }
                      else
                      {
                          bufferQueue.push_back(ImageMat);
                       }
          
                  }
                  sleep(100);
          //        ui->listInfo->addItem(QString::number(bufferQueue.size()));
                  qDebug() << bufferQueue.size();
          

          }
          capture.release();
          }

          P 1 Reply Last reply 13 Aug 2020, 17:37
          0
          • I Imran Hassan
            13 Aug 2020, 17:27

            I am using windows so I can not know where memory is being leaked.

            I am using thread. Here is the code

            void MainWindow::mainfucntion(){
            std::thread producer_t(&MainWindow::RunDefaultCamera,this);

            for(;;){

                    time(&start);
                    timer = double(getTickCount());
                    tic();
            
                    if(!bufferQueue.empty()){
                      lock_guard<std::mutex> lock(fmutex);
                        readFrame = bufferQueue.front();
                        qDebug() << "1 : " << bufferQueue.size();
                        bufferQueue.pop_front();
                        qDebug() << "2 : " << bufferQueue.size();
                    }
                    else{
                        if(keepRunning == true)
                        {
                            if(threadEnable==false)
                            {
                               std::thread producer_t(&MainWindow::RunDefaultCamera,this);
                            }
                            continue;
                        }
                        else{
                            producer_t.join();
                            return -1;
                        }
                    }
            

            // cap >> readFrame;
            cv::resize(readFrame, readFrame, size);

            img = QImage((uchar*) readFrame.data, readFrame.cols, readFrame.rows, readFrame.step, QImage::Format_BGR888);
            image = QPixmap::fromImage(img);
            // QPixmap image = QPixmap(img);

                    ui->lblDisplayVideo->setPixmap(image);
            

            }

            Thread function is here

            void RunDefaultCamera()
            {
            while(capture.isOpened())
            {

                    qDebug() << "thread is running";
                    capture >> ImageMat;
                    bufferQueue.push_back(ImageMat);
                    
                    if(!ImageMat.empty())
                    {
                        frameCounter++;
            
                        lock_guard<std::mutex> lock(fmutex);
                        if (int(bufferQueue.size()) >= bufferSize)
                        {
                            bufferQueue.clear();
                        }
                        else
                        {
                            bufferQueue.push_back(ImageMat);
                         }
            
                    }
                    sleep(100);
            //        ui->listInfo->addItem(QString::number(bufferQueue.size()));
                    qDebug() << bufferQueue.size();
            

            }
            capture.release();
            }

            P Offline
            P Offline
            Pablo J. Rogina
            wrote on 13 Aug 2020, 17:37 last edited by
            #4

            @Imran-Hassan said in Live camera image using OpenCV showing in Qlabel crashes due to out of memory error:

            using windows so I can not know where memory is being leaked.

            you may want to take a look at using Heob with Qt Creator...

            Upvote the answer(s) that helped you solve the issue
            Use "Topic Tools" button to mark your post as Solved
            Add screenshots via postimage.org
            Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            0
            • C Offline
              C Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 13 Aug 2020, 18:06 last edited by
              #5

              @Imran-Hassan said in Live camera image using OpenCV showing in Qlabel crashes due to out of memory error:

              ui->lblDisplayVideo->setPixmap(image);

              You must not do this outside the main thread.

              img = QImage((uchar*) readFrame.data, readFrame.cols, readFrame.rows, readFrame.step, QImage::Format_BGR888);

              where do you free this memory? Please take a look at the documentation

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

              I 1 Reply Last reply 14 Aug 2020, 04:51
              3
              • C Christian Ehrlicher
                13 Aug 2020, 18:06

                @Imran-Hassan said in Live camera image using OpenCV showing in Qlabel crashes due to out of memory error:

                ui->lblDisplayVideo->setPixmap(image);

                You must not do this outside the main thread.

                img = QImage((uchar*) readFrame.data, readFrame.cols, readFrame.rows, readFrame.step, QImage::Format_BGR888);

                where do you free this memory? Please take a look at the documentation

                I Offline
                I Offline
                Imran Hassan
                wrote on 14 Aug 2020, 04:51 last edited by Imran Hassan
                #6

                @Christian-Ehrlicher said in Live camera image using OpenCV showing in Qlabel crashes due to out of memory error:

                ui->lblDisplayVideo->setPixmap(image);
                You must not do this outside the main thread.

                Actually its in for loop and we have to do it. We have to continuously update the screen with new image.

                mg = QImage((uchar*) readFrame.data, readFrame.cols, readFrame.rows, readFrame.step, QImage::Format_BGR888);

                where do you free this memory?

                I was not releasing the memory ,,, I added the statement and I am now releasing the memory with null pointer like
                img = QImage();

                But still facing the same problem.

                J 1 Reply Last reply 14 Aug 2020, 05:25
                0
                • I Imran Hassan
                  14 Aug 2020, 04:51

                  @Christian-Ehrlicher said in Live camera image using OpenCV showing in Qlabel crashes due to out of memory error:

                  ui->lblDisplayVideo->setPixmap(image);
                  You must not do this outside the main thread.

                  Actually its in for loop and we have to do it. We have to continuously update the screen with new image.

                  mg = QImage((uchar*) readFrame.data, readFrame.cols, readFrame.rows, readFrame.step, QImage::Format_BGR888);

                  where do you free this memory?

                  I was not releasing the memory ,,, I added the statement and I am now releasing the memory with null pointer like
                  img = QImage();

                  But still facing the same problem.

                  J Offline
                  J Offline
                  J.Hilk
                  Moderators
                  wrote on 14 Aug 2020, 05:25 last edited by J.Hilk
                  #7

                  @Imran-Hassan

                  since when is reassigning a pointer to nullptr equal to freeing memory, if it's not created on the stack?
                  You could argue since c++11, but you're not using those classes.

                  And It won't matter anyway, because QImage inertially has nothing ti do with that data, using that constructor.

                  Explicitly delete the QImage, than call delete [] on that char array than creating a new QImage

                  Actually its in for loop and we have to do it. We have to continuously update the screen with new image.

                  No you don't have to, it's just - for you - the most convenient way to do it right now.


                  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.

                  1 Reply Last reply
                  1
                  • B Offline
                    B Offline
                    Bonnie
                    wrote on 14 Aug 2020, 06:43 last edited by Bonnie
                    #8

                    Hey guys, if readFrame is a cv::Mat then he doesn't need to free the memory bacause it has its own memory managing mechanism.
                    But the whole mainfucntion is very weird, I don't know where it is ...
                    It looks like a main() function, but it has this and ui, so it seems like a member function of MainWindow, which is even weirder to have for(;;) in that...

                    1 Reply Last reply
                    0
                    • I Offline
                      I Offline
                      Imran Hassan
                      wrote on 14 Aug 2020, 07:53 last edited by
                      #9

                      Thank you all guys. Your suggestions and expert opinion helped a lot with solving the problem. QT forum is always so helpful.

                      After trying many things here is the conclusion and code is now working perfectly fine

                      I removed the tic toc part

                      time(&start);
                      timer = double(getTickCount());
                      tic();
                      It was working but crashing then just to make sure that QImage is not NULL I removed the img = QImage(); // releasing memory

                      with

                      if(!img.isNull())
                      img = QImage();
                      Its working perfectly fine now.

                      1 Reply Last reply
                      2

                      9/9

                      14 Aug 2020, 07:53

                      • Login

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