ubuntu18.04编译cuda版本的opcv4.5.5
ref:https://www.jianshu.com/p/543b5d66578e
ref:https://blog.csdn.net/m0_37210884/article/details/115660669
ref:https://www.codenong.com/cs109506378/(错误解决)
1、安装包
- opencv4.5.5&opencv_contrib-4.5.5
- nvidia显卡驱动(安装参考:https://blog.csdn.net/weixin_44857882/article/details/124108458?spm=1001.2014.3001.5501)
- cuda(安装参考:https://blog.csdn.net/weixin_44857882/article/details/124108458?spm=1001.2014.3001.5501)
- cuDNN(安装参考:https://blog.csdn.net/qq_29600137/article/details/122414416)
2、检查nvidia显卡驱动及支持的最大cuda版本(CUDA Driver Version)
键入
nvidia-smi
可以看到,显卡驱动版本为470.129.06,可支持的cuda最高版本为11.4
3、查看安装的cuda版本(CUDA Runtime Version)
键入
nvcc -V
可以看到,cuda的版本是10.2.89
//CUDA Driver Version和CUDA Runtime Version的版本不必非要一致,但CUDA Runtime Version要<=CUDA Driver Version
显然,合理!
4、查看cudnn版本
键入
cat /usr/local/cuda/include/cudnn.h | grep CUDNN_MAJOR -A 2
无果,查看已安装的版本
dpkg -l | grep cudnn
可以看到有8.4.1.50
5、下载opencv版本和相应的cuda版本(opencv_contrib)
opencv下载地址: https://opencv.org/releases/
opencv_contrib下载地址: https://github.com/opencv/opencv_contrib
解压,将opencv_contrib-4.5.5放置opencv4.5.5目录下
mv opencv_contrib-4.5.1 opencv-4.5.1/
6、编译安装
cd opencv-4.5.1
mkdir build
cd build
cmake \
-D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_C_COMPILER=/usr/bin/gcc \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules \
-D CUDA_CUDA_LIBRARY=/usr/lib/x86_64-linux-gnu/libcuda.so \
-D CUDA_ARCH_BIN=6.1 \
-D CUDA_ARCH_PTX="" \
-D WITH_CUDA=ON \
-D WITH_TBB=ON \
-D WITH_FFMPEG=ON \
-D WITH_CUFFT=ON \
-D WITH_OPENMP=ON \
-D INSTALL_C_EXAMPLES=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D BUILD_EXAMPLES=ON \
-D WITH_QT=ON \
-D WITH_GSTREAMER=ON \
-D WITH_OPENGL=ON \
-D ENABLE_FAST_MATH=1 \
-D CUDA_FAST_MATH=1 \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D OPENCV_PC_FILE_NAME=opencv.pc \
-D CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda \
-D CMAKE_LIBRARY_PATH=/usr/local/cuda/lib64/stubs \
-D WITH_CUBLAS=ON \
-D WITH_NVCUVID=ON \
-D BUILD_opencv_cudacodec=ON \
-D OPENCV_DNN_CUDA=ON \
-D WITH_CUDNN=ON \
-D OPENCV_ENABLE_NONFREE=ON\
-D WITH_GSTREAMER=ON \
-D BUILD_EXAMPLES=ON ..
简单点:
cmake -D CMAKE_INSTALL_PREFIX=/usr/local -D CMAKE_BUILD_TYPE=Release -D OPENCV_GENERATE_PKGCONFIG=ON -D ENABLE_CXX11=1 -D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib-4.5.5/modules -D OPENCV_ENABLE_NONFREE=True -D INSTALL_PYTHON_EXAMPLES=ON -D INSTALL_C_EXAMPLES=ON -D WITH_CUDA=ON -D WITH_TBB=ON -D ENABLE_FAST_MATH=1 -D WITH_OPENMP=ON -D WITH_CUFFT=ON -D WITH_CUBLAS=ON -D CUDA_FAST_MATH=ON ..
CUDA_ARCH_BIN查看:https://blog.csdn.net/CSS360/article/details/109696878
CMAKE_INSTALL_PREFIX:安装路径,如果已经安装过其他版本可换个路径/usr/local/opencv4.5.5/,默认路径为/usr/local/
OPENCV_EXTRA_MODULES_PATH:为contrib中modules的路径
其他参数自行查看,分享至评论区的话大为感谢!!!
-- General configuration for OpenCV 4.5.5 =====================================
-- Version control: unknown
--
-- Extra modules:
-- Location (extra): /home/mj/lib/opencv-4.5.5/opencv_contrib/modules
-- Version control (extra): unknown
--
-- Platform:
-- Timestamp: 2022-09-12T13:22:50Z
-- Host: Linux 5.4.0-124-generic x86_64
-- CMake: 3.22.4
-- CMake generator: Unix Makefiles
-- CMake build tool: /usr/bin/make
-- Configuration: RELEASE
--
-- CPU/HW features:
-- Baseline: SSE SSE2 SSE3
-- requested: SSE3
-- Dispatched code generation: SSE4_1 SSE4_2 FP16 AVX AVX2 AVX512_SKX
-- requested: SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
-- SSE4_1 (18 files): + SSSE3 SSE4_1
-- SSE4_2 (2 files): + SSSE3 SSE4_1 POPCNT SSE4_2
-- FP16 (1 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
-- AVX (5 files): + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
-- AVX2 (33 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
-- AVX512_SKX (8 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2 AVX_512F AVX512_COMMON AVX512_SKX
--
-- C/C++:
-- Built as dynamic libs?: YES
-- C++ standard: 11
-- C++ Compiler: /usr/bin/c++ (ver 7.5.0)
-- C++ flags (Release): -fsigned-char -ffast-math -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-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 -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -fopenmp -O3 -DNDEBUG -DNDEBUG
-- C++ flags (Debug): -fsigned-char -ffast-math -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-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 -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -fopenmp -g -O0 -DDEBUG -D_DEBUG
-- C Compiler: /usr/bin/gcc
-- C flags (Release): -fsigned-char -ffast-math -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-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 -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -fopenmp -O3 -DNDEBUG -DNDEBUG
-- C flags (Debug): -fsigned-char -ffast-math -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-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 -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -fopenmp -g -O0 -DDEBUG -D_DEBUG
-- Linker flags (Release): -Wl,--gc-sections -Wl,--as-needed
-- Linker flags (Debug): -Wl,--gc-sections -Wl,--as-needed
-- ccache: NO
-- Precompiled headers: NO
-- Extra dependencies: m pthread cudart_static dl rt nppc nppial nppicc nppicom nppidei nppif nppig nppim nppist nppisu nppitc npps cublas cudnn cufft -L/usr/local/cuda/lib64 -L/usr/lib/x86_64-linux-gnu
-- 3rdparty dependencies:
--
-- OpenCV modules:
-- To be built: alphamat aruco barcode bgsegm bioinspired calib3d ccalib core cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev cvv datasets dnn dnn_objdetect dnn_superres dpm face features2d flann freetype fuzzy gapi hdf hfs highgui img_hash imgcodecs imgproc intensity_transform line_descriptor mcc ml objdetect optflow phase_unwrapping photo plot python2 quality rapid reg rgbd saliency sfm shape stereo stitching structured_light superres surface_matching text tracking ts video videoio videostab viz wechat_qrcode xfeatures2d ximgproc xobjdetect xphoto
-- Disabled: world
-- Disabled by dependency: -
-- Unavailable: java julia matlab ovis python3
-- Applications: tests perf_tests examples apps
-- Documentation: NO
-- Non-free algorithms: YES
--
-- GUI: QT5
-- QT: YES (ver 5.9.5 )
-- QT OpenGL support: YES (Qt5::OpenGL 5.9.5)
-- GTK+: YES (ver 3.22.30)
-- GThread : YES (ver 2.56.4)
-- GtkGlExt: NO
-- OpenGL support: YES (/usr/lib/x86_64-linux-gnu/libGL.so /usr/lib/x86_64-linux-gnu/libGLU.so)
-- VTK support: YES (ver 8.2.0)
--
-- Media I/O:
-- ZLib: /usr/local/lib/libz.so (ver 1.2.11)
-- JPEG: /usr/lib/x86_64-linux-gnu/libjpeg.so (ver 80)
-- WEBP: /usr/lib/x86_64-linux-gnu/libwebp.so (ver encoder: 0x020e)
-- PNG: /usr/lib/x86_64-linux-gnu/libpng.so (ver 1.6.34)
-- TIFF: /usr/lib/x86_64-linux-gnu/libtiff.so (ver 42 / 4.0.9)
-- JPEG 2000: OpenJPEG (ver 2.3.0)
-- OpenEXR: /usr/lib/x86_64-linux-gnu/libImath.so /usr/lib/x86_64-linux-gnu/libIlmImf.so /usr/lib/x86_64-linux-gnu/libIex.so /usr/lib/x86_64-linux-gnu/libHalf.so /usr/lib/x86_64-linux-gnu/libIlmThread.so (ver 2_2)
-- HDR: YES
-- SUNRASTER: YES
-- PXM: YES
-- PFM: YES
--
-- Video I/O:
-- DC1394: YES (2.2.5)
-- FFMPEG: YES
-- avcodec: YES (57.107.100)
-- avformat: YES (57.83.100)
-- avutil: YES (55.78.100)
-- swscale: YES (4.8.100)
-- avresample: YES (3.7.0)
-- GStreamer: NO
-- v4l/v4l2: YES (linux/videodev2.h)
--
-- Parallel framework: TBB (ver 2017.0 interface 9107)
--
-- Trace: YES (with Intel ITT)
--
-- Other third-party libraries:
-- VA: NO
-- Lapack: NO
-- Eigen: YES (ver 3.3.90)
-- Custom HAL: NO
-- Protobuf: build (3.19.1)
--
-- NVIDIA CUDA: YES (ver 10.2, CUFFT CUBLAS FAST_MATH)
-- NVIDIA GPU arch: 61
-- NVIDIA PTX archs:
--
-- cuDNN: YES (ver 8.5.0)
--
-- OpenCL: YES (no extra features)
-- Include path: /home/mj/lib/opencv-4.5.5/3rdparty/include/opencl/1.2
-- Link libraries: Dynamic load
--
-- Python 2:
-- Interpreter: /usr/bin/python2.7 (ver 2.7.17)
-- Libraries: /usr/lib/x86_64-linux-gnu/libpython2.7.so (ver 2.7.17)
-- numpy: /usr/lib/python2.7/dist-packages/numpy/core/include (ver 1.13.3)
-- install path: lib/python2.7/dist-packages/cv2/python-2.7
--
-- Python (for build): /usr/bin/python2.7
--
-- Java:
-- ant: NO
-- JNI: /usr/lib/jvm/java-8-openjdk-amd64/include /usr/lib/jvm/java-8-openjdk-amd64/include/linux /usr/lib/jvm/java-8-openjdk-amd64/include
-- Java wrappers: NO
-- Java tests: NO
--
-- Install to: /usr/local
-- -----------------------------------------------------------------
希望你是幸运儿!!!
//最大线程,若有幸没出错
make -j`nproc`
sudo make install
7、错误合集
1、文件下载不下来
-
face_landmark_model.dat
-
ippicv_2020_lnx_intel64_20191018_general.tgz
-
….i文件
解决办法: -
下载相应文件
-
修改相应下载地址改为自己的文件存放路径
-
ref:https://blog.csdn.net/m0_37210884/article/details/115660669
2、关于cuda静/动态连接库的问题
说明/usr/local/cuda-11.6/lib64/文件夹下没有libcudart_static.a静态连接库
locate libcudart_static.a
将该文件放入上述路径
奇怪的点:系统并没有安装cuda-11.6,但libopencv_sfm需要,就很奇怪,为了编译成功,我自己创建该文件夹,并将找到的库文件拷贝到此文件夹下
继续编译,出现上述错误,都使用这种方法
我这里以为要拷贝很多库,直接将装的cuda-10.2的文件复制了一份命名为cuda-11.6。