Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?

How to install OpenCV 3.1 on Ubuntu 14.04 64bits

Update latest packages and installed

$ sudo apt-get update
$ sudo apt-get upgrade

apt-get update - 更新最新的套件資訊 apt-get upgrade - 更新套件

安裝基本開發工具

$ sudo apt-get install build-essential cmake pkg-config git

安裝影像I/O開發工具,例:JPEG, PNG, TIFF, etc.

$ sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev

安裝影音I/O開發工具,讀影影片、接收串流、影音解碼...

$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev

安裝GTK開發工具,OpenCV highgui module顯示視窗時需要

$ sudo apt-get install libgtk2.0-dev
  • libgtk2.0-dev - (Gimp ToolKit)跨平台的圖形介面開發套件
$ sudo apt-get install libtbb-dev
  • libtbb-dev - (Intel® Threading Building Blocks )平行計算引擎

安裝OpenCV優化的開發工具

$ sudo apt-get install libatlas-base-dev gfortran

安裝Python 2.7 及 Python 3 header檔,OpenCV + Python bindings時需要

$ sudo apt-get install python2.7-dev python3-dev

Grab the OpenCV source code

下載OpenCV原始碼

$ git clone https://github.com/Itseez/opencv.git
$ cd opencv
$ git checkout 3.1.0

多個特徵擷取被移至contrib

$ git clone https://github.com/Itseez/opencv_contrib.git
$ cd opencv_contrib
$ git checkout 3.1.0

編譯

$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D INSTALL_C_EXAMPLES=OFF \
    -D INSTALL_PYTHON_EXAMPLES=ON \
    -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
    -D BUILD_EXAMPLES=ON ..

$ make -j4
$ sudo make install
$ sudo ldconfig

Create Separate Python Environments

Install

$ sudo pip install virtualenv virtualenvwrapper
$ sudo rm -rf ~/.cache/pip

Update our ~/.bashrc

export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
$ source ~/.bashrc
$ mkvirtualenv cv

$WORKON_HOME製作虛擬環境

$ mkvirtualenv [-i package] [-r requirements_file] [virtualenv options] ENVNAME

列出所有的虛擬環境

$ lsvirtualenv [-b] [-l] [-h]
  • -b 是簡短模式
  • -l 是詳細模式
  • -h 是印出 help 資訊

移除虛擬環境

$ rmvirtualenv ENVNAME

啟動虛擬環境

$ workon [environment_name]

離開虛擬環境

$ deactivate

確認Python與OpenCV使否已經設定完成 For Python 2.7:

$ ls -l /usr/local/lib/python2.7/site-packages/
total 1440
-rw-r--r-- 1 root staff 1473028 Dec  7 04:10 cv2.so

連結opencv與cv虛擬環境

$ cd ~/.virtualenvs/cv/lib/python2.7/site-packages/
$ ln -s /usr/local/lib/python2.7/site-packages/cv2.so cv2.so
$ workon cv
$ python
>>> import cv2
>>> cv2.__version__
'3.1.0'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment