Image display using OpenCV 2.2 on Linux Ubuntu platform with Qt

Hey guys, I am using Qt as a C ++ IDE platform on top of Ubuntu 10.10 with OpenCV 2.2. I will just write the code snippets 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 look something like this:

 VIDIOC_QUERYMENU: Invalid argument 

And everything seems beautiful (the camera is working).

I had to add using Qt and add 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 3 warning lines, but now the "frame" window is gray and nothing is displayed.

This is my first time using Qt, so I really don't know how this works. I can only guess that QApplication is gaining control over window management, which means that the imshow command imshow not open a new window.

I would be grateful for your help, Thnx!

+4
source share
4 answers

Your problem is that Qt processes its own event loop, and therefore the OpenCV event loop is hungry and never starts.

The way to get them to work together is quite simple: display OpenCV as a QPixmap (by converting your image to QImage, then use QLabel to display it). Then add this QLabel to your QWidget. Your QWidget can 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 opencv cv :: Mat to qimage

To display this QImage, see Show QImage with QtGui

+1
source

Your guess is wrong. OpenCV will have its own connection to the X server and will not depend on the Qt window management (however, input processing waitKey () and app.exec () will not work trivially in parallel).

I also tested similar code to detect any unforeseen side effects. All windows are perfectly drawn on my Debian machine with OpenCV 2.2 and Qt 4.6.2.

The trivial test on your side might be to create an application object right after the while loop. However, it could just be a random change in your stack location, which has led to your warnings becoming a serious problem? You should also check with valgrind.

+2
source

I recently ran into a similar problem (displaying webcam information using OpenCV and Qt as the final GUI). The best way I've learned to play a video (which is basically a collection of images) is to use GLWidget. In this GLWidget, you can create a rectangle and attach a texture to it, where the texture is the image you want to display. Another problem is that the image format is different for OpenCV and OpenGL, but you can easily change the format by changing the order of the channels.

In your code, you create a QApplication that has its own iddle process, so you will never reach the while loop. You also did not create any Qt display window, I recommend that you check the examples that come with Qt to check the basic structure of the application and start with QMainWindow or QDialog.

Steps:

+1
source

You cannot mix openCV event loop control and qt app.exec

Either use the Qt flavor of cvNamedWindow, or just grab the images from openCV and show them in QLabel. Or is it better to inherit from QWidget and write your own QImage artist

 void OpencvWidget::paintEvent(QPaintEvent*) { //m_win is the window size QPainter p(this); p.drawImage(m_win,m_image,m_image.rect()); } 
+1
source

Source: https://habr.com/ru/post/1342023/


All Articles