cvmat qimage互转
作者:fly 发布于:2016-12-21 15:36 分类:Linux
如果转换出现问题,请查阅调试信息。有时ARGB32与RGB32格式搞错会导致没有图像输出的。
cv::Mat MainWindow::QImage2cvMat(QImage *image) { cv::Mat mat; switch(image->format()) { case QImage::Format_ARGB32: printf("-ARGB32"); case QImage::Format_RGB32: printf("-RGB32"); case QImage::Format_ARGB32_Premultiplied: printf("-ARGB32_Premu"); mat = cv::Mat(image->height(), image->width(), CV_8UC4, (void*)image->constBits(), image->bytesPerLine()); break; case QImage::Format_RGB888: printf("-888"); mat = cv::Mat(image->height(), image->width(), CV_8UC3, (void*)image->constBits(), image->bytesPerLine()); cv::cvtColor(mat, mat, CV_BGR2RGB); break; case QImage::Format_Indexed8: printf("-index8"); mat = cv::Mat(image->height(), image->width(), CV_8UC1, (void*)image->constBits(), image->bytesPerLine()); break; default: printf("-other");break; } return mat; } QImage MainWindow::cvMat2QImage(const cv::Mat mat) { if(mat.type() == CV_8UC1) { printf("-8uc1"); QImage image(mat.cols, mat.rows, QImage::Format_Indexed8); image.setColorCount(256); for(int i = 0; i < 256; i++) { image.setColor(i, qRgb(i, i, i)); } uchar *pSrc = mat.data; for(int row = 0; row < mat.rows; row ++) { uchar *pDest = image.scanLine(row); memcpy(pDest, pSrc, mat.cols); pSrc += mat.step; } return image; } else if(mat.type() == CV_8UC3) { printf("-8uc3"); const uchar *pSrc = (const uchar*)mat.data; QImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_RGB888); return image.rgbSwapped(); } else if(mat.type() == CV_8UC4) { printf("-8uc4"); const uchar *pSrc = (const uchar*)mat.data; QImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_RGB32); return image.copy(); } else { printf("-other"); return QImage(); } }
日历
分类
存档
- 2018年3月(1)
- 2017年9月(1)
- 2017年7月(2)
- 2017年5月(1)
- 2017年3月(2)
- 2017年2月(6)
- 2017年1月(9)
- 2016年12月(13)
- 2016年11月(4)
- 2016年8月(17)
- 2016年7月(4)
- 2016年3月(1)
- 2016年2月(2)
- 2015年12月(4)
- 2015年10月(5)
- 2015年9月(1)
- 2015年8月(1)
- 2015年6月(2)
- 2015年5月(4)
- 2015年4月(1)
- 2015年3月(4)
- 2015年2月(2)
- 2015年1月(24)
- 2014年12月(3)
- 2014年10月(5)
- 2014年9月(5)
- 2014年8月(4)
- 2014年7月(13)
- 2014年6月(18)
- 2014年5月(20)
- 2014年4月(24)
- 2014年3月(90)
- 2014年2月(121)
- 2014年1月(11)
最新评论
- qq
哈 - AdOnCn
很好!感谢! - FlyLu
fly您好,看到你写的文章很不错,也很佩... - 地铁诡事
[给力] - 婚嫁百科
如果有爱,如果有结局! - 安安博客
看不懂 - wys.me
博主好,请教个问题哈。 怎么修改vis... - 知道91博客
哈哈,这个算是编程很基础很基础的了吧 - 婚嫁百科
晕死,怎么都不会显示出来的啊 - 小俊
看不懂
发表评论: