|
QImage is a Qt class for image handling (I/O and simple pixel access and manipulation), but it is far from a right choice for image processing. Interfacing Qt an OpenCV involves a natural need for converting QImage to OpenCV images of Mat. Here presents some solutions for their conversions.
1. From QImage to CV
//Original
QImage lenaRGB=lena.convertToFormat(QImage::Format_RGB888);
ui->imgWindow->setPixmap(QPixmap::fromImage(lena,Qt::AutoColor));
intW=lenaRGB.width();
intH=lenaRGB.height();
//method1: trival
Mat lena_cv,out;
QImage lena2=lenaRGB.rgbSwapped();
lena_cv=Mat(H,W,CV_8UC3,lena2.bits(),lena2.bytesPerLine());
qDebug()<<"Imagesizebymethod1:"<<lena_cv.cols<<lena_cv.rows;
//cvtColor(lena_cv,out,CV_RGB2BGR);
namedWindow("Method1");
imshow("Method1",lena_cv);
//Method2: naive
Mat lena_cv2,lena_cv2_2;
lena_cv2.create(Size(W,H),CV_8UC3);
memcpy(lena_cv2.data,lena2.bits(),sizeof(char)*W*H*3);
namedWindow("Method2");
imshow("Method2",lena_cv2);
qDebug()<<"Imagechannels:"<<lena_cv2.channels();
//method3:robust
Mat lena_cv3;
lena_cv3.create(Size(W,H),CV_8UC3);
unsignedchar*lena2_src=lena2.bits();
unsignedchar*lena2_dst=lena_cv3.data;
intstep=lena2.bytesPerLine();
for(introw=0;row<H;row++)
{
memcpy(lena2_dst+W*3*row,lena2_src+row*step,W*3);
}
namedWindow("Method3");
imshow("Method3",lena_cv3);
Comment:
1) swapping RGB in QImage to BGR in CV::Mat is needed prior to or after the converstion.
2. From CV to QImage
Sources:
http://qtandopencv.blogspot.com/2013/08/how-to-convert-between-cvmat-and-qimage.html
https://stackoverflow.com/questions/11886123/how-to-convert-qimage-to-opencv-mat
https://asmaloney.com/2013/11/code/converting-between-cvmat-and-qimage-or-qpixmap/
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-10-19 23:35
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社