How to convert QImage to opencv Mat

I searched on the Internet but cannot find a way to convert QImage (or QPixmap ) to OpenCV Mat . How can I do it?

Any help is appreciated.

+4
source share
4 answers

If QImage will still exist, and you just need to perform a quick operation on it, then ou can build cv :: Mat using QImage memory

cv::Mat mat(image.rows(),image.cols(),CV_8UC3,image.scanline()); This assumes that Qimage is 3 channels, i.e. RGB888

If Qimage goes away, you need to copy the data, see Qimage to cv :: Mat convertion weird behavior

If QImage is Format_ARGB32_Premultiplied (preferred format), then you will need to convert each pixel to an OpenGV BGR layout. The cv :: cvtcolor () function can convert ARGB to RGB in recent versions.
Or you can use QImage :: convertToformat () to convert to RGB before copying data

+5
source

A year after you posted this question, there were excellent answers on the Internet:

But as I see it, if you are working with Qt and OpenCV at the QImage , then enter QImage , probably only for display , in this case you can use QPixmap , since it is optimized for display. So here is what I am doing:

  • Download the image as cv::Mat , if you want to display the image, convert it to QPixmap using the non-copy method presented in the second article.
  • Do image processing in cv::Mat .
  • At any point in the workflow, you can call something like Mat2QPixmap() to get the result in real time.
  • Never convert QPixmap to cv::Mat , it makes no sense to do this for each type of goal.
+7
source

I would save the image to disk and reopen it from cv;)

@ ^ I tried the previous solution. It does not seem to work with me. Although I get some image, it looks like some kind of barcode information. I think there are some parameter mismatches.

0
source

My attempt at OpenCV 3.1+ style code:

 void qimage_to_mat(const QImage& image, cv::OutputArray out) { switch(image.format()) { case QImage::Format_Invalid: { Mat empty; empty.copyTo(out); break; } case QImage::Format_RGB32: { Mat view(image.height(),image.width(),CV_8UC4,(void *)image.constBits(),image.bytesPerLine()); view.copyTo(out); break; } case QImage::Format_RGB888: { Mat view(image.height(),image.width(),CV_8UC3,(void *)image.constBits(),image.bytesPerLine()); cvtColor(view, out, COLOR_RGB2BGR); break; } default: { QImage conv = image.convertToFormat(QImage::Format_ARGB32); Mat view(conv.height(),conv.width(),CV_8UC4,(void *)conv.constBits(),conv.bytesPerLine()); view.copyTo(out); break; } } } void mat_to_qimage(cv::InputArray image, QImage& out) { switch(image.type()) { case CV_8UC4: { Mat view(image.getMat()); QImage view2(view.data, view.cols, view.rows, view.step[0], QImage::Format_ARGB32); out = view2.copy(); break; } case CV_8UC3: { Mat mat; cvtColor(image, mat, COLOR_BGR2BGRA); //COLOR_BGR2RGB doesn't behave so use RGBA QImage view(mat.data, mat.cols, mat.rows, mat.step[0], QImage::Format_ARGB32); out = view.copy(); break; } case CV_8UC1: { Mat mat; cvtColor(image, mat, COLOR_GRAY2BGRA); QImage view(mat.data, mat.cols, mat.rows, mat.step[0], QImage::Format_ARGB32); out = view.copy(); break; } default: { throw invalid_argument("Image format not supported"); break; } } } 
0
source

All Articles