软件下载:
1. opencv-3.1.0.zip
https://github.com/Itseez/OpenCV/archive/3.1.0.zip
2. opencv_contrib-3.1.0.zip
https://github.com/opencv/opencv_contrib/archive/3.1.0.zip
注意:所有权限问题不能执行的命令,一律在命令前加sudo
黄色亮底为输入的命令行,蓝色亮底为出现的错误
源库更新
sudo apt-get update sudo apt-get upgrade
编译环境安装:
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
编译opencv 3.1.0
在路径/usr/local文件夹中创建文件夹opencv310
cd /usr/local mkdir opencv310
将文件opencv-3.1.0.zip复制到文件夹opencv310中
cp <文件所在路径>/opencv-3.1.0.zip /usr/local/opencv310/
解压文件opencv-3.1.0.zip
cd /usr/local/opencv310 unzip opencv-3.1.0.zip
gedit /usr/local/opencv310/opencv-3.1.0/modules/cudalegacy/src/graphcuts.cpp
将代码
#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER)
替换成
#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER) || (CUDART_VERSION>=8000)
进入到解压好的opencv-3.1.0文件夹中
cd opencv-3.1.0/
创建build文件夹并进入
mkdir build cd build
开始cmake
cmake -DCMAKE_BUILD_TYPE=Release –D CMAKE_INSTALL_PREFIX=/usr/local ..
这一步可能出现的问题:
ICV: Downloading ippicv_linux_20151201.tgz... CMake Error at 3rdparty/ippicv/downloader.cmake:73 (file): file DOWNLOAD HASH mismatch for file: [/usr/local/opencv310/opencv-3.1.0/3rdparty/ippicv/downloads/linux-808b791a6eac9ed78d32a7666804320e/ippicv_linux_20151201.tgz] expected hash: [808b791a6eac9ed78d32a7666804320e] actual hash: [5368b733a8f4311edf31a4abc5474843]
解决方案:下载ippicv_linux_20151201.tgz失败,手动下载该文件,百度搜索文件名,或在该地址下载:http://download.csdn.net/download/lx928525166/9479919
将下载好的文件ippicv_linux_20151201.tgz拷贝到文件夹路径中:/usr/local/opencv310/opencv-3.1.0/3rdparty/ippicv/downloads/linux-808b791a6eac9ed78d32a7666804320e/
cp <文件所在路径>/ippicv_linux_20151201.tgz /usr/local/opencv310/opencv-3.1.0/3rdparty/ippicv/downloads/linux-808b791a6eac9ed78d32a7666804320e/
注意,在报错之前,系统不会创建该文件夹,报错之后系统会创建该文件夹用来存储ippicv_linux_20151201.tgz文件,但是下载失败,所以可以把手动下载的文件替换。
重新cmake
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
开始编译
make –j8
开始安装
make install /bin/bash -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf' sudo ldconfig sudo gedit /etc/ld.so.conf.d/opencv.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
编译opencv_contrib扩展
进入到文件夹opencv310
cd /usr/local/opencv310
将文件opencv_contrib-3.1.0.zip复制到文件夹opencv310中
cp <文件所在路径>/opencv_contrib-3.1.0.zip /usr/local/opencv310/
解压
unzip opencv_contrib-3.1.0.zip
进入到解压好的opencv_contrib-3.1.0 文件夹中
cd opencv_contrib-3.1.0/
创建build文件夹并进入
mkdir build cd build
开始cmake
Cmake -D OPENCV_EXTRA_MODULES_PATH=/usr/local/opencv310/opencv_contrib-3.1.0/modules/ /usr/local/opencv310/opencv-3.1.0/
这一步可能出现的问题1:
Downloading protobuf-cpp-3.1.0.tar.gz... CMake Error at /usr/local/opencv310/opencv-3.1.0/cmake/OpenCVUtils.cmake:895 (file): file DOWNLOAD HASH mismatch for file: [/usr/local/opencv310/opencv_contrib-master/build/3rdparty/protobuf/downloads/bd5e3eed635a8d32e2b99658633815ef/protobuf-cpp-3.1.0.tar.gz] expected hash: [bd5e3eed635a8d32e2b99658633815ef] actual hash: [d41d8cd98f00b204e9800998ecf8427e]
解决方案:和前面一样,有一个文件下载失败,同样手动下载,百度搜索 protobuf-cpp-3.1.0.tar.gz,
放置的文件夹路径为/usr/local/opencv310/opencv_contrib-3.1.0/build/3rdparty/protobuf/downloads/bd5e3eed635a8d32e2b99658633815ef/
可能出现的问题2:
Downloading ... CMake Error at /Users/Documents/opencv/source/opencv-3.2.0/cmake/OpenCVUtils.cmake:1045 (file): file DOWNLOAD cannot open file for write. Call Stack (most recent call first): /Users/Documents/opencv/source/opencv_contrib-master/modules/dnn/cmake/OpenCVFindLibProtobuf.cmake:32 (ocv_download) /Users/Documents/opencv/source/opencv_contrib-master/modules/dnn/CMakeLists.txt:5 (include) CMake Error at /Users/Documents/opencv/source/opencv-3.2.0/cmake/OpenCVUtils.cmake:1049 (message): Failed to download . Status= Call Stack (most recent call first): /Users/Documents/opencv/source/opencv_contrib-master/modules/dnn/cmake/OpenCVFindLibProtobuf.cmake:32 (ocv_download) /Users/Documents/opencv/source/opencv_contrib-master/modules/dnn/CMakeLists.txt:5 (include)
解决方案:将如下路径的三个文件中的关键词FILENAME替换成关键词PACKAGE
/usr/local/opencv310/opencv_contrib-3.1.0/modules/dnn/cmake/OpenCVFindLibProtobuf.cmake /usr/local/opencv310/opencv_contrib-3.1.0/modules/xfeatures2d/cmake/download_boostdesc.cmake /usr/local/opencv310/opencv_contrib-3.1.0/modules/xfeatures2d/cmake/download_vgg.cmake
问题3:
如果你下载的opencv_contrib文件为opencv_contrib-master而不是 opencv_contrib-3.1.0,就可能出现如下错误:
Unknown CMake command "ocv_add_testdata".
因为opencv_contrib的版本和opencv版本不匹配,opencv_contrib-master没有"ocv_add_testdata"。
如果使用Opencv时有如下错误:
module object has no attribute 'xfeatures2d'
因为 xfeatures2d属于扩展模块opencv_contrib,需要编译 opencv_contrib-3.1.0
重新cmake
Cmake -D OPENCV_EXTRA_MODULES_PATH=/usr/local/opencv310/opencv_contrib-3.1.0/modules/ /usr/local/opencv310/opencv-3.1.0/
再编译安装
make –j8 make install /bin/bash -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf' sudo ldconfig
查看opencv安装情况:
pkg-config --cflags opencv pkg-config --modversion opencv