本篇博文是使用 32 位的 MinGW 在 Windows 下编译 OpenCV 生成 32 位的 dll。
关于使用 64 位的 MinGW 编译 OpenCV 生成 64 位的 dll,见:OpenCV使用CMake和MinGW-w64的编译安装
Github . huihut/OpenCV-MinGW-Build
下载的 OpenCV 文件夹会有:
使用 MSVC 的话,直接在
build/x64/vc14
里面就有了,配置好路径就可以使用。
OpenCV 没有为我们编译好 MinGW 版,所以我们只能自己编译,下面就是介绍 MinGW 版的编译流程。
也可以直接下载使用我编译好了的 OpenCV (在上文)。
CMake 自行安装,Qt 和 MinGW 可以直接使用qt-opensource-windows-x86-5.9.3.exe
安装包安装,注意选择安装的组件(components)
的时候勾选 MinGW
:
安装好后打开QtCreator,在工具
-选项
-构建和运行
-构建套件
,选中Desktop Qt 5.9.3 MinGW 32bit
,设为默认,OK。
为系统变量 Path
添加 E:\Qt\Qt5.9.3\Tools\mingw530_32\bin
打开 cmake-gui,设置源码和生成路径:
E:/OpenCV_3.3.1/opencv/sources
E:/OpenCV_3.3.1/opencv-build
点击 Configure,设置编译器
MinGW Makefiles
E:\Qt\Qt5.9.3\Tools\mingw530_32\bin\gcc.exe
E:\Qt\Qt5.9.3\Tools\mingw530_32\bin\g++.exe
编译配置:
WITH_QT
WITH_OPENGL
WITH_IPP
点击 Configure,再次配置:
QT_MAKE_EXECUTABLE
为 E:\Qt\Qt5.9.3\5.9.3\mingw53_32\bin\qmake.exe
Qt5Concurrent_DIR
为 E:\Qt\Qt5.9.3\5.9.3\mingw53_32\lib\cmake\Qt5Concurrent
Qt5Core_DIR
为 E:\Qt\Qt5.9.3\5.9.3\mingw53_32\lib\cmake\Qt5Core
Qt5Gui_DIR
为 E:\Qt\Qt5.9.3\5.9.3\mingw53_32\lib\cmake\Qt5Gui
Qt5Test_DIR
为 E:\Qt\Qt5.9.3\5.9.3\mingw53_32\lib\cmake\Qt5Test
Qt5Widgets_DIR
为 E:\Qt\Qt5.9.3\5.9.3\mingw53_32\lib\cmake\Qt5Widgets
Qt5OpenGL_DIR
为 E:\Qt\Qt5.9.3\5.9.3\mingw53_32\lib\cmake\Qt5OpenGL
CMAKE_BUILD_TYPE
为 Release
或者 RelWithDebInfo
点击 Generate 生成 Makefile
打开终端进行编译:(-j
是使用 8
个线程进行编译,请根据你的计算机配置合理设置线程数)
E:
cd E:\OpenCV_3.3.1\opencv-build
mingw32-make -j 8
mingw32-make install
如果 mingw32-make -j 8
遇到错误,请看下面的 编译 OpenCV 常见错误,否则执行 mingw32-make install
,完成安装。
如果使用了多线程编译,导致错误,但是错误信息不明确,如:
Makefile:161: recipe for target 'all' failed
mingw32-make: *** [all] Error 2
使用单线程编译:
mingw32-make
以查看详细的错误提示,再根据具体情况解决。
... windres.exe: unknown option -- W ...
或者
FORMAT is one of rc, res, or coff, and is deduced from the file name
extension if not specified. A single file name is an input file.
No input-file is stdin, default rc. No output-file is stdout, default rc.
在 cmake-gui 编译配置中:
ENABLE_PRECOMPILED_HEADERS
然后重新Configure
-Generate
-mingw32-make
...opencv/sources/modules/videoio/src/cap_dshow.cpp...
... 'sprintf_instead_use_StringCbPrintfA_or_StringCchPrintfA' was not declared in this scope ...
或者
Makefile:161: recipe for target 'all' failed
mingw32-make: *** [all] Error 2
修改E:\OpenCV_3.3.1\opencv\sources\modules\videoio\src\cap_dshow.cpp
文件,在#include "DShow.h"
这行的上面加一行#define NO_DSHOW_STRSAFE
,如:
#define NO_DSHOW_STRSAFE
#include "DShow.h"
然后重新Configure
-Generate
-mingw32-make
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]
return s == nullptr || *s == 0;
^
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*)':
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
return s == nullptr || *s == 0;
^
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
mingw32-make[2]: *** [3rdparty/protobuf/CMakeFiles/libprotobuf.dir/src/google/protobuf/stubs/io_win32.cc.obj] Error 1
CMakeFiles\Makefile2:710: recipe for target '3rdparty/protobuf/CMakeFiles/libprotobuf.dir/all' failed
mingw32-make[1]: *** [3rdparty/protobuf/CMakeFiles/libprotobuf.dir/all] Error 2
Makefile:161: recipe for target 'all' failed
mingw32-make: *** [all] Error 2
在 cmake-gui 编译配置中:
ENABLE_CXX11
然后重新Configure
-Generate
-mingw32-make
Path
添加 E:\OpenCV_3.3.1\opencv-build\install\x86\mingw\bin
在 .pro
文件里面添加:
win32 {
INCLUDEPATH += E:\OpenCV_3.3.1\opencv-build\install\include
LIBS += E:\OpenCV_3.3.1\opencv-build\install\x86\mingw\bin\libopencv_*.dll
}
或者:(区分 debug 和 release 是因为 OpenCV 对其两者有不同的库,你需要把路径改为你自己的,我编译 MinGW 的 OpenCV 只有 release 库)
win32 {
INCLUDEPATH += E:\OpenCV_3.3.1\opencv-build\install\include
CONFIG(debug, debug|release): {
LIBS += E:\OpenCV_3.3.1\opencv-build\install\x86\mingw\bin\libopencv_*d.dll
} else:CONFIG(release, debug|release): {
LIBS += -LE:\OpenCV_3.3.1\opencv-build\install\x86\mingw\bin \ -llibopencv_core331 \ -llibopencv_highgui331 \ -llibopencv_imgcodecs331 \ -llibopencv_imgproc331 \ -llibopencv_features2d331 \ -llibopencv_calib3d331
}
}
然后在 MainWindow 中如下:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// read an image
cv::Mat image = cv::imread("E:/Pictures/H_white.png", 1);
// create image window named "My Image"
cv::namedWindow("My Image");
// show the image on window
cv::imshow("My Image", image);
}
MainWindow::~MainWindow()
{
delete ui;
}
最后运行起来了,效果如图: