QImage与Mat格式相互转换

参考链接:https://blog.csdn.net/liyuanbhu/article/details/46662115

Qt 中处理图像主要用的是QImage类,OpenCV中主要用的是cv::Mat类。下面的两个函数可以用来实现这两个类相互转换。主要由两个函数实现:  QImage转Mat函数cv::Mat QImage2cvMat(QImage image)和Mat转QImage函数QImage Mat2QQImage(const cv::Mat& InputMat)。我将这两个函数封装成类MatConvertQImage,方便以后调用。

MatConvertQImage.h文件

#ifndef MATQIMAGE_HPP
#define MATQIMAGE_HPP
#include 
#include 
#include 


class MatConvertQImage
{
public:
    MatConvertQImage();
    ~MatConvertQImage();

    cv::Mat QImage2cvMat(QImage image);
    QImage Mat2QImage(const cv::Mat& InputMat);

};

#endif // MATQIMAGE_HPP

MatConvertQImage.cpp文件

#include "MatConvertQImage.h"
#include 
#include 

MatConvertQImage::MatConvertQImage()
{

}
MatConvertQImage::~MatConvertQImage()
{

}

cv::Mat MatConvertQImage::QImage2cvMat(QImage image)
{
    cv::Mat mat;
    //qDebug() << image.format();
    switch(image.format())
    {
    case QImage::Format_ARGB32:
    case QImage::Format_RGB32:
    case QImage::Format_ARGB32_Premultiplied:
        mat = cv::Mat(image.height(), image.width(), CV_8UC4, (void*)image.constBits(), image.bytesPerLine());
        break;
    case QImage::Format_RGB888:
        mat = cv::Mat(image.height(), image.width(), CV_8UC3, (void*)image.constBits(), image.bytesPerLine());
        cv::cvtColor(mat, mat, CV_BGR2RGB);
        break;
    case QImage::Format_Indexed8:
        mat = cv::Mat(image.height(), image.width(), CV_8UC1, (void*)image.constBits(), image.bytesPerLine());
        break;
    }
    return mat;
}

QImage MatConvertQImage::Mat2QImage(const cv::Mat& InputMat)

{

    cv::Mat TmpMat;

    // convert the color space to RGB
    if (InputMat.channels() == 1)

    {
        cv::cvtColor(InputMat, TmpMat, CV_GRAY2RGB);
    }

    else

    {
        cv::cvtColor(InputMat, TmpMat, CV_BGR2RGB);
    }


    // construct the QImage using the data of the mat, while do not copy the data

    QImage Result = QImage((const uchar*)(TmpMat.data), TmpMat.cols, TmpMat.rows,

                           QImage::Format_RGB888);

    // deep copy the data from mat to QImage

    Result.bits();

    return Result;


}
 测试文件main:

void test1()

{

    cv::Mat mat = cv::imread("Q:\\Koala.jpg", cv::IMREAD_UNCHANGED);

    cv::cvtColor(mat, mat, CV_BGR2BGRA);

    QImage image = cvMat2QImage(mat);

    qDebug() << (mat.type() == CV_8UC4);

    cvNamedWindow("cvMat2QImage RGB32", CV_WINDOW_AUTOSIZE);

    imshow("cvMat2QImage RGB32", mat);

    QLabel label;

    label.setPixmap(QPixmap::fromImage(image));

    label.show();

    cv::waitKey(10000);

}

void test2()

{

    cv::Mat mat = cv::imread("Q:\\Koala.jpg", cv::IMREAD_UNCHANGED);

    cv::cvtColor(mat, mat, CV_BGR2GRAY);

    QImage image = cvMat2QImage(mat);

    cvNamedWindow("cvMat2QImage gray", CV_WINDOW_AUTOSIZE);

    imshow("cvMat2QImage gray", mat);

    QLabel label;

    label.setPixmap(QPixmap::fromImage(image));

    label.show();

    cv::waitKey(10000);

}

void test3()

{

    QImage image("Q:\\Koala.jpg");

    image = image.convertToFormat(QImage::Format_RGB32);

    cv::Mat mat = QImage2cvMat(image);

    //cv::cvtColor(mat, mat, CV_BGR2RGB);

    imshow("QImage2cvMat RGB32", mat);

    cv::waitKey(10000);

}

 

void test4()

{

    QImage image("Q:\\Koala.jpg");

    image = image.convertToFormat(QImage::Format_RGB888);

    cv::Mat mat = QImage2cvMat(image);

    imshow("QImage2cvMat RGB24", mat);

    cv::waitKey(10000);

}

void test5()

{

    QImage image("Q:\\Koala.jpg");

    image = image.convertToFormat(QImage::Format_Indexed8);

    cv::Mat mat = QImage2cvMat(image);

    imshow("QImage2cvMat Indexed8", mat);

    cv::waitKey(10000);

}

int main(int argc, char *argv[])

{

    QApplication a(argc, argv);

    //test1();

    //test2();

    //test3();

    //test4();

    //test5();

    test1();

    return a.exec();

}
 

 

你可能感兴趣的:(Opencv,Qt,C++)