头文件
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);