1. 卸载 原来的CPU版 opencv
sudo apt purge libopencv*
sudo apt update
2. 找一个路径,存放下载 opencv 安装包
sudo wget -O opencv-4.9.0.zip wget https://github.com/opencv/opencv/archive/4.9.0.zip
sudo wget -O opencv_contrib-4.9.0.zip wget https://github.com/opencv/opencv_contrib/archive/4.9.0.zip
解压、放到一个文件夹中
unzip opencv.zip && unzip opencv_contrib.zip
组织成这种层次:
├── 4.9.0
│ ├── opencv-4.9.0
│ └── opencv_contrib-4.9.0
3. 编译支持GPU加速的 OpenCV
cd opencv-4.9.0
# 3. Create build directory and switch into it
mkdir build && cd build
├── 4.9.0
│ ├── opencv-4.9.0
│ ├── build
│ └── opencv_contrib-4.9.0
查看 agx orin nx 的 CUDA_ARCH_BIN 为 8.7。 jetson 除了网址可以去查:https://developer.nvidia.com/cuda-gpus#compute ,也可以使用 jtop 查询 CUDA_ARCH_BIN。
Configure, 在 build 目录内
sudo cmake -D CMAKE_BUILD_TYPE=Release \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.9.0/modules \
-D CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda \
-D CUDA_ARCH_BIN=8.7 \
-D CUDA_ARCH_PTX=8.7 \
-D WITH_CUDA=ON \
-D OPENCV_DNN_CUDA=ON \
-D WITH_CUDNN=ON \
-D ENABLE_FAST_MATH=ON \
-D CUDA_FAST_MATH=ON \
-D WITH_CUBLAS=ON \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D BUILD_EXAMPLES=ON \
-D BUILD_NEW_PYTHON_SUPPORT=ON \
-D BUILD_opencv_python3=ON \
-D HAVE_opencv_python3=ON \
-D OPENCV_ENABLE_NONFREE=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D OPENCV_PYTHON3_INSTALL_PATH=/usr/local/lib/python3.8/dist-packages \
-D PYTHON_EXECUTABLE=/usr/bin/python3.8 \
..
注意:
- CMAKE_INSTALL_PREFIX=/usr/local : opencv 的安装路径
- OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.9.0/modules
ll /usr/local/cuda
可以查看 cuda 是谁的软链接- CUDA_ARCH_BIN CUDA_ARCH_PTX 都是 8.7
- python3 支持的话,请使用
which python3
和ll /usr/bin/python3
找到是哪个 python3, 从而 获得 OPENCV_PYTHON3_INSTALL_PATH 路径。
当前操作主要是网络问题可能会报错,请使用梯子
https://www.cnblogs.com/odesey/p/17960079
export http_proxy=http:你的IP:7890
export https_proxy=http:你的IP:7890
失败的话: 清除 rm CMakeCache.txt
报错:
-- Checking for module 'gtk+-2.0'
-- No package 'gtk+-2.0' found
-- Allocator metrics storage type: 'int'
CMake Error at modules/core/CMakeLists.txt:158 (message):
CUDA: OpenCV requires enabled 'cudev' module from 'opencv_contrib'
repository: https://github.com/opencv/opencv_contrib
解决:opencv_contrib-4.9.0 的路径没填对,已更正。
sudo apt install -y libgtk2.0-dev
成功的配置:
-- General configuration for OpenCV 4.9.0 =====================================
-- Version control: unknown
--
-- Extra modules:
-- Location (extra): /home/nvidia/Downloads/4.9.0/opencv_contrib-4.9.0/modules
-- Version control (extra): unknown
--
-- Platform:
-- Timestamp: 2024-03-27T06:50:39Z
-- Host: Linux 5.10.104-tegra aarch64
-- CMake: 3.16.3
-- CMake generator: Unix Makefiles
-- CMake build tool: /usr/bin/make
-- Configuration: Release
--
-- CPU/HW features:
-- Baseline: NEON FP16
-- Dispatched code generation: NEON_DOTPROD NEON_FP16
-- requested: NEON_FP16 NEON_BF16 NEON_DOTPROD
-- NEON_DOTPROD (1 files): + NEON_DOTPROD
-- NEON_FP16 (2 files): + NEON_FP16
--
-- C/C++:
-- Built as dynamic libs?: YES
-- C++ standard: 11
-- C++ Compiler: /usr/bin/c++ (ver 9.4.0)
-- C++ flags (Release): -fsigned-char -ffast-math -fno-finite-math-only -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -DNDEBUG
-- C++ flags (Debug): -fsigned-char -ffast-math -fno-finite-math-only -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -fvisibility=hidden -fvisibility-inlines-hidden -g -O0 -DDEBUG -D_DEBUG
-- C Compiler: /usr/bin/cc
-- C flags (Release): -fsigned-char -ffast-math -fno-finite-math-only -W -Wall -Wreturn-type -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -fvisibility=hidden -O3 -DNDEBUG -DNDEBUG
-- C flags (Debug): -fsigned-char -ffast-math -fno-finite-math-only -W -Wall -Wreturn-type -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -fvisibility=hidden -g -O0 -DDEBUG -D_DEBUG
-- Linker flags (Release): -Wl,--gc-sections -Wl,--as-needed -Wl,--no-undefined
-- Linker flags (Debug): -Wl,--gc-sections -Wl,--as-needed -Wl,--no-undefined
-- ccache: NO
-- Precompiled headers: NO
-- Extra dependencies: m pthread cudart_static dl rt nppc nppial nppicc nppidei nppif nppig nppim nppist nppisu nppitc npps cublas cudnn cufft -L/usr/local/cuda/lib64 -L/usr/lib/aarch64-linux-gnu
-- 3rdparty dependencies:
--
-- OpenCV modules:
-- To be built: aruco bgsegm bioinspired calib3d ccalib core cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev datasets dnn dnn_objdetect dnn_superres dpm face features2d flann freetype fuzzy gapi hfs highgui img_hash imgcodecs imgproc intensity_transform line_descriptor mcc ml objdetect optflow phase_unwrapping photo plot python3 quality rapid reg rgbd saliency shape stereo stitching structured_light superres surface_matching text tracking ts video videoio videostab wechat_qrcode xfeatures2d ximgproc xobjdetect xphoto
-- Disabled: world
-- Disabled by dependency: -
-- Unavailable: alphamat cannops cvv hdf java julia matlab ovis python2 sfm viz
-- Applications: tests perf_tests examples apps
-- Documentation: NO
-- Non-free algorithms: YES
--
-- GUI: GTK3
-- GTK+: YES (ver 3.24.20)
-- GThread : YES (ver 2.64.6)
-- GtkGlExt: NO
-- VTK support: NO
--
-- Media I/O:
-- ZLib: /usr/lib/aarch64-linux-gnu/libz.so (ver 1.2.11)
-- JPEG: /usr/lib/aarch64-linux-gnu/libjpeg.so (ver 80)
-- WEBP: build (ver encoder: 0x020f)
-- PNG: /usr/lib/aarch64-linux-gnu/libpng.so (ver 1.6.37)
-- TIFF: /usr/lib/aarch64-linux-gnu/libtiff.so (ver 42 / 4.1.0)
-- JPEG 2000: build (ver 2.5.0)
-- OpenEXR: /usr/lib/aarch64-linux-gnu/libImath.so /usr/lib/aarch64-linux-gnu/libIlmImf.so /usr/lib/aarch64-linux-gnu/libIex.so /usr/lib/aarch64-linux-gnu/libHalf.so /usr/lib/aarch64-linux-gnu/libIlmThread.so (ver 2_3)
-- HDR: YES
-- SUNRASTER: YES
-- PXM: YES
-- PFM: YES
--
-- Video I/O:
-- DC1394: YES (2.2.5)
-- FFMPEG: YES
-- avcodec: YES (58.54.100)
-- avformat: YES (58.29.100)
-- avutil: YES (56.31.100)
-- swscale: YES (5.5.100)
-- avresample: YES (4.0.0)
-- GStreamer: YES (1.16.3)
-- v4l/v4l2: YES (linux/videodev2.h)
--
-- Parallel framework: pthreads
--
-- Trace: YES (with Intel ITT)
--
-- Other third-party libraries:
-- Lapack: NO
-- Eigen: NO
-- Custom HAL: YES (carotene (ver 0.0.1, Auto detected))
-- Protobuf: build (3.19.1)
-- Flatbuffers: builtin/3rdparty (23.5.9)
--
-- NVIDIA CUDA: YES (ver 11.4, CUFFT CUBLAS FAST_MATH)
-- NVIDIA GPU arch: 87
-- NVIDIA PTX archs: 87
--
-- cuDNN: YES (ver 8.6.0)
--
-- OpenCL: YES (no extra features)
-- Include path: /home/nvidia/Downloads/4.9.0/opencv-4.9.0/3rdparty/include/opencl/1.2
-- Link libraries: Dynamic load
--
-- Python 3:
-- Interpreter: /usr/bin/python3.8 (ver 3.8.10)
-- Libraries: /usr/lib/aarch64-linux-gnu/libpython3.8.so (ver 3.8.10)
-- numpy: /usr/lib/python3/dist-packages/numpy/core/include (ver 1.17.4)
-- install path: /usr/local/lib/python3.8/dist-packages/cv2/python-3.8
--
-- Python (for build): /usr/bin/python3.8
--
-- Java:
-- ant: NO
-- Java: NO
-- JNI: NO
-- Java wrappers: NO
-- Java tests: NO
--
-- Install to: /usr/local
-- -----------------------------------------------------------------
--
-- Configuring done
-- Generating done
-- Build files have been written to: /home/nvidia/Downloads/4.9.0/opencv-4.9.0/build
编译:
sudo make -j$(nproc)
$(nproc) 可以使用的最大线程
4. 安装 OpenCV
sudo make install -j 12
注意:如果没有报错,可能会卡住,你需要等待。
卡住的话或许你也可以使用:sudo make install -j 1
, 然后等待。然后一直在安装的话,你就可以换为:`` sudo make install -j 12`
5. python 测试 和 jtop 确认:
使用 python3 解释器和 cv2.cuda.printCudaDeviceInfo(0) 来验证库是否正常工作:
除了上面的 python 来测试。jetson 也可以用 jtop
来查看。
opencv with CUDA YES
6. 清理
安装后,安装文件和压缩文件 我们将不再需要,可以安全删除:
cd ../..
sudo rm -rf ./*
其他
我找到一个安装脚本:https://github.com/s95huang/Jetson_orin_cv_cuda/tree/main
此外还有 docker 的: https://github.com/JulianAssmann/opencv-cuda-docker
但是我没有尝试
标签:ver,opencv4.9,--,4.9,nx,opencv,cuda,YES,usr From: https://www.cnblogs.com/odesey/p/18099858