树莓派(图像识别)---安装OpenCV

参考:
opencv3.3.0:https://www.pyimagesearch.com/2017/09/04/raspbian-stretch-install-opencv-3-python-on-your-raspberry-pi/
opencv3.0.0:https://www.pyimagesearch.com/2015/10/26/how-to-install-opencv-3-on-raspbian-jessie/
基于Rasbian Buster系统:https://www.pyimagesearch.com/2019/09/16/install-opencv-4-on-raspberry-pi-4-and-raspbian-buster/
图像识别教程:https://www.hackster.io/mjrobot/real-time-face-recognition-an-end-to-end-project-a10826#toc-step-1--bom---bill-of-material-2

步骤1:安装依赖文件
sudo apt-get update
sudo apt-get upgrade
sudo rpi-update
sudo reboot
sudo apt-get install build-essential git 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 #安装gtk
sudo apt-get install libatlas-base-dev gfortran
sudo apt-get install python2.7-dev python3-dev

步骤2:获取OpenCV源代码
cd ~
wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.0.0.zip
unzip opencv.zip
wget -O opencv_contrib.zip https://github.com/Itseez/opencv_contrib/archive/3.0.0.zip
unzip opencv_contrib.zip

步骤3:安装Python
wget https://bootstrap.pypa.io/get-pip.py
sudo python get-pip.py
sudo pip install virtualenv virtualenvwrapper #虚拟环境
sudo rm -rf ~/.cache/pip
cd ~ 
nano .profile
末尾添加:
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
运行
source ~/.profile

mkvirtualenv cv

如果想用python3
mkvirtualenv cv -p python3

步骤4:编译并安装OpenCV
workon cv
pip install numpy #这是为OpenCV编译Python绑定时的重要依赖项

步骤4:编译并安装OpenCV
workon cv
cd ~/opencv-3.0.0
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE \
	-D CMAKE_INSTALL_PREFIX=/usr/local \
	-D INSTALL_C_EXAMPLES=ON \
	-D INSTALL_PYTHON_EXAMPLES=ON \
	-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.0.0/modules \
	-D BUILD_EXAMPLES=ON ..
make -j4 #4是内核数
如果遇到错误
	make clean
	make
sudo make install
sudo ldconfig

步骤5:完成安装
ls -l /usr/local/lib/python2.7/site-packages/ #查看opencv

# 将opencv软连接到cv虚拟环境中
cd ~/.virtualenvs/cv/lib/python2.7/site-packages/
ln -s /usr/local/lib/python2.7/site-packages/cv2.so cv2.so

# 如果是python3
cd /usr/local/lib/python3.4/site-packages/
sudo mv cv2.cpython-34m.so cv2.so
cd ~/.virtualenvs/cv/lib/python3.4/site-packages/
ln -s /usr/local/lib/python3.4/site-packages/cv2.so cv2.so

# 验证opencv是否安装好
$ workon cv
$ python
>>> import cv2
>>> cv2.__version__
基于树莓派Raspbian Buster系统,快速安装版本(适合新手)

	1.更新和升级现有软件包
		sudo apt-get update && sudo apt-get upgrade
	2.我们需要安装一些开发人员工具,包括CMake,它可以帮助我们配置OpenCV构建过程
		sudo apt-get install build-essential cmake pkg-config
	3.安装映像I/O包,以允许我们从磁盘加载各种映像文件格式。此类文件格式的示例包括JPEG,PNG,TIFF等
		sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng-dev
	4.安装视频I/O包。这些库使我们可以从磁盘读取各种视频文件格式,以及直接使用视频流
		sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
		sudo apt-get install libxvidcore-dev libx264-dev
	5.OpenCV库带有一个名为highgui的子模块,该子模块 用于在我们的屏幕上显示图像并构建基本的GUI。为了编译 highgui模块,我们需要安装GTK开发库和前提条件
		sudo apt-get install libfontconfig1-dev libcairo2-dev
		sudo apt-get install libgdk-pixbuf2.0-dev libpango1.0-dev
		sudo apt-get install libgtk2.0-dev libgtk-3-dev
	6.安装一些额外的依赖项,可以进一步优化OpenCV内部的许多操作
		sudo apt-get install libatlas-base-dev gfortran
	7.用于HDF5数据集和Qt GUI
		sudo apt-get install libhdf5-dev libhdf5-serial-dev libhdf5-103
		sudo apt-get install libqtgui4 libqtwebkit4 libqt4-test python3-pyqt5
	8.如果您的RPi上装有Raspberry Pi摄像机模块,则还应该安装  PiCamera API
		pip install "picamera[array]"
	9.安装OpenCV
		pip install opencv-contrib-python==4.1.0.25 # '=='后面是版本号
	10.测试OpenCV是否安装成功
		python3
		>>> import cv2
		>>> cv2.__version__
        '4.1.0'

你可能感兴趣的:(【树莓派】)