环境:一键编译opencv-4.8.0(cuda),前提是已经安装好了cuda和cudnn
Anaconda安装
参考:
https://blog.csdn.net/weixin_46947765/article/details/130980957
opencv4.8.0编译安装
一键编译shell脚本
bash
VERSION=4.8.0
test -e ${VERSION}.zip || wget https://github.com/opencv/opencv/archive/refs/tags/${VERSION}.zip
test -e opencv-${VERSION} || unzip ${VERSION}.zip
test -e opencv_extra_${VERSION}.zip || wget -O opencv_extra_${VERSION}.zip https://github.com/opencv/opencv_contrib/archive/refs/tags/${VERSION}.zip
test -e opencv_contrib-${VERSION} || unzip opencv_extra_${VERSION}.zip
cd opencv-${VERSION}
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local/opencv-4.8.0 \
-D WITH_TBB=ON \
-D ENABLE_FAST_MATH=1 \
-D CUDA_FAST_MATH=1 \
-D WITH_CUBLAS=1 \
-D WITH_CUDA=ON \
-D BUILD_opencv_cudacodec=ON \
-D WITH_CUDNN=ON \
-D OPENCV_DNN_CUDA=ON \
-D WITH_QT=OFF \
-D WITH_OPENGL=ON \
-D BUILD_opencv_apps=OFF \
-D BUILD_opencv_python2=OFF \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D OPENCV_PC_FILE_NAME=opencv.pc \
-D OPENCV_ENABLE_NONFREE=ON \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-${VERSION}/modules \
-D INSTALL_PYTHON_EXAMPLES=OFF \
-D INSTALL_C_EXAMPLES=OFF \
-D BUILD_EXAMPLES=OFF \
-D CUDA_ARCH_BIN=7.5 \
-D WITH_FFMPEG=ON \
-D CUDNN_INCLUDE_DIR=/usr/local/cuda/include \
-D CUDNN_LIBRARY=/usr/local/cuda/lib64/libcudnn.so \
..
make -j8
sudo make -j8 install
环境设置:
bash
打开主目录下的 .bashrc文件添加如下路径,例如我的.bashrc文件在/home/lu/下。
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/opencv-4.8.0/lib/pkgconfig
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/opencv-4.8.0/lib
终端运行:source ~/.bashrc
如果报错:
bash
# 报错:
terminate called after throwing an instance of 'cv::Exception'
what(): OpenCV(4.8.0) /media/lu/workspace/WorkSpace/visual_studio/YOLOv8-TensorRT-CPP/libs/tensorrt-cpp-api/scripts/opencv_contrib-4.8.0/modules/cudev/include/opencv2/cudev/grid/detail/transform.hpp:264: error: (-217:Gpu API call) no kernel image is available for execution on the device in function 'call'
已放弃 (核心已转储)
# 或者
error: (-216:No CUDA support) OpenCV was not built to work with the selected device. Please check CUDA_ARCH_PTX or CUDA_ARCH_BIN in your build configuration. in function 'initCUDABackend'
可能是编译opencv的时候指定的算力和显卡算力不一致导致的,可参考:
Please check CUDA_ARCH_PTX or CUDA_ARCH_BIN in your build configuration错误解决-CSDN博客
查看算力可参考下面文章: