环境
一、下载openCV并 配置
将下载的opencv-4.5.0-vc14_vc15.exe双击,解压到D盘,在opencv\build\x64目录里新建一个mingw文件夹
二、下载cmake ,安装cmake
安装比较简单,一路next,注意添加path环境变量,选add cmake to the system path for all user,如下所示
三、MinGw 配置
下载mingw8.1
配置环境变量 path
: 新增 D:\mingw\bin
四、cmake配置
点configure按钮,按如下选择
选择 MinGW 中的 C 和 C++ 编译的exe程序 我的是在D:\mingw64\bin
设置完毕点击 Finish,耐心等待进度条读完,当看到 Configuring done。再次点击 Configure 等待进度条读完。之后点击Generate 稍等一会,出现 Generating done。然后,可以看到 opencv\build\x64\MinGW有很多文件
如编译的时候出现这问题,在cmake里面找到OPENCV_ENABLE_ALLOCATOR_STATS或者在search里面搜索,然后把后面的勾选去掉,接着Configure和Generate后,接着在cmd窗口中mingw32-make即可继续编译
五、编译安装
打开CMD命令窗口,cd D:\opencv\build\x64\MinGW
,输入 minGW32-make
命令 ,使用多线程minGW32-make -j 8, 大概花40 分钟左右的时间来编译,编译完成之后,输入 minGW32-make install
来完成装载。
然后,配置环境变量 path
:新增 D:\opencv\build\x64\MinGW\bin
最后贴出我的三个环境变量总结如下图
六、项目配置
1、在vsCode中新建一个目录并打开,新建 test.cpp
#include<opencv2/opencv.hpp>
#include<opencv2/highgui.hpp>
using namespace cv;
int main()
{
Mat img=imread("C:\\Users\\axin\\Desktop\\sad.jpg");
cv::imshow("image",img);
cv::waitKey();
return 0;
}
2、F5运行下,会自动生成 launch.json
文件,copy下面代码复制到 launch.json
中,将 "miDebuggerPath"
改成自己的
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/${fileBasenameNoExtension}.o",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "D:/mingw64/bin/gdb.exe",
"preLaunchTask": "g++",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},
]
}
3、在.vscode
文件夹下,新建 c_cpp_properties.json
,copy 如下代码,修改includePath
的后三个路径为自己的,compilerPath
同理。
{
"configurations": [
{
"name": "win",
"includePath": [
"${workspaceFolder}/**",
"D:\\opencv\\build\\include",
"D:\\opencv\\build\\include\\opencv2",
"D:\\opencv\\build\\include\\opencv"
],
"defines": [],
"compilerPath": "D:/mingw64/bin/gcc.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
4、在.vscode
文件夹下,新建 tasks.json
,copy以下代码,修改 “-I” 和 “-L” 的路径。
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"command": "g++",
"args": [
"-g",
"-std=c++11",
"${file}",
"-o",
"${fileBasenameNoExtension}.o",
"-I", "D:\\opencv\\build\\include",
"-I", "D:\\opencv\\build\\include\\opencv2",
"-I", "D:\\opencv\\build\\include\\opencv",
"-L", "D:\\opencv\\build\\x64\\MinGW\\lib",
"-l", "opencv_core450",
"-l", "opencv_imgproc450",
"-l", "opencv_imgcodecs450",
"-l", "opencv_video450",
"-l", "opencv_ml450",
"-l", "opencv_highgui450",
"-l", "opencv_objdetect450",
"-l", "opencv_flann450",
"-l", "opencv_imgcodecs450",
"-l", "opencv_photo450",
"-l", "opencv_videoio450"
],// 编译命令参数
"problemMatcher":{
"owner": "cpp",
"fileLocation":[
"relative",
"${workspaceFolder}"
],
"pattern":[
{
"regexp": "^([^\\\\s].*)\\\\((\\\\d+,\\\\d+)\\\\):\\\\s*(.*)$",
"file": 1,
"location": 2,
"message": 3
}
]
},
"group": {
"kind": "build",
"isDefault": true
}
}
配置完,按 F5
运行 test.cpp,就可以出现你的图片了