user3228547
user3228547

Reputation: 91

Making video visible in Qt

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..

Upvotes: 3

Views: 783

Answers (2)

Яois
Яois

Reputation: 3858

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);

Upvotes: 2

Marek R
Marek R

Reputation: 38161

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.

Upvotes: 6

Related Questions