Raspberry3 搭建opencv3

注意只适用于Python2, Opencv3, Raspberry3, Ubuntu14, debian jessie

一系列更新

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

# ubuntu 不需要
$ sudo rpi-update

重启

$ sudo reboot

安装图像I/O库

sudo apt-get install libjpeg-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

安装GTK开发环境,用来编译opencv 的highgui 子模块

主要用于屏幕显示图像,并且构建简单的GUI interfaces

$ sudo apt-get install libgtk2.0-dev

安装opencv内部操作(矩阵),优化依赖关系

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

一些便捷安装

# 安装zsh
$ apt install zsh
# 安装oh-my-zsh
sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
# 或者
sh -c "$(wget https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -)"

安装git

 sudo apt-get install git

配置git

# 常用的命令都设置alias,尽量少敲键盘
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.co checkout
git config --global alias.st status
# 很好看地显示git log
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen (%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --"
# 设置用户信息
git config --global user.name "Mousse"
git config --global user.email [email protected]
# 缺省使用颜色显示
git config --global color.ui true

设置vim

sudo apt-get install vim
git clone --depth=1 https://github.com/amix/vimrc.git ~/.vim_runtime
sh ~/.vim_runtime/install_awesome_vimrc.sh

解决问题

perl: warning: Please check that your locale settings: /usr/local/webserver/php/bin/phpize Configur
Raspberry3 搭建opencv3_第1张图片

$ echo "export LC_ALL=C" >> /root/.bashrc
$ source /root/.bashrc 
# 或者
$ echo "export LC_ALL=C" >> /root/.zshrc
$ source /root/.zshrc 

安装Python环境

# 安装python2 ,python3头文件和OpenCV绑定
sudo apt-get install python2.7-dev python3-dev
# 安装pip依赖关系
sudo apt-get install build-essential libssl-dev libevent-dev libjpeg-dev libxml2-dev libxslt-dev

安装pip

sudo apt-get install python-pip

安装virtualenv

sudo pip install virtualenv virtualenvwrapper

用virtualenvwrapper管理虚拟环境

$ vi ~/.profile 或者 vi ~/.zshrc

# virtualenv and virtualenvwrapper
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh

$ source ~/.zshrc

创建cv环境

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

下载opencv3

$ mkdir opencv3
$ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.3.1.zip
$ unzip opencv.zip
###  下载opencv_contrib(主要使用SIFT和SURF)
和opencv版本必须统一
$ wget -O opencv_contrib.zip https://github.com/Itseez/opencv_contrib/archive/3.3.1.zip
$ unzip opencv_contrib.zip

安装numpy(有点慢)

$ pip install numpy -i https://pypi.douban.com/simple/ 

安装cmake

$ apt-get install cmake

编译安装(ARM)opencv

设置 ENABLE_NEON=ON , ENABLE_VFPV3=ON 是为了提高树莓派的运算能力

$ cd ~/opencv/opencv-3.3.1
$ mkdir bulid
$ cd bulid
$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv/opencv_contrib-3.3.1/modules \
    -D ENABLE_NEON=ON \
    -D ENABLE_VFPV3=ON \
    -D BUILD_TESTS=OFF \
    -D INSTALL_PYTHON_EXAMPLES=OFF \
    -D BUILD_EXAMPLES=OFF ..

编译成功的样子

$ cd ~/opencv/opencv-3.3.1
$ mkdir bulid
$ cd bulid
$ 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/opencv_contrib-3.3.1/modules \
    -D BUILD_EXAMPLES=ON ..

注意: 编译时候在 tiny-dnn: Download: v1.0.0a3.tar.gz 时候会很慢很慢,等一下就可以了

增大swap空间

opencv3.3b编译会消耗大量内存,树莓派内存太小,所以只有增大swap来完成

$vi  /etc/dphys-swapfile
# 找到CONF_SWAPSIZE=100改为1024
# set size to absolute value, leaving empty (default) then uses computed value
#   you most likely don't want this, unless you have an special disk situation
CONF_SWAPSIZE=1024
重启交换服务
sudo /etc/init.d/dphys-swapfile stop
sudo /etc/init.d/dphys-swapfile start
开启4核编译

编译超级慢,1个半小时吧,一定要增大swap要不然编译会挂起

$ make -j4

安装

$ sudo make install
$ sudo ldconfig

将cv2链接到虚拟环境下

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

测试

$ source ~/.zshrc
$ workon cv
$ python
>>> import cv2
>>> cv2.__version__
'3.3.1'

你可能感兴趣的:(数字图像处理,常用配置)