Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedding an OpenCV window into a Qt GUI

Tags:

c++

c

opencv

qt

OpenCV recently upgraded its display window, when it is used in Qt. It looks very good, however I did not find any possibility for it to be embedded into an existing Qt GUI window. The only possibility seems to be the creation of a cvNamedWindow or cv::namedWindow, but it creates a free-floating independent window.

Is there any possibility to create that OpenCV window inside an existing GUI? All I could find on the OpenCV forums is an unanswered question, somewhat similar to my own.

There is a straight-forward possibility to show an OpenCV image in Qt, but it has two major problems:

  1. it involves copying the image pixel by pixel, and it's quite slow. It has function calls for every pixel! (in my test application, if I create a video out of the images, and display it in a cvNamedWindow, it runs very smoothly even for multiple videos the same time, but if I go through the IplImage --> QImage --> QPixmap --> QLabel route, it has severe lag even for one single video)
  2. I can't use those nice new controls of the cvNamedWindow with it.
like image 403
vsz Avatar asked Jul 29 '12 14:07

vsz


1 Answers

First of all, the image conversion is not as inefficient as you think. The 'function calls' per pixel at least in my code (one of the answers to the question you referenced) are inlined by optimized compilation.

Second, the code in highgui/imshow does the same. You have to get from the matrix to an ARGB image either way. The conversion QImage -> QPixmap is essentially nothing else than moving the data from main memory to GPU memory. That's also the reason why you cannot access the QPixmap data directly and have to go through QImage.

Third, it is several times faster if you use a QGLWidget to draw the image, and I assume you have QT_OPENGL enabled in your OpenCV build. I use QPainter to draw the QPixmap within a QGLWidget, and speed is no issue. Here is example code:

http://sourceforge.net/p/gerbil/svn/19/tree/gerbil-gui/scaledview.h

http://sourceforge.net/p/gerbil/svn/19/tree/gerbil-gui/scaledview.cpp

Now to your original question: Your current option is to take the code from OpenCV, include into your project under a different namespace, and alter it to fit your needs. Apart from that you have no alternative right now. OpenCV's highgui uses its own event loop, connection to the X server etc. and it is nothing you can intercept.

like image 122
ypnos Avatar answered Oct 04 '22 05:10

ypnos