如何转换QImage到opencv Mat [英] How to convert QImage to opencv Mat

查看:980
本文介绍了如何转换QImage到opencv Mat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已在互联网上搜索,但我找不到将 QImage (或 QPixmap )转换为OpenCV Mat

I have searched on the internet but I cannot find a method of converting a QImage(or QPixmap) to a OpenCV Mat. How would I do this?.

推荐答案

如果QImage仍然存在,你只需要对它执行快速操作,那么ou可以使用QImage内存构造一个cv :: Mat

If the QImage will still exist and you just need to perform a quick operation on it then ou can construct a cv::Mat using the QImage memory

cv :: Mat mat(image.rows(),image.cols(),CV_8UC3,image.scanline());
假设Qimage是3channels RGB888

cv::Mat mat(image.rows(),image.cols(),CV_8UC3,image.scanline()); This assumes that the Qimage is 3channels, ie RGB888

如果Qimage正在消失,那么您需要复制数据,请参阅

If the Qimage is going away then you need to copy the data, see Qimage to cv::Mat convertion strange behaviour

如果QImage是Format_ARGB32_Premultiplied(优先格式),那么你的将需要将每个像素转换为openCV的BGR布局。 cv :: cvtcolor()函数可以在最新版本中将ARGB转换为RGB。

或者您可以使用
QImage :: convertToformat()在复制数据之前转换为RGB

If QImage is Format_ARGB32_Premultiplied (the preffered format) then you will need to convert each pixel to openCV's BGR layout. The cv::cvtcolor() function can convert ARGB to RGB in the latest versions.
Or you can use QImage::convertToformat() to convert to RGB before copying the data

这篇关于如何转换QImage到opencv Mat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆