Qwidget+opencv显示图像

步骤

1. 设置opencv库路径

在.pro文件中添加

INCLUDEPATH += D:/opencv/OpencvMingw/opencv310/include
LIBS += D:/opencv/OpencvMingw/opencv310/x86/mingw/lib/libopencv_*

2. 界面显示图像

首先在界面上添加一个QLabel,然后在代码中调用Opencv并显示图像

    Mat im0 = imread("d://dlrb.png");
    QImage image = Mat2QImage(im0);
    ui->label->move(0,0);
    ui->label->resize(im0.cols, im0.rows);
    ui->label->setPixmap(QPixmap::fromImage(image));

其中Mat2QImage定义如下

static QImage Mat2QImage(Mat& image)
{
    QImage img;

    if (image.channels()==3) {
        cvtColor(image, image, CV_BGR2RGB);
        img = QImage((const unsigned char *)(image.data), image.cols, image.rows,
                image.cols*image.channels(), QImage::Format_RGB888);
    } else if (image.channels()==1) {
        img = QImage((const unsigned char *)(image.data), image.cols, image.rows,
                image.cols*image.channels(), QImage::Format_ARGB32);
    } else {
        img = QImage((const unsigned char *)(image.data), image.cols, image.rows,
                image.cols*image.channels(), QImage::Format_RGB888);
    }

    return img;
}
posted @ 2017-03-31 10:15  mr-xbt  阅读(678)  评论(0编辑  收藏  举报