Ubuntu16.04安装OpenCV过程记录

Ubuntu16.04安装OpenCV过程记录_第1张图片

1.安装OpenCV依赖和工具

$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install build-essential cmake pkg-config
$ sudo apt-get install libjpeg8-dev libtiff5-dev libjasper-dev libpng12-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
$ sudo apt-get install python2.7-dev python3.5-dev

2.下载OpenCV源码和库函数并解压

$ cd ~
$ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip
$ unzip opencv.zip
$ wget -O opencv_contrib.zip https://github.com/Itseez/opencv_contrib/archive/3.1.0.zip
$ unzip opencv_contrib.zip

3.设置Python环境

#这里实用虚拟环境安装OpenCV
$ sudo pip install virtualenv virtualenvwrapper
#
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
$ echo -e "\n# virtualenv and virtualenvwrapper" >> ~/.bashrc
$ echo "export WORKON_HOME=$HOME/.virtualenvs" >> ~/.bashrc
$ echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc
$ source ~/.bashrc

4.创建虚拟环境

#Python2
$ mkvirtualenv cv -p python2
#Python3
$ mkvirtualenv cv -p python3

5.启用虚环境

$ workon cv

检验是否启用虚拟环境:检查你的命令行- 如果你看到的文字 (CV ) 提示符之前,那么你 是在 CV 虚拟环境:

Ubuntu16.04安装OpenCV过程记录_第2张图片

如果开头没有(cv)字样则没有在虚拟环境中:
Ubuntu16.04安装OpenCV过程记录_第3张图片

5.在虚拟环境中安装Numpy

#虚拟环境中
$ pip install numpy

6.Ubuntu 16.04上配置和编译OpenCV

#虚拟环境中
$ cd ~/opencv-3.1.0/
$ mkdir build
$ cd build
$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D INSTALL_PYTHON_EXAMPLES=ON \
    -D INSTALL_C_EXAMPLES=OFF \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.1.0/modules \
    -D PYTHON_EXECUTABLE=~/.virtualenvs/cv/bin/python \
    -D BUILD_EXAMPLES=ON ..

7.输出检查

<1>Python 2.7

Ubuntu16.04安装OpenCV过程记录_第4张图片

<2>Python 3

Ubuntu16.04安装OpenCV过程记录_第5张图片

<3>如前面的过程没有任何错误退出,则进行编译

$ make -j4 #make-j4 后面的数字与计算机核数相同。

<4>如果编译出错,可以尝试下面操作

$ make clean
$ make

<5>编译成功截图(编译过程较长)

Ubuntu16.04安装OpenCV过程记录_第6张图片

<6>安装

$ sudo make install
$ sudo ldconfig

8.绑定虚拟环境

<1>Python2.7

#检验python2.7绑定的OpenCV
$ ls -l /usr/local/lib/python2.7/site-packages/
total 1972
-rw-r--r-- 1 root staff 2016608 Sep 15 09:11 cv2.so
#绑定到   Python 2.7的cv虚拟环境中:
$ cd ~/.virtualenvs/cv/lib/python2.7/site-packages/
$ ln -s /usr/local/lib/python2.7/site-packages/cv2.so cv2.so

<2>Python3.5

#python3.5安装后是绑定的cv2.cpython-35m-x86_64-linux-gnu.so文件
$ ls -l /usr/local/lib/python3.5/site-packages/
total 1972
-rw-r--r-- 1 root staff 2016816 Sep 13 17:24 cv2.cpython-35m-x86_64-linux-gnu.so

$ cd /usr/local/lib/python3.5/site-packages/
$ sudo mv cv2.cpython-35m-x86_64-linux-gnu.so cv2.so

$ cd ~/.virtualenvs/cv/lib/python3.5/site-packages/
$ ln -s /usr/local/lib/python3.5/site-packages/cv2.so cv2.so

9.测试

$ cd ~
$ workon cv
$ python
Python 3.5.2 (default, Jul  5 2016, 12:43:10) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'3.1.0'

>

如果安装过程出现错误或者想了解更多详细的资料可以参考这里:https://www.pyimagesearch.com/2016/10/24/ubuntu-16-04-how-to-install-opencv/

你可能感兴趣的:(OpenCV)