opencv转qimage

opencv转qimage

 

#include <opencv2/opencv.hpp>
#include <QImage>
#include <QColor>

QImage MatToQImage(const cv::Mat &mat) {
    // 获取图像尺寸和通道数
    int width = mat.cols;
    int height = mat.rows;
    int channels = mat.channels();

    // 创建QImage对象
    QImage image(width, height, QImage::Format_RGB888); // 假设为BGR彩色图像

    // 复制数据到QImage
    for (int y = 0; y < height; ++y) {
        for (int x = 0; x < width; ++x) {
            cv::Vec3b pixel = mat.at<cv::Vec3b>(y, x); // BGR格式
            QRgb rgbPixel = qRgb(pixel[2], pixel[1], pixel[0]); // BGR转ARGB
            image.setPixel(x, y, rgbPixel);
        }
    }

    return image;
}

 

 

===============

posted @ 2024-01-18 00:05  西北逍遥  阅读(29)  评论(0编辑  收藏  举报