使用SDFormatter格式化SD卡,使用Win32DiskImager烧录镜像
在boot路径下新建SSH空文件
插卡上电开机,接入网线,通过路由器查询IP,使用xshell的ssh连接树莓派。
用户名 pi
密码 raspberry
sudo raspi-config
打开VNC,设置分辨率,重启
开启图形化时代
设置国家,中国,汉语,上海
设置密码
填充屏幕
选择wifi
更新(建议跳过,换源后更新)
可能要重启
备份原文件
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
sudo cp /etc/apt/sources.list.d/raspi.list /etc/apt/sources.list.d/raspi.list.bak
换源1
sudo nano /etc/apt/sources.list
deb http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ buster main contrib non-free rpi
deb-src http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ buster main contrib non-free rpi
换源2
sudo nano /etc/apt/sources.list.d/raspi.list
deb http://mirrors.tuna.tsinghua.edu.cn/raspberrypi/ buster main ui
deb-src http://mirrors.tuna.tsinghua.edu.cn/raspberrypi/ buster main ui
更新
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install vim
vim /etc/vim/vimrc
添加代码
set nu
set pastetoggle=
下载地址
https://github.com/opencv/opencv
https://github.com/opencv/opencv_contrib
新建文件夹
mkdir ~/opencv_build
将下载好的文件复制到树莓派并解压
路径为:
~/opencv_build/opencv
~/opencv_build/opencv_contrib
增加交换空间以避免因内存问题导致的编译挂起
sudo vim /etc/dphys-swapfile
第16行,将 CONF_SWAPSIZE 值从默认值更改 100 为 1024 :
运行以下命令以使更改生效
sudo /etc/init.d/dphys-swapfile restart
sudo apt install build-essential cmake git pkg-config libgtk-3-dev libcanberra-gtk*
sudo apt install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev
sudo apt install libjpeg-dev libpng-dev libtiff-dev gfortran openexr libatlas-base-dev opencl-headers
sudo apt install python3-dev python3-numpy libtbb2 libtbb-dev libdc1394-22-dev
mkdir -p ~/opencv_build/opencv/build
cd ~/opencv_build/opencv/build
命令行输入
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_C_EXAMPLES=OFF \
-D INSTALL_PYTHON_EXAMPLES=OFF \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D ENABLE_NEON=ON \
-D ENABLE_VFPV3=ON \
-D BUILD_TESTS=OFF \
-D OPENCV_ENABLE_NONFREE=ON \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_build/opencv_contrib/modules \
-D BUILD_EXAMPLES=OFF ..
需要运行一小会
运行结果
-- Configuring done
-- Generating done
-- Build files have been written to: /home/pi/opencv_build/opencv/build
make -j4
这个过程需要2-3小时
中间可能会报错,如图所示
将boostdesc_bgm.i等文件拷贝到下列路径
~/opencv_build/opencv_contrib/modules/xfeatures2d/src
再次编译
make -j4
继续之前的编译
结果为
[100%] Linking CXX executable ../../bin/opencv_perf_optflow
[100%] Built target opencv_perf_optflow
[100%] Linking CXX executable ../../bin/opencv_perf_superres
[100%] Built target opencv_perf_superres
[100%] Linking CXX shared module ../../lib/cv2.so
[100%] Built target opencv_python2
[100%] Linking CXX shared module ../../lib/python3/cv2.cpython-37m-arm-linux-gnueabihf.so
[100%] Built target opencv_python3
安装已编译的文件
sudo make install
很快结束,结果为
-- Installing: /usr/local/bin/opencv_interactive-calibration
-- Set runtime path of "/usr/local/bin/opencv_interactive-calibration" to "/usr/local/lib"
-- Installing: /usr/local/bin/opencv_version
C++库,命令行输入
pkg-config --modversion opencv4
结果为
4.3.0
Python库,命令行输入
python3 -c "import cv2; print(cv2.__version__)"
结果为
4.3.0-dev
可以删除OpenCV包
rm -rf ~/opencv_build
将交换空间更改回原始大小:
将1024改回100
sudo vim /etc/dphys-swapfile
使修改生效
sudo /etc/init.d/dphys-swapfile restart