本文介绍了如何将opencv cv :: Mat转换为qimage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何将OpenCV C ++标准cv :: Mat类型转换为Qimage。我一直在寻找,但没有运气。我发现一些代码,将IPlimage转换为Qimage,但这不是我想要的。感谢

I am wondering how would I convert the OpenCV C++ standard cv::Mat type to Qimage. I have been searching around, but have no luck. I have found some code that converts the IPlimage to Qimage, but that is not what I want. Thanks

推荐答案

这里是24bit RGB和灰度浮点的代码。易于调整其他类型。它是一样高效的。

Here is code for 24bit RGB and grayscale floating point. Easily adjustable for other types. It is as efficient as it gets.

QImage Mat2QImage(const cv::Mat3b &src) {
        QImage dest(src.cols, src.rows, QImage::Format_ARGB32);
        for (int y = 0; y < src.rows; ++y) {
                const cv::Vec3b *srcrow = src[y];
                QRgb *destrow = (QRgb*)dest.scanLine(y);
                for (int x = 0; x < src.cols; ++x) {
                        destrow[x] = qRgba(srcrow[x][2], srcrow[x][1], srcrow[x][0], 255);
                }
        }
        return dest;
}


QImage Mat2QImage(const cv::Mat_<double> &src)
{
        double scale = 255.0;
        QImage dest(src.cols, src.rows, QImage::Format_ARGB32);
        for (int y = 0; y < src.rows; ++y) {
                const double *srcrow = src[y];
                QRgb *destrow = (QRgb*)dest.scanLine(y);
                for (int x = 0; x < src.cols; ++x) {
                        unsigned int color = srcrow[x] * scale;
                        destrow[x] = qRgba(color, color, color, 255);
                }
        }
        return dest;
}

这篇关于如何将opencv cv :: Mat转换为qimage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-17 06:16