樹莓派(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
相關文章
相關標籤/搜索