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. 安装依赖以及编译
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
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 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-configprefix=/home/jetson/opencv-4.10.0/install
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include/opencv4Name: 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-configprefix=prefix=/usr/local
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include/opencv4Name: 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