目录
换源(非常重要!!!)
下载安装 Qt5
32位树莓派系统下载编译opencv
64位树莓派系统下载编译opencv
树莓派的操作系统, 基于 Debian 开发,所以要选择版本对应的源,如果你下载的树莓派官方提供的最新系统,就更新Debian 11(bullseye)源.
然后通过 uname -m
确定你使用的系统的架构
uname -m
armv7l 用户:
sudo nano /etc/apt/sources.list
将原来的树莓派自带源注释,添加以下内容
deb http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ bullseye main non-free contrib rpi
deb-src http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ bullseye main non-free contrib rpi
###########################################################################################
sudo nano /etc/apt/sources.list.d/raspi.list
将原来的树莓派自带源注释,添加以下内容
deb http://mirrors.tuna.tsinghua.edu.cn/raspberrypi/ bullseye main
aarch64 用户:
sudo nano /etc/apt/sources.list
将原来的树莓派自带源注释,添加以下内容
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye main contrib non-free
deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-updates main contrib non-free
deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-updates main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-backports main contrib non-free
deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-backports main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian-security bullseye-security main contrib non-free
deb-src https://mirrors.tuna.tsinghua.edu.cn/debian-security bullseye-security main contrib non-free
###########################################################################################
sudo nano /etc/apt/sources.list.d/raspi.list
将原来的树莓派自带源注释,添加以下内容
deb http://mirrors.tuna.tsinghua.edu.cn/raspberrypi/ bullseye main
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install qtbase5-dev qtchooser
sudo apt-get install qt5-qmake qtbase5-dev-tools
sudo apt-get install qtcreator
sudo apt-get install qtdeclarative5-dev
查看style是否等于gtk3
sudo nano /etc/xdg/qt5ct/qt5ct.conf
不是则改为gtk3.
扩大交换区,防止爆内存
sudo nano /sbin/dphys-swapfile
sudo nano /etc/dphys-swapfile
因为下载,编译opencv的步骤较多,所以这里为大家提供一个脚本,一键下载,编译opencv.
创建一个.sh脚本文件,并打开.
mkdir OpenCV-4-5-5.sh
sudo chmod 755 ./OpenCV-4-5-5.sh
sudo nano OpenCV-4-5-5.sh
将以下内容粘贴到OpenCV-4-5-5.sh中
#!/bin/bash
set -e
echo "Installing OpenCV 4.5.5 on your Raspberry Pi 32-bit OS"
echo "It will take minimal 2.0 hour !"
cd ~
# install the dependencies
sudo apt-get install -y build-essential cmake git unzip pkg-config
sudo apt-get install -y libjpeg-dev libtiff-dev libpng-dev
sudo apt-get install -y libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install -y libgtk2.0-dev libcanberra-gtk* libgtk-3-dev
sudo apt-get install -y libgstreamer1.0-dev gstreamer1.0-gtk3
sudo apt-get install -y libgstreamer-plugins-base1.0-dev gstreamer1.0-gl
sudo apt-get install -y libxvidcore-dev libx264-dev
sudo apt-get install -y python3-dev python3-numpy python3-pip
sudo apt-get install -y libtbb2 libtbb-dev libdc1394-22-dev
sudo apt-get install -y libv4l-dev v4l-utils
sudo apt-get install -y libopenblas-dev libatlas-base-dev libblas-dev
sudo apt-get install -y liblapack-dev gfortran libhdf5-dev
sudo apt-get install -y libprotobuf-dev libgoogle-glog-dev libgflags-dev
sudo apt-get install -y protobuf-compiler
# download the latest version
cd ~
sudo rm -rf opencv*
wget -O opencv.zip https://github.com/opencv/opencv/archive/4.5.5.zip
wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.5.5.zip
# unpack
unzip opencv.zip
unzip opencv_contrib.zip
# some administration to make live easier later on
mv opencv-4.5.5 opencv
mv opencv_contrib-4.5.5 opencv_contrib
# clean up the zip files
rm opencv.zip
rm opencv_contrib.zip
# set install dir
cd ~/opencv
mkdir build
cd build
# run cmake
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
-D ENABLE_NEON=ON \
-D WITH_OPENMP=ON \
-D WITH_OPENCL=OFF \
-D BUILD_TIFF=ON \
-D WITH_FFMPEG=ON \
-D WITH_TBB=ON \
-D BUILD_TBB=ON \
-D WITH_GSTREAMER=ON \
-D BUILD_TESTS=OFF \
-D WITH_EIGEN=OFF \
-D WITH_V4L=ON \
-D WITH_LIBV4L=ON \
-D WITH_VTK=OFF \
-D WITH_QT=ON \
-D OPENCV_ENABLE_NONFREE=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D INSTALL_PYTHON_EXAMPLES=OFF \
-D PYTHON3_PACKAGES_PATH=/usr/lib/python3/dist-packages \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D BUILD_EXAMPLES=OFF ..
# run make
make -j4
sudo make install
sudo ldconfig
# cleaning (frees 300 MB)
make clean
sudo apt-get update
echo "Congratulations!"
echo "You've successfully installed OpenCV 4.5.5 on your Raspberry Pi 32-bit OS"
然后运行OpenCV-4-5-5.sh脚本文件
./OpenCV-4-5-5.sh
最后将交换空间重置回原来的 100 MB。闪存只能写入有限数量的周期。最后,它会磨损你的 SD 卡。
如果你有 4 GB 或 8 GB 内存的 Raspberry Pi 4,则不用做任何改变。只有当您拥有2 GB RAM 的 Raspberry Pi 4时,您才需要在 zram 配置文件中增加交换空间。增加交换空间的操作和上面的32位树莓派系统相同
(非必要)可以更新 EEPROM 的内容,在视觉应用中,只需更新 EEPROM 的内容,CPU 的热量就会从 65 °C (149 °F) 下降到 48 °C (118 °F),较低的 CPU 温度会延长树莓派的寿命。
sudo rpi-eeprom-update
sudo rpi-eeprom-update -a
sudo reboot
创建一个.sh脚本文件,并打开.
mkdir OpenCV-4-5-5.sh
sudo chmod 755 ./OpenCV-4-5-5.sh
sudo nano OpenCV-4-5-5.sh
将以下内容粘贴到OpenCV-4-5-5.sh中
#!/bin/bash
set -e
echo "Installing OpenCV 4.5.5 on your Raspberry Pi 64-bit OS"
echo "It will take minimal 2.0 hour !"
cd ~
# install the dependencies
sudo apt-get install -y build-essential cmake git unzip pkg-config
sudo apt-get install -y libjpeg-dev libtiff-dev libpng-dev
sudo apt-get install -y libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install -y libgtk2.0-dev libcanberra-gtk* libgtk-3-dev
sudo apt-get install -y libgstreamer1.0-dev gstreamer1.0-gtk3
sudo apt-get install -y libgstreamer-plugins-base1.0-dev gstreamer1.0-gl
sudo apt-get install -y libxvidcore-dev libx264-dev
sudo apt-get install -y python3-dev python3-numpy python3-pip
sudo apt-get install -y libtbb2 libtbb-dev libdc1394-22-dev
sudo apt-get install -y libv4l-dev v4l-utils
sudo apt-get install -y libopenblas-dev libatlas-base-dev libblas-dev
sudo apt-get install -y liblapack-dev gfortran libhdf5-dev
sudo apt-get install -y libprotobuf-dev libgoogle-glog-dev libgflags-dev
sudo apt-get install -y protobuf-compiler
# download the latest version
cd ~
sudo rm -rf opencv*
wget -O opencv.zip https://github.com/opencv/opencv/archive/4.5.5.zip
wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.5.5.zip
# unpack
unzip opencv.zip
unzip opencv_contrib.zip
# some administration to make live easier later on
mv opencv-4.5.5 opencv
mv opencv_contrib-4.5.5 opencv_contrib
# clean up the zip files
rm opencv.zip
rm opencv_contrib.zip
# set install dir
cd ~/opencv
mkdir build
cd build
# run cmake
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
-D ENABLE_NEON=ON \
-D WITH_OPENMP=ON \
-D WITH_OPENCL=OFF \
-D BUILD_TIFF=ON \
-D WITH_FFMPEG=ON \
-D WITH_TBB=ON \
-D BUILD_TBB=ON \
-D WITH_GSTREAMER=ON \
-D BUILD_TESTS=OFF \
-D WITH_EIGEN=OFF \
-D WITH_V4L=ON \
-D WITH_LIBV4L=ON \
-D WITH_VTK=OFF \
-D WITH_QT=ON \
-D OPENCV_ENABLE_NONFREE=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D INSTALL_PYTHON_EXAMPLES=OFF \
-D PYTHON3_PACKAGES_PATH=/usr/lib/python3/dist-packages \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D BUILD_EXAMPLES=OFF ..
# run make
make -j4
sudo make install
sudo ldconfig
# cleaning (frees 300 MB)
make clean
sudo apt-get update
然后运行OpenCV-4-5-5.sh脚本文件
./OpenCV-4-5-5.sh