参考该篇博文安装即可,有的地方要连外网。
下面是总结 ,直接copy即可:
#确保Ubuntu上有安装过程中使用到的一些工具,如果没有,请输入以下指令安装
sudo apt install -y g++
sudo apt install -y cmake
sudo apt install -y make
sudo apt install -y wget unzip
#输入以下指令安装OpenCV依赖的基本库
sudo apt-get install build-essential libgtk2.0-dev libgtk-3-dev libavcodec-dev libavformat-dev libjpeg-dev libswscale-dev libtiff5-dev
#安装4.5.0版本
wget -O opencv.zip https://github.com/opencv/opencv/archive/4.5.0.zip
#或安装opencv最新版本
wget -O opencv.zip https://github.com/opencv/opencv/archive/master.zip
#其他版本可从opencv官网下载
#下载完后解压缩并重命名为opencv(源码包大小大概90M)
unzip opencv.zip
mv opencv4.5.0 opencv
--------------------------------------------------------------------------------------------
配置和编译OpenCV
---------------------------------------------------------------------------------------------
#进入到下载好的opencv目录中,新建目录build并进入build目录
cd opencv
mkdir build
cd build
#使用cmake配置opencv
$ cmake -D CMAKE_BUILD_TYPE=Release -D OPENCV_GENERATE_PKGCONFIG=YES ..
#使用make编译
make -j4
#使用make安装
sudo make install
// 默认安装路径为:
// /usr/local/bin - executable files
// /usr/local/lib - libraries (.so)
// /usr/local/cmake/opencv4 - cmake package
// /usr/local/include/opencv4 - headers
// /usr/local/share/opencv4 - other files (e.g. trained cascades in XML format)
--------------------------------------------------------------------------------------------
环境配置
---------------------------------------------------------------------------------------------
#配置pkg-config环境
#opencv4.pc文件的默认路径为/usr/local/lib/pkgconfig/opencv4.pc,如果此目录下没有,可以使用以下命令搜索:
find / -iname opencv4.pc
#将路径加入到PKG_CONFIG_PATH,打开是个空文件
sudo vim /etc/profile.d/pkgconfig.sh
#在文件后面加入下面一行:
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH
#保存并退出后激活,激活后不会退出可以关闭终端
source /etc/profile
#验证是否成功
$ pkg-config --libs opencv4
-L/usr/local/lib -lopencv_dnn -lopencv_highgui -lopencv_ml -lopencv_objdetect -lopencv_photo
-lopencv_stitching -lopencv_video -lopencv_calib3d -lopencv_features2d -lopencv_flann
-lopencv_videoio -lopencv_imgcodecs -lopencv_imgproc -lopencv_core
#配置动态库环境,打开文件(可能为空文件)
sudo vim /etc/ld.so.conf.d/opencv4.conf
#在该文件末尾加上OpenCV的lib路径
/usr/local/lib
#使配置的路径生效
sudo ldconfig
#测试安装成功与否,输出版本信息
pkg-config --modversion opencv4
--------------------------------------------------------------------------------------------
卸载 OpenCV4(通过源码编译安装)
---------------------------------------------------------------------------------------------
#找到源码路径,在终端中进入到build目录,依次执行
sudo make uninstall
cd ..
sudo rm -r build
#在/usr/local下搜索opencv4
sudo find ./ -name opencv4*
#将找到的opencv相关项删除即可,大概删除以下项
sudo rm -r /usr/local/include/opencv4
sudo rm -r /usr/local/lib/pkgconfig/opencv4.pc
sudo rm -r /usr/local/lib/cmake/opencv4
sudo rm -r /usr/local/share/licenses/opencv4
sudo rm -r /usr/local/share/opencv4
c_cpp_properties.json
,是整个C++的环境配置,配置如下:
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/usr/include/x86_64-linux-gnu/sys",
"/usr/local/lib/**",
"/usr/local/include/opencv4/**"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu17",
"cppStandard": "gnu++14",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}
launch.json
是生成和调试活动文件的配置,先根据task.json的配置生成文件,配置如下:
{
"configurations": [
{
"name": "C/C++: g++ 生成和调试活动文件",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"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++ 生成活动文件",
"miDebuggerPath": "/usr/bin/gdb",
"miDebuggerArgs": "-q -ex quit; wait() { fg >/dev/null; }; /usr/bin/gdb -q --interpreter=mi"
},
],
"version": "2.0.0"
}
tasks.json
配置生成活动文件的方式,配置如下:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ 生成活动文件",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"`pkg-config",
"--libs",
"--cflags",
"opencv4`"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "调试器生成的任务。"
},
],
"version": "2.0.0"
}
注意在点击运行时,选择的配置文件需要和这三个json文件
一致,比如是C/C++: g++ 生成和调试活动文件
就不要选cpp生成和调试
,不然会自动生成cpp的配置文件,并且运行它,而不是你设置好的g++配置文件。
如果之前已经选错了,点击箭头也没有重新选择配置方式的提醒,那就修改json文件
,实在不行重新创建一个launch.json即可。