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博客

相关推荐
White_Mountain1 小时前
在Ubuntu中配置mysql,并允许外部访问数据库
数据库·mysql·ubuntu
cominglately2 小时前
centos单机部署seata
linux·运维·centos
魏 无羡2 小时前
linux CentOS系统上卸载docker
linux·kubernetes·centos
coder_pig3 小时前
📝小记:Ubuntu 部署 Jenkins 打包 Flutter APK
flutter·ubuntu·jenkins
CircleMouse3 小时前
Centos7, 使用yum工具,出现 Could not resolve host: mirrorlist.centos.org
linux·运维·服务器·centos
菜狗woc3 小时前
opencv-python的简单练习
人工智能·python·opencv
木子Linux3 小时前
【Linux打怪升级记 | 问题01】安装Linux系统忘记设置时区怎么办?3个方法教你回到东八区
linux·运维·服务器·centos·云计算
mit6.8243 小时前
Ubuntu 系统下性能剖析工具: perf
linux·运维·ubuntu
鹏大师运维3 小时前
聊聊开源的虚拟化平台--PVE
linux·开源·虚拟化·虚拟机·pve·存储·nfs
watermelonoops4 小时前
Windows安装Ubuntu,Deepin三系统启动问题(XXX has invalid signature 您需要先加载内核)
linux·运维·ubuntu·deepin