本文章主要介绍基于Windows10下如何编译安装Opencv的过程,其中遇到过各种各样奇葩的Bugs,但最终在不断的尝试和分析搜索中完成了Opencv-MinGW开发环境的搭建,其主要目的是在没有安装搭建Visual Studio的情况下,基于VS Code对Opencv库的使用需求而做的开发环境,当然在这个过程中也对CMake相关编译调试过程有了进一步的深入了解,接下来我们开始正式进入主题:
一、基本材料准备
1. 预装Windows10系统PC一台
2. 下载最新版本Opencv4软件压缩包(opencv-4.2.0-vc14_vc15.exe)
3. 下载mingw64编译软件(tdm64-gcc-9.2.0)
4. 下载最新版本CMake软件(cmake-3.17.2-win64-x64.msi)
上述资源如果下载非常慢,请移步我的下载地址:
Opencv4_2+Mingw64+CMake.7z
下载完成之后,分别解压并安装各软件,我习惯将软件一般安装在D盘当中,注意在安装过程中勾选mingw64的环境变量,并在安装完成之后手动添加环境变量:
Notice1:注意在安装mingw64的过程当中一定要选择安装mingw-64,如果不知道安装那些包,请直接选择Full Installation即可。
Notice2:如果在mingw安装过程中未选择添加环境变量请手动添加mingw相关环境变量至Path中,如下所示:
Notice3:请注意,MinGW一定要是用的是MinGW64而不是MinGW,MinGW与MinGW64有着很大的区别,MinGW64是Cygwin的分支,其支持pthread的Linux多线程库,而对于MinGW则不支持,因此使用MinGW是无法编译通过的,出现mutex等结构体未定义等错误。
二、开始编译安装Opencv程序
1. 首先以管理员模式打开CMD命令行,切换到Opencv目录下(Source旁边),并新建编译文件夹:
cd /d D:\opencv\
mkdir mingw_build
2. 以管理员权限打开CMake软件,设置相关编译参数
A. 在这里需要配置好opencv的源代码路径以及编译文件夹,点击Configure即可触发配置,第一次配置需要输入编译器类型以及Makefile类型:
B. 由于我们使用的是MinGW的编译方式,需要生成的是MinGW Makefile,编译器由我们自己来指定-->Next:
C. 点击Next之后,就需要我们选择C编译器以及C++编译器的绝对路径了(注意在安装mingw64的时的位置):
D. 点击Finished,开始根据选项配置相关文件:
注:在这里如果没有配置Opencv源文件中的ffmpeg文件,则会出现报错信息:opencv_ffmpeg.dll等下载失败
解决办法如下:根据报错信息提供的相关文件的下载地址,手动下载相关文件到Opencv源代码的ffmpeg文件夹中,如下所示:
Opencv4.2-ffmpeg点击下载
在这里开始配置编译选项,如下:
- ENABLE_PRECONPILED_HEADERS 取消勾选
- BUILD_opencv_ts 取消勾选
- WITH_CUDA 取消勾选
配置完基本选项之后即可点击Generate开始生成MinGW Makefile
E. 生成好Makefile之后,我们开始编译Opencv源代码
注:在编译之前,需要在MinGW64-bin文件夹中添加libmingwex-0.dll文件(点击下载),在编译的过程中需要用到,否则编译失败,将文件放置在如下位置:
接下来正式开始编译:
F. 开始安装测试Opencv4.2
1) 编译结果:
2) 安装结果:
建立目标,准备安装
开始安装
3) 测试Opencv-MinGW64编译结果
首先配置好VSC的基本编译运行环境:
c_cpp_properties.json
{ "configurations": [ { "name": "Win32", "includePath": [ "${workspaceFolder}/**", "D:/TDM-GCC-64/include/*", "D:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/include/c++/*", "D:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/include/c++/x86_64-w64-mingw32/*", "D:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/include/c++/backward/*", "D:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/include/*", "D:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/include-fixed", "D:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/include", "D:/opencv/mingw_build/install/include", "D:/opencv/mingw_build/install/include/opencv2" ], "defines": [], "compilerPath":"D:/TDM-GCC-64/bin/gcc.exe", "cStandard": "c11", "cppStandard": "c++17", "intelliSenseMode": "clang-x64", "browse": { "path": [ "${workspaceFolder}", "D:/TDM-GCC-64/include/**", "D:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/include", "D:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/include/c++", "D:/opencv/mingw_build/install/include" ], "limitSymbolsToIncludedHeaders": true, "databaseFilename": "" } } ], "version": 4 }
launch.json
{ "version": "0.2.0", "configurations": [ { "name": "(gdb) Launch", // 配置名称,将会在启动配置的下拉菜单中显示 "type": "cppdbg", // 配置类型,这里只能为cppdbg "request": "launch", // 请求配置类型,可以为launch(启动)或attach(附加) "program": "${workspaceFolder}/${fileBasenameNoExtension}.exe", // 将要进行调试的程序的路径 "args": [], // 程序调试时传递给程序的命令行参数,一般设为空即可 "stopAtEntry": false, // 设为true时程序将暂停在程序入口处,一般设置为false "cwd": "${workspaceFolder}", // 调试程序时的工作目录,一般为${workspaceRoot}即代码所在目录 workspaceRoot已被弃用,现改为workspaceFolder "environment": [], "externalConsole": true, // 调试时是否显示控制台窗口,一般设置为true显示控制台 "MIMode": "gdb", "miDebuggerPath": "D:/TDM-GCC-64/bin/gdb.exe", // miDebugger的路径,注意这里要与MinGw的路径对应 "preLaunchTask": "g++", // 调试会话开始前执行的任务,一般为编译程序,c++为g++, c为gcc "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ] } ] }
tasks.json
{ "version": "2.0.0", "command": "g++", "args": [ "-g", "${file}", "-o", "${fileBasenameNoExtension}.exe", "-ID:/opencv/mingw_build/install/include", "-ID:/opencv/mingw_build/install/include/opencv2", "-LD:/opencv/mingw_build/install/x64/mingw/bin", "-LD:/opencv/mingw_build/install/x64/mingw/lib", "-llibopencv_calib3d420", "-llibopencv_core420", "-llibopencv_dnn420", "-llibopencv_features2d420", "-llibopencv_flann420", "-llibopencv_highgui420", "-llibopencv_imgcodecs420", "-llibopencv_imgproc420", "-llibopencv_ml420", "-llibopencv_objdetect420", "-llibopencv_photo420", "-llibopencv_stitching420", "-llibopencv_video420", "-llibopencv_videoio420", ], "problemMatcher": { "owner": "cpp", "fileLocation": [ "relative", "${workspaceFolder}" ], "pattern": { "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$", "file": 1, "line": 2, "column": 3, "severity": 4, "message": 5 } }, "group": { "kind": "build", "isDefault": true } }
添加Opencv相关动态库dll文件的环境变量:
编写基本的测试程序:
1 #include2 #include 3 #include 4 #include 5 6 using namespace std; 7 8 int main(void) 9 { 10 char s[] = "Hi,Cpp."; 11 int n = strlen(s); 12 printf("%s:%d\n",s,n); 13 14 cout << "Hello Opencv4.2" << endl; 15 cout << cv::COLOR_RGB2BGRA << cv::COLOR_RGB2GRAY << endl; 16 cv::Mat a = cv::imread("C:/Users/pc/Desktop/C_VSC/iron_man.jpg"); 17 cv::imshow("hello",a); 18 cv::waitKey(0); 19 return 0; 20 }
运行结果:
Cheers!终于大功告成啦,在使用mingw编译安装opencv的过程中碰到了各种各样的坑,零零散散也花费了将近一周的时间才编译安装完成,然后就是opencv的测试验证,也整理测试了可用的.vscode中的json文件,这里总结一下,在安装编译的过程中选择合适合理的版本是非常重要的,例如MinGW64的版本以及Opencv,CMake的版本,需要对Opencv的源代码有一个大致的了解,需要使用那些库等等(例如:pthread库,在MinGW中就没有,必须下载MinGW64)。
Reference:
1. mingw-w64相关问题:https://www.cnblogs.com/goushibao/p/6673450.html#4581646
2. 编译OpenCV4.0时opencv_ffmpeg.dll下载失败解决思路:https://www.cnblogs.com/huluwa508/p/10142718.html