Mat 转 QImage

QImage MatToQImage(const cv::Mat& inMat)
{
    switch (inMat.type())
    {
    case CV_8UC4: 
    {
        QImage image(inMat.data,
            inMat.cols, inMat.rows,
            static_cast(inMat.step),
            QImage::Format_ARGB32);

        return image;
    }

    case CV_8UC3:
    {
        QImage image(inMat.data,
            inMat.cols, inMat.rows,
            static_cast(inMat.step),
            QImage::Format_RGB888);

        return image.rgbSwapped();
    }
    case CV_8UC1:// 8-bit, 1 channel
    {
#if QT_VERSION >= QT_VERSION_CHECK(5, 5, 0)
        QImage image(inMat.data,
            inMat.cols, inMat.rows,
            static_cast(inMat.step),
            QImage::Format_Grayscale8); 
#else
        static QVector  sColorTable;

        // only create our color table the first time
        if (sColorTable.isEmpty())
        {
            sColorTable.resize(256);

            for (int i = 0; i < 256; ++i)
            {
                sColorTable[i] = qRgb(i, i, i);
            }
        }

        QImage image(inMat.data,
            inMat.cols, inMat.rows,
            static_cast(inMat.step),
            QImage::Format_Indexed8);

        image.setColorTable(sColorTable);
#endif
        return image;
    }

    default:
        qWarning() << "error" << inMat.type();
        break;
    }

    return QImage();
}

你可能感兴趣的:(qt,开发语言)