Hardware Preparation: It is recommended to use a 32G MicroSD card to install Raspberry Pi (16G and 8G MicroSD cards have not been tested).
1 – Ensure Network Stability
This is very important; if the network is unstable, it is recommended not to waste time.
2 – Install Dependencies/Libraries
Install the necessary compiler:
sudo apt-get install build-essential
Install the necessary dependencies: Refer to the official installation: https://docs.opencv.org/master/d7/d9f/tutorial_linux_install.html
sudo apt-get install cmake git
sudo apt-get install libgtk2.0-dev
sudo apt-get install pkg-config libavcodec-dev libavformat-dev libswscale-dev
If you encounter some installation issues, you can first run
sudo apt-get update
to update the repository link, then runsudo apt-get install ***
to install, which usually resolves the problem.
Install the optional dependencies provided by the official:
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
Install a series of image and video processing libraries, which are very important for future operations!
sudo apt-get install libjpeg-dev libpng-dev libtiff-dev
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt-get install libxvidcore-dev libx264-dev
Install GTK:
sudo apt-get install libgtk-3-dev
Install GTK warning message filter to reduce annoying warnings:
sudo apt-get install libcanberra-gtk*
The asterisk * indicates to automatically obtain the corresponding version for the Raspberry Pi ARM architecture.
Install libraries for OpenCV optimization data:
sudo apt-get install libatlas-base-dev gfortran
Install libraries related to Python3 development:
sudo apt-get install python3-dev
sudo apt-get install libjasper-dev libjasper1
Install HDF5 and QT libraries:
sudo apt-get install libhdf5-dev
sudo apt-get install libhdf5-serial-dev
sudo apt-get install libatlas-base-dev
sudo apt-get install libjasper-dev
sudo apt-get install libqtgui4
sudo apt-get install libqt4-test
All these dependencies are summarized from the official and online resources. I installed them all without hesitation.
3 – Expand the File System
Enter the following command in the terminal to access the Raspberry Pi configuration interface:
sudo raspi-config
Select “7 Advanced Options” and press Enter
Select “A1 Expand Filesystem” and press Enter
Restart the Raspberry Pi and continue your journey.
4 – Increase Virtual Memory
During the installation process, a large amount of memory is required, so we need to change the size of the virtual memory here.
First, edit the virtual memory configuration file:
sudo nano /etc/dphys-swapfile
Find the CONF_SWAPSIZE item and set it to 4096, which is about 4G of memory space.
CONF_SWAPSIZE=4096
Save and exit.
Then, run the following commands to stop and restart the virtual memory, making it effective for 4G space.
sudo /etc/init.d/dphys-swapfile stop
sudo /etc/init.d/dphys-swapfile start
5 – Download the Latest Version of OpenCV and OpenCV_contrib
# The purpose of running cd is to return to the default user directory. In this example, the installation is in the default directory, but you can choose to install it in your preferred directory.
cd
# Clone the repository from github
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git
6 – Build and Configure OpenCV
Building means compiling, run the following commands in the terminal:
# The purpose of running cd is to return to the default user directory
cd
# Open the opencv folder
cd opencv
# Create a new build folder
mkdir build
# Open the build folder
cd build
# Run cmake configuration
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 OPENCV_EXTRA_EXE_LINKER_FLAGS=-latomic -D ENABLE_VFPV3=ON -D BUILD_TESTS=OFF -D OPENCV_ENABLE_NONFREE=ON -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules -D BUILD_EXAMPLES=OFF ..
The following are the configuration information I ran after cmake, which you can refer to:
7 – Compile OpenCV
Once ready, in the terminal under the build directory, enter the following command to compile:
make
If your Pi has good performance, you can choose
make -j$(nproc)
ormake -j2
, -j means how many threads to use for compilation.
After a long wait……. finally reaching 100%
8 – Install OpenCV
Finally, run the following command to install:
sudo make install
This installation process is very fast, so don’t worry.
These processes should be done in one go. The most important reasons are network stability and that the dependencies/libraries are all installed.
Finally – Test if Installation is Successful!
Enter the Python3 development environment in the terminal:
python3
Enter the code:
import cv2
cv2.__version__
At this point, the latest version of OpenCV 4.1.2 has been successfully installed.
Summary
The installation process took 2 days, 48 hours, and with poor network speed, it was unavoidable, but with a little patience, it will be installed successfully. For those who want to install the latest version, keep it up!
References:
-
https://www.learnopencv.com/install-opencv-4-on-raspberry-pi/
-