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
-
Answer:
Here is code for 24bit RGB and grayscale floating point. Easily adjustable for other types. It is as efficient as it gets. QImage Mat2QImage(const cv::Mat3b &src) { QImage dest(src.cols, src.rows, QImage::Format_ARGB32); for (int y = 0; y < src.rows; ++y) { const cv::Vec3b *srcrow = src[y]; QRgb *destrow = (QRgb*)dest.scanLine(y); for (int x = 0; x < src.cols; ++x) { destrow[x] = qRgba(srcrow[x][2], srcrow[x][1], srcrow[x][0], 255); } } return dest; } QImage Mat2QImage(const cv::Mat_<double> &src) { double scale = 255.0; QImage dest(src.cols, src.rows, QImage::Format_ARGB32); for (int y = 0; y < src.rows; ++y) { const double *srcrow = src[y]; QRgb *destrow = (QRgb*)dest.scanLine(y); for (int x = 0; x < src.cols; ++x) { unsigned int color = srcrow[x] * scale; destrow[x] = qRgba(color, color, color, 255); } } return dest; }
Hien at Stack Overflow Visit the source
Other answers
To convert from cv::Mat to QImage, you could try to use the QImage(uchar * data, int width, int height, Format format) constructor as follows (mat is a cv::Mat) : QImage img((uchar*)mat.data, mat.cols, mat.rows, QImage::Format_RGB32); Update: because OpenCV uses BGR order by default, you should first use cvtColor(src, dst, CV_BGR2RGB) to get an image layout that Qt understands.
Michal Kottman
cv::Mat has a conversion operator to IplImage, so if you have something that converts the IplImage to a QImage, just use that (or make the - probably minor - adjustments to take the cv::Mat directly, the memory layout is the same, it's "just" the header that is different.)
etarion
http://umanga.wordpress.com/2010/04/19/how-to-covert-qt-qimage-into-opencv-iplimage-and-wise-versa/ shows how to convert a QImage to OpenCV's IplImage and vise-versa. After that, if you need help to convert between IplImage* to cv::Mat: // Assume data is stored by: // IplImage* image; cv::Mat mat(image, true); // Copies the data from image cv::Mat mat(image, false); // Doesn't copy the data! It's a hack, but will get the job done.
karlphillip
Mat opencv_image = imread("fruits.jpg", CV_LOAD_IMAGE_COLOR); Mat dest; cvtColor(opencv_image, dest,CV_BGR2RGB); QImage image((uchar*)dest.data, dest.cols, dest.rows,QImage::Format_RGB888); This is what worked for me. I modified Michal Kottman's code above.
Mathai
Related Q & A:
- How to make a professional CV?Best solution by Yahoo! Answers
- How can I make a CV in pdf format?Best solution by pdfcv.com
- How do I make a CV to apply for a job?Best solution by wikihow.com
- How to write a professional CV?Best solution by Yahoo! Answers
- How can I make my CV notable?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.