3

This is the parent window, here I have a button 'select video', when I click the button(which is having the label 'select video from webcam') it will capture video from web-came. But the parent window is covering the video which is currently playing, or video which is playing currently is behind the parent window I am doing a project on face recognition. Qt is used for creating the front-end.

When I click the button(which is having the label 'select video from webcam') it will capture video from web-cam, but it is not visible. Not visible in the sense, the parent window is covering the video which is currently playing, or video which is playing currently is behind the parent window. what should I do to make it in front of all the parent windows, until the video ends.

void admin_db_creation::on_pushButton_3_clicked()
  {
capture = cvCaptureFromCAM(0);
    if(!capture)
        {
            cout<<"Could not initialize capturing..."<<endl;
        }
    while(1)
        {
            frame2 = cvQueryFrame(capture);
            frame3=detectFace(frame2);
            imshow("window", frame2);
            char key = cvWaitKey(10);
                if (key == 27)
                        break;
        }
 }

This is button click code,it contains code for playing the video..

Яois
  • 3,838
  • 4
  • 28
  • 50
user3228547
  • 91
  • 11
  • 1
    Didn't understand the problem. Maybe you can put a screenshot? – guneykayim Apr 30 '14 at 20:04
  • How can we help you if we don't know how you are playing the video? Post the code. – Matteo Italia May 02 '14 at 03:25
  • You need to add the code you're using to create the window where you're playing the video back from. – Nicholas Smith May 02 '14 at 09:52
  • Make sure your video widget has its parent correctly set, and make sure parent window does not set stays on top hint. – ismail May 02 '14 at 09:53
  • 1
    Your question is not clear.. may you can try putting more screenshots/code to make workflow clear – Abhishek Bansal May 02 '14 at 09:55
  • 2
    Your while(1) loop is probably blocking the main Qt gui update thread. Secondly, imshow() is not Qt related at all. You might want to render the video images to a QLabel or something instead of calling imshow() from OpenCV from Qt. Finally, you might just want to use Qt as the control application and use OpenCV to display the video in a separate window. – PurpleAlien May 05 '14 at 02:31

2 Answers2

6

Problem is that you are mixing event loops.

Qt provides own event loop for processing all system events, including mouse and keyboard. When you use char key = cvWaitKey(10); you create another event loop which is provided by OpenCV only for testing purposes.

This is Qt code so instead using this infinitive loop (for Qt it is infinitive), create a slot which will fetch single frame and process it (display it) and call it repetitively using QTimer (timout signal). Forget about UI features from OpenCv. From OpenCv use only image processing functions, nothing else.

Here is similar problem. And here is something that may be also useful for this topic.

Community
  • 1
  • 1
Marek R
  • 32,568
  • 6
  • 55
  • 140
2

First, convert your image from cv::Mat to QImage. Then show it using a QLabel on your GUI. And forget about OpenCV highgui module, it won't get along with Qt GUI!!

1) Conversion example:

// Mat __cvFrame is your OpenCV image, 
QImage __frame; 
if (__cvframe.channels()==3)
{
   Mat __cvRGBframe;
   cvtColor(__cvframe,__cvRGBframe,CV_BGR2RGB);
   __frame = QImage((const unsigned char*)(__cvRGBframe.data),
               __cvRGBframe.cols,__cvRGBframe.rows,QImage::Format_RGB888);
}
else
{
   __frame = QImage((const unsigned char*)(__cvframe.data),
                         __cvframe.cols,__cvframe.rows,QImage::Format_Indexed8);
}

2) Putting your QImage onto a QLabel:

// QLabel* label has to exist in your GUI :)
label->setPixmap(QPixmap::fromImage(__frame));
label->setScaledContents(true);
Яois
  • 3,838
  • 4
  • 28
  • 50
  • yes.We can show video in QLabel using this code, but without using 'imshow' function we are not able to view the video, but at the background webcam is capturing frames.When we use 'imshow' function we can view video in the QLabel and at the same time it shows the same video in the opencv window also. – user3228547 May 07 '14 at 04:38
  • You can't use a QtGUI and the OpenCV UI utils at the same time. As Marek and myself pointed out: "Forget about UI features from OpenCv". – Яois May 07 '14 at 10:31
  • Ok,that I accept. But without giving 'imshow' function video is not showing in the QLabel. – user3228547 May 09 '14 at 02:55
  • That's not true. `imshow` controls the visualization at the OpenCV UI windows. I use Qt-based GUIs to show images being processed with OpenCV and it works, just in the way I suggested. If the video is not being shown, there has to be another reason for it. – Яois May 09 '14 at 07:19