I’m using Qt as my C++ IDE platform over Ubuntu 10.10 with OpenCV 2.2.
I’ll just write pieces of code and show where the problem is:
#include "opencv2/highgui/highgui.hpp"
using namespace cv;
int main(int argc, char *argv[])
{
VideoCapture cap = VideoCapture(0);
Mat frame;
do
{
cap >> frame;
imshow("frame",frame);
} while (waitKey(10) != 27);
return 0;
}
I get 3 warning printouts that seems something like this:
VIDIOC_QUERYMENU: Invalid argument
And everything seems to be fine (the camera works).
I had to add usage of the Qt and added 3 lines of code, and it looks like this:
#include <QApplication>
#include "opencv2/highgui/highgui.hpp"
using namespace cv;
int main(int argc, char *argv[])
{
QApplication app(argc,argv);
VideoCapture cap = VideoCapture(0);
Mat frame;
do
{
cap >> frame;
imshow("frame",frame);
} while (waitKey(10) != 27);
return app.exec();
}
I still get the 3 warning lines but now, the "frame" window is grey, and nothing is shown.
This is my first time using Qt, so I don’t really know how it works. I can only guess that QApplication is getting control over the window management, that causes the imshow command to not be able to open a new window.
Your problem is with Qt handling its own event loop and thus the event loop of OpenCV is starved and never get run.
The way to get them to work together is quite simple: Display your OpenCV as a QPixmap (by convert your image to QImage then use QLabel to display it). Then add this QLabel to your QWidget. Your QWidget can either be embedded or become the main widget of your QApplication.
To use the buffer of your cv::Mat image as your QImage, see this answer how to convert an opencv cv::Mat to qimage
To display this QImage, see
Display QImage with QtGui