函数方法封装——图片类型QPixmap、QImage与Mat的相互转化

头文件
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);
  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值