Qt5.12与Opencv4.5环境搭建及配置(MinGW)笔记

Qt5.12与Opencv4.5环境搭建及配置(MinGW)笔记

Qt5.12下载及安装

从QT官网上下载qt-opensource-windows-x86-5.12.8版本,地址https://www.qt.io/

安装时注意勾选以下组件:

  • Qt-Qt5.12-MingGW 5.12.8 64 bit

  • Qt-Tools-MinGW 5.12.8

安装完成,配置Qt,打开QtCreator,在工具-选项-Kits-构建套件,选中Desktop Qt 5.12.8 MinGW,设置为默认

系统变量 Path中为MinGw添加环境变量(Qt安装目录下)

  • C:\Qt\Qt5.12.8\5.12.8\mingw73_64\bin

  • C:\Qt\Qt5.12.8\Tools\mingw730_64\bin

Opencv4.5下载及安装

从Opencv官网上下载,window下版本https://sourceforge.net/projects/opencvlibrary/files/4.5.0/opencv-4.5.0-vc14_vc15.exe/download

下载完成后将opencv压缩包提取到"D:/",D盘下会自动生成opencv文件下,下面有build和source两个子文件夹,build下有vc14和vc15两个适用于visual studio编译好的版本,需要要编译一个适用于Qt的MinGW版本。

Cmake下载及安装

从Cmake官网下载cmake-3.7.2-win64-x64.msihttps://cmake.org/download/,安装时勾选Add CMake to the system PATH for all users,即为CMake添加系统环境变量

使用CMake生成OpenCV的Makefile

打开 cmake-gui,设置源码和生成路径

  • where is the source code: D:/opencv/sources
  • where to build the binaries: D:/opencv/buildmingw

点击 Configure,设置编译器

  • Specify the generator for this project: MinGW Makefiles
  • Specify native compilers
  • Next
  • Compilers C: C:\Qt\Qt5.12.8\Tools\mingw730_64\bin\gcc.exe
  • Compilers C++: C:\Qt\Qt5.12.8\Tools\mingw730_64\bin\g++.exe
  • Finish

点击 Configure

编译配置:

  • 勾选 WITH_QT
  • 勾选 WITH_OPENGL
  • 不勾选 OPENCV_ENABLE_ALLOCATOR_STATS
  • CMAKE_BUILD_TYPE 选择 Release

点击 Configure,Generate 生成 Makefile,成功后会有“Configuring done
Generating done”

配置信息如下

General configuration for OpenCV 4.5.0 =====================================
  Version control:               unknown

  Platform:
    Timestamp:                   2020-11-15T04:33:30Z
    Host:                        Windows 10.0.19042 AMD64
    CMake:                       3.7.2
    CMake generator:             MinGW Makefiles
    CMake build tool:            C:/Qt/Qt5.12.8/Tools/mingw730_64/bin/mingw32-make.exe
    Configuration:               Release

  CPU/HW features:
    Baseline:                    SSE SSE2 SSE3
      requested:                 SSE3
    Dispatched code generation:  SSE4_1 SSE4_2 FP16 AVX AVX2
      requested:                 SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
      SSE4_1 (17 files):         + SSSE3 SSE4_1
      SSE4_2 (2 files):          + SSSE3 SSE4_1 POPCNT SSE4_2
      FP16 (1 files):            + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
      AVX (5 files):             + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
      AVX2 (31 files):           + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2

  C/C++:
    Built as dynamic libs?:      YES
    C++ standard:                11
    C++ Compiler:                C:/Qt/Qt5.12.8/Tools/mingw730_64/bin/g++.exe  (ver 7.3.0)
    C++ flags (Release):         -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG  -DNDEBUG
    C++ flags (Debug):           -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUG
    C Compiler:                  C:/Qt/Qt5.12.8/Tools/mingw730_64/bin/gcc.exe
    C flags (Release):           -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -O3 -DNDEBUG  -DNDEBUG
    C flags (Debug):             -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -g  -O0 -DDEBUG -D_DEBUG
    Linker flags (Release):      -Wl,--gc-sections  
    Linker flags (Debug):        -Wl,--gc-sections  
    ccache:                      NO
    Precompiled headers:         NO
    Extra dependencies:          glu32 opengl32
    3rdparty dependencies:

  OpenCV modules:
    To be built:                 calib3d core dnn features2d flann gapi highgui imgcodecs imgproc ml objdetect photo stitching ts video videoio
    Disabled:                    world
    Disabled by dependency:      -
    Unavailable:                 java js python2 python3
    Applications:                tests perf_tests apps
    Documentation:               NO
    Non-free algorithms:         NO

  Windows RT support:            NO

  GUI: 
    QT:                          YES (ver 5.12.8)
      QT OpenGL support:         YES (Qt5::OpenGL 5.12.8)
    Win32 UI:                    YES
    OpenGL support:              YES (glu32 opengl32)
    VTK support:                 NO

  Media I/O: 
    ZLib:                        build (ver 1.2.11)
    JPEG:                        build-libjpeg-turbo (ver 2.0.5-62)
    WEBP:                        build (ver encoder: 0x020f)
    PNG:                         build (ver 1.6.37)
    TIFF:                        build (ver 42 - 4.0.10)
    JPEG 2000:                   build (ver 2.3.1)
    OpenEXR:                     build (ver 2.3.0)
    HDR:                         YES
    SUNRASTER:                   YES
    PXM:                         YES
    PFM:                         YES

  Video I/O:
    DC1394:                      NO
    FFMPEG:                      NO
      avcodec:                   NO
      avformat:                  NO
      avutil:                    NO
      swscale:                   NO
      avresample:                NO
    GStreamer:                   NO
    DirectShow:                  YES

  Parallel framework:            none

  Trace:                         YES (built-in)

  Other third-party libraries:
    Lapack:                      NO
    Eigen:                       NO
    Custom HAL:                  NO
    Protobuf:                    build (3.5.1)

  OpenCL:                        YES (no extra features)
    Include path:                D:/opencv/sources/3rdparty/include/opencl/1.2
    Link libraries:              Dynamic load

  Python (for build):            NO

  Java:                          
    ant:                         NO
    JNI:                         NO
    Java wrappers:               NO
    Java tests:                  NO

  Install to:                    D:/opencv/buildmingw/install
-----------------------------------------------------------------

Configuring done
Generating done

编译OpenCV

打开PowerShell进行编译:(-j 是使用 8 个线程进行编译,请根据你的计算机配置合理设置线程数)

cd D:\opencv\buildmingw
mingw32-make -j 8
mingw32-make install

OpenCV(MinGW)添加环境变量

系统变量 Path中为OpenCV添加环境变量

  • D:\opencv\buildmingw\install\x64\mingw\bin

创建Qt Widgets Application

在xx.pro中添加如下配置

INCLUDEPATH += D:\opencv\buildmingw\install\include
CONFIG(debug, debug|release): {
LIBS += D:\opencv\buildmingw\install\x64\mingw\bin\libopencv_*d.dll
} else:CONFIG(release, debug|release): {
LIBS += -LD:\opencv\buildmingw\install\x64\mingw\bin\
    -llibopencv_core450 \
    -llibopencv_highgui450 \
    -llibopencv_imgcodecs450 \
    -llibopencv_imgproc450 \
    -llibopencv_features2d450 \
    -llibopencv_calib3d450
}

注意需使用Release构建程序(上面编译OPENCV_BUILD_TYPE选择了Release)

mainwindow.cpp代码如下

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include 
#include 

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

    // read an image
     cv::Mat image = cv::imread("C:\\Users\\wayne\\Desktop\\Image_20201109143933747.bmp",0);
     // create image window named "My Image"
     cv::namedWindow("My Image");
     // show the image on window
     cv::imshow("My Image", image);
}

MainWindow::~MainWindow()
{
    delete ui;
}

参考资料

  • 官方教程 https://wiki.qt.io/How_to_setup_Qt_and_openCV_on_Windows
  • OpenCV使用CMake和MinGW的编译安装及其在Qt配置运行 https://blog.csdn.net/huihut/article/details/78701814
  • https://blog.csdn.net/nohopenolove/article/details/106411477

你可能感兴趣的:(qt,opencv,cmake)