树莓派(Stretch)配置OpenCV环境

1、更新源

笔者使用的是stretch版本,因此使用如下源,若是是其余版本设置相应的源html

deb https://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ stretch main contrib non-free rpi
deb http://mirrors.ustc.edu.cn/raspbian/raspbian/ stretch main contrib non-free rpi

因为没有vim,因此使用nano进行修改python

sudo nano /etc/apt/sources.list

而后更新linux

sudo apt-get update
sudo apt-get upgrade

 2、安装vim

sudo apt-get update
sudo apt-get install vim

而后配置sudo vim /etc/vim/vimrc,git

syntax on
set tabstop=4
set softtabstop=4
set shiftwidth=4
set cindent
set nu

 3、扩展文件系统

sudo raspi-config

选择7 Advanced Options-->A1 Expand Filesystem,而后sudo reboot重启github

4、下载依赖!!!!!!!(重要)

sudo apt-get update && sudo apt-get upgrade
sudo apt-get install build-essential cmake pkg-config
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
sudo apt-get install libgtk2.0-dev libgtk-3-dev
sudo apt-get install libatlas-base-dev gfortran
sudo apt-get install python2.7-dev python3-dev
sudo apt-get install aptitude 
sudo aptitude install python-dev
sudo apt-get install python3-pip
wget https://bootstrap.pypa.io/get-pip.py
sudo python3 get-pip.py
sudo pip3 install --upgrade pip
sudo pip3 install opencv-python 

 

安装Opencv-python报错:THESE PACKAGES DO NOT MATCH THE HASHES FROM THE REQUIREMENTS FILE. If you have updated the package versions, please update the hashes. Otherwise, examine the package contents carefully; someone may have tampered with them.bootstrap

解决方法:vim

sudo apt-get install 
sudo pip3 install --upgrade opencv-python

5、安装OpenCV(3.4.0)(最重要)

安装Opencv包,若是unzip报错使用jar xvf opencv.zipbash

cd ~
wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.4.0.zip
unzip opencv.zip

使用Python 虚拟环境避免Python2和Python3混乱问题(笔者使用Python3)app

sudo pip install virtualenv virtualenvwrapper
sudo rm -rf ~/.cache/pip
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
source /usr/local/bin/virtualenvwrapper.sh
echo -e "\n# virtualenv and virtualenvwrapper" >> ~/.profile
echo "export WORKON_HOME=$HOME/.virtualenvs" >> ~/.profile
echo "export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3" >> ~/.profile
echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.profile
source ~/.profile
mkvirtualenv cv -p python3

若是须要从新建立虚拟环境测试

source ~/.profile
workon cv

进行编译

cd ~/opencv-3.4.0/
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_OPENMP=ON .. 

在编译前配置交换空间大小 vim /etc/dphys-swapfile,将CONF_SWAPSIZE设置为1024,而后从新启动

sudo /etc/init.d/dphys-swapfile stop
sudo /etc/init.d/dphys-swapfile start

而后进行编译

make -j4
sudo make install
sudo ldconfig

6、完成最后配置

若是安装成功,OpenCV+Pyhton应该在/usr/local/lib/python3.5/site-packages或者是/usr/local/lib/python3.5/dist-packages,笔者是在dist-packages,因此能够经过ls -l /usr/local/lib/python3.5/dist-packages/查看。将cv2.cpython-35m-arm-linux-gnueabihf.so重命名,而后进行软链接

cd /usr/local/lib/python3.5/dist-packages/
sudo mv cv2.cpython-35m-arm-linux-gnueabihf.so cv2.so
cd ~/.virtualenvs/cv/lib/python3.5/dist-packages/
ln -s /usr/local/lib/python3.5/dist-packages/cv2.so cv2.so

7、进行测试

source ~/.profile 
workon cv
python3
>>> import cv2
>>> cv2.__version__
'3.4.4'

若是出现CV2 not find,见以前博客http://www.javashuo.com/article/p-hxberjfg-dk.html,注意本身的配置环境若是是在虚拟环境下那么不要使用sudo

若是出现:ImportError: libQtTest.so.4: cannot open shared object file: No such file or directory丢失包,那么

sudo apt install libqt4-test
sudo apt install libqtgui4

安装其余的库

 sudo pip install scipy
 sudo pip install scikit-image
 sudo pip install sklearn
 sudo pip install numpy
相关文章
相关标签/搜索