I am working on an interactive application which needs to read and manipulate several very large images at once (25 images at a time, roughly 350 Mb total size). OpenCV is quite speedy and handles the algorithms with relative ease. But drawing them with Qt is proving to be a problem. Here are two less-than-ideal solutions I have tried.
Solution 1 (too slow)
Every time you need to draw a different OpenCV image, convert it to a
QImageand draw that. The conversion, unfortunately, takes a while and
we cannot switch between images at interactive speeds.Solution 2 (too memory-intensive)
Maintain two stacks of images, one for OpenCV and one for Qt. Use the
appropriate one at the appropriate time.
I have direct access to the OpenCV pixel data. I know the width and height of the image, and I know that pixels are 3-byte RGB values. It seems like it should be possible to draw the OpenCV image quickly without copying it to a QImage container that (as far as I can tell) just contains a duplicate of the data.
Where do I need to look to get this kind of capability out of Qt?
You can share the data between QImage and openCV – both of them have a ctor which uses existing data – supplied by a pointer.
cv::Mat(int _rows, int _cols, int _type, void* _data, size_t _step=AUTO_STEP)QImage ( uchar * data, int width, int height, int bytesPerLine, Format format)There might be an issue with the padding if the rows don’t end up being multiples of 4bytes but I would expect the padding to align on both types with the same pixel size – at least on the same hardware
One issue is that openCV uses BGR by default which isn’t very optimal for QImage (or any other display). Although I’m not sure that QImage::Format_ARGB32_Premultiplied is necessarily that much quicker anymore on Qt which use accelerated openGL for rendering QImage.
An alternative is to use opencv then copy the resulting data direct to an openGL texture and then use QGlWidget to display the image without another copy.