问题描述
我已在互联网上搜索,但我找不到将 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正在消失,那么您需要复制数据,请参阅在复制数据之前转换为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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!