5
votes

How can we convert directly cv::Mat to QPixmap without going through filename loading?

I have made some research about it but no hints!

As a first step, what I have tried is that I save the image, and then load it. But it's not what I want to have.

2
You could probably use the function QPixmap::loadFromData(...) to load the field cv::Mat::data directly into the QPixmap. I never tried to do such a thing though. - Jehan
And by the way, possible duplicate since it's easy to switch between Qimage and QPixmap. - Jehan
And how to switch between QImage and QPixmap? - user2354422
Cannot be easier: QPixmap::fromImage(). - Jehan

2 Answers

9
votes

Jehan is right.

For people referring to this thread in the future: First convert image from BGR(used by OpenCV) to RGB.

Then:

QPixmap::fromImage(QImage((unsigned char*) mat.data, mat.cols, mat.rows, QImage::Format_RGB888));
2
votes

Mehfoos Yacoob is right, that is the best way to convert a cv::Mat to a QPixmap. First, you need to convert the cv::Mat to a QImage, and then using the fromImage function, assign its result to a QPixmap. It is important to keep in mind the type of the cv::Mat in order to use the correct QImage format.

This is a little example of how to do it: http://asmaloney.com/2013/11/code/converting-between-cvmat-and-qimage-or-qpixmap/