qt 使用OpenCV

使用MinGW编译OpenCV源码
下载OpenCV源码CMake编译工具
编译安装完:
1
Qt工程配置

INCLUDEPATH += D:/qt_work/opencv/build/install/include/

LIBS += -LD:/qt_work/opencv/build/install/x86/mingw/bin \
    -lopencv_core411 \
    -lopencv_imgcodecs411 \
    -lopencv_highgui411

举例一

#include "opencv2/opencv.hpp"
using namespace cv;

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    Mat img = imread("D:/4.jpg");
    if (img.empty())
    {
        std::cout << "Error: load image failed." << std::endl;
        return;
    }
    imshow("myImage", img);
    waitKey(0);
}

2
举例二
Qt工程文件增加库文件引用

LIBS += -lopencv_imgproc411
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    Mat img = imread("D:/4.jpg");
    if (img.empty())
    {
        std::cout << "Error: load image failed." << std::endl;
        return;
    }

    if(img.channels()==3)
    {
        cvtColor(img,img,COLOR_BGR2RGB);
    }
    QImage qImag = QImage(img.data, img.cols, img.rows, QImage::Format_RGB888);
    ui->label->setPixmap(QPixmap::fromImage(qImag.scaled(ui->label->size())));
}

3

posted @ 2019-09-01 12:46  thomas_blog  阅读(307)  评论(0编辑  收藏  举报