用vscode来写Opencv的c++代码需要自行编译Opencv,这期间主要用到的工具有MinGW-w64和CMake。由于自行编译的过程耗时繁复,并不会一帆风顺,常出现编译错误的现象,需要有极强的折腾的精神。
本博文使用他人已经编译过的Opencv库来搭建我们需要的开发环境,在解决好版本兼容问题后,能减少许多麻烦事。
MinGW是C++编译器,用于编译C++文件。
添加路径到环境变量Path,如:D:\mingw64\bin
访问网站:https://github.com/huihut/OpenCV-MinGW-Build
该网站包含了已经编译过的Opencv库,同时还提供了现成的版本兼容信息,点击Configuration可以看到所对应的MinGW和CMake版本,故下载安装时就需要据此选择合适的MinGW版本。
#include
#include
#include
using namespace cv;
int main()
{
Mat img=imread("man.jpg");
imshow("result",img);
waitKey();
return 0;
}
{
"version": "0.2.0",
"configurations": [
{
"name": "opencv4.5.0 debug", // 配置名称,将会在启动配置的下拉菜单中显示
"type": "cppdbg", // 配置类型,这里只能为cppdbg
"request": "launch", // 请求配置类型,可以为launch(启动)或attach(附加)
"program": "${workspaceFolder}/DeBug/${fileBasenameNoExtension}.exe",// 将要进行调试的程序的路径
"args": [], // 程序调试时传递给程序的命令行参数,一般设为空即可
"stopAtEntry": false, // 设为true时程序将暂停在程序入口处,一般设置为false
"cwd": "${workspaceFolder}", // 调试程序时的工作目录,一般为${workspaceFolder}即代码所在目录
"environment": [],
"externalConsole": true, // 调试时是否显示控制台窗口,一般设置为true显示控制台
"MIMode": "gdb",
"miDebuggerPath": "D:/mingw64/bin/gdb.exe", // miDebugger的路径,注意这里要与MinGw的路径对应
"preLaunchTask": "g++", // 调试会话开始前执行的任务,一般为编译程序,c++为g++, c为gcc
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"D:/OpenCV-4.5.0/include",
"D:/OpenCV-4.5.0/include/opencv2"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "D:/mingw64/bin/gcc.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
{
"version": "2.0.0",
"command": "g++",
"args": [
"-g",
"${file}",
"-o",
"${workspaceFolder}/DeBug/${fileBasenameNoExtension}.exe",
"-I", "D:/OpenCV-4.5.0/include",
"-I", "D:/OpenCV-4.5.0/include/opencv2",
"-L", "D:/OpenCV-4.5.0/x64/mingw/lib",
"-l", "libopencv_core411",
"-l", "libopencv_imgproc411",
"-l", "libopencv_imgcodecs411",
"-l", "libopencv_video411",
"-l", "libopencv_dnn411",
"-l", "libopencv_ml411",
"-l", "libopencv_highgui411",
"-l", "libopencv_objdetect411",
"-l", "libopencv_flann411",
"-l", "libopencv_imgcodecs411",
"-l", "libopencv_photo411",
"-l", "libopencv_videoio411"
], // 编译命令参数
"problemMatcher": {
"owner": "cpp",
"fileLocation": ["relative", "\\"],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
}