环境:一键编译opencv-4.8.0(cuda),前提是已经安装好了cuda和cudnn
参考:
https://blog.csdn.net/weixin_46947765/article/details/130980957
- 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
- 打开主目录下的 .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
如果报错:
- # 报错:
- 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博客
查看算力可参考下面文章:
如果cmake生成配置文件时下载第三方库失败,具体日志参考build目录下CMakeDownloadLog.txt文件,可以先手动下载再编译,可以使用build目录下的download_with_wget.sh进行下载,如果报错解析主机失败连接不上,可以替换下载连接,将下载脚本中的raw.githubusercontent.com全部替换为aw.gitmirror.com,再次下载。
- lu@host:/home/lu/project/opencv-4.8.0/build$ cat download_with_wget.sh
- mkdir -p $(dirname /home/lu/project/opencv-4.8.0/.cache/ippicv/43219bdc7e3805adcbe3a1e2f1f3ef3b-ippicv_2021.8_lnx_intel64_20230330_general.tgz) && wget -O "/home/lu/project/opencv-4.8.0/.cache/ippicv/43219bdc7e3805adcbe3a1e2f1f3ef3b-ippicv_2021.8_lnx_intel64_20230330_general.tgz" "https://raw.gitmirror.com/opencv/opencv_3rdparty/1224f78da6684df04397ac0f40c961ed37f79ccb/ippicv/ippicv_2021.8_lnx_intel64_20230330_general.tgz"
- mkdir -p $(dirname /home/lu/project/opencv-4.8.0/.cache/wechat_qrcode/238e2b2d6f3c18d6c3a30de0c31e23cf-detect.caffemodel) && wget -O "/home/lu/project/opencv-4.8.0/.cache/wechat_qrcode/238e2b2d6f3c18d6c3a30de0c31e23cf-detect.caffemodel" "https://raw.gitmirror.com/WeChatCV/opencv_3rdparty/a8b69ccc738421293254aec5ddb38bd523503252/detect.caffemodel"
- mkdir -p $(dirname /home/lu/project/opencv-4.8.0/.cache/wechat_qrcode/6fb4976b32695f9f5c6305c19f12537d-detect.prototxt) && wget -O "/home/lu/project/opencv-4.8.0/.cache/wechat_qrcode/6fb4976b32695f9f5c6305c19f12537d-detect.prototxt" "https://raw.gitmirror.com/WeChatCV/opencv_3rdparty/a8b69ccc738421293254aec5ddb38bd523503252/detect.prototxt"
- mkdir -p $(dirname /home/lu/project/opencv-4.8.0/.cache/wechat_qrcode/cbfcd60361a73beb8c583eea7e8e6664-sr.caffemodel) && wget -O "/home/lu/project/opencv-4.8.0/.cache/wechat_qrcode/cbfcd60361a73beb8c583eea7e8e6664-sr.caffemodel" "https://raw.gitmirror.com/WeChatCV/opencv_3rdparty/a8b69ccc738421293254aec5ddb38bd523503252/sr.caffemodel"
- mkdir -p $(dirname /home/lu/project/opencv-4.8.0/.cache/wechat_qrcode/69db99927a70df953b471daaba03fbef-sr.prototxt) && wget -O "/home/lu/project/opencv-4.8.0/.cache/wechat_qrcode/69db99927a70df953b471daaba03fbef-sr.prototxt" "https://raw.gitmirror.com/WeChatCV/opencv_3rdparty/a8b69ccc738421293254aec5ddb38bd523503252/sr.prototxt"
- mkdir -p $(dirname /home/lu/project/opencv-4.8.0/.cache/xfeatures2d/boostdesc/0ea90e7a8f3f7876d450e4149c97c74f-boostdesc_bgm.i) && wget -O "/home/lu/project/opencv-4.8.0/.cache/xfeatures2d/boostdesc/0ea90e7a8f3f7876d450e4149c97c74f-boostdesc_bgm.i" "https://raw.gitmirror.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_bgm.i"
- mkdir -p $(dirname /home/lu/project/opencv-4.8.0/.cache/xfeatures2d/boostdesc/232c966b13651bd0e46a1497b0852191-boostdesc_bgm_bi.i) && wget -O "/home/lu/project/opencv-4.8.0/.cache/xfeatures2d/boostdesc/232c966b13651bd0e46a1497b0852191-boostdesc_bgm_bi.i" "https://raw.gitmirror.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_bgm_bi.i"
- mkdir -p $(dirname /home/lu/project/opencv-4.8.0/.cache/xfeatures2d/boostdesc/324426a24fa56ad9c5b8e3e0b3e5303e-boostdesc_bgm_hd.i) && wget -O "/home/lu/project/opencv-4.8.0/.cache/xfeatures2d/boostdesc/324426a24fa56ad9c5b8e3e0b3e5303e-boostdesc_bgm_hd.i" "https://raw.gitmirror.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_bgm_hd.i"
- mkdir -p $(dirname /home/lu/project/opencv-4.8.0/.cache/xfeatures2d/boostdesc/202e1b3e9fec871b04da31f7f016679f-boostdesc_binboost_064.i) && wget -O "/home/lu/project/opencv-4.8.0/.cache/xfeatures2d/boostdesc/202e1b3e9fec871b04da31f7f016679f-boostdesc_binboost_064.i" "https://raw.gitmirror.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_binboost_064.i"
- mkdir -p $(dirname /home/lu/project/opencv-4.8.0/.cache/xfeatures2d/boostdesc/98ea99d399965c03d555cef3ea502a0b-boostdesc_binboost_128.i) && wget -O "/home/lu/project/opencv-4.8.0/.cache/xfeatures2d/boostdesc/98ea99d399965c03d555cef3ea502a0b-boostdesc_binboost_128.i" "https://raw.gitmirror.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_binboost_128.i"
- mkdir -p $(dirname /home/lu/project/opencv-4.8.0/.cache/xfeatures2d/boostdesc/e6dcfa9f647779eb1ce446a8d759b6ea-boostdesc_binboost_256.i) && wget -O "/home/lu/project/opencv-4.8.0/.cache/xfeatures2d/boostdesc/e6dcfa9f647779eb1ce446a8d759b6ea-boostdesc_binboost_256.i" "https://raw.gitmirror.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_binboost_256.i"
- mkdir -p $(dirname /home/lu/project/opencv-4.8.0/.cache/xfeatures2d/boostdesc/0ae0675534aa318d9668f2a179c2a052-boostdesc_lbgm.i) && wget -O "/home/lu/project/opencv-4.8.0/.cache/xfeatures2d/boostdesc/0ae0675534aa318d9668f2a179c2a052-boostdesc_lbgm.i" "https://raw.gitmirror.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_lbgm.i"
- mkdir -p $(dirname /home/lu/project/opencv-4.8.0/.cache/xfeatures2d/vgg/e8d0dcd54d1bcfdc29203d011a797179-vgg_generated_48.i) && wget -O "/home/lu/project/opencv-4.8.0/.cache/xfeatures2d/vgg/e8d0dcd54d1bcfdc29203d011a797179-vgg_generated_48.i" "https://raw.gitmirror.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_48.i"
- mkdir -p $(dirname /home/lu/project/opencv-4.8.0/.cache/xfeatures2d/vgg/7126a5d9a8884ebca5aea5d63d677225-vgg_generated_64.i) && wget -O "/home/lu/project/opencv-4.8.0/.cache/xfeatures2d/vgg/7126a5d9a8884ebca5aea5d63d677225-vgg_generated_64.i" "https://raw.gitmirror.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_64.i"
- mkdir -p $(dirname /home/lu/project/opencv-4.8.0/.cache/xfeatures2d/vgg/7cd47228edec52b6d82f46511af325c5-vgg_generated_80.i) && wget -O "/home/lu/project/opencv-4.8.0/.cache/xfeatures2d/vgg/7cd47228edec52b6d82f46511af325c5-vgg_generated_80.i" "https://raw.gitmirror.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_80.i"
- mkdir -p $(dirname /home/lu/project/opencv-4.8.0/.cache/xfeatures2d/vgg/151805e03568c9f490a5e3a872777b75-vgg_generated_120.i) && wget -O "/home/lu/project/opencv-4.8.0/.cache/xfeatures2d/vgg/151805e03568c9f490a5e3a872777b75-vgg_generated_120.i" "https://raw.gitmirror.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_120.i"
- mkdir -p $(dirname /home/lu/project/opencv-4.8.0/.cache/data/7505c44ca4eb54b4ab1e4777cb96ac05-face_landmark_model.dat) && wget -O "/home/lu/project/opencv-4.8.0/.cache/data/7505c44ca4eb54b4ab1e4777cb96ac05-face_landmark_model.dat" "https://raw.gitmirror.com/opencv/opencv_3rdparty/8afa57abc8229d611c4937165d20e2a2d9fc5a12/face_landmark_model.dat"
- lu@host:/home/lu/project/opencv-4.8.0/build$
如果还是不行可以考虑修改hosts文件
进入 IPAddress.com
输入 raw.githubusercontent.com 查询真实的IP地址
现在查到的是 185.199.108.133
在最后一行添加以下内容,保存完事
- $ cat /etc/hosts
- 127.0.0.1 localhost
- 127.0.1.1 host
-
- # The following lines are desirable for IPv6 capable hosts
- ::1 ip6-localhost ip6-loopback
- fe00::0 ip6-localnet
- ff00::0 ip6-mcastprefix
- ff02::1 ip6-allnodes
- ff02::2 ip6-allrouters
- # new add line
- 185.199.108.133 raw.githubusercontent.com
-