PDA

View Full Version : Fastest way to display a BGR image from OpenCV



Stringtheory
11th April 2018, 09:43
I'm writing an app that requires fairly fast updates to an image display that originates in OpenCV.

Of course the first problem is OpenCV's unfortunate choice of BGR format. I'm dealing with that via:
cvtColor(mat_in, mat_out, CV_BGR2RGB);
Then creating a QImage via:
QImage qImage(mat_out.data, mat_out.cols, mat_out.rows, mat_out.step,
QImage::Format_RGB888);
And finally writing a QPixMap so I can display it:
QPixmap pixMap = QPixmap::fromImage(qImage);

This seems very obscure, and I have no idea what Qt is doing internally, so it's tough to gauge whether performance can be optimized here.
Given the prevalence of OpenCV, I presume that this is an often-solved problem. Can the process above be improved?

Second problem: Most refs on Qt recommend using a QLabel for displaying the image. It works, but doesn't Qt have any widgets that are specifically optimized for image display? I'm ending up with a lot of extraneous code for doing scrolling, zoom, etc. You'd think there would be a pre-existing widget that already has those provisions. I'd settle for a custom library, if that exists.

Any suggestions appreciated.

Stringtheory
13th April 2018, 08:21
I'm writing an app that requires fairly fast updates to an image display that originates in OpenCV.

Of course the first problem is OpenCV's unfortunate choice of BGR format. I'm dealing with that via:
cvtColor(mat_in, mat_out, CV_BGR2RGB);
Then creating a QImage via:
QImage qImage(mat_out.data, mat_out.cols, mat_out.rows, mat_out.step,
QImage::Format_RGB888);
And finally writing a QPixMap so I can display it:
QPixmap pixMap = QPixmap::fromImage(qImage);

This seems very obscure, and I have no idea what Qt is doing internally, so it's tough to gauge whether performance can be optimized here.
Given the prevalence of OpenCV, I presume that this is an often-solved problem. Can the process above be improved?

Second problem: Most refs on Qt recommend using a QLabel for displaying the image. It works, but doesn't Qt have any widgets that are specifically optimized for image display? I'm ending up with a lot of extraneous code for doing scrolling, zoom, etc. You'd think there would be a pre-existing widget that already has those provisions. I'd settle for a custom library, if that exists.

Any suggestions appreciated.
No replies? Is there a better forum for this type of question, perhaps?

^NyAw^
13th April 2018, 09:05
I suggest you to start using QGraphicsView. The QGraphicsPixmapItem is what you need.

Stringtheory
14th April 2018, 06:42
Thanks, NyAw. I have found a pretty good chapter that covers this in Tazehkandi's book "Computer Vision with OpenCV 3 and Qt5."
Now that I've seen more detail, I'm wondering why QLabels are used so much for displaying images.