Opencv 使用cmake和mingw 编译安装

末蓝、 2022-05-21 02:23 600阅读 0赞

转自 opencv 使用cmake he mingw编译安装

软件环境

  • Windows-10-64bit
  • Qt-5.9.3
  • MinGW-5.3.0-32bit
  • CMake-3.9.2
  • OpenCV-3.3.1 / 3.4.1(适用)

编译好的 OpenCV(MinGW 版):

Github . huihut/OpenCV-MinGW-Build

OpenCV 的 MSVC 版及 MinGW 版

MSVC 版

下载的 OpenCV 文件夹会有:

  • build (已编译好的库)
  • sources (源码)

使用 MSVC 的话,直接在

  1. build/x64/vc14

里面就有了,配置好路径就可以使用。

MinGW 版

OpenCV 没有为我们编译好 MinGW 版,所以我们只能自己编译,下面就是介绍 MinGW 版的编译流程。

也可以直接下载使用我编译好了的 OpenCV (在上文)。

安装及配置 Qt、MinGW、CMake

安装

CMake 自行安装,Qt 和 MinGW 可以直接使用qt-opensource-windows-x86-5.9.3.exe 安装包安装,注意选择安装的组件(components)的时候勾选 MinGW :

  • Qt-Qt5.9-MingGW 5.3.0 32 bit
  • Qt-Tools-MinGW 5.3.0

配置 Qt、MinGW

安装好后打开QtCreator,在工具-选项-构建和运行-构建套件,选中Desktop Qt 5.9.3 MinGW 32bit,设为默认,OK。

添加 MinGW 到环境变量

为系统变量 Path 添加 E:\Qt\Qt5.9.3\Tools\mingw530_32\bin

使用 CMake 生成 OpenCV 的 Makefile

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

  • Where is the source code: E:/OpenCV_3.3.1/opencv/sources
  • Where to build the binaries: E:/OpenCV_3.3.1/opencv-build

点击 Configure,设置编译器

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

编译配置:

  • 勾选 WITH_QT
  • 勾选 WITH_OPENGL
  • 不勾选 WITH_IPP

点击 Configure,再次配置:

  • 设置 QT_MAKE_EXECUTABLEE:\Qt\Qt5.9.3\5.9.3\mingw53_32\bin\qmake.exe
  • 设置 Qt5Concurrent_DIRE:\Qt\Qt5.9.3\5.9.3\mingw53_32\lib\cmake\Qt5Concurrent
  • 设置 Qt5Core_DIRE:\Qt\Qt5.9.3\5.9.3\mingw53_32\lib\cmake\Qt5Core
  • 设置 Qt5Gui_DIRE:\Qt\Qt5.9.3\5.9.3\mingw53_32\lib\cmake\Qt5Gui
  • 设置 Qt5Test_DIRE:\Qt\Qt5.9.3\5.9.3\mingw53_32\lib\cmake\Qt5Test
  • 设置 Qt5Widgets_DIRE:\Qt\Qt5.9.3\5.9.3\mingw53_32\lib\cmake\Qt5Widgets
  • 设置 Qt5OpenGL_DIRE:\Qt\Qt5.9.3\5.9.3\mingw53_32\lib\cmake\Qt5OpenGL
  • 设置 CMAKE_BUILD_TYPERelease 或者 RelWithDebInfo

点击 Generate 生成 Makefile

OpenCVCMakeGenerate

编译 OpenCV

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

  1. E:
  2. cd E:\OpenCV_3.3.1\opencv-build
  3. mingw32-make -j 8mingw32-make install

如果 mingw32-make -j 8 遇到错误,请看下面的 编译 OpenCV 常见错误,否则执行 mingw32-make install,完成安装。

编译 OpenCV 常见错误

0. 多线程编译错误信息不明确

表现

如果使用了多线程编译,导致错误,但是错误信息不明确,如:

  1. Makefile:161: recipe for target 'all' failed
  2. mingw32-make: *** [all] Error 2

MakeOpenCV\_cap\_dshow

解决

使用单线程编译:

  1. mingw32-make

以查看详细的错误提示,再根据具体情况解决。

1. RC 错误

表现

  1. ... windres.exe: unknown option -- W ...

或者

  1. FORMAT is one of rc, res, or coff, and is deduced from the file name
  2. extension if not specified. A single file name is an input file.
  3. No input-file is stdin, default rc. No output-file is stdout, default rc.

MakeOpenCVRCError

解决

在 cmake-gui 编译配置中:

  • 不勾选 ENABLE_PRECOMPILED_HEADERS

然后重新Configure-Generate-mingw32-make

2. sprintf_instead_use_StringCbPrintfA_or_StringCchPrintfA 错误

表现

  1. ...opencv/sources/modules/videoio/src/cap_dshow.cpp...
  2. ... 'sprintf_instead_use_StringCbPrintfA_or_StringCchPrintfA' was not declared in this scope ...

或者

  1. Makefile:161: recipe for target 'all' failed
  2. mingw32-make: *** [all] Error 2

MakeOpenCV\_cap\_dshow

解决

修改E:\OpenCV_3.3.1\opencv\sources\modules\videoio\src\cap_dshow.cpp文件,在#include "DShow.h"这行的上面加一行#define NO_DSHOW_STRSAFE,如:

  1. #define NO_DSHOW_STRSAFE
  2. #include "DShow.h"

然后重新Configure-Generate-mingw32-make

3. identifier ‘nullptr’ is a keyword in C++11 错误【2018年3月2日编译OpenCV 3.4.1时遇到并解决】

表现

  1. D:\opencv-3.4.1\opencv-3.4.1\3rdparty\protobuf\src\google\protobuf\stubs\io_win32.cc:94:3: warning: identifier 'nullptr' is a keyword in C++11 [-Wc++0x-compat]
  2. return s == nullptr || *s == 0;
  3. ^
  4. D:\opencv-3.4.1\opencv-3.4.1\3rdparty\protobuf\src\google\protobuf\stubs\io_win32.cc: In function 'bool google::protobuf::internal::win32::{ anonymous}::null_or_empty(const char_type*)':
  5. D:\opencv-3.4.1\opencv-3.4.1\3rdparty\protobuf\src\google\protobuf\stubs\io_win32.cc:94:15: error: 'nullptr' was not declared in this scope
  6. return s == nullptr || *s == 0;
  7. ^
  8. 3rdparty\protobuf\CMakeFiles\libprotobuf.dir\build.make:412: recipe for target '3rdparty/protobuf/CMakeFiles/libprotobuf.dir/src/google/protobuf/stubs/io_win32.cc.obj' failed
  9. mingw32-make[2]: *** [3rdparty/protobuf/CMakeFiles/libprotobuf.dir/src/google/protobuf/stubs/io_win32.cc.obj] Error 1
  10. CMakeFiles\Makefile2:710: recipe for target '3rdparty/protobuf/CMakeFiles/libprotobuf.dir/all' failed
  11. mingw32-make[1]: *** [3rdparty/protobuf/CMakeFiles/libprotobuf.dir/all] Error 2
  12. Makefile:161: recipe for target 'all' failed
  13. mingw32-make: *** [all] Error 2

MakeOpenCV\_nullptr\_Error

解决

在 cmake-gui 编译配置中:

  • 勾选 ENABLE_CXX11

然后重新Configure-Generate-mingw32-make

添加 OpenCV 编译的库到环境变量

  • 为系统变量 Path 添加 E:\OpenCV_3.3.1\opencv-build\install\x86\mingw\bin

新建 OpenCV 的 Qt 项目

.pro 文件里面添加:

  1. win32 {
  2. INCLUDEPATH += E:\OpenCV_3.3.1\opencv-build\install\include
  3. LIBS += E:\OpenCV_3.3.1\opencv-build\install\x86\mingw\bin\libopencv_*.dll
  4. }

或者:(区分 debug 和 release 是因为 OpenCV 对其两者有不同的库,你需要把路径改为你自己的,我编译 MinGW 的 OpenCV 只有 release 库)

  1. win32 {
  2. INCLUDEPATH += E:\OpenCV_3.3.1\opencv-build\install\include
  3. CONFIG(debug, debug|release): {
  4. LIBS += E:\OpenCV_3.3.1\opencv-build\install\x86\mingw\bin\libopencv_*d.dll
  5. } else:CONFIG(release, debug|release): {
  6. LIBS += -LE:\OpenCV_3.3.1\opencv-build\install\x86\mingw\bin \ -llibopencv_core331 \ -llibopencv_highgui331 \ -llibopencv_imgcodecs331 \ -llibopencv_imgproc331 \ -llibopencv_features2d331 \ -llibopencv_calib3d331
  7. }
  8. }

配置也可以这样写

打开pro文件 添加如下:

  1. INCLUDEPATH += G:\opencv\opencv\cmake4\install\include\
  2. INCLUDEPATH += G:\opencv\opencv\cmake4\install\include\opencv\
  3. INCLUDEPATH += G:\opencv\opencv\cmake4\install\include\opencv2\
  4. LIBS += G:\opencv\opencv\cmake4\lib\libopencv_*.a

OpenCVMinGWBin

然后在 MainWindow 中如下:

  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include <opencv2/core/core.hpp>
  4. #include <opencv2/highgui/highgui.hpp>
  5. MainWindow::MainWindow(QWidget *parent) :
  6. QMainWindow(parent),
  7. ui(new Ui::MainWindow)
  8. {
  9. ui->setupUi(this);
  10. // read an image
  11. cv::Mat image = cv::imread("E:/Pictures/H_white.png", 1);
  12. // create image window named "My Image"
  13. cv::namedWindow("My Image");
  14. // show the image on window
  15. cv::imshow("My Image", image);
  16. }
  17. MainWindow::~MainWindow()
  18. {
  19. delete ui;
  20. }

最后运行起来了,效果如图:

OpenCVQtDemo

官方教程

  • How to setup Qt and openCV on Windows

发表评论

表情:
评论列表 (有 0 条评论,600人围观)

还没有评论,来说两句吧...

相关阅读