问题出现描述
- Opencv : 4.3
- CUDA: 11.1
- Ubuntu 18.04
编译支持CUDA的OpenCV, CMake命令和报错内容如下:
cmake \
-D INCLUDE_DIRS=${OPENCV_INCLUDE_DIR_ANACONDA} $INCLUDE_DIRS \
-D LIBRARY_DIRS=$OPENCV_LIBRARY_ANACONDA $LIBRARY_DIRS \
-D BUILD_PYTHON_SUPPORT=ON \
-D BUILD_DOCS=ON \
-D BUILD_PERF_TESTS=OFF \
-D BUILD_TESTS=OFF \
-D CMAKE_INSTALL_PREFIX=/usr/local/opencv \
-D OPENCV_EXTRA_MODULES_PATH=/home/timber/Library/opencv_contrib/modules \
-D BUILD_opencv_python3=ON \
-D BUILD_opencv_python2=OFF \
-D PYTHON3_EXECUTABLE=${OPENCV_PYTHON_EXECUTABLE_ANACONDA} \
-D PYTHON3_INCLUDE_DIR=${OPENCV_INCLUDE_DIR_ANACONDA} \
-D PYTHON3_PACKAGES_PATH=${OPENCV_PACKAGE_ANACONDA} \
-D BUILD_EXAMPLES=OFF \
-D WITH_GTK=ON \
-D WITH_GTK_2_X=ON \
-D WITH_IPP=ON \
-D BUILD_TIFF=ON \
-D BUILD_opencv_cvv=OFF \
-D INSTALL_CREATE_DISTRIB=ON \
-D WITH_FFMPEG=ON \
-D WITH_GSTREAMER=ON \
-D WITH_V4L=ON \
-D WITH_LIBV4L=ON \
-D WITH_TBB=ON \
-D WITH_QT=ON \
-D WITH_OPENGL=ON \
-D WITH_CUDA=ON \
-D CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda \
-D CMAKE_LIBRARY_PATH=/usr/local/cuda/lib64/stubs \
-D CUDA_ARCH_BIN="8.6" \
-D CUDA_ARCH_PTX="" \
-D WITH_CUBLAS=ON \
-D WITH_NVCUVID=ON \
-D ENABLE_FAST_MATH=1 \
-D CUDA_FAST_MATH=1 \
-D ENABLE_PRECOMPILED_HEADERS=OFF \
-D CMAKE_BUILD_TYPE=Release \
..
其中一些变量是python是虚拟环境相关的,在激活虚拟环境后运行如下命令获得,编译时取消激活防止库冲突。
OPENCV_PYTHON_EXECUTABLE_ANACONDA=$(which python${PYTHON_VERSION}) && \
OPENCV_INSTALL_PATH=$(python -c "import sys; print(sys.prefix)") && \
OPENCV_INCLUDE_DIR_ANACONDA=$(python -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") && \
OPENCV_PACKAGE_ANACONDA=$(python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())") && \
OPENCV_LIBRARY_ANACONDA=$(python -c "import distutils.sysconfig as sysconfig; print(sysconfig.get_config_var('LIBDIR'))")
报错内容如下:
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
CUDA_nppicom_LIBRARY (ADVANCED)
解决办法
-
先按照OpenCV安装时CUDA_nppi_LIBRARY (ADVANCED) 报错处理方法的方法更改,但是依旧报错。
-
解决办法来自*: Build OpenCV-4.0.0 with cuda 11 on Ubuntu 18.04
只需要在cmake命令中添加-D CUDA_nppicom_LIBRARY=true
这个未定义的变量即可。
完整命令:
cmake \
-D INCLUDE_DIRS=${OPENCV_INCLUDE_DIR_ANACONDA} $INCLUDE_DIRS \
-D LIBRARY_DIRS=$OPENCV_LIBRARY_ANACONDA $LIBRARY_DIRS \
-D BUILD_PYTHON_SUPPORT=ON \
-D BUILD_DOCS=ON \
-D BUILD_PERF_TESTS=OFF \
-D BUILD_TESTS=OFF \
-D CMAKE_INSTALL_PREFIX=/usr/local/opencv \
-D OPENCV_EXTRA_MODULES_PATH=/home/timber/Library/opencv_contrib/modules \
-D BUILD_opencv_python3=ON \
-D BUILD_opencv_python2=OFF \
-D PYTHON3_EXECUTABLE=${OPENCV_PYTHON_EXECUTABLE_ANACONDA} \
-D PYTHON3_INCLUDE_DIR=${OPENCV_INCLUDE_DIR_ANACONDA} \
-D PYTHON3_PACKAGES_PATH=${OPENCV_PACKAGE_ANACONDA} \
-D BUILD_EXAMPLES=OFF \
-D WITH_GTK=ON \
-D WITH_GTK_2_X=ON \
-D WITH_IPP=ON \
-D BUILD_TIFF=ON \
-D BUILD_opencv_cvv=OFF \
-D INSTALL_CREATE_DISTRIB=ON \
-D WITH_FFMPEG=ON \
-D WITH_GSTREAMER=ON \
-D WITH_V4L=ON \
-D WITH_LIBV4L=ON \
-D WITH_TBB=ON \
-D WITH_QT=ON \
-D WITH_OPENGL=ON \
-D WITH_CUDA=ON \
-D CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda \
-D CMAKE_LIBRARY_PATH=/usr/local/cuda/lib64/stubs \
-D CUDA_ARCH_BIN="8.6" \
-D CUDA_ARCH_PTX="" \
-D WITH_CUBLAS=ON \
-D WITH_NVCUVID=ON \
-D ENABLE_FAST_MATH=1 \
-D CUDA_FAST_MATH=1 \
-D ENABLE_PRECOMPILED_HEADERS=OFF \
-D CMAKE_BUILD_TYPE=Release \
-D CUDA_nppicom_LIBRARY=true \
..
结果: