Qt 工程下使用 cmake MinGW OpenCV 4.1.1(涵盖 MinGW32 与 MinGW64)

OpenCV 官方没有编译好的 MinGW 版本,Qt 提供了 MinGW 编译方式,由于有一些开源项目利用的MinGW编译方式,想让 Qt 工程使用 MinGW OpenCV,需要自己使用 Cmake 编译 OpenCV

可选项:安装Git

你可以选择忽略此步,因为使用 cmd 与 PowerShell 也能进行,但是 OpenCV 4.1.1 的 Cmake 运用了 Chmod 使用 cmd 或 PowerShell 需要修改 Cmake 源码,后续会提到如何修改

选择安装平台 Windows 与你需要下载的版本,比如我安装的版本为 Git-2.24.0.2-64-bit,下载后打开一路点击 Next 最后 点击 Install 就行了
Git
Git

step1:下载Qt

Qt 提供 opensource 的版本,只需注册认证就能下载,也可以在我的百度云盘下载,密码:a0wh,我的版本是 Qt 5.12.5
Qt官网
下载打开后一路安装,选择所有插件全部安装,避免 Cmake 出现缺少插件的错误
,分别添加环境变量,配置 32 就只添加 32,配置 64 就删掉 32,只配置 64,因为 cmake 默认选项太多
环境变量

D:\programing\Qt5.12.5\Tools\mingw730_32\bin
D:\programing\Qt5.12.5\Tools\mingw730_64\bin

Step2:下载OpenCV源码

我下载的 OpenCV 4.1.1
opencv界面
选择要解压的地址:比如我的最后选择解压位置为:D:\programing\opencv

解压
解压后有如下文件夹解压后文件夹

Step3:下载Cmake

我下载的 cmake-3.15.5-win64-x64.zipCmake
解压文件,你可以选择将 bin 加入环境目录,这样你可以使用 cmd 命令行的方式进行编译
环境变量
注意你的环境变量,发现在我的环境变量下出现了

D:\programing\TDM_GCC_64\bin

TDM_GCC_64是我当时使用MATLAB时使用的,不过后面下载安装了MATLAB自带的插件,就没什么用了,但是,这个文件支持MinGW,为了不让cmake 自动选择系统环境变量下的文件进行(可能会产生 x86 与 64 的混合,然而 x64 支持 x86,x86 不支持 x64),可以选择删除,我直接删除了

Step4:配置

32 位的 MinGW 安装如下:

cmake 中会有一些警告,后续会说明,🐟红不慌,才是一个稳重程序员应有的样子
打开 cmake-gui 我的位置位于:

 D:\programing\cmake-3.15.5-win64-x64\cmake-3.15.5-win64-x64\bin

在 Cmake-gui 界面 两栏输入如下:
Where is the source code:D:/programing/opencv/sources (必须是source路径)
Where to build the binaries:D:/programing/opencv/OpenCVMinGW32 (这个文件夹可以自己任意指定,我编译的是 MinGW32 ,所以命名为 OpenCVMinGW32)
点击 Configure ,在下拉菜单中中选择 MinGW Makefiles 在四个选项中选择 Specify native compilers 点击 Next
步骤
步骤
在弹出的窗口选择本地 Compilers
C:D:/programing/Qt5.12.5/Tools/mingw730_32/bin/gcc.exe
C++:D:/programing/Qt5.12.5/Tools/mingw730_32/bin/g++.exe

点击 Finish步骤
坐等完成
步骤
完成后,有很多红色区域
步骤
勾选 WITH_OPENGL 与 WITH_QT,点击 Configure,出现红色部分,确认这些文件夹路径是否正确,再次点击 Configure,直到没有红色部分
步骤
步骤
点击 Generate,坐等直到完成
步骤
出现 Qt5 Dir NotFound,需要手动添加,例如我的文件夹如下,注意32位
Qt5_DIR:D:\programing\Qt5.12.5\5.12.5\mingw73_32\lib\cmake\Qt5
错误
每修改一次就 Configure 一次,直到 Configure done,没有错误,列表没有红色(或许会有一些无需在意的警告)
后面报错会讲解到

查看CMAKE_RC_COMPILER(windres.exe),这个配置隐藏较深,由于以前安装过树莓派配置文件或者什么原因,反正你要防止 cmake 使用默认环境变量含有MinGW的文件路径,我的文件目录如下(我甚至直接删除原来的环境变量),需要替换位 Qt 目录文件:
C:/Strawberry/c/bin/windres.exe → D:/programing/Qt5.12.5/Tools/mingw730_32/bin/windres.exe
你的目录应该如此:
目录
下面是我的配置列表:
注意查看C++ CompilerC CompilerCMake build tool是否为匹配的 32 位,是否位 Qt 目录下的文件,通过选择 CMAKE_BUILD_TYPE 的模式确定是 Debug 还是 Release,我选择的 Debug,如下:
配置

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

  Platform:
    Timestamp:                   2019-12-23T17:37:34Z
    Host:                        Windows 10.0.18362 AMD64
    CMake:                       3.15.5
    CMake generator:             MinGW Makefiles
    CMake build tool:            D:/programing/Qt5.12.5/Tools/mingw730_32/bin/mingw32-make.exe
    Configuration:               Debug

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

  C/C++:
    Built as dynamic libs?:      YES
    C++ Compiler:                D:/programing/Qt5.12.5/Tools/mingw730_32/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 -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -mfpmath=sse -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 -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -mfpmath=sse -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUG
    C Compiler:                  D:/programing/Qt5.12.5/Tools/mingw730_32/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 -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -mfpmath=sse -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 -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -mfpmath=sse -fvisibility=hidden -g  -O0 -DDEBUG -D_DEBUG
    Linker flags (Release):      -Wl,--gc-sections  
    Linker flags (Debug):        -Wl,--gc-sections  
    ccache:                      NO
    Precompiled headers:         YES
    Extra dependencies:          opengl32 glu32
    3rdparty dependencies:

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

  Windows RT support:            NO

  GUI: 
    QT:                          YES (ver 5.12.5)
      QT OpenGL support:         YES (Qt5::OpenGL 5.12.5)
    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 2.0.2-62)
    WEBP:                        build (ver encoder: 0x020e)
    PNG:                         build (ver 1.6.37)
    TIFF:                        build (ver 42 - 4.0.10)
    JPEG 2000:                   build (ver 1.900.1)
    OpenEXR:                     build (ver 2.3.0)
    HDR:                         YES
    SUNRASTER:                   YES
    PXM:                         YES
    PFM:                         YES

  Video I/O:
    DC1394:                      NO
    FFMPEG:                      YES (prebuilt binaries)
      avcodec:                   YES (58.35.100)
      avformat:                  YES (58.20.100)
      avutil:                    YES (56.22.100)
      swscale:                   YES (5.3.100)
      avresample:                YES (4.0.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:/programing/opencv/sources/3rdparty/include/opencl/1.2
    Link libraries:              Dynamic load

  Python (for build):            E:/Anaconda3/envs/tensorflow/python.exe

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

  Install to:                    D:/programing/opencv/OpenCVMinGW32/install
-----------------------------------------------------------------

Configuring done

点击 Generate
等待:

Generating done

step5:使用Cmake 命令编译

打开 cmd 进入 D:\programing\opencv\OpenCVMinGW32,或者 在 D:\programing\opencv\OpenCVMinGW32 下按住 Shift + 鼠标右键 打开 Powershell 或者按住 Shift + 鼠标右键 打开 Git Bash Here,输入 mingw32-makemingw32-make -j 8-j 8代表八个线程,能加快运行速度,但要确定拥有足够的运行空间,可以使用 CPU-Z 查看线程数量

CPU-Z下载:下载地址

使用 Qt 自带的 mingw32-make,32位的位置在:

D:\programing\Qt5.12.5\Tools\mingw730_32\bin\mingw32-make

你可以将其添加环境变量,如果不加入环境变量,在命令输入时需要添加文件路径,如下:

 D:\programing\Qt5.12.5\Tools\mingw730_32\bin\mingw32-make
 D:\programing\Qt5.12.5\Tools\mingw730_32\bin\mingw32-make install

为了避免多线程出错,我仍然选择单线程编译:
编译
运行时间较长,耐心等待,我自己运行时用的 PowerShell,手动滑稽 🙃

成功运行结果如下:
成功
成功后输入 mingw32-make install,成功后如下
成功
此时,你已经成功的 cmake 了 OpenCV 至 MinGW32 Debug,其 D:\programing\opencv\OpenCVMinGW32\install\x86\mingw\bin 文件夹下有各种 DLL 文件,如下图,install就是需要的文件夹(名称为 libopencv_*d.dll,说明为 Debug 模式):成功

错误解决问题1:

如果使用 mingw32-make -j 8 出现错误,优先改用 mingw32-make 尝试,这个错误在任意进度都有可能出现,你可以尝试先运行,如果能运行通过则不修改
错误

错误解决问题2:

如果使用的 cmd 与 PowerShell,由于Windows 平台不具有 Chmod 命令,所以你需要更改 Cmake 源码,更改如下:
在 D:\programing\opencv\sources\cmake\OpenCVPCHSupport错误ake 中311行处,注释或者删除如下第一行,更改为第二行
更改

#COMMAND chmod +x "${_pch_generate_file_cmd}"
${_pch_prepare_command}

错误
修正后再次 mingw32-makemingw32-make -j 8 再次坐等

你可以尝试先运行,如果能运行通过则不修改,不出意外的话,应该会在36%左右出错

错误解决问题3:

错误

最后还要拷贝 D:\programing\opencv\sources\modules\dnn\test\test_common.hpp 到 D:\programing\opencv\OpenCVMinGW32\modules\dnn 目录下

你可以尝试先运行,如果能运行通过则不修改,不出意外的话,应该会在93%左右出错

修正后再次 mingw32-makemingw32-make -j 8 再次坐等

错误解决问题4:

Anaconda 安装的 64 位 Python 与 32 位不兼容报错,在 cmake-gui 中取消勾选与 Python 有关的选项,在 Search 框中输入 python 取消掉:

BUILD_opencv_python3
BUILD_opencv_python_bindings_generator
BUILD_opencv_python_tests

错误
你可以尝试先运行,如果能运行通过则不修改,不出意外的话,应该会在100%左右出错
因为改变了 cmake 的选项,所以这次需要 Configure → Generate → mingw32-make 或 mingw32-make -j 8

错误解决问题5:

编译 opencv/sources/modules/core/perf/opencl/perf_arithm.cpp 时候出错,错误原因是too many sections,file too big,原因是 build 过程中生成的.obj文件是COFF(Common Object File Format)格式,而COFF文件的header里有两位是表示number of sections的,不能超过32766,而这里汇编生成的obj的section超过了这个值,所以直接报错提示file too big
错误
解决:

D:\programing\opencv\sources\CMakeLists.txt

添加如下:

if(MINGW)
  # Work around 'too many sections' error with MINGW/GCC
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wa,-mbig-obj")
endif()

添加
解决方案参考于:github
完成后再次 mingw32-make 或 mingw32-make -j 8

错误解决问题6:

warining:

CMake Warning at cmake/OpenCVGenSetupVars.cmake:54 (message):
  CONFIGURATION IS NOT SUPPORTED: validate setupvars script in install
  directory
Call Stack (most recent call first):
  CMakeLists.txt:1059 (include)

警告
这是 Python 路径不对,由于此次不需要 Python 所以不必理睬,python 具有自己的 opencv-python pip 安装方式,最好取消如下选项:

BUILD_opencv_python3
BUILD_opencv_python_bindings_generator
BUILD_opencv_python_tests

总结顺序如下:
添加 Qt 环境变量→去掉 python→勾选 WITH_QT→勾选 WITH_OPENGL→添加 QT_DIR→检查列表 exe→检查配置→编译→安装

64 位的 MinGW 安装如下:

添加环境变量:

D:\programing\Qt5.12.5\Tools\mingw730_64\bin

记住删除编译32时创建的环境变量
环境变量
选择 64 位的Qt gcc,g++,Configure,检查,勾选:
gcc g++
我的配置如下:
配置
我选择的 Release 模式

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

  Platform:
    Timestamp:                   2019-12-24T15:49:32Z
    Host:                        Windows 10.0.18362 AMD64
    CMake:                       3.15.5
    CMake generator:             MinGW Makefiles
    CMake build tool:            D:/programing/Qt5.12.5/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 (15 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 (29 files):           + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2

  C/C++:
    Built as dynamic libs?:      YES
    C++ Compiler:                D:/programing/Qt5.12.5/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 -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 -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:                  D:/programing/Qt5.12.5/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:         YES
    Extra dependencies:          opengl32 glu32
    3rdparty dependencies:

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

  Windows RT support:            NO

  GUI: 
    QT:                          YES (ver 5.12.5)
      QT OpenGL support:         YES (Qt5::OpenGL 5.12.5)
    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 2.0.2-62)
    WEBP:                        build (ver encoder: 0x020e)
    PNG:                         build (ver 1.6.37)
    TIFF:                        build (ver 42 - 4.0.10)
    JPEG 2000:                   build (ver 1.900.1)
    OpenEXR:                     build (ver 2.3.0)
    HDR:                         YES
    SUNRASTER:                   YES
    PXM:                         YES
    PFM:                         YES

  Video I/O:
    DC1394:                      NO
    FFMPEG:                      YES (prebuilt binaries)
      avcodec:                   YES (58.35.100)
      avformat:                  YES (58.20.100)
      avutil:                    YES (56.22.100)
      swscale:                   YES (5.3.100)
      avresample:                YES (4.0.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:/programing/opencv/sources/3rdparty/include/opencl/1.2
    Link libraries:              Dynamic load

  Python (for build):            E:/Anaconda3/envs/tensorflow/python.exe

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

  Install to:                    D:/programing/opencv/OpenCVMinGW64/install
-----------------------------------------------------------------

Configuring done
Generating done

在生成目录下打开 PowerShell 或 cmd 或 Git Bash Here,编译,安装:

D:\programing\Qt5.12.5\Tools\mingw730_64\bin\mingw32-make
D:\programing\Qt5.12.5\Tools\mingw730_64\bin\mingw32-make install

编译完成(MinGW64貌似时间挺长,可能我小破机的缘故吧):
编译
安装完成:
安装
最后将文件夹添加到环境变量

D:\programing\opencv\OpenCVMinGW32\install\x86\mingw\bin
D:\programing\opencv\OpenCVMinGW64\install\x64\mingw\bin

如果不添加在测试程序会报如下错误:

17:15:49: Starting C:\Users\HaoHao\Desktop\build-test-Desktop_Qt_5_12_5_MinGW_32_bit-Debug\debug\test.exe ...
17:15:49: The program has unexpectedly finished.
17:15:49: The process was ended forcefully.
17:15:49: C:/Users/HaoHao/Desktop/build-test-Desktop_Qt_5_12_5_MinGW_32_bit-Debug/debug/test.exe crashed.

错误
在 cmd 中输入 ECHO %PATH% 即可看见存在的环境变量
环境变量
测试程序如下创建:
创建一个新项目:Qt Widgets Application
项目
Kits 选择 MinGW 32-bit 或 MinGW 64-bit(我都测试过,可以运行,读者可以任选,或者都选)
项目
将一张图片放入项目的同级目录下:
例如我放的 Head.jpg文件夹
开始编写程序,首先在 .pro 文件里加入 INCLUDEPATH,LIBS,其余不用管,效果如下,我编写示例为加入 32位:

INCLUDEPATH += D:\programing\opencv\OpenCVMinGW32\install\include
LIBS += D:\programing\opencv\OpenCVMinGW32\install\x86\mingw\bin\libopencv_*d.dll

pro
编写 main.cpp,其余 cpp 文件不用管:

#include "mainwindow.h"
#include <QApplication>

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace cv;

int main(int argc, char *argv[])
{
    Mat image = imread("E:\\picture\\Head.jpg");

    namedWindow("My Image", 1);
    imshow("My Image", image);
    waitKey(0);

    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

再次确认 你选择运行的 Kits 是否为 MinGW 32-bit
Kits
最后运行效果如下
成功
这里有一个大坑,我在运行时出现了如下错误:
错误
因为我将图片放在项目文件夹后,我读入图片用的:

Mat image = imread("Head.jpg");

后来我想确认图片是否被读入,编写了如下验证代码:

#include "mainwindow.h"
#include <QApplication>

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

#include "iostream"
using namespace std;

using namespace cv;

int main(int argc, char *argv[])
{
    Mat image = imread("Head.jpg");

    if(!image.empty())
        printf("exist");
    if(image.empty())
        printf("empty");

    cout << endl << _pgmptr << endl;

    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

运行结果如下:
结果
可以看到在下方栏的输出为:

empty
C:\Users\HaoHao\Desktop\build-test-Desktop_Qt_5_12_5_MinGW_32_bit-Debug\debug\test.exe

说明图片未被读入,并打印了工程运行时的文件夹位置,当我把图片放在工程文件夹目录下时,运行成功了
文件
成功
注:运行截图中的 ASSERT failure in QCoreApplication: “there should be only one application object”, file kernel\qcoreapplication.cpp, line 792 是我使用 Ctrl + Alt + A 截图时由于按了 Alt 这个按键,这个按键相当于 Qt 里的一个快捷键,所以才报 failure,直接使用电脑按键 PrtSc 则不会出现 failure 截图如下:
截图
到此,OpenCV cmake MinGW32 与 MinGW 64 就结束了,作此记录想要生成 Release版本,同理操作,只需要修改 type 下拉选项为 Release

  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
CMake是一个跨平台的开源工具,用于管理和自动生成项目的构建脚本。而MinGW是一个Windows上的GNU工具集,提供了一系列开源的工具、库以及二进制文件,可用于开发Windows应用程序。OpenCV是一个开源的计算机视觉库,可用于处理图像和视频等视觉任务。 使用CMake配置MinGW编译环境和OpenCV库的步骤如下: 1. 安装CMakeMinGW:首先确保在计算机上安装了CMakeMinGWCMake可以通过官方网站下载,而MinGW可以通过其官网下载安装。 2. 下载OpenCV源代码:从OpenCV官方网站下载最新的源代码。 3. 打开CMake GUI:打开CMake GUI界面,选择下载的OpenCV源代码目录作为源代码路径,创建一个新的构建目录(例如build),指定构建目录作为生成的文件路径。 4. 配置构建:点击“Configure”按钮,选择MinGW Makefiles作为生成器,并在弹出的对话框中选择MinGW的安装路径。 5. 设置OpenCV选项:在配置完成后,可以设置OpenCV的一些选项,例如构建Java支持、构建Python支持等。 6. 点击“Generate”按钮:点击“Generate”按钮生成构建脚本。 7. 构建项目:在命令行或使用MinGW提供的终端工具进入构建目录,运行命令`mingw32-make` 开始构建OpenCV项目。 8. 完成编译编译完成后,在生成的文件路径中将得到可执行文件、静态库和动态库等。 通过以上步骤,你就可以使用CMakeMinGW成功配置和编译OpenCV库了。在具体的项目中,你可以使用OpenCV提供的各种函数和功能进行图像和视频处理,将其应用于计算机视觉任务中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值