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;
            
        }
    }
  • 相关阅读:
    【NIPS 2018】完整论文下载链接
    【今日CV 视觉论文速览】30 Nov 2018
    【超好玩的在线AI项目】浏览器里玩AI------持续更新
    hdu 4035 Maze 概率DP
    hdu 4089 Activation 概率DP
    hdu 4405 Aeroplane chess 概率DP
    zoj 3329 One Person Game 概率DP
    poj 2096 Collecting Bugs 概率DP
    poj 3744 Scout YYF I
    2013 Multi-University Training Contest 5 Partition
  • 原文地址:https://www.cnblogs.com/hsy1941/p/15040160.html
Copyright © 2011-2022 走看看