(C++)vscode 配置opencv
在vscode配置opencv的过程中,看了很多博文,也失败了很多次,花了一周时间,今天终于成功了!迫不及待给大家分享一下!
编译opencv是最麻烦的事情,总是遇到很多坑。我当时也在想:如果有一个现成的,拿来即用,那该多好啊!可惜在网上找不到现成的,只能从头做一遍,总算是成功了!
今天不分享从0到1的过程,只分享如何使用现成的,省得各位麻烦!
一、软件版本
MinGw:x86_64-posix-seh-rev3
Opencv:opencv4.10_MinGW_Release_x64_mingw1120_64
二、资源文件
三、设置过程
3.1、解压文件
解压后有3个文件夹
.vscode-放在你的项目文件夹里
mingw和opencv-放在哪个文件夹都行
3.2、设置环境变量
对 mingw64\bin 和 opencv\x64\mingw\bin 设置环境变量,当然是要根据你放置的位置设定。(我是放在D:\setup)
3.3、编辑json文件
在.vscode文件夹里有3个后缀为.json的文件,分别是:c_cpp_properties.json,launch.json和tasks.json
c_cpp_properties.json:
{
"configurations": [
{
"name": "Win64",
"includePath": [
"${workspaceFolder}/**",
"D:/setup/opencv/include"//根据自己放置的路径设置,其他的不用改
],
"defines": ["_DEBUG", "UNICODE", "_UNICODE"],
"compilerPath": "D:/setup/mingw64/bin/g++.exe"
}
],
"version": 4
}
launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"preLaunchTask": "build",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "D:/setup/mingw64/bin/gdb.exe", // gdb.exe路径,根据你放置mingw64的位置来设定,其他的不用修改
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": false
}
]
}
]
}
tasks.json:
{
"tasks": [
{
"type": "cppbuild",
"label": "build",
"command": "g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-I",
"D:/setup/opencv/include",//根据您opencv放置的路径设定
"-L",
"D:/setup/opencv/x64/mingw/bin",//根据您opencv放置的路径设定
"-l", "libopencv_world4100",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}
四、测试程序
#include "opencv2/opencv.hpp"
#include "iostream"
int main(int argc, char const *argv[])
{
cv::Mat img = cv::imread("D:\\1.jpg");\\"D:\\1.jpg":须修改为实际路径
if (img.empty())
std::cout << "image is empty or the path is invalid!" << std::endl;
cv::imshow("Origin", img);
cv::waitKey(0);
cv::destroyAllWindows();
return 0;
}