zoukankan      html  css  js  c++  java
  • 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;
            
        }
    }
  • 相关阅读:
    【逆向】《0day安全-软件漏洞分析技术》实验笔记2
    【逆向】《0day安全-软件漏洞分析技术》实验笔记1
    WorkWithPlus 13.15 升级!列表对象的优化效果显著!
    「版本更新」Genexus 16 Upgrade 9已发布!
    下一波数字化转型来临,我们需要选择更智能的开发工具
    太棒!企业和程序员都高兴!2-3个月打造全栈工程师
    GeneXus中如何使用聊天机器人
    「版本更新」GeneXus16 Upgrade 8的特性
    看完视频我终于明白:资深架构师角度的技术架构是这样!
    数据报告和分析:Dashboard
  • 原文地址:https://www.cnblogs.com/hsy1941/p/15040160.html
Copyright © 2011-2022 走看看