jetson nano刷机之后自带 ubuntu18.04和opencv4.1.1。
安装opencv-contrib,需要重新编译opencv和opencv-contrib;既然都是要重新编译,还不如索性全部重装好了。
参考博客:《Jetson nano编译安装OpenCV4.1.1和OpenCV_contirb-4.1.1》
github下载方式:
opencv4.1.1: https://github.com/opencv/opencv/tags?after=4.3.0
opencv-contrib-4.1.1: https://github.com/opencv/opencv_contrib/tags?after=3.4.12
百度盘下载地址为:
链接:https://pan.baidu.com/s/1cyy9PinUsjsq3IIF13kF1w
密码:sblg
这里我用的上述参考博客中的百度盘的资源,其中包括opencv4.1.1、opencv_contrib-4.1.1和相关现在文件。
cd /opt
sudo mkdir opencv4
cd opencv4
sudo unzip 4.1.1.zip
sudo unzip boostdesc_bgm.i.zip
sudo unzip contri-4.1.1.zip
boostdesc_bgm.i.zip解压后文件为”boostdesc_bgm.i等“,将该目录重命名为"installFile",
sudo mv -f boostdesc_bgm.i等 installFile
分别修改opencv_contrib-4.1.1/modules/xfeatures2d/cmake/download_vgg.cmake和opencv_contrib-4.1.1/modules/face/CMakeLists.txt中的URL地址
sudo vim opencv_contrib-4.1.1/modules/xfeatures2d/cmake/download_vgg.cmake
和
sudo vim opencv_contrib-4.1.1/modules/face/CMakeLists.txt
将
"https://raw.githubusercontent.com/opencv/opencv_3rdparty/${OPENCV_3RDPARTY_COMMIT}/"
修改为
"file:///opt/opencv4/installFile/"
#"https://raw.githubusercontent.com/opencv/opencv_3rdparty/${OPENCV_3RDPARTY_COMMIT}/"
此处修改五处文件,如下
2.1. 将#include全部变成.i文件的绝对路径(我们所有的.i文件放在/opt/opencv4/installFile/目录下)
sudo vim /opt/opencv4/opencv_contrib-4.1.1/modules/xfeatures2d/src/boostdesc.cpp
如图,
2.2. 将#include全部变成.i文件的绝对路径(我们所有的.i文件放在/opt/opencv4/installFile/目录下)
sudo vim /opt/opencv4/opencv_contrib-4.1.1/modules/xfeatures2d/src/vgg.cpp
sudo vim /opt/opencv4/opencv_contrib-4.1.1/modules/xfeatures2d/test/test_features2d.cpp
将#include 修改为绝对路径,
#include "/opt/opencv4/opencv-4.1.1/modules/features2d/test/test_detectors_regression.impl.hpp"
#include "/opt/opencv4/opencv-4.1.1/modules/features2d/test/test_descriptors_regression.impl.hpp"
sudo vim /opt/opencv4/opencv_contrib-4.1.1/modules/xfeatures2d/test/test_rotation_and_scale_invariance.cpp
将#include 修改为绝对路径,
#include "/opt/opencv4/opencv-4.1.1/modules/features2d/test/test_detectors_invariance.impl.hpp" // main OpenCV repo
#include "/opt/opencv4/opencv-4.1.1/modules/features2d/test/test_descriptors_invariance.impl.hpp" // main OpenCV repo
sudo vim /opt/opencv4/opencv-4.1.1/samples/gpu/surf_keypoint_matcher.cpp
将#include改为绝对路径,如下
#include "/opt/opencv4/opencv_contrib-4.1.1/modules/xfeatures2d/include/opencv2/xfeatures2d/cuda.hpp"
进入opencv-4.1.1,创建build目录,进入build目录,
cd /opt/opencv4/opencv-4.1.1
sudo mkdir build
cd build
安装libv4l-dev依赖,使得opencv能够有效设置fps,
sudo apt-get install libv4l-dev
输入cmake命令,具体为(复制粘贴即可)
sudo cmake \
-DCMAKE_BUILD_TYPE=Release \
-DBUILD_PNG=OFF \
-DBUILD_TIFF=OFF \
-DBUILD_TBB=OFF \
-DBUILD_JPEG=OFF \
-DBUILD_JASPER=OFF \
-DBUILD_ZLIB=OFF \
-DBUILD_EXAMPLES=OFF \
-DBUILD_opencv_java=OFF \
-DBUILD_opencv_python2=OFF \
-DBUILD_opencv_python3=ON \
-DENABLE_PRECOMPILED_HEADERS=OFF \
-DWITH_OPENCL=OFF \
-DWITH_OPENMP=OFF \
-DWITH_LIBV4L=ON \
-DWITH_FFMPEG=ON \
-DWITH_GSTREAMER=OFF \
-DWITH_GSTREAMER_0_10=OFF \
-DWITH_CUDA=ON \
-DWITH_GTK=ON \
-DWITH_VTK=OFF \
-DWITH_TBB=ON \
-DWITH_1394=OFF \
-DWITH_OPENEXR=OFF \
-DCUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-10.2 \
-DCUDA_ARCH_BIN=5.3 \
-DCUDA_ARCH_PTX="" \
-DINSTALL_C_EXAMPLES=ON \
-DOPENCV_ENABLE_NONFREE=ON \
-DINSTALL_TESTS=OFF \
-DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.1.1/modules \
..
注意:如果你的设备不是jetson nano,请自行确定机器的-DCUDA_ARCH_BIN (jetson nx为7.2);请确认你cuda安装路径-DCUDA_TOOLKIT_ROOT_DIR,具体可以进入/usr/local查看。
用8个线程去做make,加快速度。
sudo make -j8
sudo make install
opencv_version
或者使用pkg-config --modversion opencv4
前,执行sudo apt install libopencv-dev
。