ubuntu卸载及重新安装opencv(解决CUDA10与opencv 3.1.0版本不兼容问题)

目录

  • 1. 查看opencv版本
  • 2. 卸载opencv
  • 3. 安装opencv 3.1.0
  • 4. reference

前言:最近在复现关于动态语义SLAM论文,需要用到caffe,发现之前安装的opencv有问题,遂卸载opencv3.4,重装opencv 3.1.0。

1. 查看opencv版本

pkg-config --modversion opencv

2. 卸载opencv

  1. 直接搜索opencv,找到opencv 3.4.0,进入build目录下,终端输入:
sudo make uninstall
cd  ..
sudo rm -r build
  1. 清理 /usr 中所有 opencv 相关项
sudo rm -r /usr/local/include/opencv2 /usr/local/include/opencv /usr/include/opencv /usr/include/opencv2 /usr/local/share/opencv /usr/local/share/OpenCV /usr/share/opencv /usr/share/OpenCV /usr/local/bin/opencv* /usr/local/lib/libopencv*
cd /usr

find . -name "*opencv*" | xargs sudo rm -rf

然后在home目录下也去搜索opencv,并删除。

在这里插入图片描述

3. 安装opencv 3.1.0

opencv官网选择 3.1.0 版本的 source , 下载 opencv-3.1.0.zip。

这个是真的慢,我直接windows下载了再拷贝过来。

解压到你要安装的位置,命令行进入已解压的文件夹 opencv-3.1.0 目录下,执行:

mkdir build 

cd build

cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..

trick1:
ubuntu卸载及重新安装opencv(解决CUDA10与opencv 3.1.0版本不兼容问题)_第1张图片

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_nppi_LIBRARY (ADVANCED)
    linked by target "opencv_cudev" in directory /home/x/opencv-3.1.0/modules/cudev
    linked by target "opencv_cudev" in directory /home/x/opencv-3.1.0/modules/cudev
    linked by target "opencv_test_cudev" in directory /home/x/opencv-3.1.0/modules/cudev/test
    linked by target "opencv_test_core" in directory /home/x/opencv-3.1.0/modules/core
    linked by target "opencv_core" in directory /home/x/opencv-3.1.0/modules/core
    linked by target "opencv_core" in directory /home/x/opencv-3.1.0/modules/core
    linked by target "opencv_perf_core" in directory /home/x/opencv-3.1.0/modules/core
....

解决:

修改源码包中某些文件:

  1. FindCUDA.cmake文件:
    ubuntu卸载及重新安装opencv(解决CUDA10与opencv 3.1.0版本不兼容问题)_第2张图片
    改为:
  find_cuda_helper_libs(nppial)
  find_cuda_helper_libs(nppicc)
  find_cuda_helper_libs(nppicom)
  find_cuda_helper_libs(nppidei)
  find_cuda_helper_libs(nppif)
  find_cuda_helper_libs(nppig)
  find_cuda_helper_libs(nppim)
  find_cuda_helper_libs(nppist)
  find_cuda_helper_libs(nppisu)
  find_cuda_helper_libs(nppitc)

即:
ubuntu卸载及重新安装opencv(解决CUDA10与opencv 3.1.0版本不兼容问题)_第3张图片

  1. set(CUDA_npp_LIBRARY "${CUDA_nppc_LIBRARY};${CUDA_nppi_LIBRARY};${CUDA_npps_LIBRARY}")

改为:

set(CUDA_npp_LIBRARY "${CUDA_nppc_LIBRARY};${CUDA_nppial_LIBRARY};${CUDA_nppicc_LIBRARY};
${CUDA_nppicom_LIBRARY};${CUDA_nppidei_LIBRARY};${CUDA_nppif_LIBRARY};
${CUDA_nppig_LIBRARY};${CUDA_nppim_LIBRARY};${CUDA_nppist_LIBRARY};
${CUDA_nppisu_LIBRARY};${CUDA_nppitc_LIBRARY};${CUDA_npps_LIBRARY}") 
  1. unset(CUDA_nppi_LIBRARY CACHE) 改为:
unset(CUDA_nppial_LIBRARY CACHE)  
unset(CUDA_nppicc_LIBRARY CACHE)  
unset(CUDA_nppicom_LIBRARY CACHE)  
unset(CUDA_nppidei_LIBRARY CACHE)  
unset(CUDA_nppif_LIBRARY CACHE)  
unset(CUDA_nppig_LIBRARY CACHE)  
unset(CUDA_nppim_LIBRARY CACHE)  
unset(CUDA_nppist_LIBRARY CACHE)  
unset(CUDA_nppisu_LIBRARY CACHE)  
unset(CUDA_nppitc_LIBRARY CACHE)  

即:
在这里插入图片描述
ubuntu卸载及重新安装opencv(解决CUDA10与opencv 3.1.0版本不兼容问题)_第4张图片

  1. 找到文件 OpenCVDetectCUDA.cmake

set(__cuda_arch_ptx "")   
 if(CUDA_GENERATION STREQUAL "Fermi")     
 	set(__cuda_arch_bin "2.0")   
 elseif(CUDA_GENERATION STREQUAL "Kepler")     
 	set(__cuda_arch_bin "3.0 3.5 3.7")

改成:

set(__cuda_arch_ptx "")
  if(CUDA_GENERATION STREQUAL "Kepler")
    set(__cuda_arch_bin "3.0 3.5 3.7")
  elseif(CUDA_GENERATION STREQUAL "Maxwell")
    set(__cuda_arch_bin "5.0 5.2")

ubuntu卸载及重新安装opencv(解决CUDA10与opencv 3.1.0版本不兼容问题)_第5张图片

  1. cuda10中有一个单独的halffloat(cuda_fp16.h)头文件,也应该被包括在opencv的目录里

将头文件cuda_fp16.h添加至 opencv\modules\cudev\include\opencv2\cudev\common.hpp

即在common.hpp中添加

#include 

ubuntu卸载及重新安装opencv(解决CUDA10与opencv 3.1.0版本不兼容问题)_第6张图片
重新cmake编译:

cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..

trick2:

CMake Error at cmake/OpenCVDetectCUDA.cmake:214 (else):
  else An ELSE command was found outside of a proper IF ENDIF structure.  Or
  its arguments did not match the opening IF command.
Call Stack (most recent call first):
  cmake/OpenCVFindLibsPerf.cmake:32 (include)
  CMakeLists.txt:537 (include)


CMake Error at cmake/OpenCVDetectCUDA.cmake:217 (endif):
  endif An ENDIF command was found outside of a proper IF ENDIF structure.
  Or its arguments did not match the opening IF command.
Call Stack (most recent call first):
  cmake/OpenCVFindLibsPerf.cmake:32 (include)
  CMakeLists.txt:537 (include)

由于cuda9以上不再支持2.0架构,在编译的命令里面添加一条-D WITH_CUDA=OFF就行了。

cmake -D CMAKE_BUILD_TYPE=Release -D WITH_CUDA=OFF -D CMAKE_INSTALL_PREFIX=/usr/local ..

ubuntu卸载及重新安装opencv(解决CUDA10与opencv 3.1.0版本不兼容问题)_第7张图片

make -j8 

trick3:

/usr/include/c++/7/cstdlib:75:15: fatal error: stdlib.h: No such file or directory
 #include_next <stdlib.h>

cmake时加上参数-D ENABLE_PRECOMPILED_HEADERS=OFF:

cmake -D CMAKE_BUILD_TYPE=Release -D WITH_CUDA=OFF -D ENABLE_PRECOMPILED_HEADERS=OFF -D CMAKE_INSTALL_PREFIX=/usr/local ..

重新:

make -j8 
sudo make install

4. reference

  1. Ubuntu16.04卸载opencv 3.0.0
  2. Ubuntu16.04 Caffe 安装步骤记录(超详尽)
  3. OpenCV3.3+CUDA9.0+Cmake3.9 环境搭建
  4. 在Ubuntu16.04/18.04 安装opencv3.3.0(可选带contrib和cuda)
  5. ubuntu编译opencv时出现CMake Error: CUDA_nppi_LIBRARY (ADVANCED)

你可能感兴趣的:(SLAM,opencv,ubuntu,caffe,python)