在图像处理领域,OpenCV的使用是必不可少的,这里介绍一下OpenCV的安装及其在vscode中的配置
1.OpenCV的安装
(1)安装依赖
sudo apt-get install build-essentialsudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
第三行的安装可能会出现无法定位包的错误,有三种解决方案
第一种:
sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"sudo apt updatesudo apt install libjasper1 libjasper-dev
这个我自己用是有效果的
第二种:
sudo add-apt-repository "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ xenial main multiverse restricted universe"sudo apt updatesudo apt install libjasper1 libjasper-dev
第三种:不用管,其实这个依赖对后续的使用影响不大,甚至可能有些淘汰了
(2)下载OpenCV源码
网站放在这里了:https://opencv.org/releases/,下载对应版本的source就OK了
(3)安装
先解压文件,这里我解压在/home目录下了
进入OpenCV文件夹,创建build目录
cd opencv4.5.5mkdir buildcd ./build
之后是cmake步骤
cmake -D CMAKE_BUILD_TYPE=Release -D OPENCV_GENERATE_PKGCONFIG=ON -D CMAKE_INSTALL_PREFIX=/usr/local ..
再执行make指令
sudo make -j6 #数字取决于你电脑的进程数,数字越大,make越快sudo make install
(4)环境配置
我们之前安装的时候 OPENCV_GENERATE_PKGCONFIG=ON 所以在/usr/local/lib/pkgconfig下生成了opencv4.pc文件,里面记录了OpenCV头文件、库文件的路径。这个文件注意一下,因为最后你如果出错,很有可能缺少了这个文件
sudo gedit /etc/bash.bashrc
#文件末尾添加以下内容 并保存PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfigexport PKG_CONFIG_PATH
#更新sudo updatedbsource /etc/bash.bashrc
#打开下列文件sudo gedit /etc/ld.so.conf.d/opencv.conf # 添加lib路径,在末尾保存退出/usr/local/lib# 更新sudo ldconfig
#终端输入以下两命令,显示正常则安装成功pkg-config --modversion opencv4 #查看版本号pkg-config --libs opencv4 #查看libs库
2.vscode的配置
先保证自己C++相关插件下载好
实现一个软连接
cd /usr/local/include/sudo ln -s opencv4/opencv2 opencv2
配置以下三个文件
c_cpp_properties.json
{"configurations": [{"name": "Linux","includePath": ["${workspaceFolder}/**","/usr/local/include/","/usr/include/"],"defines": [],"compilerPath": "/usr/bin/gcc","cStandard": "c17","cppStandard": "gnu++14","intelliSenseMode": "linux-gcc-x64"}],"version": 4}
launch.json
{"version": "0.2.0","configurations": [{"name": "g++ - Build and debug active file","type": "cppdbg","request": "launch","program": "${fileDirname}/${fileBasenameNoExtension}", //程序文件路径"args": [], //程序运行需传入的参数"stopAtEntry": false,"cwd": "${fileDirname}","environment": [],"externalConsole": true, //运行时是否显示控制台窗口"MIMode": "gdb","setupCommands": [{"description": "Enable pretty-printing for gdb","text": "-enable-pretty-printing","ignoreFailures": true}],"preLaunchTask": "C/C++: g++ build active file","miDebuggerPath": "/usr/bin/gdb"}]}
tasks.json
{"tasks": [{"type": "cppbuild","label": "C/C++: g++ build active file", /* 与launch.json文件里的preLaunchTask的内容保持一致 */"command": "/usr/bin/g++","args": ["-std=c++11","-g",//"${file}", /* 编译单个文件 */"${fileDirname}/*.cpp", /* 编译多个文件 */"-o","${fileDirname}/${fileBasenameNoExtension}", /* 输出文件路径 *//* 项目所需的头文件路径 */"-I","${workspaceFolder}/","-I","/usr/local/include/","-I","/usr/local/include/opencv4/","-I","/usr/local/include/opencv4/opencv2",/* 项目所需的库文件路径 */"-L", "/usr/local/lib",/* OpenCV的lib库 */"/usr/local/lib/libopencv_*",],"options": {"cwd": "${fileDirname}"},"problemMatcher": ["$gcc"],"group": {"kind": "build","isDefault": true},"detail": "Task generated by Debugger."}],"version": "2.0.0"}
就ok了