本文主要是讲:如何在Ubuntu16.04下安装OpenCV3.1.0:
首先第一步:先下载 OpenCV3.1.0 的源码
OpenCV官方下载链接
下载OpenCV3.1.0压缩包之后,可以直接对OpenCV压缩包右键点击 "提取到此处",把文件夹解压到当前路径。
首先打开终端(安装编译器和依赖项):
sudo apt-get install build-essential
必须安装(下面的模块有的找不到可以选择不装那一项):
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodedec-dev libavformat-dev libswscale-dev
可选:
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
然后进入到OpenCV的文件夹之内打开终端,输入以下的指令:
mkdir release && cd release
输入以下的指令进行编译:
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D CUDA_GENERATION=Kepler ..
如果提示没有cmake这样的错误(就是类似提示下面这样子的错误):
Command 'cmake' not found, but can be installed with:
sudo apt install cmake
则使用下面的之类安装CMake:
sudo apt install cmake
第二步骤,make工程,输入 -j12 是为了多线程编译,加了这条语句编译速度快非常多:
make -j12
编译过程中,可能会出现以下的错误,下面的问题是由于OpenCV3.X.X之后对CUDA8.0以上不兼容,所以要改一下代码编译:
modules/cudalegacy/CMakeFiles/opencv_cudalegacy.dir/build.make:290: recipe for target 'modules/cudalegacy/CMakeFiles/opencv_cudalegacy.dir/src/graphcuts.cpp.o' failed
make[2]: *** [modules/cudalegacy/CMakeFiles/opencv_cudalegacy.dir/src/graphcuts.cpp.o] Error 1
make[2]: *** 正在等待未完成的任务....
CMakeFiles/Makefile2:9585: recipe for target 'modules/cudalegacy/CMakeFiles/opencv_cudalegacy.dir/all' failed
make[1]: *** [modules/cudalegacy/CMakeFiles/opencv_cudalegacy.dir/all] Error 2
make[1]: *** 正在等待未完成的任务....
[ 94%] Linking CXX shared library ../../lib/libopencv_photo.so
[ 94%] Built target opencv_photo
Makefile:162: recipe for target 'all' failed
make: *** [all] Error 2
修改 ~/opencv-3.1.x/modules/cudalegacy/src/graphcuts.cpp文件内容,将第45行修改成以下内容(可能45行,具体的看下面的解释把):
// #if !defined (HAVE_CUDA) || defined (CUDA_DISABLER)
#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER) || (CUDART_VERSION >= 8000)
就是在
#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER)
的后面加
|| (CUDART_VERSION >= 8000)
然后再次编译:
make -j12
下图是编译成功的结果:
成功之后输入(如果不使用sudo,可能在make install的使用提示权限不够):
sudo make install
上面是完全安装成功的截图,添加链接:
sudo gedit /etc/ld.so.conf.d/opencv.conf
写入:/usr/local/lib
sudo ldconfig
下面是错误解决方法集合:
(1)Unsupported gpu architecture 'compute_XX' 解决方法
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D CUDA_GENERATION=Kepler ..
主要是的强制一个cmake编译参数,就是是下面那一句,相信你一定懂cmake把它加到where
-D CUDA_GENERATION=Kepler
(2)opencv3.1.X 与CUDA 8.0 以上安装冲突问题
/home/wyu/WORKSPACE_KE/0001_opencv-3.1.0/opencv-3.1.0/modules/cudalegacy/src/graphcuts.cpp: In function ‘void cv::cuda::graphcut(cv::cuda::GpuMat&, cv::cuda::GpuMat&, cv::cuda::GpuMat&, cv::cuda::GpuMat&, cv::cuda::GpuMat&, cv::cuda::GpuMat&, cv::cuda::GpuMat&, cv::cuda::Stream&)’:
/home/wyu/WORKSPACE_KE/0001_opencv-3.1.0/opencv-3.1.0/modules/cudalegacy/src/graphcuts.cpp:174:51: error: ‘nppiGraphcutGetSize’ was not declared in this scope
nppSafeCall( nppiGraphcutGetSize(sznpp, &bufsz) );
^
/home/wyu/WORKSPACE_KE/0001_opencv-3.1.0/opencv-3.1.0/modules/core/include/opencv2/core/private.cuda.hpp:165:52: note: in definition of macro ‘nppSafeCall’
#define nppSafeCall(expr) cv::cuda::checkNppError(expr, __FILE__, __LINE__, CV_Func)
^
/home/wyu/WORKSPACE_KE/0001_opencv-3.1.0/opencv-3.1.0/modules/cudalegacy/src/graphcuts.cpp:182:61: error: ‘nppiGraphcutInitAlloc’ was not declared in this scope
NppiGraphcutStateHandler state(sznpp, buf.ptr(), nppiGraphcutInitAlloc);
^
In file included from /home/wyu/WORKSPACE_KE/0001_opencv-3.1.0/opencv-3.1.0/release/modules/cudalegacy/precomp.hpp:75:0:
/home/wyu/WORKSPACE_KE/0001_opencv-3.1.0/opencv-3.1.0/modules/cudalegacy/src/graphcuts.cpp:191:146: error: ‘nppiGraphcut_32s8u’ was not declared in this scope
static_cast(terminals.step), static_cast(leftTransp.step), sznpp, labels.ptr(), static_cast(labels.step), state) );
^
/home/wyu/WORKSPACE_KE/0001_opencv-3.1.0/opencv-3.1.0/modules/core/include/opencv2/core/private.cuda.hpp:165:52: note: in definition of macro ‘nppSafeCall’
#define nppSafeCall(expr) cv::cuda::checkNppError(expr, __FILE__, __LINE__, CV_Func)
^
/home/wyu/WORKSPACE_KE/0001_opencv-3.1.0/opencv-3.1.0/modules/cudalegacy/src/graphcuts.cpp:196:146: error: ‘nppiGraphcut_32f8u’ was not declared in this scope
static_cast(terminals.step), static_cast(leftTransp.step), sznpp, labels.ptr(), static_cast(labels.step), state) );
^
/home/wyu/WORKSPACE_KE/0001_opencv-3.1.0/opencv-3.1.0/modules/core/include/opencv2/core/private.cuda.hpp:165:52: note: in definition of macro ‘nppSafeCall’
#define nppSafeCall(expr) cv::cuda::checkNppError(expr, __FILE__, __LINE__, CV_Func)
^
[ 88%] Building CXX object modules/cudalegacy/CMakeFiles/opencv_cudalegacy.dir/src/image_pyramid.cpp.o
/home/wyu/WORKSPACE_KE/0001_opencv-3.1.0/opencv-3.1.0/modules/cudalegacy/src/graphcuts.cpp: In function ‘void cv::cuda::graphcut(cv::cuda::GpuMat&, cv::cuda::GpuMat&, cv::cuda::GpuMat&, cv::cuda::GpuMat&, cv::cuda::GpuMat&, cv::cuda::GpuMat&, cv::cuda::GpuMat&, cv::cuda::GpuMat&, cv::cuda::GpuMat&, cv::cuda::GpuMat&, cv::cuda::GpuMat&, cv::cuda::Stream&)’:
/home/wyu/WORKSPACE_KE/0001_opencv-3.1.0/opencv-3.1.0/modules/cudalegacy/src/graphcuts.cpp:246:52: error: ‘nppiGraphcut8GetSize’ was not declared in this scope
nppSafeCall( nppiGraphcut8GetSize(sznpp, &bufsz) );
^
/home/wyu/WORKSPACE_KE/0001_opencv-3.1.0/opencv-3.1.0/modules/core/include/opencv2/core/private.cuda.hpp:165:52: note: in definition of macro ‘nppSafeCall’
#define nppSafeCall(expr) cv::cuda::checkNppError(expr, __FILE__, __LINE__, CV_Func)
^
/home/wyu/WORKSPACE_KE/0001_opencv-3.1.0/opencv-3.1.0/modules/cudalegacy/src/graphcuts.cpp:254:61: error: ‘nppiGraphcut8InitAlloc’ was not declared in this scope
NppiGraphcutStateHandler state(sznpp, buf.ptr(), nppiGraphcut8InitAlloc);
^
In file included from /home/wyu/WORKSPACE_KE/0001_opencv-3.1.0/opencv-3.1.0/release/modules/cudalegacy/precomp.hpp:75:0:
/home/wyu/WORKSPACE_KE/0001_opencv-3.1.0/opencv-3.1.0/modules/cudalegacy/src/graphcuts.cpp:267:146: error: ‘nppiGraphcut8_32s8u’ was not declared in this scope
static_cast(terminals.step), static_cast(leftTransp.step), sznpp, labels.ptr(), static_cast(labels.step), state) );
^
/home/wyu/WORKSPACE_KE/0001_opencv-3.1.0/opencv-3.1.0/modules/core/include/opencv2/core/private.cuda.hpp:165:52: note: in definition of macro ‘nppSafeCall’
#define nppSafeCall(expr) cv::cuda::checkNppError(expr, __FILE__, __LINE__, CV_Func)
^
/home/wyu/WORKSPACE_KE/0001_opencv-3.1.0/opencv-3.1.0/modules/cudalegacy/src/graphcuts.cpp:274:146: error: ‘nppiGraphcut8_32f8u’ was not declared in this scope
static_cast(terminals.step), static_cast(leftTransp.step), sznpp, labels.ptr(), static_cast(labels.step), state) );
^
/home/wyu/WORKSPACE_KE/0001_opencv-3.1.0/opencv-3.1.0/modules/core/include/opencv2/core/private.cuda.hpp:165:52: note: in definition of macro ‘nppSafeCall’
#define nppSafeCall(expr) cv::cuda::checkNppError(expr, __FILE__, __LINE__, CV_Func)
^
modules/cudalegacy/CMakeFiles/opencv_cudalegacy.dir/build.make:314: recipe for target 'modules/cudalegacy/CMakeFiles/opencv_cudalegacy.dir/src/graphcuts.cpp.o' failed
make[2]: *** [modules/cudalegacy/CMakeFiles/opencv_cudalegacy.dir/src/graphcuts.cpp.o] Error 1
make[2]: *** 正在等待未完成的任务....
CMakeFiles/Makefile2:9176: recipe for target 'modules/cudalegacy/CMakeFiles/opencv_cudalegacy.dir/all' failed
make[1]: *** [modules/cudalegacy/CMakeFiles/opencv_cudalegacy.dir/all] Error 2
make[1]: *** 正在等待未完成的任务....
[ 88%] Linking CXX shared library ../../lib/libopencv_photo.so
[ 88%] Built target opencv_photo
Makefile:160: recipe for target 'all' failed
make: *** [all] Error 2
出现上面的错误是由于OpenCV3.1.X和CUDA8.0以上版本不兼容,解决方案:
修改 ~/opencv-3.1.x/modules/cudalegacy/src/graphcuts.cpp文件内容,将第45行修改成以下内容
// #if !defined (HAVE_CUDA) || defined (CUDA_DISABLER)
#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER) || (CUDART_VERSION >= 8000)
就是在
#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER)
的后面加
|| (CUDART_VERSION >= 8000)
(3)编译其他程序提示没有 ippicv,解决方法如下:
传送门