Qt + OpenCV 4.2.0 环境搭建

下载OpenCV安装包

在官网下载opencv-4.2.0-vc14_vc15.exe包安装

Qt用的是MSVC2015_64bit构建套件

安装包里已经有VS的编译好的bin,就省略编译。如果用的是MinGW,自己得用CMake编译下,剩余的问下度娘。

新建一个Qt Widgets Application

Qt + OpenCV 4.2.0 环境搭建_第1张图片

修改pro文件配置

  • 包含OpenCV头文件目录
INCLUDEPATH += D:\XQ\opencv\build\include
  • 引入静态库
LIBS += D:\XQ\opencv\build\x64\vc15\lib\opencv_world420d.lib

完整pro文件如下:

#-------------------------------------------------
#
# Project created by QtCreator 2020-04-06T22:55:20
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = RecognitionID
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0


SOURCES += main.cpp\
        mainwindow.cpp

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui

INCLUDEPATH += D:\XQ\opencv\build\include

LIBS += D:\XQ\opencv\build\x64\vc15\lib\opencv_world420d.lib

注意、注意、注意:
修改pro文件后,请记住qmake下,要不然你会哭!

main.cpp 中加入OpenCV 代码

代码如下:

#include "mainwindow.h"
#include 

#include 

using namespace cv;

int main(int argc, char *argv[])
{
//    QApplication a(argc, argv);
//    MainWindow w;
//    w.show();

//    return a.exec();

      VideoCapture capture(0);
      while(1)
      {
          Mat frame;
          capture>>frame;
          imshow("video",frame);
          waitKey(30);
      }

      return 0;
}

拷入opencv_world420d.dll到Debug目录

opencv_world420d.dll在安装目录的位置:opencv\build\x64\vc15\bin

编译后出效果

Qt + OpenCV 4.2.0 环境搭建_第2张图片

你可能感兴趣的:(OpenCV,环境配置)