文章目录
- 一:Opencv + Cuda源码的下载、编译
- 1.卸载jetson上自带的无cuda加速Opencv
- 2.安装Opencv依赖库
- 3.下载 OpenCV 和 opencv_contrib 源码
- 4.编译安装 OpenCV、opencv_contrib
- 二:Opencv 的环境配置
- 三:Vscode 中的Opencv环境配置
- 四:Opencv的demo测试
一:Opencv + Cuda源码的下载、编译
1.卸载jetson上自带的无cuda加速Opencv
运行如下代码即可:
sudo apt purge libopencv*
sudo apt autoremove
sudo apt update
(不建议):
不放心的话可以在根目录下搜索opencv关键词,删去多余文件
2.安装Opencv依赖库
运行如下代码:
sudo apt install -y build-essential checkinstall cmake pkg-config yasm git gfortran
sudo apt update
sudo apt install -y libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
sudo apt install -y libjpeg8-dev libjasper-dev libpng12-dev libtiff5-dev libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev libxine2-dev libv4l-dev
sudo apt install -y libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev libgtk2.0-dev libtbb-dev libatlas-base-dev libfaac-dev libmp3lame-dev libtheora-dev libvorbis-dev libxvidcore-dev libopencore-amrnb-dev libopencore-amrwb-dev x264 v4l-utils
sudo apt install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
坑一:
笔者建议先更换一下Jetson自带的镜像源,否则过程中可能会出现各种丢失问题,更换步骤如下:
修改/etc/apt/sources.list
,改为下面的源
deb [arch=arm64] https://mirrors.ustc.edu.cn/ubuntu-ports/ focal main restricted universe multiverse
# deb-src http://mirrors.ustc.edu.cn/ubuntu-ports/ focal main main restricted universe multiverse
deb [arch=arm64] https://mirrors.ustc.edu.cn/ubuntu-ports/ focal-updates main restricted universe multiverse
# deb-src http://mirrors.ustc.edu.cn/ubuntu-ports/ focal-updates main restricted universe multiverse
deb [arch=arm64] https://mirrors.ustc.edu.cn/ubuntu-ports/ focal-backports main restricted universe multiverse
# deb-src http://mirrors.ustc.edu.cn/ubuntu-ports/ focal-backports main restricted universe multiverse
deb [arch=arm64] https://mirrors.ustc.edu.cn/ubuntu-ports/ focal-security main restricted universe multiverse
# deb-src http://mirrors.ustc.edu.cn/ubuntu-ports/ focal-security main restricted universe multiverse
最后sudo apt-get update
即可。
坑二:
运行倒数第三条命令时,若遇到“ 无法定位libjasper-dev软件包 “报错,执行如下命令即可安装:
sudo add-apt-repository "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ xenial main multiverse restricted universe"
sudo apt update
sudo apt install libjasper1 libjasper-dev
坑三:
运行倒数第三条命令时,若遇到“ libpng12-dev没有可安装候选 “报错,是因为ubuntu14以上已经废弃,代替为libpng-dev,安装命令如下:
sudo apt install libpng-dev
3.下载 OpenCV 和 opencv_contrib 源码
分别前往 opencv 和 Opencv_contrib 的 github 仓库下载 Latest release 源码 ( 👈 点击超链接跳转)
点击Source code(zip)
下载Opencv源码
点击zip
下载opencv_contrib源码
注意: OpenCV 和 opencv_contrib 的版本要对应正确
4.编译安装 OpenCV、opencv_contrib
- 解压opencv-4.7.0.zip 和 opencv_contrib-4.7.0.zip 得到两个文件夹,然后将opencv_contrib-4.7.0文件夹放在opencv-4.7.0文件中!
- 执行如下命令
cd opencv-4.5.3
mkdir build
cd build
- 执行cmake
cmake \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX=/usr/local \
-DOPENCV_ENABLE_NONFREE=1 \
-DBUILD_opencv_python2=1 \
-DBUILD_opencv_python3=1 \
-DWITH_FFMPEG=1 \
-DCUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda \
-DCUDA_ARCH_BIN=8.7 \
-DCUDA_ARCH_PTX=8.7 \
-DWITH_CUDA=1 \
-DENABLE_FAST_MATH=1 \
-DCUDA_FAST_MATH=1 \
-DWITH_CUBLAS=1 \
-DOPENCV_GENERATE_PKGCONFIG=1 \
-DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib-4.7.0/modules \
..
注意点1:
DCUDA_ARCH_BIN
、DCUDA_ARCH_PTX
要改成cuda对应的容量值,运行如下指令查看:
cd /usr/local/cuda/samples/1_Utilities/deviceQuery
sudo make
./deviceQuery
打印出如下信息,我的cuda11.4对应的系数是8.7
注意点2:
DOPENCV_EXTRA_MODULES_PATH
要改成opencv_contrib-4.7.0
的对应地址。
- 进行 make前,事先调整 CPU 性能模式打开尽量多的核心,如下命令打印核心数:
cat /proc/stat | grep cpu[0-9] -c # 查看线程数,我的是8,如果占有较少,网上有指令教学
- 进行make编译,1小时左右完成
make -j4 # 四线程编译
- 编译完成后,进行安装
sudo make install
至此,Opencv的安装全部完成,如下显示yes:
二:Opencv 的环境配置
具体可以参考本人之前的Ubuntu20.04 配置Opencv+VScode+cmake,里面有配置细节,这里我基本展示一下:
--------------------------------------------------------------------------------------------
环境配置
---------------------------------------------------------------------------------------------
#配置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
三:Vscode 中的Opencv环境配置
话不多说,直接上三种json配置文件内容:
c_cpp_properties.json:
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/usr/local/include/opencv4"
],
"defines": [],
"compilerPath": "/usr/bin/clang-8",
"cStandard": "c17",
"cppStandard": "c++14",
"intelliSenseMode": "linux-clang-arm64"
}
],
"version": 4
}
task.json:
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ 生成活动文件",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"-std=c++11",
"${file}",
"-o",
"${fileDirname}/a.out",
"-I",
"${workspaceFolder}/",
"-I",
"/usr/local/include/",
"-I",
"/usr/local/include/opencv4/",
"-I",
"/usr/local/include/opencv4/opencv2",
"-L",
"/usr/local/lib",
"-L",
"/usr/local/lib/libopencv_*",
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "编译器: /usr/bin/g++"
}
]
}
launch.json:
(调试文件其实什么都未添加)
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) 启动",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/a.out",
"preLaunchTask": "C/C++: g++ 生成活动文件",
"args": [],
"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
}
],
"miDebuggerArgs": "-q -ex quit; wait() { fg >/dev/null; }; /bin/gdb -q --interpreter=mi",
}
]
}
四:Opencv的demo测试
最后,检测一下是否能跑通:
#include<opencv2/opencv.hpp>
#include<iostream>
#include<iostream>
using namespace std;
using namespace cv;
int main(int argc,char** argv){
Mat src = imread("./ceshi.jpg"); //读取图片
Mat src2;
if (src.empty()) { //判断是否找到图片
printf("没有找到图片 "); //输出文字
return -1;
}
else
{
namedWindow("input",WINDOW_AUTOSIZE);// 显示窗口命名为input ;WINDOW_AUTOSIZE显示大小为图片自定义大小,且不可以更改大小
resize(src, src2, Size(612, 612));
imshow("input",src2); //显示
waitKey(0);//显示的毫秒时间,如果函数参数<=0表示一直显示。>0表示显示的时间
destroyAllWindows();
}
cout << "success" << endl;
return 0;
}
我在jetson终端上跑有一个warning“ Failed to load module canberra-gtk-module ”,运行“sudo apt install libcanberra-gtk-module”重装该模块即可。
完结撒化 !!!!!
我是刚出cv泥潭不久,又入jetson终端部署泥潭的江南咸鱼,希望大家能从一而终,不留遗憾!!!
觉得文章有用的话,各位客官点个赞吧!!