zoukankan      html  css  js  c++  java
  • Qt QImage 与 Opencv Mat转换

    cv::Mat 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());
            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  MatToQImage(Mat mat)
    {
        if (mat.type() == CV_8UC1)
        {
            QImage image(mat.cols, mat.rows, QImage::Format_Indexed8);
            image.setColorCount(256);
            for (int i = 0; i < 256; i++)
            {
                image.setColor(i, qRgb(i, i, i));
            }
            uchar *pSrc = mat.data;
            for (int row = 0; row < mat.rows; row++)
            {
                uchar *pDest = image.scanLine(row);
                memcpy(pDest, pSrc, mat.cols);
                pSrc += mat.step;
            }
            return image;
        }
        else if (mat.type() == CV_8UC3)
        {
            const uchar *pSrc = (const uchar*)mat.data;
            QImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);
            return image.rgbSwapped();
        }
        else if (mat.type() == CV_8UC4)
        {
            //qDebug() << "CV_8UC4";
            const uchar *pSrc = (const uchar*)mat.data;
            QImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_ARGB32);
            return image.copy();
        }
        else
        {
            //qDebug() << "ERROR: Mat could not be converted to QImage.";
            return QImage();
        }



    }
    后知后觉、越学越菜
  • 相关阅读:
    学习:GridView中asp:BoundField的Visible=false时,无法取到这个字段的值
    C#读、写、删除注册表
    ERROR [IM002] [Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序
    Gridview隐藏列的取值问题
    SQL 2005导入EXCEL
    GridView使用LinkButton和Button两种方式的删除确认
    (转)ASP.Net的AccessDataSource设置错误"未将对象引用设置到对象的实例"的解决方案
    给GridView文本加上边框
    (转)从客户端中检测到有潜在危险的 Request.Form 值
    (转)Asp.net中的ServerVariables集合
  • 原文地址:https://www.cnblogs.com/chenhuanting/p/11678619.html
Copyright © 2011-2022 走看看