QT5.14.2 + Cmake3.18 + OPENCV3.4.3 + Opencv_contrib3.4.3

本文详细介绍了在Windows环境下使用CMake编译安装OpenCV 3.4.3的过程,包括安装QT、CMake、下载源码、配置环境、修改源文件、编译配置、生成Makefile、编译安装以及在QT中验证使用OpenCV的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


Windows平台下使用Cmake编译Opencv 3.4.3 源码

1、安装QT5.14.2,我已经安装好了,就不介绍了。

mingw730_64的版本
D:\Qt\Qt5.14.2\Tools\mingw730_64\bin

mingw32-make --version
GNU Make 4.2.1
Built for x86_64-w64-mingw32
Copyright (C) 1988-2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
g++ --version
g++ (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 7.3.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

其实以上版本信息在安装QT的时候都可以选择安装,这里我已经安装好了,在CMD获取以上信息需要将QT安装路径的D:\Qt\Qt5.14.2\Tools\mingw730_64\bin添加到环境变量PATH路径当中。

2、安装Cmake 3.18 版本

当然你也可以安装最新版本,安装成功之后,添加环境变量PATH路径D:\Program Files\CMake\bin

3、安装Opencv 3.4.3 和 Opencv_contrib-3.4.3

(1)下载 opencv 3.4.3Opencv_contrib-3.4.3

下载好压缩包重命名为opencv-3.4.3-source和opencv_contrib-3.4.3,分别解压这两个压缩包到路径D:\ ,然后在该路径下创建一个空文件夹opencv-3.4.3-build,用于存放编译后生成的文件和库。

cd /d D:\
mkdir opencv-3.4.3-build
cd opencv-3.4.3-build

(2)在cmake编译前需要准备以下工作

保证环境变量PATH要有以下路径值
在这里插入图片描述
安装python37
pip3 install numpy

(3)改动源文件

修改 D:\opencv-3.4.3-source\3rdparty\protobuf\src\google\protobuf\stubs\io_win32.cc文件,将“nullptr”改为“NULL”
在这里插入图片描述
修改 D:\Qt\opencv-3.4.3-source\modules\videoio\src\cap_dshow.cpp 文件,增加宏定义“#if defined _WIN32 && defined HAVE_DSHOW
在这里插入图片描述

修改 D:\Qt\opencv-3.4.3-source\modules\photo\test\test_hdr.cpp 文件,增加头文件包含 “#include <ctime>”和“#include <cstdlib>
在这里插入图片描述

(4)编译配置

设置路径

在这里插入图片描述

选择编译器

在这里插入图片描述
在这里插入图片描述

设置编译选项

确保勾选:WITH_OPENGL、WITH_QT、ENABLE_CXX11
取消勾选:WITH_MSMF、WITH_IPP、ENABLE_PRECOMPILED_HEADERS
上面勾选操作如下
在这里插入图片描述

找到CMAKE_BUILD_TYPE设置Release ,如果你选择Debug有可能编译过程中会出错。
在这里插入图片描述
找到OPENCV_EXTRA_MODULES_PATH设置D:/Qt/opencv_contrib-3.4.3/modules
在这里插入图片描述
保证Qt5xxx的前缀路径为D:/Qt/Qt5.14.2/5.14.2/mingw73_64/lib/cmake/xxx
在这里插入图片描述
修改 C:\Windows\System32\drivers\etc\hosts 文件
添加199.232.68.133 raw.githubusercontent.com
在这里插入图片描述
如果仍然提示下载未成功找不到文件,建议手动下载,但要保证将下载文件重命名为“MD5-文件名”
参考 CMake编译OpenCV4.0时opencv_ffmpeg.dll等下载失败的解决思路总结
例如:
D:\Qt\opencv-3.4.3-source.cache\ffmpeg
在这里插入图片描述
打开cmd,执行以下命令即可,前面的一串数字都是文件的MD5值。

certutil -hashfile xxx.dll

点击 Configure ,有可能会显示红色加亮的选项,反复点击Configure直到红色加亮的选项全部消失,直到出现Configuring done

点击 Generate 生成 Makefile,直到出现 Generating done

生成信息如下:

AVX_512F is not supported by C++ compiler
AVX512_SKX is not supported by C++ compiler
Dispatch optimization AVX512_SKX is not available, skipped
libjpeg-turbo: VERSION = 1.5.3, BUILD = opencv-3.4.3-libjpeg-turbo
Could not find OpenBLAS include. Turning OpenBLAS_FOUND off
Could not find OpenBLAS lib. Turning OpenBLAS_FOUND off
Could NOT find BLAS (missing: BLAS_LIBRARIES) 
LAPACK requires BLAS
A library with LAPACK API not found. Please specify library location.
Could NOT find JNI (missing: JAVA_AWT_LIBRARY JAVA_JVM_LIBRARY JAVA_INCLUDE_PATH JAVA_INCLUDE_PATH2 JAVA_AWT_INCLUDE_PATH) 
Could NOT find Matlab (missing: MATLAB_MEX_SCRIPT MATLAB_INCLUDE_DIRS MATLAB_ROOT_DIR MATLAB_LIBRARIES MATLAB_LIBRARY_DIRS MATLAB_MEXEXT MATLAB_ARCH MATLAB_BIN) 
VTK is not found. Please set -DVTK_DIR in CMake to VTK build directory, or to VTK install subdirectory with VTKConfig.cmake file
Caffe:   NO
Protobuf:   NO
Glog:   NO
freetype2:   NO
harfbuzz:    NO
Module opencv_ovis disabled because OGRE3D was not found
No preference for use of exported gflags CMake configuration set, and no hints for include/library directories provided. Defaulting to preferring an installed/exported gflags CMake configuration if available.
Failed to find installed gflags CMake configuration, searching for gflags build directories exported with CMake.
Failed to find gflags - Failed to find an installed/exported CMake configuration for gflags, will perform search for installed gflags components.
Failed to find gflags - Could not find gflags include directory, set GFLAGS_INCLUDE_DIR to directory containing gflags/gflags.h
Failed to find glog - Could not find glog include directory, set GLOG_INCLUDE_DIR to directory containing glog/logging.h
Module opencv_sfm disabled because the following dependencies are not found: Eigen Glog/Gflags
Excluding from source files list: <BUILD>/modules/dnn/layers/layers_common.avx512_skx.cpp
Tesseract:   NO
-c:1: DeprecationWarning: SO is deprecated, use EXT_SUFFIX


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

  Extra modules:
    Location (extra):            D:/Qt/opencv_contrib-3.4.3/modules
    Version control (extra):     unknown

  Platform:
    Timestamp:                   2020-08-10T03:36:59Z
    Host:                        Windows 10.0.18363 AMD64
    CMake:                       3.18.0
    CMake generator:             MinGW Makefiles
    CMake build tool:            D:/Qt/Qt5.14.2/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 (5 files):          + SSSE3 SSE4_1
      SSE4_2 (2 files):          + SSSE3 SSE4_1 POPCNT SSE4_2
      FP16 (2 files):            + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
      AVX (6 files):             + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
      AVX2 (11 files):           + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2

  C/C++:
    Built as dynamic libs?:      YES
    C++11:                       YES
    C++ Compiler:                D:/Qt/Qt5.14.2/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-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -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-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -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:                  D:/Qt/Qt5.14.2/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-narrowing -Wno-comment -Wimplicit-fallthrough=3 -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-narrowing -Wno-comment -Wimplicit-fallthrough=3 -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:          opengl32 glu32
    3rdparty dependencies:

  OpenCV modules:
    To be built:                 aruco bgsegm bioinspired calib3d ccalib core cvv datasets dnn dnn_objdetect dpm face features2d flann fuzzy hfs highgui img_hash imgcodecs imgproc java_bindings_generator line_descriptor ml objdetect optflow phase_unwrapping photo plot python3 python_bindings_generator reg rgbd saliency shape stereo stitching structured_light superres surface_matching text tracking ts video videoio videostab xfeatures2d ximgproc xobjdetect xphoto
    Disabled:                    js world
    Disabled by dependency:      -
    Unavailable:                 cnn_3dobj cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev freetype hdf java matlab ovis python2 sfm viz
    Applications:                tests perf_tests apps
    Documentation:               NO
    Non-free algorithms:         NO

  Windows RT support:            NO

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

  Media I/O: 
    ZLib:                        build (ver 1.2.11)
    JPEG:                        build-libjpeg-turbo (ver 1.5.3-62)
    WEBP:                        build (ver encoder: 0x020e)
    PNG:                         build (ver 1.6.34)
    TIFF:                        build (ver 42 - 4.0.9)
    JPEG 2000:                   build (ver 1.900.1)
    OpenEXR:                     build (ver 1.7.1)
    HDR:                         YES
    SUNRASTER:                   YES
    PXM:                         YES

  Video I/O:
    Video for Windows:           YES
    DC1394:                      NO
    FFMPEG:                      YES (prebuilt binaries)
      avcodec:                   YES (ver 57.107.100)
      avformat:                  YES (ver 57.83.100)
      avutil:                    YES (ver 55.78.100)
      swscale:                   YES (ver 4.8.100)
      avresample:                YES (ver 3.7.0)
    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:/Qt/opencv-3.4.3-source/3rdparty/include/opencl/1.2
    Link libraries:              Dynamic load

  Python 3:
    Interpreter:                 C:/Program Files (x86)/Microsoft Visual Studio/Shared/Python37_64/python.exe (ver 3.7.8)
    Libraries:                   C:/Program Files (x86)/Microsoft Visual Studio/Shared/Python37_64/libs/libpython37.a (ver 3.7.8)
    numpy:                       C:/Program Files (x86)/Microsoft Visual Studio/Shared/Python37_64/lib/site-packages/numpy/core/include (ver 1.19.1)
    packages path:               C:/Program Files (x86)/Microsoft Visual Studio/Shared/Python37_64/Lib/site-packages

  Python (for build):            C:/Program Files (x86)/Microsoft Visual Studio/Shared/Python37_64/python.exe

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

  Matlab:                        NO

  Install to:                    D:/Qt/opencv-3.4.3-build/install
-----------------------------------------------------------------

Configuring done
Generating done

4、开始编译和安装OpenCV库

以管理员方式打开CMD,定位到 opencv-3.4.3-build 目录下

cd /d opencv-3.4.3-build
mingw32-make -j 8

等待一段时间就可以了
在这里插入图片描述
如果 mingw32-make -j 8 遇到错误,请再认真看一看上面的配置是否有遗留
否则执行 mingw32-make install,完成安装。
在这里插入图片描述
命令窗口中输出安装过程及进度,安装过程比编译过程要快得多,很快就能安装好。此时打开opencv-3.4.3-build文件夹,可以发现其下已经编译生成了很多文件,其中有一个名为 install 的子目录,进入其中即“D:\Qt\opencv-3.4.3-build\install\x64\mingw\bin”下的所有文件就是编译安装好的OpenCV库文件,将它们复制到Qt项目的 Debug 目录下就可以使用了。最终得到的OpenCV库如图所示。
在这里插入图片描述

5、验证在QT中使用opencv

在这里插入图片描述

Test.pro

INCLUDEPATH += D:\Qt\opencv-3.4.3-build\install\include
LIBS += D:\Qt\opencv-3.4.3-build\install\x64\mingw\bin\libopencv_*.dll

在这里插入图片描述
创建资源目录

当前项目右键Add New…
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

mainwindows.h
在这里插入图片描述
mainwindows.cpp
在这里插入图片描述

int MainWindow::showImage(){

    QFile file(":/res/images/lion.jpg");
    if(!file.open(QFile::ReadOnly))
    {
        qDebug()<<"读取失败";
    }else
    {
        Mat temp;
        QImage Qimg;
        QByteArray qbyte = file.readAll();
        //srcImg = imread("D:/Projects/Qt5.14.2/Test/images/lion.jpg");
        srcImg  = imdecode(std::vector<char>(qbyte.begin(),qbyte.end()),1);
        cvtColor(srcImg, temp, COLOR_BGR2RGB);//BGR convert to RGB
        Qimg = QImage((const unsigned char*)(temp.data), temp.cols, temp.rows, temp.step, QImage::Format_RGB888);
        ui->label->setPixmap(QPixmap::fromImage(Qimg.scaled(ui->label->size(),Qt::KeepAspectRatio)));
        //ui->label->resize(Qimg.size());
        ui->label->show();
    }
}

main.cpp
在这里插入图片描述

在这里插入图片描述

参考

使用MinGW-W64从Windows安装opencv源代码
OpenCV使用CMake和MinGW-w64的编译安装
Installation from source for Windows with MinGW-W64

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值