11

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.

3 Answers 3

9

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I found an interesting project, where the converting is done even faster: code.google.com/p/qt-opencv-multithreaded It's essentially working with a cv::Mat, but as it's the same as IplImage, it can be used even with the old API. const uchar *qImageBuffer = (const uchar*)mat.data; QImage img(qImageBuffer, mat.cols, mat.rows, mat.step, QImage::Format_RGB888); While the linked solution with the nested for loops lagged on my system, this one doesn't.
First of all this is a nice project and good to know. The proposed solution is restricted to a limited set of scenarios (Mat is not BGR, but RGB, data formats match, Mat object survives the QImage), but you omit a duplication. If it works for you, great! (It doesn't work for me for the reasons mentioned :).)
0

My first guess is to want to say this: I'm sure that if you dig into the code for namedWindow, you will find that they use some sort of standard, albeit not oft-referenced, object for painting said window (that's in the openCV code). If you were ambitious enough, you could extend this class yourself, to interface directly to a frame or custom widget in Qt. There might even be a way to take the entire window and embed it, using a similar method of a Qt frame, or an extension of the (general) widget class. This is a very interesting question and relates rather directly to work I've been doing of late, so I'll continue to think about and research it and see if I can't come up with something else more helpful.

[EDIT] What specific new controls are you so interested in? It might be more efficient on the part of the programmer to extend a Qt control to emulate that, as opposed to my first suggestion.[/EDIT]

1 Comment

If I can't find a better solution, I will try to implement it myself, I just don't want to reinvent the wheel. As I think I'm not the first one to use Qt and opencv together, I was thinking that a simple solution already exists.
-6

simply check out the opencv highgui implementation. as i recall it uses qt.

6 Comments

What a generic answer! In the question it is already made clear that this is a known fact.
It is the first sentence of the question.
@ypnos: again, you're wrong. "a works with b" does not imply that "a is implemented in terms of b". that said, your irrelevancy applies only to the second sentence of my answer. the first sentence is what the OP should do. which i once did.
Please don't nitpick the bad phrasing. Why not continue reading the first sentence in the document that is linked there: "This figure explains the new functionalities implemented with Qt GUI." It is fine that you wanted to give a quick answer to the question, it is just not that helpful.
@ypnos: The answer is all that's needed for any one aspiring to be a programmer. What's written in documents far out on the web somewhere is irrelevant for what can be assumed about the OP's knowledge. Now consider, if you are able to (?), that the selected "solution" says the same as this answer, only in a more verbose way. I.e. your assertion of not helpful is proved false long ago. In my opinion you represent 80% of what's wrong with SO: the unthinking conformity.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.