安装opencv参考:Ubuntu18.04安装Opencv4.5(最新最详细)
配置vscode参考:
Ubuntu20.04/Ubuntu22.04 配置VScode+Opencv+cmake(C++)
Ubuntu环境下VScode配置OpenCV的C++开发环境
vscode配置C++编译运行环境(Ubuntu)
新建一个文件夹,并新建 .cpp 的 opencv测试文件
#include
#include
using namespace std;
using namespace cv;
int main(int argc,char** argv){
Mat src = imread("test.jpeg",IMREAD_GRAYSCALE); //读取图片
if (src.empty()) { //判断是否找到图片
printf("没有找到图片 "); //输出文字
return -1;
}
else
{
namedWindow("input",WINDOW_AUTOSIZE);// 显示窗口命名为input ;WINDOW_AUTOSIZE显示大小为图片自定义大小,且不可以更改大小
imshow("input",src); //显示
waitKey(0);//显示的毫秒时间,如果函数参数<=0表示一直显示。>0表示显示的时间
destroyAllWindows();
return 0;
}
}
首先要把以下三个文件弄出来
配置编译器:c_cpp_properties.json
配置构建任务:tasks.json
配置调试设置:launch.json
c_cpp_properties.json :shift+ctrl+p搜索全局命令,搜索C/C++ Edit Configurations(UI)
添加:
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/usr/include/x86_64-linux-gnu/sys",
"/usr/local/include/opencv4", //记得把opencv4的include路径包含,按我上述方法来安装opencv的话一般就是这个路径
"/usr/include/x86_64-linux-gnu/sys" //这是装了cuda的路径,如果无cuda请去除
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu17",
"cppStandard": "gnu++17",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}
tasks.json
快捷键shift+ctrl+p搜索全局命令tasks,选择g++ 生成活动文件
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
//"command": "/usr/bin/cpp", //不能用这命名, 否则你可能会i报错 gdb failed with message 'file format not recognized'
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}.out",
"`pkg-config","--libs","--cflags","opencv4`" //这是添加第三方库代码,要添加什么库直接继续加就行,不要忘记了 ` 符号
//如下面再加个 gazebo11 第三方库(你得保证你安装的第三方库中含有.pc文件)
// "`pkg-config","--libs","--cflags","opencv4,gazebo11`"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}
launch.json:
运行——>添加配置——>C++(GDB/LLDB)——>
{
"configurations": [
{
"name": "C/C++: cpp 生成和调试活动文件",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}.out",
"args": [
"`pkg-config","--libs","--cflags","opencv4`",
],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "将反汇编风格设置为 Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++ build active file", //要和 task.json的label字段一致
"miDebuggerPath": "/usr/bin/gdb",
//下面这行命令可以去除Vscode中编译后出现 [1]+ Done.... 的提示
"miDebuggerArgs": "-q -ex quit; wait() { fg >/dev/null; }; /usr/bin/gdb -q --interpreter=mi"
}
],
"version": "2.0.0"
}
如果想生成一个终端可以改:"externalConsole": true ,弹出命令窗口
都保存后运行即可(F5)