Ubuntu16.04安装OpenCV

1.安装 opencv 所需依赖包

如果后面还要遗漏的,出错再补上

$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get -y install libopencv-dev build-essential cmake git libgtk2.0-dev pkg-config python-dev python-numpy libdc1394-22 libdc1394-22-dev libjpeg-dev libpng12-dev libtiff5-dev libjasper-dev libavcodec-dev libavformat-dev libswscale-dev libxine2-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev libtbb-dev libqt4-dev libfaac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev x264v4l-utils unzip

2.下载 opencv3.2.0

这里需要下载 opencv 和 opencv_contrib (后者会在 cmake 配置的时候用到), 这是因为 opencv3以后 SIFT 和 SURF 之类的属性被移到了 contrib 中,执行下面两条指令下载 OpenCV3.2.0

$ wget https://github.com/opencv/opencv/archive/3.2.0.zip
$ wget https://github.com/opencv/opencv_contrib/archive/3.2.0.zip

(3.2.0.zip就是opencv-3.2.0.zip)
(3.2.0.zip.1就是opencv_contrib.zip)

3.解压下载的两个文件

$ unzip opencv-3.2.0.zip -d ~/opencv
$ unzip opencv_contrib-3.2.0.zip -d ~/opencv

4.创建并进入 build 目录,执行 cmake 生成 makefile 文件

$ cd ~/opencv/opencv-3.2.0
$ mkdir build
$ cd build/
$ sudo cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH= ..

注意cmake的参数不要出错

问题一:

下载包的时候链接超时

CMake Error at 3rdparty/ippicv/downloader.cmake:77 (message):
  ICV: Failed to download ICV package: ippicv_linux_20151201.tgz.
  Status=28;"Timeout was reached"

手动下载 ippicv_linux_20151201.tgz(https://raw.githubusercontent.com/Itseez/opencv_3rdparty/81a676001ca8075ada498583e4166079e5744668/ippicv/ippicv_linux_20151201.tgz)到opencv-3.2.0/3rdparty/ippicv/downloads/目录下再cmake

$ sudo cp ~/software/opencv/ippicv_linux_20151201.tgz ~/opencv/opencv-3.2.0/3rdparty/ippicv/downloads/linux-808b791a6eac9ed78d32a7666804320e/

问题二:

-- No package 'gtk+-3.0' found

$ sudo apt-get install libgtk-3-dev

问题三:

--   No package 'gstreamer-base-1.0' found

--   No package 'gstreamer-video-1.0' found

--   No package 'gstreamer-app-1.0' found

--   No package 'gstreamer-riff-1.0' found

--   No package 'gstreamer-pbutils-1.0' found

sudo apt-get -y install libgstreamer-plugins-base1.0-dev
sudo apt-get -y install libgstreamer1.0-dev

问题四:

--   No package 'libavresample' found

--   No package 'libgphoto2' found

$ sudo apt-get -y install libavresample-dev
$ sudo apt-get -y install libgphoto2-dev

问题五:

-- Could not find OpenBLAS include. Turning OpenBLAS_FOUND off
-- Could not find OpenBLAS lib. Turning OpenBLAS_FOUND off
-- Could NOT find Atlas (missing:  Atlas_CBLAS_INCLUDE_DIR Atlas_CLAPACK_INCLUDE_DIR Atlas_CBLAS_LIBRARY Atlas_BLAS_LIBRARY Atlas_LAPACK_LIBRARY) 
-- Could NOT find Doxygen (missing:  DOXYGEN_EXECUTABLE) 
-- Could NOT find JNI (missing:  JAVA_AWT_LIBRARY JAVA_JVM_LIBRARY JAVA_INCLUDE_PATH JAVA_INCLUDE_PATH2 JAVA_AWT_INCLUDE_PATH) 
-- Could NOT find Matlab (missing:  MATLAB_MEX_SCRIPT MATLAB_INCLUDE_DIRS MATLAB_ROOT_DIR MATLAB_LIBRARIES MATLAB_LIBRARY_DIRS MATLAB_MEXEXT MATLAB_ARCH MATLAB_BIN) 
-- VTK is not found. Please set -DVTK_DIR in CMake to VTK build directory, or to VTK install subdirectory with VTKConfig.cmake file

$ sudo apt-get install libopenblas-dev

问题六:

-- Could NOT find Doxygen (missing:  DOXYGEN_EXECUTABLE) 

$ sudo apt-get install doxygen

问题七:

-- Could NOT find JNI (missing:  JAVA_AWT_LIBRARY JAVA_JVM_LIBRARY JAVA_INCLUDE_PATH JAVA_INCLUDE_PATH2 JAVA_AWT_INCLUDE_PATH) 

a.下载jdk-8u151-linux-x64.tar.gz到download目录
http://www.oracle.com/technetwork/cn/java/javase/downloads/jdk8-downloads-2133151-zhs.html

b.安装jdk

$ cd download/
$ sudo mkdir /usr/local/java
$ sudo tar zxvf jdk-8u151-linux-x64.tar.gz -C /usr/local/java
$ sudo ln -s /usr/local/java/jdk1.8.0_151/ /usr/local/java/latest

c. 添加环境变量:

$ sudo vim /etc/profile

加入如下内容:

>export JAVA_HOME=/usr/local/java/latest
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin

执行

$ source /etc/profile

进行更新。

没有报错的情况下,cmake成功后会出现

-- Configuring done
-- Generating done
-- Build files have been written to: /home/fc/opencv/opencv-
3.2.0/build

5. make

cmake成功,之后可以在 build 文件下执行编译指令,待编译成功后即可执行安装指令。

$ sudo make -j4

出错:
/opencv/opencv-3.2.0/build/opencv_lapack.h:2:45: fatal error: LAPACKE_H_PATH-NOTFOUND/lapacke.h: 没有那个文件或目录
解决方法:

$ sudo apt-get install liblapacke-dev checkinstall

make时间有点长,差不多两个小时了。

6.install

$ sudo make install

7.安装 python 接口依赖

$sudo apt-get install python-opencv
sudo apt-get install python-numpy

8.测试 Python 的 OpenCV3.2.0 模块

$ python
Python 2.7.14 |Anaconda, Inc.| (default, Oct 16 2017, 17:29:19) 
[GCC 7.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
ImportError: No module named cv2
>>> 

问题:No module named cv2
原因在于 python2.7 找不到 cv2.so 文件

$ sudo cp ~/opencv/opencv-3.2.0/build/lib/cv2.so ~/anaconda2/lib/python2.7/site-packages/

再测试:

$ python
Python 2.7.14 |Anaconda, Inc.| (default, Oct 16 2017, 17:29:19) 
[GCC 7.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'3.2.0'
>>> 

能显示opencv的版本说明安装成功

    原文作者:foochane
    原文地址: https://www.jianshu.com/p/259a6140da9d
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞