QT与OpenCV开发常用的图像格式转换函数:QImage转Mat、Mat转QImage、QImage转IplImage、IplImage转QImage

文章提供了在Qt和OpenCV开发中进行图像格式转换的函数,包括QImage转Mat、Mat转QImage、QImage转IplImage以及IplImage转QImage,便于在两者之间进行无缝操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在 Qt 和 OpenCV 开发中,常用的图像格式转换函数如下:

1. QImage 转 Mat:

Mat QImageToMat(const QImage &image, bool cloneImageData = true)
{
    Mat mat;
    switch (image.format())
    {
        case QImage::Format_ARGB32:
        case QImage::Format_ARGB32_Premultiplied:
            mat = Mat(image.height(), image.width(), CV_8UC4, const_cast<uchar*>(image.bits()), image.bytesPerLine());
            break;

        case QImage::Format_RGB32:
            mat = Mat(image.height(), image.width(), CV_8UC4, const_cast<uchar*>(image.bits()), image.bytesPerLine());
            cvtColor(mat, mat, COLOR_RGBA2BGRA);
            break;

        case QImage::Format_RGB888:
            mat = Mat(image.height(), image.width(), CV_8UC3, const_cast<uchar*>(image.bits()), image.bytesPerLine());
            cvtColor(mat, mat, COLOR_RGB2BGR);
            break;

        default:
            qWarning() << "Unsupported image format";
            break;
    }

    if (cloneImageData)
        return mat.clone();
    else
        return mat;
}

2. Mat 转 QImage:

QImage MatToQImage(const Mat &mat)
{
    QImage::Format format;
    switch (mat.type())
    {
        case CV_8UC4:
            format = QImage::Format_ARGB32;
            break;

        case CV_8UC3:
            format = QImage::Format_RGB888;
            break;

        default:
            qWarning() << "Unsupported matrix type";
            return QImage();
    }

    QImage image(mat.data, mat.cols, mat.rows, static_cast<int>(mat.step), format);
    return image.copy();
}

3. QImage 转 IplImage(需要包含 <opencv2/opencv.hpp> 头文件):

IplImage *QImageToIplImage(const QImage &image)
{
    IplImage *iplImg = cvCreateImage(cvSize(image.width(), image.height()), IPL_DEPTH_8U, 3);

    for (int y = 0; y < image.height(); ++y)
    {
        const uchar* qImageLine = image.constScanLine(y);
        uchar* iplImgLine = reinterpret_cast<uchar*>(iplImg->imageData + y * iplImg->widthStep);
        memcpy(iplImgLine, qImageLine, image.bytesPerLine());
    }

    return iplImg;
}

4. IplImage 转 QImage(需要包含 <opencv2/opencv.hpp> 头文件):

QImage IplImageToQImage(const IplImage *iplImg)
{
    QImage::Format format;
    switch (iplImg->nChannels)
    {
        case 1:
            format = QImage::Format_Grayscale8;
            break;

        case 3:
            format = QImage::Format_RGB888;
            break;

        default:
            qWarning() << "Unsupported IplImage format";
            return QImage();
    }

    QImage image(iplImg->width, iplImg->height, format);
    for (int y = 0; y < iplImg->height; ++y)
    {
        const uchar* iplImgLine = reinterpret_cast<const uchar*>(iplImg->imageData + y * iplImg->widthStep);
        uchar* qImageLine = image.scanLine(y);
        memcpy(qImageLine, iplImgLine, iplImg->widthStep);
    }

    return image;
}

上述代码提供了常见的图像格式转换函数,可以在 Qt 和 OpenCV 之间进行图像格式转换。


 欢迎大家关注我的微信公众号,不定期分享嵌入式开发的相关知识

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Alen.Wang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值