jingweimo的个人博客分享 http://blog.sciencenet.cn/u/jingweimo

博文

Conversion between QImage and CV Images

已有 1753 次阅读 2018-2-20 09:55 |系统分类:科研笔记

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/

 




https://blog.sciencenet.cn/blog-578676-1100440.html

上一篇:Create Qt widgets
下一篇:Bitwise operations: a case study
收藏 IP: 35.10.57.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-25 15:20

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部