Qt和OpenCV的有效集成 [英] Efficient integration of Qt and OpenCV

查看:71
本文介绍了Qt和OpenCV的有效集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个交互式应用程序,该应用程序需要一次读取和处理几张非常大的图像(一次25张图像,总大小约为350 Mb). OpenCV相当快,并且相对容易地处理算法.但是事实证明,用Qt绘制它们是一个问题.这是我尝试过的两种不理想的解决方案.

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.

解决方案1(太慢)

每次您需要绘制不同的OpenCV图像时,都将其转换为 QImage并绘制.不幸的是,转换需要一段时间,并且 我们无法以交互速度在图像之间切换.

Every time you need to draw a different OpenCV image, convert it to a QImage and draw that. The conversion, unfortunately, takes a while and we cannot switch between images at interactive speeds.

解决方案2(占用大量内存)

维护两堆图像,一个用于OpenCV,另一个用于Qt.使用 在适当的时候适当的一个.

Maintain two stacks of images, one for OpenCV and one for Qt. Use the appropriate one at the appropriate time.

我可以直接访问OpenCV像素数据.我知道图像的宽度和高度,并且我知道像素是3字节RGB值.似乎有可能可以快速绘制OpenCV图像,而无需将其复制到(据我所知)仅包含数据重复项的QImage容器中.

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.

我需要在哪里寻找从Qt中获得这种功能的地方?

Where do I need to look to get this kind of capability out of Qt?

推荐答案

您可以在QImage和openCV之间共享数据-两者都有一个ctor使用现有数据-由指针提供.

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)

如果行的结尾不是4字节的倍数,则填充可能存在问题,但我希望填充在两种类型上具有相同的像素大小-至少在同一硬件上对齐

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

一个问题是,openCV默认情况下使用BGR,这对于QImage(或任何其他显示)而言并不是最佳选择.尽管我不确定QImage :: Format_ARGB32_Premultiplied是否一定在使用加速openGL渲染QImage的Qt上更快得多.

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.

一种替代方法是使用opencv,然后将结果数据直接复制到openGL纹理中,然后使用QGlWidget显示图像而无需其他复制.

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.

这篇关于Qt和OpenCV的有效集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆