本文主要介绍opencv安装后vscode如何配置并使用opencv。前期的准备工作可以参考这篇文章内容很详细https://www.cnblogs.com/uestc-mm/p/12758110.html
对应的工具大家可以自行去官网下载或者可以直接下载我打包好的文件里面包含了所有需要用到的工具和配置文件工具包下载
在安装opencv后,打开vscode新建项目 编写测试代码`
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <opencv2/opencv.hpp>
using namespace std;
int main(void)
{
char s[] = "Hi,Cpp.";
int n = strlen(s);
printf("%s:%d\n",s,n);
cout << "Hello Opencv4.2" << endl;
cout << cv::COLOR_RGB2BGRA << cv::COLOR_RGB2GRAY << endl;
cv::Mat a = cv::imread("D:/Desktop/69854d9dbf064bd0b34b3e95bba08e61.PNG");
cv::imshow("hello",a);
cv::waitKey(0);
return 0;
}
然后分别配置如下几个文件
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": "E:/MinGW-64-posix/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
}
]
}
]
}
tasks.json
{
"version": "2.0.0",
"command": "g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-I","E:/opencv/opencv/new_build/install/include",//全部替换成自己的路径
"-I","E:/opencv/opencv/new_build/install/include/opencv2",
"-L","E:/opencv/opencv/new_build/install/x64/mingw/bin",
"-L","E:/opencv/opencv/new_build/install/x64/mingw/lib",
"-l","libopencv_calib3d420",//替换成自己opencv版本号 如opencv_4.2.0 后面就写420
"-l","libopencv_core420",
"-l","libopencv_dnn420",
"-l","libopencv_features2d420",
"-l","libopencv_flann420",
"-l","libopencv_highgui420",
"-l","libopencv_imgcodecs420",
"-l","libopencv_imgproc420",
"-l","libopencv_ml420",
"-l","libopencv_objdetect420",
"-l","libopencv_photo420",
"-l","libopencv_stitching420",
"-l","libopencv_video420",
"-l","libopencv_videoio420",
"-Wall",
"-static-libgcc",
"-std=c++17"
],
"options": {
"cwd": "E:/MinGW-64-posix/mingw64/bin"
},
"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
}
}
c_cpp_properties.json
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"E:/MinGW-64-posix/mingw64/include/*",
// "E:/MinGW-64-posix/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/*",
// "E:/MinGW-64-posix/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32/*",
// "E:/MinGW-64-posix/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward/*",
// "E:/MinGW-64-posix/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/*",
// "E:/MinGW-64-posix/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
"E:/opencv/opencv/new_build/install/include",
"E:/opencv/opencv",
"E:/opencv/opencv/new_build/install/include/opencv2",
"${workspaceFolder}/MinGW/include/boost/compute/interop"
],
"defines": [],
"compilerPath": "E:/MinGW-64-posix/mingw64/bin/gcc.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64",
"browse": {
"path": [
"${workspaceFolder}",
"E:/MinGW-64-posix/mingw64/include/**",
"E:/MinGW-64-posix/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include",
"E:/MinGW-64-posix/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
"E:/opencv/opencv/new_build/install/include",
"E:\\opencv\\opencv\\new_build\\install\\x64\\mingw\\lib"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
}
}
],
"version": 4
}
settings.json
{
"C_Cpp.errorSquiggles": "Enabled",
"files.associations": {
"random": "cpp"
},
"code-runner.executorMap": {
"c": "cd $dir && gcc '$fileName' -o '$fileNameWithoutExt.exe' -Wall -g -O2 -static-libgcc -std=c11 -fexec-charset=UTF-8 && &'$dir$fileNameWithoutExt'",
"cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt -I E:\\opencv\\opencv\\new_build\\install\\include -I E:\\opencv\\opencv\\new_build\\install\\opencv2 -L E:\\opencv\\opencv\\new_build\\install\\x64\\MinGW\\lib -lopencv_core420 -lopencv_imgproc420 -lopencv_video420 -lopencv_ml420 -lopencv_highgui420 -lopencv_objdetect420 -lopencv_flann420 -lopencv_imgcodecs420 -lopencv_photo420 -lopencv_videoio420 -llibopencv_dnn420 && $dir$fileNameWithoutExt "
}
}
这些文件如果没有就直接创建 然后复制粘贴 注意更改成自己的路径 以及自己的opencv版本号