3

I am trying to process a video by playing with its frames via the methods of OpenCV library and display these frames with imshow method of opencv/highgui with no problem here.

But when it comes to display them in real-time with a Qt-gui app, I couldn't managed.

The program gets the path of the video from openfile dialog and starts to grap frames of the video within a while loop and process them. After process step, when I try to display these processed frames on a QLabel, the QLabel of gui-app doesn't display anything (along the length of the video) but only the last frame at the end of the video, nothing else. I tried to refresh/update the label for each frame but it didn't work.

Do I miss something to do? Or is there any more convenient method for it?

Necessary part of my code is below. Thanks in advance.

bool stop=false;
cv::VideoCapture capture("a.avi");
cv::Mat cur_frame;

while (!stop) {    
             //...               
             capture.read(cur_frame);                
             //process steps..               
             QImage img= QImage((const unsigned char*)(cur_frame.data),
                                 cur_frame.cols,cur_frame.rows,
                                 QImage::Format_RGB888);
             ui->label->setPixmap(QPixmap::fromImage(img));
             // resize the label to fit the image
             ui->label->resize(ui->label->pixmap()->size());
             //...
             cv::waitkey(50);
             }
NG_
  • 6,895
  • 7
  • 45
  • 67
Horizon1710
  • 792
  • 10
  • 28

1 Answers1

2

Qt can only update the UI when control has returned to the event loop. You can try calling qApp->processEvents() in your loop but that may not be the optimum approach.

Arnold Spence
  • 21,942
  • 7
  • 74
  • 67
  • 1
    Thanks Arnold, it works, at least for now it is useful for me. Beside this, what could be the optimum approach to do this work? Is using a Qlabel to display frames a usable and suitable way to handle this work?(I dont think so, I used Qlabel because I dont know QT that well) – Horizon1710 Apr 24 '12 at 23:57
  • 1
    Unfortunately I don't have a good answer for you on the best way to do it. I haven't done anything as demanding as real-time video with Qt. – Arnold Spence Apr 25 '12 at 00:20
  • 1
    Even though this is an old question, but i just want to point out that this answer ( http://stackoverflow.com/a/2671834/1990758 ) may help. – Energy Mar 02 '16 at 08:07