How to convert QImage to opencv Mat
One year after you issued this question there've been great answers on the internet:
- Convert between cv::mat and Qimage correctly
- Converting Between cv::Mat and QImage or QPixmap
But the way I see it, if you're working with Qt and OpenCV at the same time then type QImage
is probably just for displaying, that case you might want to use QPixmap
since it's optimized for displaying. So this is what I do:
- Load image as
cv::Mat
, if you'd like to display the image, convert toQPixmap
using the non-copy method introduced in the second article. - Do your image processing in
cv::Mat
. - Any time during the workflow you can call upon something like
Mat2QPixmap()
to get realtime result. - Never convert
QPixmap
tocv::Mat
, there's no sense doing it considering the purpose of each type.
The answer to this with Qt 5.11 (and probably some earlier versions):
cv::Mat mat(image.height(), image.width(),CV_8UC3, image.bits());
// image.scanline() does not exist,
//and height/width is interchanged for a matrix
Again the QImage is assumed to be RGB888 (ie QImage::Format_RGB888)
If the QImage will still exist, and you just need to perform a quick operation on it then you can construct a cv::Mat using the QImage memory:
cv::Mat mat(image.height(), image.width(), CV_8UC3, (cv::Scalar*)image.scanLine(0));
This assumes that the QImage is 3-channels, ie RGB888.
If the QImage is going away then you need to copy the data, see Qimage to cv::Mat convertion strange behaviour.
If QImage is Format_ARGB32_Premultiplied (the preferred format) then you will need to convert each pixel to OpenCV's BGR layout. The cv::cvtcolor() function can convert ARGB to RGB in the latest versions.
Or you can use QImage::convertToFormat() to convert to RGB before copying the data.