1.官方32位桌面系统
2.大于16G储存卡
sudo apt-get update && sudo apt-get upgrade
sudo raspi-config
增加交换空间以避免因内存问题导致的编译挂起
sudo nano /etc/dphys-swapfile
将 CONF_SWAPSIZE 值从默认值更改 100 为 1024
保存运行以下重启命令以使更改生效
sudo /etc/init.d/dphys-swapfile restart
sudo apt-get install build-essential cmake git pkg-config -y
sudo apt-get install libjpeg8-dev -y
sudo apt-get install libtiff5-dev -y
sudo apt-get install libjasper-dev -y
sudo apt-get install libpng12-dev -y
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev -y
sudo apt-get install libgtk2.0-dev -y
sudo apt-get install libatlas-base-dev gfortran -y
进入目录
cd /home/pi/Downloads
将下载好的文件拷贝到 /home/pi/Downloads目录下进行解压,目录不要错了
unzip opencv-4.2.0.zip
unzip opencv_contrib-4.2.0.zip
进入opencv-4.2.0,新建release文件夹,进入release文件夹
cd opencv-4.2.0
mkdir release
cd release
设置编译参数
sudo cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_EXTRA_MODULES_PATH=/home/pi/Downloads/opencv_contrib-4.2.0/modules \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D INSTALL_CXX_EXAMPLES=ON \
-D BUILD_EXAMPLES=ON ..
/home/pi/Downloads/opencv_contrib-4.2.0/modules/xfeatures2d/src/
目录下/home/pi/Downloads/opencv-4.2.0/modules/features2d/test/
目录下的这些文件到/home/pi/Downloads/opencv_contrib-4.2.0/modules/xfeatures2d/test/
里面test_descriptors_invariance.impl.hpp
test_descriptors_regression.impl.hpp
test_detectors_invariance.impl.hpp
test_detectors_regression.impl.hpp
test_invariance_utils.hpp
cp /home/pi/Downloads/opencv-4.2.0/modules/features2d/test/test_descriptors_invariance.impl.hpp /home/pi/Downloads/opencv_contrib-4.2.0/modules/xfeatures2d/test/
cp /home/pi/Downloads/opencv-4.2.0/modules/features2d/test/test_descriptors_regression.impl.hpp /home/pi/Downloads/opencv_contrib-4.2.0/modules/xfeatures2d/test/
cp /home/pi/Downloads/opencv-4.2.0/modules/features2d/test/test_detectors_invariance.impl.hpp /home/pi/Downloads/opencv_contrib-4.2.0/modules/xfeatures2d/test/
cp /home/pi/Downloads/opencv-4.2.0/modules/features2d/test/test_detectors_regression.impl.hpp /home/pi/Downloads/opencv_contrib-4.2.0/modules/xfeatures2d/test/
cp /home/pi/Downloads/opencv-4.2.0/modules/features2d/test/test_invariance_utils.hpp /home/pi/Downloads/opencv_contrib-4.2.0/modules/xfeatures2d/test/
/home/pi/Downloads/opencv_contrib-4.2.0/modules/xfeatures2d/test/test_features2d.cpp
文件中这两行去掉 features2d/test/#include “features2d/test/test_detectors_regression.impl.hpp”`
#include "features2d/test/test_descriptors_regression.impl.hpp
成品如下:
#include "test_detectors_regression.impl.hpp"
#include "test_descriptors_regression.impl.hpp"
/home/pi/Downloads/opencv_contrib-4.2.0/modules/xfeatures2d/test/test_rotation_and_scale_invariance.cpp
文件中这两行去掉 features2d/test/#include “features2d/test/test_detectors_invariance.impl.hpp”
#include “features2d/test/test_descriptors_invariance.impl.hpp”
成品如下:
#include "test_detectors_invariance.impl.hpp"
#include "test_descriptors_invariance.impl.hp"
切换目录
cd /home/pi/Downloads/opencv-4.2.0/release
sudo make -j4 2>&1 | tee make.log
提醒
本次编译采用4线程,编译过程中有可能会卡,这个时候请耐心等待,不要操作其它,如果卡了很久,可以拔掉电源,重新启动系统,cd进入到之前创建的release目录,执行上面编译语句,不要担心,执行编译后会从上次退出编译的位置继续编译,编译过程中不要操作其它,这是重点!!!,以免卡死。采用读取速度98m/s的内存卡,编译时长1个多小时。
切回部署目录(如果编译完成没切出去,这步可以省略)
cd /home/pi/Downloads/opencv-4.2.0/release
sudo make install
安装时可能会到卡死一会,不要着急等会
python3 -c "import cv2; print(cv2.__version__)"
安装成功就可以正常导入模块
rm -rf ~/opencv_build
sudo vim /etc/dphys-swapfile
sudo /etc/init.d/dphys-swapfile restart
开始使用opencv吧!
点击下载
提取码 留言发邮箱