建议先安装OpenCV3.2.0,再安装CUDA10.2,否则再安装OpenCV3.2.0时,会出现不兼容的情况,解决方案比较麻烦
如下记录先安装CUDA10.2,否则再安装OpenCV3.2.0时,出现的问题
安装CUDA10.2,这里就不介绍了,在本文底部会介绍
安装OpenCV3.2.0
事实证明使用conda便捷安装的opencv是阉割版,不能实现视频和摄像头的读取功能,所以需要自己手动编译。
(1)下载opencv,我在window中用迅雷下载,下载后放到Ubuntu中,这样比较快。
进入官网 : http://opencv.org/releases.html , 选择 3.2.0 版本的 source , 下载 opencv-3.2.0.zip (这里的照片是3.1.0 版本)
解压后,放在软件安装目录下,我是放在了“/home/zqzy”目录下了
(2)更新,否则可能会面会报错
sudo apt-get update
sudo apt-get upgrade
(3)安装依赖
sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-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
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt-get install libxvidcore-dev libx264-dev
sudo apt-get install libgtk-3-dev
sudo apt-get install libatlas-base-dev gfortran pylint
sudo apt-get install python2.7-dev python3.5-dev
sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-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 # 处理图像所需的包
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev liblapacke-dev
sudo apt-get install libxvidcore-dev libx264-dev # 处理视频所需的包
sudo apt-get install libatlas-base-dev gfortran # 优化opencv功能
sudo apt-get install ffmpeg
sudo apt-get install libvtk5-dev
或者
sudo apt-get isntall libvtk6-dev
(4)修改 opencv-3.1.0/modules/cudalegacy/src/graphcuts.cpp 文件(安装opencv3.2.0不用修改)
sudo gedit opencv-3.1.0/modules/cudalegacy/src/graphcuts.cpp
如果不修改的话,在执行 make -j8 命令编译到 92% 时可能会出现以下错误:
modules/cudalegacy/src/graphcuts.cpp:120:54: error:
‘NppiGraphcutState’ has not been declared
typedef NppStatus (*init_func_t)(NppiSize oSize,
NppiGraphcutState** ppState, Npp8u* pDeviceMem);
由于opecv3.1与cuda8.0不兼容导致的。解决办法:
修改 opencv-3.1.0/modules/cudalegacy/src/graphcuts.cpp 文件内容,如图:
(5)编译
cd /home/zqzy/opencv-3.1.0
mkdir build # 创建编译的文件目录
cd build
cmake -D CMAKE_BUILD_TYPE=Release -D WITH_VTK=ON -D CMAKE_INSTALL_PREFIX=/usr/local ..
sudo make -j8 #编译
执行下面这行时出错:
cmake -D CMAKE_BUILD_TYPE=Release -D WITH_VTK=ON -D CMAKE_INSTALL_PREFIX=/usr/local ..
由于在编译过程中,需要下载ippicv_linux_20151201.tgz,这一般需要连接外网,国内一般连接不上,所以下载失败,导致出现这个问题
解决方法:
下载ippicv_linux_20151201.tgz,参考:https://blog.csdn.net/zhuiqiuzhuoyue583/article/details/106620707
将下载的ippicv_linux_20151201.tgz,放到指定的文件目录下:
/home/zqzy/catkin_ws/src/opencv-3.2.0/3rdparty/ippicv/downloads/linux-808b791a6eac9ed78d32a7666804320e
这个路径是根据出错的提示来创建的。
如果没有,就自己创建这个路径,如果该路径下有个ippicv_linux_20151201.tgz,将其删除,再把下载好的ippicv_linux_20151201.tgz放进去,因为原来那个不完整。
放入ippicv_linux_20151201.tgz后,进入build目录,重新编译
cd /home/zqzy/opencv-3.1.0
cd build
cmake -D CMAKE_BUILD_TYPE=Release -D WITH_VTK=ON -D CMAKE_INSTALL_PREFIX=/usr/local ..
sudo make -j8 #编译
如果出现如下错误:
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 D:/Cproject/opencv/opencv/sources/modules/cudev
linked by target "opencv_cudev" in directory D:/Cproject/opencv/opencv/sources/modules/cudev
linked by target "opencv_test_cudev" in directory D:/Cproject/opencv/opencv/sources/modules/cudev/test
linked by target "opencv_core" in directory D:/Cproject/opencv/opencv/sources/modules/core
linked by target "opencv_core" in directory D:/Cproject/opencv/opencv/sources/modules/core
linked by target "opencv_test_core" in directory D:/Cproject/opencv/opencv/sources/modules/core
linked by target "opencv_perf_core" in directory D:/Cproject/opencv/opencv/sources/modules/core
linked by target "opencv_test_cudaarithm" in directory D:/Cproject/opencv/opencv/sources/modules/cudaarithm
linked by target "opencv_cudaarithm" in directory D:/Cproject/opencv/opencv/sources/modules/cudaarithm
linked by target "opencv_cudaarithm" in directory D:/Cproject/opencv/opencv/sources/modules/cudaarithm
linked by target "opencv_perf_cudaarithm" in directory D:/Cproject/opencv/opencv/sources/modules/cudaarithm
linked by target "opencv_flann" in directory D:/Cproject/opencv/opencv/sources/modules/flann
linked by target "opencv_flann" in directory D:/Cproject/opencv/opencv/sources/modules/flann
linked by target "opencv_test_flann" in directory D:/Cproject/opencv/opencv/sources/modules/flann
linked by target "opencv_imgproc" in directory D:/Cproject/opencv/opencv/sources/modules/imgproc
.................................
.............
解决方案如下:
1.在opencv3.2.0的cmake中找到FindCUDA.cmake文件
a..找到行
find_cuda_helper_libs(nppi)
改为
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)
b.找到行
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}")
c.找到行
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)
2.在opencv3.2.0的cmake中找到文件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")
...
3.将头文件cuda_fp16.h添加至 opencv3.2.0/modules/cudev/include/opencv2/cudev/common.hpp
即在common.hpp中添加
#include <cuda_fp16.h>
重新编译即可
参考:https://blog.csdn.net/u014613745/article/details/78310916
如果出现如下错误:
Unsupported gpu architecture 'compute_20'
.......
使用如下命令进行cmake
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D CUDA_GENERATION=Kepler ..
关键就是在最后指定使用Kepler
参考:https://blog.csdn.net/renhanchi/article/details/80493232
(6)编译成功后安装:
sudo make -j8 #编译
sudo make install #安装
(7)配置OpenCV环境变量
配置环境
将opencv的库加入到路径,从而让系统可以找到
sudo gedit /etc/ld.so.conf.d/opencv.conf
或者,如果上面这个文件没有,就修改下面这个文件
sudo gedit /etc/ld.so.conf
末尾加入/usr/local/lib
,保存退出
执行:
sudo ldconfig #使配置生效
再执行:
sudo gedit /etc/bash.bashrc
末尾加入
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig export PKG_CONFIG_PATH
export PKG_CONFIG_PATH
保存退出
sudo source /etc/bash.bashrc #使配置生效
该步骤可能会报错找不到命令,原因是source是root命令
错误如下:
slam2@slam2-vm:~/catkin_ws/src$ sudo source /etc/bash.bashrc
[sudo] password for slam2:
sudo: source: command not found
解决方法:
执行
su #(进入root权限)
输入密码
如果出现“su: Authentication failure问题”:
参考:https://blog.csdn.net/zhuiqiuzhuoyue583/article/details/106626618
https://blog.csdn.net/cv_you/article/details/77341631
再执行:
source /etc/bash.bashrc
Ctrl+d #(推迟root)
sudo updatedb #更新database
(8)安装完成后通过查看 opencv 版本验证是否安装成功:
pkg-config --modversion opencv
附上测试摄像头的代码:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 打开摄像头并灰度化显示
import cv2
capture = cv2.VideoCapture(0)
while(True):
# 获取一帧
ret, frame = capture.read()
# 将这帧转换为灰度图
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame', gray)
if cv2.waitKey(1) == ord('q'):
break
capture.release()
cv2.destroyAllWindows()
测试实例2:
//文件名字lena.cpp
#include <stdio.h>
#include <opencv2/opencv.hpp>
using namespace cv;
int main( )
{
Mat image;
//按照自己的目录,或者将
image = imread("/home/slam2/catkin_ws/src/lena.png", 1 );
if ( !image.data )
{
printf("No image data \n");
return -1;
}
namedWindow("Display Image", WINDOW_AUTOSIZE );
imshow("Display Image", image);
waitKey(0);
return 0;
}
Lena.png
如下
编译:
g++ lena.cpp -o lena.o `pkg-config --cflags --libs opencv`
运行:
./lena.o
效果如下
参考:https://blog.csdn.net/yhaolpz/article/details/71375762#t8
https://blog.csdn.net/radiantjeral/article/details/82193370#32_OpenCV_218
安装CUDA10.2:https://blog.csdn.net/zhuiqiuzhuoyue583/article/details/107356088