jetson nano和jetson nx 重装opencv(opencv4.1.1+opencv-contrib安装)

目录

        • 问题
        • 解决方案
          • 1. 下载opencv4.1.1和opencv_contrib-4.1.1
          • 2. 解压并修改代码
          • 3. 创建build目录并cmake
          • 4. make
          • 5. 安装
          • 6. 测试

jetson nano刷机之后自带 ubuntu18.04和opencv4.1.1。

问题

安装opencv-contrib,需要重新编译opencv和opencv-contrib;既然都是要重新编译,还不如索性全部重装好了。

解决方案

参考博客:《Jetson nano编译安装OpenCV4.1.1和OpenCV_contirb-4.1.1》

1. 下载opencv4.1.1和opencv_contrib-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和相关现在文件。

2. 解压并修改代码
  • 我们在/opt目录下新建opencv4目录,
cd /opt
sudo mkdir opencv4
cd opencv4
  • 将下载文件放入opencv4目录中,如图
    在这里插入图片描述
  • 解压.zip文件
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
  • 修改URL地址为本地文件

分别修改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}/" 

如图
jetson nano和jetson nx 重装opencv(opencv4.1.1+opencv-contrib安装)_第1张图片

  • #include 文件改为绝对路径

此处修改五处文件,如下

2.1. 将#include全部变成.i文件的绝对路径(我们所有的.i文件放在/opt/opencv4/installFile/目录下)

sudo vim /opt/opencv4/opencv_contrib-4.1.1/modules/xfeatures2d/src/boostdesc.cpp

如图,
jetson nano和jetson nx 重装opencv(opencv4.1.1+opencv-contrib安装)_第2张图片
2.2. 将#include全部变成.i文件的绝对路径(我们所有的.i文件放在/opt/opencv4/installFile/目录下)

sudo vim /opt/opencv4/opencv_contrib-4.1.1/modules/xfeatures2d/src/vgg.cpp

如图,
jetson nano和jetson nx 重装opencv(opencv4.1.1+opencv-contrib安装)_第3张图片
2.3.

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"

如图
jetson nano和jetson nx 重装opencv(opencv4.1.1+opencv-contrib安装)_第4张图片
2.4.

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

如图
在这里插入图片描述
2.5.

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"

如图
在这里插入图片描述

3. 创建build目录并cmake

进入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查看。

4. make

用8个线程去做make,加快速度。

sudo make -j8
5. 安装
sudo make install 
6. 测试
opencv_version

或者使用pkg-config --modversion opencv4前,执行sudo apt install libopencv-dev

你可能感兴趣的:(opencv,人工智能,计算机视觉)