Qt——OpenCV环境配置

under Qt  OpenCV  tag OpenCV    Published on April 16th , 2020 at 10:44 pm

前言

OpenCV官方给出的Windows库是使用MSVC编译器的,不能直接用在Mingw编译器的Qt项目上。所以要在使用Mingw编译器的Qt项目中配置OpenCV环境得手动编译一下。或者直接上GitHub下载已经编译好的库。

本文省略编译过程,采用的是GitHub上OpenCV-MinGW-Build开源库

Qt工程配置

打开工程中的.pro文件,复制进以下内容:

INCLUDEPATH +=  "E:\2_Technology\Program\OpenCV\OpenCV-MinGW-x64\include"  \
                "E:\2_Technology\Program\OpenCV\OpenCV-MinGW-x64\include\opencv2"



LIBS += -LE:/2_Technology/Program/OpenCV/OpenCV-MinGW-x64/x64/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);
}

示例二

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())));
}

本文由simyng创作, 采用知识共享署名4.0 国际许可协议进行许可,转载前请务必署名
  文章最后更新时间为:April 16th , 2020 at 02:44 pm