安装环境:Win10+CMake 3.22.2+TDM-GCC-64
从OpenCV主页的链接(Release)上下载对应的Sources文件
这次下载的是OpenCV 3.4.16
由于MinGW的代码托管在sourceforge上,而对应的Install.exe因为网络原因无法下载。
这次使用采用的是TDM-GCC-64
注意:这里的MinGW是64位的,且安装目录需没有空格。
1.打开Cmake,单击“Browse Source”,选择将对应之前OpenCV安装目录下的sources文件;单击“Browse Build”,选择opencv目录下build/x64目录,并新建一个MinGW的文件夹以存放编译生成的OpenCV文件。
2.单击“Configure”,此时会跳出选择编译器的窗口,请选择MinGW Makefiles模式,点击Specify native compilers选项,点击“Next”按钮。
3.再选择编译makefile相关文件的编译器,注意选择MinGW安装目录下bin文件夹中的x86_64-w64-mingw32-gcc.exe与x86_64-w64-mingw32-g++.exe作为编译文件的编译器(若没有说明所下载的MinGW不是64位版本),点击“Finish”。
4.在列表中找到ENABLE_CXX11项,对其Value值进行勾选。
5.在列表中找到OPENCV_ENABLE_ALLOCATOR_STATS这一项,将Value值取消勾选。
6.再次单击“Configure”,再次配置生成CMakeFile相关文件,此时列表背景由红色变为白色,单击"Generate",完成CMake对OpenCV的编译部分。
1.打开cmd,通过cd+OpenCV目录下的build/x64/MinGW,若未发生跳转,请输入对应磁盘盘符,如下在D盘,则输入“D:”。
2.输入“minGW32-make”命令对makefile相关文件进行编译,此过程耗时较长。
3.编译成功后,输入minGW32-make install命令对编译文件进行装载。
五、配置环境变量
将opencv目录下build/x64/MinGW/bin文件夹添加至系统变量Path中。
六、修改VS Code配置文件
需要修改的文件为:c_cpp_properties.json、launch.json、tasks.json。
launch.json
{ "version": "0.2.0", "configurations": [ { "name": "g++.exe - 生成和调试活动文件", "type": "cppdbg", "request": "launch", "program": "${fileDirname}\\${fileBasenameNoExtension}.exe", "args": [], "stopAtEntry": false, "cwd": "${fileDirname}", "environment": [], "externalConsole": true, "MIMode": "gdb", "miDebuggerPath": "D:\\Tools\\TDM-GCC-64\\bin\\gdb.exe", "setupCommands": [ { "description": "为 gdb 启用整齐打印", "text": "-enable-pretty-printing", "ignoreFailures": true }, { "description": "将反汇编风格设置为 Intel", "text": "-gdb-set disassembly-flavor intel", "ignoreFailures": true } ], "preLaunchTask": "C/C++: g++.exe 生成活动文件" } ] }
tasks.json
{ "tasks": [ { "type": "cppbuild", "label": "C/C++: g++.exe 生成活动文件", "command": "D:\\Tools\\TDM-GCC-64\\bin\\g++.exe", "args": [ "-fdiagnostics-color=always", "-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe", "-I","D:\\Program Files\\opencv\\build\\include", "-L","D:\\Program Files\\opencv\\build\\x64\\MinGW\\bin", "-l","libopencv_calib3d3416", "-l","libopencv_core3416", "-l","libopencv_dnn3416", "-l","libopencv_features2d3416", "-l","libopencv_flann3416", "-l","libopencv_highgui3416", "-l","libopencv_imgcodecs3416", "-l","libopencv_imgproc3416", "-l","libopencv_ml3416", "-l","libopencv_objdetect3416", "-l","libopencv_photo3416", "-l","libopencv_stitching3416", "-l","libopencv_video3416", "-l","libopencv_videoio3416" ], "options": { "cwd": "${fileDirname}" }, "problemMatcher": [ "$gcc" ], "group": { "kind": "build", "isDefault": true }, "detail": "调试器生成的任务。" } ], "version": "2.0.0" }
c_cpp_properties.json
{ "configurations": [ { "name": "Win32", "includePath": [ "${workspaceFolder}", "D:\\Program Files\\opencv\\build\\include" //OpenCV工作目录 ], "defines": [ "_DEBUG", "UNICODE", "_UNICODE" ], "compilerPath": "D:\\Tools\\TDM-GCC-64\\bin\\g++.exe", "intelliSenseMode": "gcc-x64", "browse": { "path": [ "${workspaceFolder}" ], "limitSymbolsToIncludedHeaders": true, "databaseFilename": "" }, "cStandard": "c11", "cppStandard": "c++17" } ], "version": 4 }
可以通过新建工作区的方式为OpenCV单独建立工作区。
测试代码为:
#include int main() { cv::Mat srcImage = cv::imread("1.jpg"); cv::imshow("Raw", srcImage); cv::waitKey(6000); //调用OpenCV的等待函数,等待6000ms return 0; }