How to convert an OpenCV cv::Mat to QImage

Hien picture Hien · Feb 17, 2011 · Viewed 57.6k times · Source

I am wondering how would I convert the OpenCV C++ standard cv::Mat type to QImage. I have been searching around, but have no luck. I have found some code that converts the IPlimage to QImage, but that is not what I want. Thanks.

Answer

chAmi picture chAmi · Sep 7, 2012

Michal Kottman's answer is valid and give expected result for some images but it'll fail on some cases. Here is a solution i found to that problem.

QImage imgIn= QImage((uchar*) img.data, img.cols, img.rows, img.step, QImage::Format_RGB888);

Difference is adding img.step part. qt won't complain without it but some images won't show properly without it. Hope this will help.