一、更换apt源
debhttp://mirrors.ustc.edu.cn/raspbian/raspbian/ jessie main contrib non-free rpi
debhttp://mirrors.ustc.edu.cn/archive.raspberrypi.org/debian/ jessie main ui
注意:
1. 使用lsb_release –a指令查看当前的树莓派版本(Jessie/stretch)后,修改上述源中的关键字
2. 如果系统中已安装了Opencv2.4.9的版本,最好是先卸载掉。sudo apt-get removelibopencv-contrib-dev会联同libopencv-dev一起卸载。接着使用sudo apt-get automove
Ø #!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
Ø 安装基本的依赖项:
sudo apt-get install -y build-essentialcmake pkg-config
Ø 安装和图像相关的库:
sudo apt-get install -y libjpeg-devlibtiff5-dev libjasper-dev libpng12-dev
Ø 安装视频IO包:
sudo apt-get install -y libavcodec-devlibavformat-dev libswscale-dev libv4l-dev libgstreamer0.10-0-dbg libgstreamer0.10-0 libgstreamer0.10-dev libv4l-0 libv4l-dev libavcodec-devlibavformat-dev libswscale-dev libxvidcore-dev libx264-dev
Ø 安装highgui相关的依赖库:
sudo apt-get install -y libgtk2.0-dev
Ø 安装opencv进阶依赖库,操作矩阵等:
sudo apt-get install -y libatlas-base-devgfortran
Ø 其它一些库
sudo apt-get install -y python-numpy python-scipypython-matplotlib ant libgtkglext1-dev v4l-utils
Ø 以下为沙盒环境,可选择安装
Ø 下载pip
wget https://bootstrap.pypa.io/get-pip.py
Ø 安装pip工具
sudo python get-pip.py
Ø 安装virtualenv沙盒工具
sudo pip install virtualenv virtualenvwrapper
sudo rm -rf ~/.cache/pip
Ø 将沙盒环境变量写入~/.profile
echo -e "export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python2.7 \n exportWORKON_HOME=$HOME/.virtualenvs \n source /usr/local/bin/virtualenvwrapper.sh \nexport LD_LIBRARY_PATH=/usr/lib/:$LD_LIBRARY_PATH" >> ~/.profile
Ø 更新状态
source ~/.profile
Ø 创建沙盒环境
mkvirtualenv p2cv
workon p2cv
Ø 这时可以看到已经进入沙盒环境(p2cv)pi@raspberrypi
Ø 如果想离开沙盒环境,执行deactivate
Ø 沙盒环境安装结束
Ø 安装numpy,比较漫长,若报没有权限的错误,执行sudo rm -rf~/.cache/pip/
pip install numpy
Ø 正式开始准备opencv和opencv_contrib
Ø 下载OpenCV 3.3.1和OpenCV_contrib库:
Ø 下载OpenCV 3.3.1:
wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.3.1.zip
Ø 解压OpenCV 3.3.1:
unzip opencv.zip
Ø 下载OpenCV_contrib库:
wget -O opencv_contrib.ziphttps://github.com/Itseez/opencv_contrib/archive/3.3.1.zip
Ø 解压OpenCV_contrib库:
unzip opencv_contrib.zip
Ø 编译和安装OpenCV 3.3.1
Ø 进入opencv3.3.1目录:
cd opencv-3.3.1/
Ø 新建build文件夹:
mkdir build
Ø 进入build文件夹:
cd build
Ø 配置cmake(这一步直接粘贴所有行到你的terminal即可):
sudo 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.3.1/modules \
-D BUILD_EXAMPLES=ON..
Ø 由于本人实际没有安装OpenCV_contrib组件,执行如下指令代替上一条
sudo cmake -DCMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local ..
Ø 编译:(由于使用make j4容易报错,故换成make)
sudo make
Ø 安装:
sudo make install
Ø 把OpenCV生成的动态链接库加入树莓派目录:
sudo ldconfig
# 安装python插件
# sudoapt-get install -y python-picamera python-pil python-tk
# 为沙盒环境引用类库
cd ~/.virtualenvs/p2cv/lib/python2.7/site-packages/
ln -s /usr/local/lib/python2.7/site-packages/cv2.so cv2.so