• QImage,Mat ,QByteArray转换


    QImage 转为Mat

    void QImageToMat(QImage image, cv::Mat& mat)
    {
        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());
            std::vector<cv::Mat>channels;
            split(mat, channels);
            channels.pop_back();
            cv::merge(channels, mat);
        }
    
        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;
            
        }
    }
    void QByteArrayToMat(QByteArray array, cv::Mat& mat)
    {
        QBuffer buffer(&array);
        buffer.open(QIODevice::ReadOnly);
        QImageReader reader(&buffer, "JPG");
        QImage image = reader.read();
    
        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());
            std::vector<cv::Mat>channels;
            split(mat, channels);
            channels.pop_back();
            cv::merge(channels, mat);
        }
        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;
            
        }
    }

    QByteArray转为Mat

    void QByteArrayToMat(QByteArray array, cv::Mat& mat)
    {
        QBuffer buffer(&array);
        buffer.open(QIODevice::ReadOnly);
        QImageReader reader(&buffer, "JPG");
        QImage image = reader.read();
    
        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());
            std::vector<cv::Mat>channels;
            split(mat, channels);
            channels.pop_back();
            cv::merge(channels, mat);
        }
        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;
            
        }
    }
  • 相关阅读:
    数学系列:数学在计算机图形学中的应用
    数学系列:数学体系概览
    Math: Fibonacci
    算法系列:电磁频谱划分
    计算机系列:CUDA 深入研究
    算法系列:寻找最大的 K 个数
    算法系列:000
    算法系列:三元组和
    算法系列:单链表逆序
    堆栈区别
  • 原文地址:https://www.cnblogs.com/hsy1941/p/15040160.html
Copyright © 2020-2023  润新知