This article mainly introduces how to compile and install OpenCV and OpenCV contrib from source on ARM development boards.
The source code of OpenCV mainly consists of the following two parts:
-
github.com/opencv/opencv (Main repository)
-
github.com/opencv/opencv_contrib (Includes some newer, less stable, experimental modules)
Before compiling, you need to download the latest Release from GitHub (the current latest version is 4.5.1).
Now let’s see how to compile and install OpenCV.
Before installation, you first need to install some dependencies.
$ sudo apt install pkg-config cmake libgtk2.0-dev # Install some dependencies for OpenCV before installation
Unzip the downloaded source zip file, then create a build folder.
$ cd ./opencv
$ mkdir build && cd build
Next, use cmake to generate the makefile.
$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D BUILD_opencv_xfeatures2d=OFF \ # This module will automatically download some models when enabled; some models are on foreign servers, and it is not recommended to enable this if you do not need the algorithms inside.
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D ENABLE_NEON=ON \ # NEON instructions are acceleration instructions for ARM platforms, supported on ARMv7 and above.
-D WITH_TENGINE=ON \ # Tengine is a DNN backend specifically for ARM platforms that can accelerate DNN modules; this option requires the development board to be connected to the internet.
-D OPENCV_GENERATE_PKGCONFIG=ON \ # This option facilitates calling the OpenCV library.
-D OPENCV_ENABLE_NONFREE=ON \
-D OPENCV_PYTHON3_INSTALL_PATH=/usr/lib/python3/dist-packages \ # This needs to be modified to the corresponding location on your development board.
-D OPENCV_EXTRA_MODULES_PATH=/home/pi/opencv/opencv_contrib/modules \ # This needs to be modified to the directory where your opencv_contrib is located.
-D PYTHON_EXECUTABLE=/usr/bin/python3.7 \ # This needs to be modified to the directory where your Python is located; it will automatically install the Python version of the OpenCV library.
-D BUILD_EXAMPLES=ON ..
Then you can proceed with the compilation and installation.
$ make -j8 # It took 2 hours on my Raspberry Pi 4.
$ sudo make install
After completing the above steps, you can test whether OpenCV is installed successfully.
Python:
>>> import cv2
>>> print(cv2.__version__) # If you can correctly print the OpenCV version, congratulations, you have installed it successfully!
C++:
cd ./opencv/samples/cpp/example_cmake/build
./opencv_example # When you see a handsome face on the screen, it means the installation is successful! (Note: A camera is required to see the handsome face!)
Alternatively, you can also test the world's fastest face detection algorithm libfacedetection with the OpenCV DNN example to see if OpenCV is installed successfully: https://github.com/ShiqiYu/libfacedetection/tree/master/example/opencv_dnn/cpp
Webinar 1: Overview of OpenCV
Webinar 2: Acceleration of OpenCV DNN on ARM
Webinar 3: Applications and Principles of OpenCV Deep Learning
Webinar 4: Google Summer of Code 2020 OpenCV Text Recognition Project Development
Webinar 5: Google Summer of Code 2020 OpenCV RISC-V Project Development
Webinar 6: AI on Edge Devices
Officially Recommended and Certified OpenCV Online Courses by the OpenCV China Team
Join Us as OpenCV Volunteers
(Please specify “OpenCV Volunteer”)
The OpenCV China Team was established in September 2019 with the support of the Shenzhen Institute of Artificial Intelligence and Robotics, for non-profit purposes, dedicated to the development, maintenance, and promotion of OpenCV.
Long press the QR code below to follow us and get the latest updates.