编程环境为:Ubuntu20.04+VScode+opencv4.5.1
第一步(准备工作):Ubuntu 20.04 下安装配置 VScode 的 C/C++ 开发环境
参考:https://blog.csdn.net/weixin_44012745/article/details/106183331
第二步(准备工作):Ubuntu 20.04下编译供C++/python使用的opencv
参考:https://blog.csdn.net/WJ_SHI/article/details/107605439
2.1 下载opencv源码 https://opencv.org/ eg: opencv-4.5.1.zip
2.2 安装依赖
# libjasper-dev包无法定位,增加库并更新atp
sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt-get update
# 安装opencv依赖
sudo apt-get install build-essential libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg-dev libtiff-dev libswscale-dev libpng-dev libjasper-dev
2.3 安装opencv
# install cmake 工具
sudo apt-get install cmake
# 解压.zip,将解压后文件移动到/opt目录下
cd /*** # 下载的opencv-4.5.1.zip文件所在目录
unzip opencv-4.5.1.zip
sudo mv opencv-4.5.1 /opt
cd /opt/opencv-4.5.1
sudo mkdir build
cd build
sudo cmake ..
sudo make
sudo make install
注意:最后所有文件将被安装到目录“/usr/local/”下面。
2.4 修改环境变量配置
安装完成后,手动创建opencv.pc:
cd /usr/local/lib
sudo mkdir pkgconfig && cd pkgconfig
sudo gedit opencv.pc
然后添加:
prefix=/usr/local
exec_prefix=${prefix}
includedir=/usr/local/include
libdir=/usr/local/lib
Name: OpenCV
Description: Open Source Computer Vision Library
Version: 4.4.0
Libs: -L${exec_prefix}/lib -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn_objdetect -lopencv_dpm -lopencv_face -lopencv_photo -lopencv_freetype -lopencv_fuzzy -lopencv_hdf -lopencv_hfs -lopencv_img_hash -lopencv_line_descriptor -lopencv_optflow -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_dnn -lopencv_plot -lopencv_xfeatures2d -lopencv_shape -lopencv_video -lopencv_ml -lopencv_ximgproc -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_flann -lopencv_xobjdetect -lopencv_imgcodecs -lopencv_objdetect -lopencv_xphoto -lopencv_imgproc -lopencv_core
Libs.private: -ldl -lm -lpthread -lrt
Cflags: -I${includedir}
更改环境变量,输入命令:
sudo gedit /etc/bash.bashrc
打开之后,在文件最后面添加以下内容:
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH
保存退出,opencv基本上就安装完成了。通过 pkg-config 查看 opencv 版本:
pkg-config --modversion opencv
在bash.bashrc文件末尾加上
PKG_CONFIG=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH
保存后退出,执行以下指令测试opencv配置成功:
ldconfig -p | grep opencv
补充:
完成以上步骤后,创建OpenCV项目,运行后可能会报错:“libopencv_***.so.4.5: cannot open shared object file”,出现此问题原因:opencv库与程序之间缺少链接。
解决办法:在/etc/ld.so.conf.d目录中创建一个名字为opencv.conf的文件,在里面写入libopencv_core.so.2.4所在的路径(/usr/local/lib),然后运行第二条命令,完美解决:
sudo /bin/bash -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig -v
题外:
若觉得libopencv_***.so文件放在/usr/local/lib文件下太乱,可按照以下操作将文件统一移动到/usr/loacal/lib/opencv4/opencv2文件中:
执行语句:
sudo mv /usr/local/lib/lib* /usr/loacal/lib/opencv4/opencv2
但是注意需要修改环境配置及链接
sudo gedit /etc/ld.so.conf.d/opencv.conf
将opencv.conf文件中原路径“/usr/local/lib” 改成“/usr/local/lib/opencv4/opencv2”
sudo gedit /usr/local/lib/pkgconfig/opencv.pc
将opencv.pc文件中libdir原路径“/usr/local/lib” 改成“/usr/local/lib/opencv4/opencv2”,如图:“
最后一步,测试opencv配置成功,链接ldconfig:
ldconfig -p | grep opencv sudo ldconfig -v # 这一步一定要执行,链接成功
注:每次更新了/usr/local/lib库,都需要执行ldconfig更新一下链接库,否则执行程序的时候会出现"无法找到库文件lib***.so"报错!
2.5 python 链接opencv
注:上述参考链接安装过程,仅能实现C++编译使用opencv,若需要在python环境中使用,需安装opencv-python。
pip install opencv-python
进行python测试,如下:
$ python
Python 3.8.5 (default, Jul 28 2020, 12:59:40)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> print(cv2.__version__)
4.5.1
第三步:VScode将OpenCV集成于自己的项目中(独立运行,不依赖系统编译环境)
1. 新建VScode项目
Step1:新建项目文件
将/usr/local/include 内的/opencv4/opencv2/文件拷贝到项目文件/utils/opencv4/opencv2/include中
sudo cp /usr/local/include/opencv4/opencv2 项目路径/utils/opencv4/opencv2/include
将/usr/local/lib 内的有关opencv的所有libopencv_***.so文件拷贝到项目文件/utils/opencv4/opencv2/lib中
sudo cp /usr/local/lib/opencv4/opencv2/lib* 项目路径/utils/opencv4/opencv2/lib
或 sudo cp /usr/local/lib/lib* 项目路径/utils/opencv4/opencv2/lib
Step2:VScode打开源码所在的文件夹:
2. 测试系统编译OpenCV的运行情况
Step1:生成几个关键.json文件
在VScode界面选择源码所在文件(本例中main.cpp),Ctrl+shift+D,然后点击运行和调试,选择"C++(GDB/LLDB)",选择“g++ -生成和调试活动文件“,生成“launch.json”和“tasks.json”文件;
launch.json文件配置如下:
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe - 生成和调试活动文件",
"type": "cppdbg", // 只能是cppdbg
"request": "launch", // launch:启动,attach:附加
"program": "${fileDirname}/${fileBasenameNoExtension}", // 需要调试的程序
"args": [], // 调试时传递给程序的参数
"stopAtEntry": false, // 调试时是否停在程序入口:{true:是,false:否}
"cwd": "${workspaceFolder}", // 工作目录
"environment": [], // 额外的环境变量
"externalConsole": true, // true:输出到外部终端;false:只输出到软件终端(有显示不全的可能)
"MIMode": "gdb",
"setupCommands": [ // 暂时不知道作用
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++ build active file", // 预编译任务名称,和tasks.json中的label必须相同
"miDebuggerPath": "/usr/bin/gdb" // 调试gdb路径
}
]
}
Ctrl + Shift + P 打开搜索框,键入c++,会出现备选项目,选择“Edit configurations (JSON)“,生成“c_cpp_properties.json”;
Step2:配置Opencv的开发环境
这一步,与windows环境下VS配置Opencv开发环境思路相同,将OpenCV的include头文件所在路径和函数库的路径设置给编译器。
(1) 进入c_cpp_properties.json文件内进行配置修改。修改如下:
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/usr/include/opencv4", //请确保你的opencv opencv2头文件夹安装在这个目录
"/usr/include/opencv4/opencv2"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu17",
"cppStandard": "gnu++14",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
(2) 进入tasks.json文件内进行配置修改。修改如下:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g","-std=c++11",
//"${file}", // 单个cpp文件
"${workspaceFolder}/Demo/*.cpp", // 多个cpp文件
"${workspaceFolder}/src/*.cpp",
"-o",
"${fileDirname}/${fileBasenameNoExtension}", // 要调试的程序,必须与launch.json文件中的"program"相同
"-I", "${workspaceFolder}/include", // 项目include文件
"-I", "$/usr/include/opencv4", // opencv安装的include文件路径
"-I", "$/usr/include/opencv4/opencv2",
"-L", "$/usr/local/lib", //opencv安装的lib文件路径
"-l", "opencv_core", //.so文件,文件原名称“libopencv_core.so”,注意需去除“lib”
"-l", "opencv_imgproc",
"-l", "opencv_imgcodecs",
"-l", "opencv_video",
"-l", "opencv_ml",
"-l", "opencv_highgui",
"-l", "opencv_objdetect",
"-l", "opencv_flann",
"-l", "opencv_imgcodecs",
"-l", "opencv_photo",
"-l", "opencv_videoio",
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "test",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
Step3:回到测试文件,F5运行如下
3. 将OpenCV集成于自己的项目中
Step1:将编译安装于系统中OpenCV的include和lib文件拷贝于自己的项目文件中,如下图所示:
Step2:配置OpenCV开发环境
(1) 进入c_cpp_properties.json文件内进行配置修改。修改如下:
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"${workspaceFolder}/include/**",
"${workspaceFolder}/utils/opencv4/include", //请确保你的opencv opencv2头文件夹安装在这个目录
"${workspaceFolder}/utils/opencv4/include/opencv2"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu17",
"cppStandard": "gnu++14",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
(2) 进入tasks.json文件内进行配置修改。修改如下:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g","-std=c++11",
//"${file}", // 单个cpp文件
"${workspaceFolder}/Demo/*.cpp", // 多个cpp文件
"${workspaceFolder}/src/*.cpp",
"-o",
"${fileDirname}/${fileBasenameNoExtension}", // 要调试的程序,必须与launch.json文件中的"program"相同
"-I", "${workspaceFolder}/include", // 项目include文件
"-I", "${workspaceFolder}/utils/opencv4/include", // opencv的include文件路径
"-I", "${workspaceFolder}/utils/opencv4/include/opencv2",
"-L", "${workspaceFolder}/utils/opencv4//lib", //opencv的lib文件路径
"-l", "opencv_core", //.so文件,文件原名称“libopencv_core.so”,注意需去除“lib”
"-l", "opencv_imgproc",
"-l", "opencv_imgcodecs",
"-l", "opencv_video",
"-l", "opencv_ml",
"-l", "opencv_highgui",
"-l", "opencv_objdetect",
"-l", "opencv_flann",
"-l", "opencv_imgcodecs",
"-l", "opencv_photo",
"-l", "opencv_videoio",
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "test",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
Step3:编译运行,按F5运行
以上!!!