Mat to QImage
Hello,
How to convert from a Mat object to a QImage in QT, and QImage to Mat ?
Thank you, Christophe,
Hello,
How to convert from a Mat object to a QImage in QT, and QImage to Mat ?
Thank you, Christophe,
Here is a function to convert Mat to QPixmap or QImage. It's much faster than the direct pixel access using the Mat::at(y,x)
function:
QPixmap MatToPixmap(cv::Mat src)
{
QImage::Format format=QImage::Format_Grayscale8;
int bpp=src.channels();
if(bpp==3)format=QImage::Format_RGB888;
QImage img(src.cols,src.rows,format);
uchar *sptr,*dptr;
int linesize=src.cols*bpp;
for(int y=0;y<src.rows;y++){
sptr=src.ptr(y);
dptr=img.scanLine(y);
memcpy(dptr,sptr,linesize);
}
if(bpp==3)return QPixmap::fromImage(img.rgbSwapped());
return QPixmap::fromImage(img);
}
To convert a QImage to Mat, invert the parameters of the memcpy
function.
Asked: 2017-12-19 07:02:00 -0600
Seen: 2,928 times
Last updated: Dec 19 '17
What is the most effective way to access cv::Mat elements in a loop?
Is there penalty for reference counting in Mat?
Saving an image with unset pixels
android: how to put a column into Mat
OpenCV on Mac OS X 10.8 Mountain Lion
Find pixel color out of cv::Mat on specific position