学习 OpenCV2 – 用 QGraphicsView 显示 cv::Mat 图像

cv::Mat 转换为 QImage , 使用 QGraphicsView  和 QGraphicsScene 显示图像。

mainwindow.cpp

#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>

...

    using namespace cv;
    //read image
    Mat image = imread("lena.png");
	//cv::Mat BGR to Qimage RGB
    cvtColor(image,image,CV_BGR2RGB);
	//
    QImage img = QImage((const uchar*)(image.data), image.cols,image.rows,QImage::Format_RGB888);

    //Scene & View
    QGraphicsScene *scene = new QGraphicsScene();
    scene->addPixmap(QPixmap::fromImage(img));
	//
    ui->graphicsView->setScene(scene);
    ui->graphicsView->show();



也可以用 Item 添加图片

    QGraphicsPixmapItem *pixmapItem = new QGraphicsPixmapItem(QPixmap::fromImage(img));
    scene->addItem(pixmapItem);

项目文件:QtUIOpenCV

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.