学习OpenCV3:Cmake+MinGW编译OpenCV


1. 准备

1.1 下载OpenCV源码

  1. OpenCV下载地址1:https://opencv.org/releases/
官网下载
  1. OpenCV下载地址2:https://github.com/opencv/opencv/releases
github下载
  1. OpenCV下载地址3:https://github.com/opencv/opencv
OpenCV源码

提示:下载地址1和下载地址2可以直接安装OpenCV,其目录自动包含OpenCV的源码,也可直接从下载地址3下载OpenCV的源码,若网速太慢或不能下载可以参考学习OpenCV3:解决github下载速度慢的方法(码云)。

OpenCV目录
  1. opencv_contrib下载地址:https://github.com/opencv/opencv_contrib
opencv_contrib

1.2 下载安装CMake

CMake下载地址:https://cmake.org/download/

CMake官网下载
CMake目录

1.3 下载安装MinGW

MinGW下载地址:https://osdn.net/projects/mingw/releases/

mingw-get-setup.exe
MinGW目录

提示:将MinGW的bin目录(C:\Mingw-w64\mingw32\bin)添加系统变量的path中。

2. CMake生成OpenCV的Makefile

  1. 打开cmake-gui.exe
cmake-gui.exe
  1. 将opencv_contrib存放opencv目录下,新建bulid_mingw用于存放编译后OpenCV。
opencv_contrib和bulid_mingw
  1. 填写源码地址D:\opencv\sources和目标地址D:\opencv\bulid_mingw,点击Configue。选择MinGW Makefiles,使用默认选项Use default native compilers,点击Finish注意初次Configue时必然有错误提示。
Cmake
MinGW Makefiles
初次Configue结果
  1. 找到OPENCV_EXTRA_MODULES_PATH并添加opencv_contrib的modules目录(D:\opencv\opencv_contrib\modules)。勾选BUILD_opencv_word,避免编译生成过多的lib文件(实际是将所有lib文件都综合到opencv_word)。多次点击Configue直到不再报错。最后点击Generate
OPENCV_EXTRA_MODULES_PATH
BUILD_opencv_word
Generate结果
  1. 无法下载opencv_videoio_ffmpeg.dll等文件?
无法下载opencv_videoio_ffmpeg.dll

解决方法:学习OpenCV3:Cmake编译OpenCV时无法下载ffmpeg、xfeatures2d等文件

  1. validate setupvars script in install?
validate setupvars script in install

解决方法:可忽略,或者取消勾选OPENCV_GENERATE_SETUPVARS。

取消勾选OPENCV_GENERATE_SETUPVARS

3. MinGW编译OpenCV

  1. 编译命令
// 调整到bulid_mingw目录
cd /d D:\opencv\bulid_mingw
// 生成
mingw32-make
// 将生成的文件存在到install中
mingw32-make install
mingw32-make开始
mingw32-make结束
mingw32-make install
install文件夹
  1. vs_version.rc.obj ?
gcc: error: long: No such file or directory
[ 65% ] Building RC object modules/world/CMakeFiles/opencv_world.dir/vs_version.rc.obj
gcc: error: long: No such file or directory
C:\Mingw-w64\mingw32\bin\windres.exe: preprocessing failed.
mingw32-make[2]: *** [modules\world\CMakeFiles\opencv_world.dir\build.make:11240: modules/world/CMakeFiles/opencv_world.dir/vs_version.rc.obj] Error 1
mingw32-make[1]: *** [CMakeFiles\Makefile2:4789: modules/world/CMakeFiles/opencv_world.dir/all] Error 2
mingw32-make: *** [Makefile:179: all] Error 2

解决方法:学习OpenCV3:MinGW编译OpenCV到vs_version.rc.obj处出错

你可能感兴趣的:(学习OpenCV3:Cmake+MinGW编译OpenCV)