ubuntu 20.04 + Anaconda + cuda-11.8 + opencv-4.8.0(cuda)

环境:一键编译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博客

查看算力可参考下面文章:

Pytorch查看torch版本,查看torchvision版本,查看CUDA版本,查看cudnn版本,查看pytorch可用性,查看cuda可用性,查看cudnn可用性,查看显卡,指定运算GPU_C系语言的博客-CSDN博客

相关推荐
安於宿命1 分钟前
【Linux】简易版shell
linux·运维·服务器
黄小耶@13 分钟前
linux常见命令
linux·运维·服务器
叫我龙翔14 分钟前
【计网】实现reactor反应堆模型 --- 框架搭建
linux·运维·网络
古驿幽情16 分钟前
CentOS AppStream 8 手动更新 yum源
linux·运维·centos·yum
BillKu17 分钟前
Linux(CentOS)安装 Nginx
linux·运维·nginx·centos
BillKu21 分钟前
Linux(CentOS)yum update -y 事故
linux·运维·centos
a2663789625 分钟前
解决yum命令报错“Could not resolve host: mirrorlist.centos.org
linux·运维·centos
jndingxin36 分钟前
OpenCV相机标定与3D重建(1)概述
数码相机·opencv·3d
有谁看见我的剑了?1 小时前
Ubuntu 22.04.5 配置vlan子接口和网桥
服务器·网络·ubuntu
2739920292 小时前
Ubuntu20.04 安装build-essential问题
linux