How to convert an OpenCV cv::Mat to QImage
Asked Answered
K

11

53

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.

Kelleher answered 17/2, 2011 at 9:14 Comment(0)
T
47

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.

Tied answered 7/9, 2012 at 5:40 Comment(2)
This is essentially what OpenCV uses internally to convert (code.opencv.org/projects/opencv/repository/revisions/…, line 2389) image2Draw_qt = QImage(image2Draw_mat->data.ptr, image2Draw_mat->cols, image2Draw_mat->rows, image2Draw_mat->step, QImage::Format_RGB888); Also (approximately, line 2400) cvConvertImage(mat, image2Draw_mat, CV_CVTIMG_SWAP_RB); to convert from BGR to RGB.Satyriasis
img.step makes all the difference. I was having weird problems with this conversion, including having the resulting image show distorted and with the wrong colors. Testing a little bit I got the same code.Adkisson
C
32

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);

It is more efficient than manually converting the pixels to the QImage, but you have to keep the original cv::Mat image in memory. It can be easily converted to a QPixmap and displayed using a QLabel:

QPixmap pixmap = QPixmap::fromImage(img);
myLabel.setPixmap(pixmap);

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.

Update 2:

If the image you are trying to show has nonstandard stride (when it is non-continuous, submatrix), the image may appeard distorted. In this case, it is better to explicitly specify the stride using cv::Mat::step1():

QImage img((uchar*)mat.data, mat.cols, mat.rows, mat.step1(), QImage::Format_RGB32);
Currajong answered 17/2, 2011 at 23:46 Comment(6)
This will not work in the general case. What if mat has >1 channels or data format is not RGB32?Bingo
Well, yes, and it can also have multiple dimensions, or can have different 'step size'. I am expecting that Hien wants to display 'standard' cv::Mat, i.e. loaded by imread, or converted to appropriate type.Currajong
@Michael Yes that is what I wanted. I'll try out your code once I have time to work on my project. =)Kelleher
opencv uses the BGR channel order.Cerys
You can also convert the qimage to the right RGB order after its been created eg. qt_im_rgb = qt_im_bgr.rgbSwapped();Microscope
according to QImage doc: data MUST be 32-bit aligned, so Mat should accomplish this, this is particularly important when Mat type is CV_8UC3, i.e. 24 bit, the @ypnos method below is much more general and safe.Laundrywoman
G
30

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;
}
Gathering answered 15/1, 2012 at 1:56 Comment(2)
This answered a question I was about to post about converting floating-point grayscale values into a QImage...thank you!Cupriferous
this solves the QImage 32 bit alignment requirement when loading from raw data.Laundrywoman
U
15

OpenCV loads images into a Mat in Blue-Green-Red (BGR) format by default, while QImage expects RGB. This means that if you convert a Mat to QImage, the blue and red channels will be swapped. To fix this, before constructing the QImage, you need to change the BRG format of your Mat to RGB, via the cvtColor method using argument CV_BGR2RGB, like so:

Mat mat = imread("path/to/image.jpg");
cvtColor(mat, mat, CV_BGR2RGB);
QImage image(mat.data, mat.cols, mat.rows, QImage::Format_RGB888);

Alternatively, use rgbSwapped() on the QImage

QImage image = QImage(mat.data, mat.cols, mat.rows, QImage::Format_RGB888).rgbSwapped());
Unworthy answered 11/11, 2018 at 2:5 Comment(0)
P
4
    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.

Posthaste answered 5/2, 2012 at 19:34 Comment(0)
R
4

I have the same problem as you too, so I develop four functions to alleviate my pain, they are

QImage mat_to_qimage_cpy(cv::Mat const &mat, bool swap = true);

QImage mat_to_qimage_ref(cv::Mat &mat, bool swap = true);

cv::Mat qimage_to_mat_cpy(QImage const &img, bool swap = true);

cv::Mat qimage_to_mat_ref(QImage &img, bool swap = true);

These functions can handle the images with 1, 3, 4 channels, every pixel must occupy one byte only(CV_8U->Format_Indexed8, CV_8UC3->QImage::Format_RGB888, CV_8UC4->QImage::Format_ARGB32), I do not deal with other types yet(QImage::Format_RGB16, QImage::Format_RGB666 and so on). The codes are located at github.

The key concepts of **transform mat to Qimage ** are

/**
 * @brief copy QImage into cv::Mat
 */
struct mat_to_qimage_cpy_policy
{
    static QImage start(cv::Mat const &mat, QImage::Format format)
    {
       //The fourth parameters--mat.step is crucial, because 
       //opencv may do padding on every row, you need to tell
       //the qimage how many bytes per row 
       //The last thing is if you want to copy the buffer of cv::Mat
       //to the qimage, you need to call copy(), else the qimage
       //will share the buffer of cv::Mat
       return QImage(mat.data, mat.cols, mat.rows, mat.step, format).copy();
    }
};

struct mat_to_qimage_ref_policy
{
    static QImage start(cv::Mat &mat, QImage::Format format)
    {
       //every thing are same as copy policy, but this one share
       //the buffer of cv::Mat but not copy
       return QImage(mat.data, mat.cols, mat.rows, mat.step, format);
    }
};

The key concepts of transform cv::Mat to Qimage are

/**
 * @brief copy QImage into cv::Mat
 */
struct qimage_to_mat_cpy_policy
{
    static cv::Mat start(QImage const &img, int format)
    {
       //same as convert mat to qimage, the fifth parameter bytesPerLine()
       //indicate how many bytes per row
       //If you want to copy the data you need to call clone(), else QImage
       //cv::Mat will share the buffer
       return cv::Mat(img.height(), img.width(), format, 
                      const_cast<uchar*>(img.bits()), img.bytesPerLine()).clone();
    }
};

/**
 * @brief make Qimage and cv::Mat share the same buffer, the resource
 * of the cv::Mat must not deleted before the QImage finish
 * the jobs.
 */
struct qimage_to_mat_ref_policy
{
    static cv::Mat start(QImage &img, int format)
    {
       //same as copy policy, but this one will share the buffer 
       return cv::Mat(img.height(), img.width(), format, 
                      img.bits(), img.bytesPerLine());
    }
};

If would be good if some one can extend these functions and make them support more types, please inform me if there are any bugs.

Remainderman answered 27/7, 2015 at 20:43 Comment(0)
C
2

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.)

Cerys answered 17/2, 2011 at 10:24 Comment(3)
The project that I am working on requires me to print out the image onto the QtGui, but I will be working on the images in cv::Mat most of the time. The reason I am asking is that my program will do a lot of computation so I want to get rid of the overhead of converting cv::Mat to IplImage and then to qimage. This is my first image processing project so I don't really know much about the data that are in an image. I will learn about this soon enough once I dive more into the project. Anyway, if I can't find anything, then I'll follow your suggestion =)Kelleher
Can you please share the code for IplImage -> QImage conversion? I am interested to make the needed adjustments to convert it to cv::Mat.Bingo
@Hien: The overhead of the conversion cv::Mat to IplImage is, compared to all the other stuff you do when doing image processing, completely negligible. There is no copying of data involved.Cerys
I
2

This post 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.

Impedance answered 8/12, 2011 at 19:28 Comment(0)
A
1

Use the static function convert16uc1 for the depth image:

QPixmap Viewer::convert16uc1(const cv::Mat& source)
{
  quint16* pSource = (quint16*) source.data;
  int pixelCounts = source.cols * source.rows;

  QImage dest(source.cols, source.rows, QImage::Format_RGB32);

  char* pDest = (char*) dest.bits();

  for (int i = 0; i < pixelCounts; i++)
  {
    quint8 value = (quint8) ((*(pSource)) >> 8);
    *(pDest++) = value;  // B
    *(pDest++) = value;  // G
    *(pDest++) = value;  // R
    *(pDest++) = 0;      // Alpha
    pSource++;
  }

  return QPixmap::fromImage(dest);
}

QPixmap Viewer::convert8uc3(const cv::Mat& source)
{
  quint8* pSource = source.data;
  int pixelCounts = source.cols * source.rows;

  QImage dest(source.cols, source.rows, QImage::Format_RGB32);

  char* pDest = (char*) dest.bits();

  for (int i = 0; i < pixelCounts; i++)
  {
    *(pDest++) = *(pSource+2);    // B
    *(pDest++) = *(pSource+1);    // G
    *(pDest++) = *(pSource+0);    // R
    *(pDest++) = 0;               // Alpha
    pSource+=3;
  }

  return QPixmap::fromImage(dest);
}

QPixmap Viewer::convert16uc3(const cv::Mat& source)
{
  quint16* pSource = (quint16*) source.data;
  int pixelCounts = source.cols * source.rows;

  QImage dest(source.cols, source.rows, QImage::Format_RGB32);

  char* pDest = (char*) dest.bits();

  for (int i = 0; i < pixelCounts; i++)
  {
    *(pDest++) = *(pSource+2);    // B
    *(pDest++) = *(pSource+1);    // G
    *(pDest++) = *(pSource+0);    // R
    *(pDest++) = 0;               // Alpha
    pSource+=3;
  }

  return QPixmap::fromImage(dest);
}
Association answered 10/6, 2013 at 13:42 Comment(0)
U
0

If you want to re-use the data-pointer of the mat instead of copying all the data, you can use the following code:

cv::Mat mat // defined elsewhere

QImage image(mat.data, mat.cols, mat.rows, QImage::Format_YOUR_FORMAT);

mat.deallocate(); // <-- this 'removes' the pointer from mat

We use the cv::Mat::deallocate function, which leads to the cv::Mat not deleting the underlying data when it's deleted.

So if you save your QImage as a class-member or return it from a function, you'd have to normally copy the data from the Mat since after the stack ends, it would delete the array in the back.

Deallocate kind of "removes" the pointer from the Mat so it doesn't delete the data that we assigned the QImage (since otherwise the QImage would point to freed-up memory).

Urine answered 27/2, 2023 at 15:39 Comment(0)
A
-4

This did the trick for me. It's a little dodgy, has terrible performance (as pointed out in the comments), but works with all color formats I have thrown at it so far, and it is also very simple to do.

The procedure is as follows:

cv::Mat image = //...some image you want to display

// 1. Save the cv::Mat to some temporary file
cv::imwrite("../Images/tmp.jpg",image);

// 2. Load the image you just saved as a QImage
QImage img;
img.load("../Images/tmp.jpg");

Done!

If you, say, want to display it in a QLabel, then continue with:

// Set QImage as content of MyImageQLabel
ui-> MyImageQLabel->setPixmap(QPixmap::fromImage(img, Qt::AutoColor));

I personally use this for a simple image editor.

Astound answered 4/10, 2013 at 10:1 Comment(1)
@AxeEffect This is certainly not a high performing solution, considering you have to do read/write operation towards the HDD. For some usages, though, that doesn't really matter. :-)Astound

© 2022 - 2024 — McMap. All rights reserved.