树莓派(Stretch)配置OpenCV环境

一、更新源

笔者使用的是stretch版本,所以使用以下源,如果是其他版本设置相应的源

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进行修改

sudo nano /etc/apt/sources.list

然后更新

sudo apt-get update
sudo apt-get upgrade

 二、安装vim

sudo apt-get update
sudo apt-get install vim

然后配置sudo vim /etc/vim/vimrc,

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

 三、扩展文件系统

sudo raspi-config

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

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

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.

解决方法:

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

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

安装Opencv包,如果unzip报错使用jar xvf opencv.zip

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

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

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

六、完成最后配置

如果安装成功,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

七、进行测试

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

如果出现CV2 not find,见之前博客https://www.cnblogs.com/cc-xiao5/p/11258736.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

 

转载于:https://www.cnblogs.com/cc-xiao5/p/11365389.html

你可能感兴趣的:(树莓派(Stretch)配置OpenCV环境)