Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert an OpenCV cv::Mat to QImage

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.

like image 570
Hien Avatar asked Feb 17 '11 09:02

Hien


People also ask

How do you convert QImage to QPixmap?

A QPixmap object can be converted into a QImage using the toImage() function. Likewise, a QImage can be converted into a QPixmap using the fromImage(). If this is too expensive an operation, you can use QBitmap::fromImage() instead.

What is mat type in OpenCV?

The Mat class of OpenCV library is used to store the values of an image. It represents an n-dimensional array and is used to store image data of grayscale or color images, voxel volumes, vector fields, point clouds, tensors, histograms, etc.

What is the function of OpenCV to grayscale image mat?

If you need a grayscale image, use: C++ Mat img = imread(filename, IMREAD_GRAYSCALE);


1 Answers

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.

like image 150
chAmi Avatar answered Oct 23 '22 08:10

chAmi