工具准备
版本:
VSCODE版本任意
MinGW 8.1.0
OPENCV 3.4.14
CMAKE 3.20.5
VSCODE
安装过程就不赘述,安装好以后需要配置C/C++的环境。打开插件市场,安装基础插件。
在本地手动新建一个c++工程的文件夹,我新建的是工程是TextOpenCV,再新建一个.vscode文件夹。用VSCODE打开该目录,新建一个TextOpenCV.cpp,保存。
在.vscode文件夹里面新建四个json文件,名称为
- launch.json
- c_cpp_properties.json
- settings.json
- tasks.json
c_cpp_properties.json的代码如下
{
"configurations": [
{
"name": "G++",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.18362.0",
"compilerPath": "C:\\Work\\mingw64\\bin\\g++.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "windows-gcc-x64"
}
],
"version": 4
}
launch.json的代码
{
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "C:\\Work\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe build active file"
}
]
}
settings.json的代码
{
"files.associations": {
"vector": "cpp",
"string": "cpp",
"iostream": "cpp",
"*.tcc": "cpp"
}
}
tasks.json的代码
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\Work\\mingw64\\bin\\g++.exe",
"args": [
"-g",
"${workspaceFolder}\\*.cpp",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "编译器: C:\\Work\\mingw64\\bin\\g++.exe"
}
]
}
CMAKE
解压出来添加环境变量到Path:C:\Work\cmake-3.20.5-windows-x86_64\bin
运行cmake-gui.exe
MinGW
下载好压缩包解压出来即可,我解压到的是D盘根目录。右键我的电脑属性添加环境变量到path。
运行cmd命令,敲入g++ --version命令,出现图片所示内容表示配置成功
OPENCV
将下载好的opencv-3.4.14-vc14_vc15.exe解压到D盘根目录。在该目录下新建一个MinGW文件夹
配置过程
运行C:\Work\cmake-3.20.5-windows-x86_64\bin 下的cmake-gui.exe。按图选择好配置。
然后点Configure,按图选择好选项点next,C选择gcc.exe,C++选择g++.exe 。点finish后等待
出现ConfiguringDone之后,找到ENABLE_CXX11,勾选,再点击一次Configure,等待Configuring Done,接着点Generate等待出现Generating done。
打开cmd,cd命令到D:\opencv\build\x64\MinGW 输入命令minGW32-make等待编译完成。根据电脑性能编译的时间不同,本笔记本编译了一个小时。
编译完之后输入minGW32-make install 安装。添加环境变量path:D:\opencv\build\x64\MinGW\bin
打开VSCODE,在c_cpp_properties文件中添加opencv的路径,
在task.json添加opencv的路径
主文件
#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
using namespace cv;
int main()
{
Mat img=imread("C:\\Users\\ASUS\\Pictures\\dragonball\\gouku2.jpg");
imshow("image",img);
waitKey();
return 0;
}
F5运行如图,配置成功。