PDA

View Full Version : How to display opencv output window inside a Label or Widget in form.ui



pj0909
27th March 2017, 17:07
Hi all,

I am new to Qt and opencv, I am programming a graphical interface to record a video through webcam using opencv libraries in Qt. Problem is when I push the start recording button on my interface, a new window gets launched and I dont want that. It should be like after pressing the start recording button it should display the opencv output of webcam inside a Label or Widget on my graphical interface and not on a newly launched window.

I am using Qt 5.8 on windows 10. Below is the while loop which captures the frame and displays in a window:


while (1)
{

Mat frame;

bool bSuccess = cap.read(frame); // read a new frame from video

if (!bSuccess)
{
cout << "ERROR: Cannot read a frame from video file" << endl;
break;
}

oVideoWriter.write(frame);


imshow("MyVideo", frame); //this line launches a new output window


if (waitKey(10) == 27)
{

cout << "esc key is pressed by user" << endl;
return ;

}
}

Can anyone please guide me on the code of how can I embed the opencv output window inside a Label or Widget in my form.ui.

Thank you for your time and knowledge.

high_flyer
29th March 2017, 10:44
Back in the day I was doing such things using QImage and QPixmap.
Have a look at the documentation of both classes, ask if you have any further questions.

d_stranz
29th March 2017, 18:09
And don't use infinite while() loops in Qt applications without also calling QCoreApplication::processEvents() somewhere within it. Otherwise, your application GUI will lock up. All GUI updates in Qt depend on having an event loop that that can process events (like painting); if you write an infinite while() loop that doesn't let this processing occur, then even if your loop is retrieving frames and updating an image to be displayed on a label, the label will never have chance to repaint itself.