之前通过Adrian Rosebrock的博客编译opencv遇到了很多问题,在此做一下笔记。
首先树莓派内置了python 2.7和3.0+,在Adrian Rosebrock的博客里说开发到底是选择2.7还是3.0+
Before we get started, take a second and consider which version of Python you are going to use. Are you going to compile OpenCV 3.0 with Python 2.7 bindings? Or are you going to compile OpenCV 3.0 Python 3 bindings?
There are pros and cons of each, but the choice is honestly up to you. If you use Python 3 regularly and are comfortable with it, then go ahead and compile with Python 3 bindings. However, if you do a lot of scientific Python development, you might want to stick with Python 2.7 (for the time being at least). While packages such as NumPy, Scipy, and scikit-learn are certainly increasing the Python 3+ adoption rate in the scientific community, there are still many scientific packages that still require Python 2.7 — because of this, you can easily pigeonhole yourself if you go with Python 3 and then realize that many of the packages you use on a daily basis only support Python 2.7.
When in doubt, I normally suggest that scientific developers use Python 2.7 since it ensures capability with a larger set of scientific packages and allows you to run experiments with legacy code. However, that is quickly changing — so proceed with whichever Python version you are most comfortable with!
在我们开始之前,花几秒钟考虑哪个版本的Python,是要用python2.7还是用python3+来绑定opencv?
然而如果你使用python2.7也是可行的,虽然像NumPy, Scipy, scikit-learn等库都已经支持了python3+但是仍有许多的库只支持python2.7。
你可以继续使用python3作为你的开发版本,但使用中可能会发现有些库只能支持python2.7。
所以Adrian Rosebrock建议现阶段推荐使用python2.7来进行开发,毕竟在初学时不会遇到不支持的库。但是python的发展是迅速的,以后使用任何一个版本的python来开发都是可行的!
一、准备编译环境
更新树莓派的系统与内核
sudo apt-get update
sudo apt-get upgrade
sudo rpi-update
更新内核时需要一定的sd卡空间,所以首先检查树莓派的sd卡是否扩展,否则可能会报“No space left on device”错误,即存储空间不足。
下载编译工具与git
需要通过git来获取opencv的源码包,以及需要cmake来进行编译
sudo apt-get install build-essential git cmake pkg-config
安装用于图片加载及解析的包
sudo apt-get install libjpeg8-dev libtiff4-dev libjasper-dev libpng12-dev
这里面有的系统的源已经将
libtiff4-dev
更新至了libtiff5-dev
可能会产生下载警告。
安装视频IO包
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
安装GTK2.0
sudo apt-get install libgtk2.0-dev
安装opencv优化支持工具
sudo apt-get install libatlas-base-dev gfortran
通过github下载opencv源码,并check 3.1.0版本
cd ~
git clone https://github.com/Itseez/opencv.git
cd opencv
git checkout 3.1.0
通过github下载opencv的contrib库
contrib库中包括了面部识别和文本探测,还包括文本识别、边缘检测器、深度图处理、光线流和追踪算法等
cd ~
git clone https://github.com/Itseez/opencv_contrib.git
cd opencv_contrib
git checkout 3.1.0
安装python2.7或python3的头文件
sudo apt-get install python2.7-dev
sudo apt-get install python3-dev
下载pip工具
wget https://bootstrap.pypa.io/get-pip.py
二、创建python2.7沙盒环境
如果你选择用python3来开发的话,跳过此步。
安装pip工具
sudo python get-pip.py
安装virtualenv沙盒工具
sudo pip install virtualenv virtualenvwrapper
sudo rm -rf ~/.cache/pip
将沙盒环境变量写入~/.profile
nano ~/.profile
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python2.7
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
保存退出
source ~/.profile
也可以不将环境变量写入profile文件,但每次使用沙盒环境前,需要手动运行
source /usr/local/bin/virtualenvwrapper.sh
否则workon
命令会提示找不到
创建沙盒环境
mkvirtualenv p2cv
workon p2cv
这时可以看到已经进入沙盒环境
(p2cv)pi@raspberrypi
如果想离开沙盒环境,执行deactivate
安装numpy
pip install numpy
安装比较漫长,若报没有权限的错误,执行
sudo rm -rf ~/.cache/pip/
三、创建python3沙盒环境
如果你选择用python2.7来开发的话,跳过此步。
安装pip工具
sudo python3 get-pip.py
安装virtualenv沙盒工具
sudo pip3 install virtualenv virtualenvwrapper
sudo rm -rf ~/.cache/pip
将沙盒环境变量写入~/.profile
nano ~/.profile
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
保存退出
source ~/.profile
也可以不将环境变量写入profile文件,但每次使用沙盒环境前,需要手动运行
source /usr/local/bin/virtualenvwrapper.sh
否则workon
命令会提示找不到
创建沙盒环境
mkvirtualenv p3cv
workon p3cv
这时可以看到已经进入沙盒环境
(p3cv)pi@raspberrypi
如果想离开沙盒环境,执行deactivate
安装numpy
pip3 install numpy
安装比较漫长,若报没有权限的错误,执行
sudo rm -rf ~/.cache/pip/
在沙盒环境下编译opencv
创建build文件夹
cd ~/opencv
mkdir build
cd build
cmake
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 ..
INSTALL_C_EXAMPLES是创建C实例,如果开启,cmake会报错。具体解决方法待续。
检查python的环境是否正常,如果Libraries找不到,则opencv的python库不会关联到
到时候会报ImportError: No module named cv2
的错误
若没有此项目,检查.profile,加入
export LD_LIBRARY_PATH=/usr/lib/:$LD_LIBRARY_PATH
source ~/.profile
make源码
make -j4
这里编译比较漫长,大概需要一个半小时左右。
安装opencv
sudo make install
sudo ldconfig
为沙盒环境引用类库
如果是python2.7沙盒环境
cd ~/.virtualenvs/p2cv/lib/python2.7/site-packages/
ln -s /usr/local/lib/python2.7/site-packages/cv2.so cv2.so
如果是python3+沙盒环境
cd ~/.virtualenvs/p3cv/lib/python3.2/site-packages/
ln -s /usr/local/lib/python3.2/site-packages/cv2.so cv2.so
安装成功,检查版本号
python
>>> import cv2
>>> print(cv2.__version__)
3.1.0
若成功显示版本号,则opencv安装成功