2016年3月17日 星期四

Installing OpenCV 3 on Raspbian Jessie

Installing OpenCV 3 on Raspbian Jessie

Step.1 安裝

更新和升級樹梅派上已經安裝的套件,並更新韌體

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

更新完韌體必須重開機

$ sudo reboot

安裝基本開發工具
1. build-essential
2. git
3. cmake
4. pkg-config

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

安裝影像I/O開發工具,例:JPEG, PNG, TIFF, etc.
1. jpeg
2. tiff
3. png
4. jasper

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

安裝影音I/O開發工具,讀影影片、接收串流、影音解碼…
1. avcodec - 影音編碼與解碼 ( encoding and decoding of audio, video )
2. avformat - 影音格式解析 ( muxing and demuxing containers )
3. swscale - 影像縮放、色彩空間轉換
4. v4l
5. xvidcore
6. x264 - H.264 / MPEG-4 AVC 編碼器

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

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

$ sudo apt-get install libgtk2.0-dev

安裝OpenCV優化的開發工具
1. gfortran
2. BLAS

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

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

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

Step #2: Grab the OpenCV source code

下載OpenCV原始碼
1. opencv
2. opencv_contrib

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

Step #3: Setup Python

安裝Python套件管理pip
1. pip - Python package manager

$ wget https://bootstrap.pypa.io/get-pip.py
$ sudo python get-pip.py

安裝Python虛擬環境工具
1. virtualenv
2. virtualenvwrapper

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

更新~/.profile檔案

export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
$ source ~/.profile

run as python 2.7

$ mkvirtualenv cv

run as python 3.0

$ mkvirtualenv cv -p python3

重啟terminal或登出後,必須重新設定cv至虛擬環境

$ source ~/.profile
$ workon cv

安裝OpenCV使用到的Python套件
1. numpy

$ pip install numpy

Step #4: Compile and install OpenCV

確認在cv虛擬環境,並開始建置

$ workon cv
$ cd ~/opencv-3.0.0/
$ mkdir build
$ cd build
$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D INSTALL_C_EXAMPLES=ON \
    -D INSTALL_PYTHON_EXAMPLES=ON \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.0.0/modules \
    -D BUILD_EXAMPLES=ON ..

Compile OpenCV

$ make -j4

Install it on system

$ sudo make install
$ sudo ldconfig

Step #5: Finishing the install

確認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

For Python 3:

$ ls /usr/local/lib/python3.4/site-packages/
cv2.cpython-34m.so

Step #6: Verifying your OpenCV 3 install

Test sample

$ workon cv
$ python
>>> import cv2
>>> cv2.__version__
'3.0.0'

參考來源:piimagesearch

0 意見:

張貼留言