当前位置:网站首页>函数方法封装——图片类型QPixmap、QImage与Mat的相互转化
函数方法封装——图片类型QPixmap、QImage与Mat的相互转化
2022-07-20 02:16:00 【上帝不会知道】
头文件
ImageType_Transform.h
#ifndef IMAGETYPE_TRANSFORM_H
#define IMAGETYPE_TRANSFORM_H
#include<QImage>
#include<opencv.hpp>//这个要在pro文件里加上其include文件与lib文件的路径
class ImageType_Transform
{
public:
//QImage转为Mat
static cv::Mat QImageToMat(QImage input_qimage);
//Mat转为QImage
static QImage MatToQImage(cv::Mat input_mat);
//QPixmap转为Mat
static cv::Mat QPixmapToMat(QPixmap input_qpixmap);
//Mat转为QPixmap
static QPixmap MatToQPixmap(cv::Mat input_mat);
//QImage转为QPixmap
static QPixmap QImageToPixmap(QImage input_qimage);
//QPixmap转为QImage
static QImage QPixmapToQImage(QPixmap input_pixmap);
};
#endif // IMAGETYPE_TRANSFORM_H
源文件
ImageType_Transform.cpp
#include"ImageType_Transform.h"
#include <opencv2/imgproc/types_c.h>//缺了这个头文件,CV_BGR2RGB将报错未定义
#include<QImage>
#include<QPixmap>
#include<opencv.hpp>
//QImage转为Mat
cv::Mat ImageType_Transform::QImageToMat(QImage qimage)
{
cv::Mat mat;
switch(qimage.format())
{
case QImage::Format_ARGB32:
case QImage::Format_RGB32:
case QImage::Format_ARGB32_Premultiplied:
mat = cv::Mat(qimage.height(), qimage.width(), CV_8UC4, (void*)qimage.constBits(), qimage.bytesPerLine());
break;
case QImage::Format_RGB888:
mat = cv::Mat(qimage.height(), qimage.width(), CV_8UC3, (void*)qimage.constBits(), qimage.bytesPerLine());
cv::cvtColor(mat, mat, CV_BGR2RGB);
break;
case QImage::Format_Indexed8:
mat = cv::Mat(qimage.height(), qimage.width(), CV_8UC1, (void*)qimage.constBits(), qimage.bytesPerLine());
break;
}
return mat;
}
//Mat转为QImage
QImage ImageType_Transform::MatToQImage(cv::Mat mat)
{
cv::Mat temp;
//Mat与QImage的储存方式不一样,所以需要转换一下格式
cv::cvtColor(mat,temp,CV_BGR2RGB);
QImage qimage((const uchar*)temp.data,temp.cols,temp.rows,temp.step,QImage::Format_BGR888);
//强制生成相应的data,以避免在input释放后出错
qimage.bits();
return qimage;
}
//QPixmap转为Mat
cv::Mat ImageType_Transform::QPixmapToMat(QPixmap qpixmap)
{
QImage qimage=qpixmap.toImage();
cv::Mat mat;
switch(qimage.format())
{
case QImage::Format_ARGB32:
case QImage::Format_RGB32:
case QImage::Format_ARGB32_Premultiplied:
mat = cv::Mat(qimage.height(), qimage.width(), CV_8UC4, (void*)qimage.constBits(), qimage.bytesPerLine());
break;
case QImage::Format_RGB888:
mat = cv::Mat(qimage.height(), qimage.width(), CV_8UC3, (void*)qimage.constBits(), qimage.bytesPerLine());
cv::cvtColor(mat, mat, CV_BGR2RGB);
break;
case QImage::Format_Indexed8:
mat = cv::Mat(qimage.height(), qimage.width(), CV_8UC1, (void*)qimage.constBits(), qimage.bytesPerLine());
break;
}
return mat;
}
//Mat转为QPixmap
QPixmap ImageType_Transform::MatToQPixmap(cv::Mat mat)
{
cv::Mat temp;
//Mat与QImage的储存方式不一样,所以需要转换一下格式
cv::cvtColor(mat,temp,CV_BGR2RGB);
QImage qimage((const uchar*)temp.data,temp.cols,temp.rows,temp.step,QImage::Format_BGR888);
//强制生成相应的data,以避免在input释放后出错
qimage.bits();
QPixmap pixmap=QPixmap::fromImage(qimage);
return pixmap;
}
//QImage转为QPixmap
QPixmap ImageType_Transform::QImageToPixmap(QImage qimage)
{
QPixmap qpixmap=QPixmap::fromImage(qimage);
return qpixmap;
}
//QPixmap转为QImage
QImage ImageType_Transform::QPixmapToQImage(QPixmap qpixmap)
{
QImage qimage=qpixmap.toImage();
return qimage;
}
使用方法:
创建一个名为ImageType_Transform.h的头文件,
将上面ImageType_Transform.h头文件内的代码放入其中。
创建一个名为ImageType_Transform.cpp的源文件,
将上面ImageType_Transform.cpp源文件内的代码放入其中。
在你需要使用本封装方法进行图片转化的cpp文件里,
添加#include"ImageType_Transform.h"
使用示范:
QPixmap p=QPixmap(":/clock.png");
QImage i;
i=ImageType_Transform::QPixmapToQImage(p);
cv::Mat mat;
mat=ImageType_Transform::QImageToMat(i);
QImage i_1=ImageType_Transform::MatToQImage(mat);
QPixmap p_1=ImageType_Transform::QImageToPixmap(i_1);
cv::Mat mat_1=ImageType_Transform::QPixmapToMat(p_1);
QPixmap p_2=ImageType_Transform::MatToQPixmap(mat_1);
边栏推荐
- Distribution rules of weights of binary neural networks
- Tutorial on principles and applications of database systems (029) -- data integrity of MySQL (II): defining primary keys
- acwing 867. 分解质因数
- Leetcode exercises grammar supplementary summary notes
- 数据库系统原理与应用教程(027)—— MySQL 修改表中数据(三):改(update)
- 洛谷 P1024 [NOIP2001 提高组] 一元三次方程求解
- Tutorial on principles and applications of database system (030) -- data integrity of MySQL (III): defining unique constraints
- Kubernetes kube-scheduler调度器
- 中职网络安全—2022年国赛逆向PE Reverse解题思路
- acwing 871. 约数之和
猜你喜欢
网络安全—综合渗透测试-CVE-2017-12629-Apache Solr远程代码执行
快速排序及优化
Blurred photos, second high-definition big picture, flying propeller ppde takes you to reproduce the image restoration model cmfnet
NC | 全球变化背景下溶解有机碳和微生物的生态网络关系研究取得进展
ShardingSphere-proxy 搭配 MogDB/openGauss 实现分布式数据库
Baidu PaddlePaddle easydl helps manufacturing enterprises with intelligent transformation
ICLR 2022 | GNNAsKernel: 能提升任意GNN表达能力的通用框架
Shardingsphere proxy with mogdb/opengauss to realize distributed database
机器学习练习 8 -异常检测和推荐系统(协同过滤)
acwing 867. 分解质因数
随机推荐
洛谷 P2440 木材加工
剑指 Offer II 041. 滑动窗口的平均值_____使用队列 / 循环数组实现
Dix minutes pour générer un effet de design intérieur de qualité film et télévision, comment mettre à niveau l'industrie de la maison traditionnelle avec Red Star McLaren Design Cloud
力扣第 302 场周赛复盘
cannot import name ‘import_string‘ from ‘werkzeug‘【bug解决】
Tutorial on principles and applications of database system (032) -- data integrity of MySQL (V): define auto_increment
Xtrabackup realizes MySQL: full backup + incremental backup
HandBrake安装问题:提示安装frameworks .NET
xtrabackup实现mysql:全量备份+增量备份
网络安全—综合渗透测试-CVE-2019-15107-Webmin远程代码执行
Distribution rules of weights of binary neural networks
Visio使用
The five safety links that are easy to be ignored are more dangerous than expected!
Baidu PaddlePaddle easydl helps manufacturing enterprises with intelligent transformation
Ppde Q2 welcome | welcome 22 AI developers to join the propeller developer technical expert program!
Tutorial on principles and applications of database system (030) -- data integrity of MySQL (III): defining unique constraints
acwing 869. 试除法求约数
WebRTC系列-SDP之类关系梳理
LeetCode.558. 四叉树交集___分治
中职网络安全技能大赛P100-漏洞检测