Bootstrap

jetson orin系列开发版安装cuda的gpu版本的opencv

opencv安装包下载地址:

https://github.com/opencv/opencv/

扩展库下载地址:

https://github.com/opencv/opencv_contrib

1. 删除jetpack包中的opencv版本

原先的opencv库安装在目录/usr/lib/aarch64-linux-gnu/下(一般其他的第三方库也都安装在该目录下),首先将该目录下所有的libopencv*库删除:

sudo rm /usr/lib/aarch64-linux-gnu/libopencv*

再删除/usr/lib/aarch64-linux-gnu/pkgconfig/目录下的opencv.pc文件,如果是opencv4.X版本的,则为opencv4.pc,使用命令删除该文件:

sudo rm /usr/lib/aarch64-linux-gnu/pkgconfig/opencv*

再删除/usr/share/目录下的opencv文件夹

sudo rm -r /usr/share/opencv*

再删除/usr/include/目录下的头文件文件夹opencv

sudo rm -r /usr/include/opencv*

再删除/usr/bin/目录下的应用程序

sudo rm /usr/bin/opencv*

至此,jetpack包中的opencv已全部删除。

2. 删除手动安装包中的opencv版本

sudo apt-get remove --purge libopencv* python3-opencv
sudo rm -rf /usr/local/lib/libopencv_*
sudo rm -rf /usr/local/lib/pkgconfig/opencv4.pc
sudo rm -rf /usr/local/include/opencv*
sudo rm -rf /usr/local/bin/opencv_*

3. 安装依赖以及编译

一键编译脚本build-opencv.sh,将这个脚本放在下载的opencv同级目录:

#!/bin/bash

sudo apt-get update

sudo apt-get install -y \
    build-essential \
    pkg-config \
    libgtk2.0-dev \
    libavcodec-dev \
    libavformat-dev \
    libswscale-dev \
    libv4l-dev \
    libxvidcore-dev \
    libx264-dev \
    libjpeg-dev \
    libtiff5-dev \
    libvtk7-dev

# 设置工作目录
BASE_DIR=$(pwd)
OPENCV_DIR="${BASE_DIR}/opencv-4.10.0"
INSTALL_DIR="${OPENCV_DIR}/install"
BUILD_DIR="${OPENCV_DIR}/build"
CONTRIB_DIR="${BASE_DIR}/opencv_contrib-4.10.0"

# 创建安装和构建目录
mkdir -p "${INSTALL_DIR}"
mkdir -p "${BUILD_DIR}"

# 进入构建目录
cd "${BUILD_DIR}"

# 运行 CMake
cmake \
-D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX="${INSTALL_DIR}" \
-D OPENCV_EXTRA_MODULES_PATH="${CONTRIB_DIR}/modules" \
-D WITH_LIBV4L=ON \
-D WITH_CUDA=ON \
-D WITH_CUFFT=ON \
-D WITH_IPP=ON \
-D WITH_EIGEN=ON \
-D CUDA_SDK_ROOT_DIR=/usr/local/cuda-12.4 \
-D CUDNN_LIBRARY=/usr/lib/aarch64-linux-gnu/libcudnn.so.8 \
-D CUDNN_INCLUDE_DIR=/usr/include \
-D ENABLE_FAST_MATH=ON \
-D CUDA_FAST_MATH=ON \
-D WITH_CUBLAS=ON \
-D WITH_NVCUVID=ON \
-D WITH_TBB=ON \
-D WITH_OPENMP=ON \
-D WITH_OPENGL=ON \
-D ENABLE_CXX11=ON \
-D OPENCV_ENABLE_NONFREE=ON \
-D CUDA_nppi_LIBRARY=true \
-D OPENCV_GENERATE_PKGCONFIG=YES \
-D ENABLE_PRECOMPILED_HEADERS=OFF ..

# 编译
make -j$(nproc)  # 调整为根据实际CPU核心数选择合适的线程数
make install

3.1 更新系统依赖库

sudo apt-get update
sudo apt-get install -y \
    build-essential \
    pkg-config \
    libgtk2.0-dev \
    libavcodec-dev \
    libavformat-dev \
    libswscale-dev \
    libv4l-dev \
    libxvidcore-dev \
    libx264-dev \
    libjpeg-dev \
    libtiff5-dev \
    libvtk7-dev

3.2 创建编译build目录以及运行 CMake

cd  opencv-4.10.0 && mkdir build && mkdir install && cd build
cmake \
-D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/home/jetson/opencv-4.10.0/install \
-D OPENCV_EXTRA_MODULES_PATH=/home/jetson/opencv_contrib-4.10.0/modules \
-D WITH_LIBV4L=ON \
-D WITH_CUDA=ON \
-D WITH_CUFFT=ON \
-D WITH_IPP=ON \
-D WITH_EIGEN=ON \
-D CUDA_SDK_ROOT_DIR=/usr/local/cuda-12.4 \
-D CUDNN_LIBRARY=/usr/lib/aarch64-linux-gnu/libcudnn.so.8 \
-D CUDNN_INCLUDE_DIR=/usr/include \
-D ENABLE_FAST_MATH=ON \
-D CUDA_FAST_MATH=ON \
-D WITH_CUBLAS=ON \
-D WITH_NVCUVID=ON \
-D WITH_TBB=ON \
-D WITH_OPENMP=ON \
-D WITH_OPENGL=ON \
-D WITH_FFMPEG=ON \
-D ENABLE_CXX11=ON \
-D OPENCV_ENABLE_NONFREE=ON \
-D CUDA_nppi_LIBRARY=true \
-D OPENCV_GENERATE_PKGCONFIG=YES \
-D ENABLE_PRECOMPILED_HEADERS=OFF ..

3.3 开始编译以及生成库

make -j4 && make install

3.4 opencv库的安装

命令执行结束,在install生成库文件,我的文件夹结构为:

/home/jetson/
├── opencv-4.10.0
│   ├── build
│   └── install
│       ├── bin
│       ├── include
│       ├── lib
│       └── share
└── opencv_contrib-4.10.0
    └── modules

下面开始将库文件拷贝到系统目录,在install目录下打开终端,分别执行以下命令:

(1)修改opencv-4.10.0/install/lib/pkgconfig/opencv4.pc

vim lib/pkgconfig/opencv4.pc

原始的:

# Package Information for pkg-config

prefix=/home/jetson/opencv-4.10.0/install
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include/opencv4

Name: OpenCV
Description: Open Source Computer Vision Library
Version: 4.10.0
Libs: -L${exec_prefix}/lib -lopencv_gapi -lopencv_stitching -lopencv_alphamat -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_cudabgsegm -lopencv_cudafeatures2d -lopencv_cudaobjdetect -lopencv_cudastereo -lopencv_dnn_objdetect -lopencv_dnn_superres -lopencv_dpm -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hdf -lopencv_hfs -lopencv_img_hash -lopencv_intensity_transform -lopencv_line_descriptor -lopencv_mcc -lopencv_quality -lopencv_rapid -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_signal -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_superres -lopencv_cudacodec -lopencv_surface_matching -lopencv_tracking -lopencv_highgui -lopencv_datasets -lopencv_text -lopencv_plot -lopencv_videostab -lopencv_cudaoptflow -lopencv_optflow -lopencv_cudalegacy -lopencv_videoio -lopencv_cudawarping -lopencv_viz -lopencv_wechat_qrcode -lopencv_xfeatures2d -lopencv_shape -lopencv_ml -lopencv_ximgproc -lopencv_video -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_imgcodecs -lopencv_features2d -lopencv_dnn -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_cudaimgproc -lopencv_cudafilters -lopencv_imgproc -lopencv_cudaarithm -lopencv_core -lopencv_cudev
Libs.private: -lm -lpthread -lcudart_static -ldl -lrt -lnppc -lnppial -lnppicc -lnppidei -lnppif -lnppig -lnppim -lnppist -lnppisu -lnppitc -lnpps -lcublas -lcudnn -lcufft -L-L/usr/local/cuda-11.4 -llib64 -L-L/usr/lib -laarch64-linux-gnu
Cflags: -I${includedir}

新的:

# Package Information for pkg-config

prefix=prefix=/usr/local
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include/opencv4

Name: OpenCV
Description: Open Source Computer Vision Library
Version: 4.10.0
Libs: -L${exec_prefix}/lib -lopencv_gapi -lopencv_stitching -lopencv_alphamat -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_cudabgsegm -lopencv_cudafeatures2d -lopencv_cudaobjdetect -lopencv_cudastereo -lopencv_dnn_objdetect -lopencv_dnn_superres -lopencv_dpm -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hdf -lopencv_hfs -lopencv_img_hash -lopencv_intensity_transform -lopencv_line_descriptor -lopencv_mcc -lopencv_quality -lopencv_rapid -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_signal -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_superres -lopencv_cudacodec -lopencv_surface_matching -lopencv_tracking -lopencv_highgui -lopencv_datasets -lopencv_text -lopencv_plot -lopencv_videostab -lopencv_cudaoptflow -lopencv_optflow -lopencv_cudalegacy -lopencv_videoio -lopencv_cudawarping -lopencv_viz -lopencv_wechat_qrcode -lopencv_xfeatures2d -lopencv_shape -lopencv_ml -lopencv_ximgproc -lopencv_video -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_imgcodecs -lopencv_features2d -lopencv_dnn -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_cudaimgproc -lopencv_cudafilters -lopencv_imgproc -lopencv_cudaarithm -lopencv_core -lopencv_cudev
Libs.private: -lm -lpthread -lcudart_static -ldl -lrt -lnppc -lnppial -lnppicc -lnppidei -lnppif -lnppig -lnppim -lnppist -lnppisu -lnppitc -lnpps -lcublas -lcudnn -lcufft -L-L/usr/local/cuda-11.4 -llib64 -L-L/usr/lib -laarch64-linux-gnu
Cflags: -I${includedir}

(2)将 bin 文件夹复制到 /usr/local

sudo cp -r bin/* /usr/local/bin/

(3)将 include 文件夹复制到 /usr/local/include

sudo cp -r include/* /usr/local/include/

(4)将 lib 文件夹复制到 /usr/local/lib

sudo mv lib/python3.8/site-packages    lib/python3.8/dist-packages 
sudo cp -r lib/* /usr/local/lib/

(5)将 share 文件夹复制到 /usr/local/share

sudo cp -r share/* /usr/local/share/

4. 将OpenCV的库添加到系统路径

4.1 方法一:配置ld.so.conf文件

sudo vim /etc/ld.so.conf

在文件中加上一行

include /usr/local/lib

这个路径是cmake编译时填的动态库安装路径加上/lib

配置ld.so.conf文件

4.2 方法二:手动生成opencv.conf文件

sudo vim /etc/ld.so.conf.d/opencv.conf

是一个新建的空文件,直接添加路径,同理这个路径是cmake编译时填的动态库安装路径加上/lib

/usr/local/lib

4.3 应用配置更新

以上两种方法配置好后,执行如下命令使得配置的路径生效

sudo ldconfig

配置系统bash
因为在cmake时,选择了自动生成OpenCV的pkgconfig文件,在/usr/local/lib/pkgconfig路径可以看到文件opencv4.pc

确保文件存在,执行如下命令

sudo vim /etc/bash.bashrc

在文末添加

PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH

如下:

bash.bashrc

保存退出,然后执行如下命令使配置生效

source /etc/bash.bashrc
echo 'export CPLUS_INCLUDE_PATH=/usr/local/include/opencv:/usr/local/include/opencv4:$CPLUS_INCLUDE_PATH' >> ~/.bashrc
echo 'export LIBRARY_PATH=/usr/local/lib:$LIBRARY_PATH' >> ~/.bashrc
source ~/.bashrc

至此,Linux\Ubuntu18.04环境下OpenCV的安装以及配置已经全部完成,可以使用以下命令查看是否安装和配置成功

pkg-config --modversion opencv4
pkg-config --cflags opencv4
pkg-config --libs opencv4

最后添加软链接,确保C++编译没问题:

sudo mv /usr/local/include/opencv4  /usr/local/include/opencv
sudo ln -s /usr/local/include/opencv/opencv2   /usr/local/include/opencv2
sudo ln -s /usr/local/include/opencv/opencv2   /usr/local/include/opencv4

安装jtop,也可查看:

sudo -H pip3 install -U jetson-stats

以下是我的jtop截图
在这里插入图片描述

我编译的install可以在这里下载:

https://download.csdn.net/download/weixin_43269994/90024750

下面是一键安装install.sh,将这个脚本放在install目录直接执行即可

#!/bin/bash

# 定义一个函数来执行带有密码的sudo命令
sudo_with_password() {
    echo "nvidia" | sudo -S "$@"
}


sudo_with_password rm -rf /usr/lib/python3/dist-packages/OpenSSL
# 安装 Python 依赖项
echo "Installing Python dependencies..."
pip install numpy==1.23.5 -i https://pypi.tuna.tsinghua.edu.cn/simple
sudo_with_password  pip uninstall -y opencv-python
pip install pyOpenSSL -i https://pypi.tuna.tsinghua.edu.cn/simple
 


# 更新软件包列表
echo "Updating package list..."
sudo_with_password apt-get -y update


sudo_with_password -H pip3 install -U jetson-stats


# 删除JetPack中的OpenCV版本
echo "Deleting JetPack OpenCV version..."
sudo_with_password rm -f /usr/lib/aarch64-linux-gnu/libopencv*
sudo_with_password rm -f /usr/lib/aarch64-linux-gnu/pkgconfig/opencv*
sudo_with_password rm -rf /usr/share/opencv*
sudo_with_password rm -rf /usr/include/opencv*

# 删除手动安装的OpenCV版本
echo "Deleting manually installed OpenCV version..."
sudo_with_password apt-get remove --purge -y libopencv* python3-opencv
sudo_with_password rm -rf /usr/local/lib/libopencv_*
sudo_with_password rm -rf /usr/local/lib/pkgconfig/opencv4.pc
sudo_with_password rm -rf /usr/local/include/opencv*
sudo_with_password rm -rf /usr/local/bin/opencv_*

echo "OpenCV has been completely removed."

# 安装编译依赖项
echo "Installing dependencies for building OpenCV..."
sudo_with_password apt-get install -y \
    build-essential \
    pkg-config \
    libgtk2.0-dev \
    libavcodec-dev \
    libavformat-dev \
    libswscale-dev \
    libv4l-dev \
    libxvidcore-dev \
    libx264-dev \
    libjpeg-dev \
    libtiff5-dev \
    libvtk7-dev



# 假设接下来的操作是为了将新编译或下载的OpenCV文件移动到正确的位置
# 注意:这里假设了 'bin', 'include' 和 'lib' 目录已经存在并包含了正确的文件
echo "Copying new OpenCV files to system directories..."
sudo_with_password cp -r bin/* /usr/local/bin/
sudo_with_password cp -r include/* /usr/local/include/
sudo_with_password cp -r lib/* /usr/local/lib/
sudo_with_password cp -r lib/*.so /usr/local/lib/

# 检查并移动 Python 包目录
if [ -d "lib/python3.8/site-packages" ]; then
    echo "Moving site-packages to dist-packages..."
    sudo_with_password mv lib/python3.8/site-packages lib/python3.8/dist-packages
fi

# 创建符号链接以便兼容性
sudo_with_password mv /usr/local/include/opencv4 /usr/local/include/opencv
sudo_with_password ln -s /usr/local/include/opencv/opencv2 /usr/local/include/opencv2
sudo_with_password ln -s /usr/local/include/opencv/opencv2 /usr/local/include/opencv4
sudo_with_password ldconfig
echo "OpenCV installation preparation complete."
;