将QImage转化成Mat方法

简介: 本文提供了一个将QImage转换为OpenCV Mat格式的Python函数,通过转换QImage到RGB32格式并使用numpy数组重塑来实现转换。

将QImage转化成Mat方法

def convertQImageToMat(incomingImage):
    '''  Converts a QImage into an opencv MAT format  '''
    # Format_RGB32 = 4,存入格式为B,G,R,A 对应 0,1,2,3
    # RGB32图像每个像素用32比特位表示,占4个字节,
    # R,G,B分量分别用8个bit表示,存储顺序为B,G,R,最后8个字节保留
    incomingImage = incomingImage.convertToFormat(4)
    width = incomingImage.width()
    height = incomingImage.height()

    ptr = incomingImage.bits()
    ptr.setsize(incomingImage.byteCount())
    arr = np.array(ptr).reshape(height, width, 4)  # Copies the data
    # arr为BGRA,4通道图片
    return arr
相关文章
|
BI
Qt 报表实现(二)----QtXlsx
Qt 报表实现(二)----QtXlsx
648 2
|
Ubuntu 安全 Linux
Linux(34)Rockchip RK3568 Ubuntu22.04和Debian 10上配置远程桌面工具
Linux(34)Rockchip RK3568 Ubuntu22.04和Debian 10上配置远程桌面工具
2312 0
Qt 之 QDebug,QString
Qt 之 QDebug,QString
423 0
|
编译器 C语言
C语言常见编译错误分类及其解决方案
C语言常见编译错误分类及其解决方案
2150 1
C语言常见编译错误分类及其解决方案
|
容器
【Qt 学习笔记】Qt常用控件 | 容器类控件 | Tab Widget的使用及说明
【Qt 学习笔记】Qt常用控件 | 容器类控件 | Tab Widget的使用及说明
2747 2
《QT从基础到进阶·十三》QPixmap、QImage的缩放——
《QT从基础到进阶·十三》QPixmap、QImage的缩放——
972 0
|
Web App开发 XML 存储
一篇文章讲明白JPG、PNG、GIF、SVG等格式图片区别
一篇文章讲明白JPG、PNG、GIF、SVG等格式图片区别
|
人工智能 算法 小程序
AI算命:千亿市场的好生意?
明知被骗,却依然甘愿掉入AI算命的陷阱。
AI算命:千亿市场的好生意?
|
C语言
break与continue详解
break与continue详解
684 1