How to convert QImage to opencv Mat c++
-
I have searched on the internet but I cannot find a method in C++ of converting a QImage ( QImage(2048, 1148, QImage::Format_Grayscale8) ) to a OpenCV Mat for write frames to cv::VideoWriter("output.avi",cv::VideoWriter::fourcc('D','I','V','X'),10, size,0);
This is the method I'm using but it throws exception when write to cv::VideoWriter :
cv::Mat QImageToCvMat(QImage &inImage)
{
cv::Mat mat(inImage.height(), inImage.width(),
CV_16UC1, inImage.bits(), inImage.bytesPerLine());
return mat;
}The exception:
OpenCV Error: Image step is wrong () in cvSetData, file /build/opencv-XDqSFW/opencv-3.2.0+dfsg/modules/core/src/array.cpp, line 939
terminate called after throwing an instance of 'cv::Exception'
what(): /build/opencv-XDqSFW/opencv-3.2.0+dfsg/modules/core/src/array.cpp:939: error: (-13) in function cvSetDataHow would I do this the right way ?.
Any help is appreciated.
-
I have searched on the internet but I cannot find a method in C++ of converting a QImage ( QImage(2048, 1148, QImage::Format_Grayscale8) ) to a OpenCV Mat for write frames to cv::VideoWriter("output.avi",cv::VideoWriter::fourcc('D','I','V','X'),10, size,0);
This is the method I'm using but it throws exception when write to cv::VideoWriter :
cv::Mat QImageToCvMat(QImage &inImage)
{
cv::Mat mat(inImage.height(), inImage.width(),
CV_16UC1, inImage.bits(), inImage.bytesPerLine());
return mat;
}The exception:
OpenCV Error: Image step is wrong () in cvSetData, file /build/opencv-XDqSFW/opencv-3.2.0+dfsg/modules/core/src/array.cpp, line 939
terminate called after throwing an instance of 'cv::Exception'
what(): /build/opencv-XDqSFW/opencv-3.2.0+dfsg/modules/core/src/array.cpp:939: error: (-13) in function cvSetDataHow would I do this the right way ?.
Any help is appreciated.
-
I have searched on the internet but I cannot find a method in C++ of converting a QImage ( QImage(2048, 1148, QImage::Format_Grayscale8) ) to a OpenCV Mat for write frames to cv::VideoWriter("output.avi",cv::VideoWriter::fourcc('D','I','V','X'),10, size,0);
This is the method I'm using but it throws exception when write to cv::VideoWriter :
cv::Mat QImageToCvMat(QImage &inImage)
{
cv::Mat mat(inImage.height(), inImage.width(),
CV_16UC1, inImage.bits(), inImage.bytesPerLine());
return mat;
}The exception:
OpenCV Error: Image step is wrong () in cvSetData, file /build/opencv-XDqSFW/opencv-3.2.0+dfsg/modules/core/src/array.cpp, line 939
terminate called after throwing an instance of 'cv::Exception'
what(): /build/opencv-XDqSFW/opencv-3.2.0+dfsg/modules/core/src/array.cpp:939: error: (-13) in function cvSetDataHow would I do this the right way ?.
Any help is appreciated.
@Malikov said in How to convert QImage to opencv Mat c++:
Image step is wrong () in cvSetData
Set correct
imageStep
and it should work.See here:
https://stackoverflow.com/questions/17127762/cvmat-to-qimage-and-back/17137998or here:
https://stackoverflow.com/questions/5026965/how-to-convert-an-opencv-cvmat-to-qimage -
I have searched on the internet but I cannot find a method in C++ of converting a QImage ( QImage(2048, 1148, QImage::Format_Grayscale8) ) to a OpenCV Mat for write frames to cv::VideoWriter("output.avi",cv::VideoWriter::fourcc('D','I','V','X'),10, size,0);
This is the method I'm using but it throws exception when write to cv::VideoWriter :
cv::Mat QImageToCvMat(QImage &inImage)
{
cv::Mat mat(inImage.height(), inImage.width(),
CV_16UC1, inImage.bits(), inImage.bytesPerLine());
return mat;
}The exception:
OpenCV Error: Image step is wrong () in cvSetData, file /build/opencv-XDqSFW/opencv-3.2.0+dfsg/modules/core/src/array.cpp, line 939
terminate called after throwing an instance of 'cv::Exception'
what(): /build/opencv-XDqSFW/opencv-3.2.0+dfsg/modules/core/src/array.cpp:939: error: (-13) in function cvSetDataHow would I do this the right way ?.
Any help is appreciated.
@Malikov said in How to convert QImage to opencv Mat c++:
cv::Mat mat(inImage.height(), inImage.width(),
CV_16UC1, inImage.bits(), inImage.bytesPerLine());CV_16UC1
is for 16-bit data.QImage::Format_Grayscale8
is for 8-bit data. If I'm not mistaken, these are incompatible. (Try converting your QImage to a 16-bit format first)Also, see this answer: https://stackoverflow.com/a/17137998/1144539
-
@Malikov said in How to convert QImage to opencv Mat c++:
cv::Mat mat(inImage.height(), inImage.width(),
CV_16UC1, inImage.bits(), inImage.bytesPerLine());CV_16UC1
is for 16-bit data.QImage::Format_Grayscale8
is for 8-bit data. If I'm not mistaken, these are incompatible. (Try converting your QImage to a 16-bit format first)Also, see this answer: https://stackoverflow.com/a/17137998/1144539
-
@Malikov said in How to convert QImage to opencv Mat c++:
Image step is wrong () in cvSetData
Set correct
imageStep
and it should work.See here:
https://stackoverflow.com/questions/17127762/cvmat-to-qimage-and-back/17137998or here:
https://stackoverflow.com/questions/5026965/how-to-convert-an-opencv-cvmat-to-qimageHi
I'm not sure what format type I need to set in the Mat contractor.
Each frame has 1 channels and 2 elem bytes. I initialize QImage variable this way:img = QImage(width, height, QImage::Format_Grayscale8); uchar *pSrc = (uchar *)m_frame.pBuffer + m_frame.usHeader; uchar *pDst = (uchar *)img.bits(); int pixels = frame.width * frame.height * frame.channels; for (int i = 0; i < pixels; ++i) { *pDst++ = *pSrc; pSrc += elementBytes; }
My issue is to convert this QImage img to Mat variable in order to write frames to cv::VideoWriter.
-
@Malikov See this answer: https://stackoverflow.com/a/50848100/6622587
static cv::Mat QImageToMat(const QImage & image){ cv::Mat out; switch(image.format()) { case QImage::Format_Invalid: { cv::Mat empty; empty.copyTo(out); break; } case QImage::Format_RGB32: { cv::Mat view(image.height(),image.width(),CV_8UC4,(void *)image.constBits(),image.bytesPerLine()); view.copyTo(out); break; } case QImage::Format_RGB888: { cv::Mat view(image.height(),image.width(),CV_8UC3,(void *)image.constBits(),image.bytesPerLine()); cv::cvtColor(view, out, cv::COLOR_RGB2BGR); break; } default: { QImage conv = image.convertToFormat(QImage::Format_ARGB32); cv::Mat view(conv.height(),conv.width(),CV_8UC4,(void *)conv.constBits(),conv.bytesPerLine()); view.copyTo(out); break; } } return out; }
-
Sorry its not helping me
The format is QImage::Format_Grayscale8 and Im not sire what format I need for Mat.