注:安装环境不一致的请自行进行相应代码更改。
$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo shutdown -r now
更新内核时需要一定的sd卡空间,所以首先检查树莓派的sd卡是否扩展,否则可能会报“No space left on device”错误,即存储空间不足。
耗时:5min
$ sudo apt-get install build-essential git cmake pkg-config
通过git可以获取opencv的源码包,需要cmake来进行编译
耗时:30s
$ sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev
这里面有的系统的源已经进行了更新,可能会产生下载警告。
耗时:27s
$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
耗时:80s
$ sudo apt-get install libgtk2.0-dev
耗时:3min
$ sudo apt-get install libatlas-base-dev gfortran
耗时:1min
$ sudo apt-get install unzip
耗时:10s
$ sudo apt-get install python2.7-dev
耗时:30s
$ wget https://bootstrap.pypa.io/get-pip.py
$ sudo python get-pip.py
耗时: 1min
$ sudo pip install numpy
耗时:18min
$ cd ~
$ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.2.0.zip
$ unzip opencv.zip
$ wget -O opencv_contrib.zip https://github.com/Itseez/opencv_contrib/archive/3.2.0.zip
$ unzip opencv_contrib.zip
contrib库中包括了面部识别和文本探测,还包括文本识别、边缘检测器、深度图处理、光线流和追踪算法等
$ cd ~/opencv-3.2.0/
$ mkdir build
$ cd build
$cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \ -D INSTALL_PYTHON_EXAMPLES=ON \ -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.2.0/modules \ -D BUILD_EXAMPLES=ON ..
耗时:5min
$ make -j4
耗时:90min
多核编译可能会出错,失败后可以使用单核编译,不过比较慢
$ make clean
$ make
$ sudo make install
$ sudo ldconfig
如果想在python中使用opencv的话,需要把opencv编译的cv2.so和原始的cv.py或者cv.cpp拷贝到python的site-packages路径下面
$ sudo cp /home/pi/opencv-3.2.0/build/lib/cv2.so /usr/local/lib/python2.7/site-packages/
$ sudo cp /home/pi/opencv-3.2.0/modules/python/src2/cv2.cpp /usr/local/lib/python2.7/site-packages/
$ python
>>> import cv2
>>> print cv2.__version__
3.2.0
http://www.pyimagesearch.com/2016/04/18/install-guide-raspberry-pi-3-raspbian-jessie-opencv-3/
http://blog.csdn.net/talkxin/article/details/50471986
http://www.cnblogs.com/itech/archive/2012/05/15/2502284.html