Rockpi4/dev/install-opencv
ROCK Pi 4 > Development > Install OpenCV
This a guide to install OpenCV on your ROCK Pi 4 that running Ubuntu. All the instructions are done in terminal.
Compile OpenCV (4.0.1)
To compile OpenCV on ROCK Pi, we need at least 4GB ram and we recommend to compile on ubuntu arm64 images.
Step 1: Get Ubuntu running on ROCK Pi 4
To use OpenCV, an OS desktop is necessary. If your ROCK Pi 4 is running Ubuntu but without desktop, you can install Ubuntu mate desktop.
$ sudo apt install ubuntu-mate-core && sudo apt install ubuntu-mate-desktop
Step 2: Install all the recommended packages
- Compilers:
$ sudo apt-get install build-essential
- Required:
$ sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
- Recommended optional packages
$ sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libdc1394-22-dev $ sudo apt install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev libavresample-dev $ sudo apt install tesseract-ocr cmake-data liblept5
Step 3: Download OpenCV source
We suggest that you are at the home folder, so that you don't have to change the code in the next steps.
We need to download OpenCV source. OpenCV latest release: [here]. As of writing this, the latest release is 4.0.1. For the newer version, just replace the link for the source code zip file.
$ cd ~ $ wget -O opencv.zip https://github.com/opencv/opencv/archive/4.0.1.zip $ unzip opencv.zip
And we also need to install the contribution packages of OpenCV. Here we get the latest release version:
$ cd ~ $ wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.0.1.zip $ unzip opencv_contrib.zip
Step 4: Configuring and compiling
Confifure thte build using cmake:
$ cd ~/opencv-4.0.1/ $ mkdir build $ cd build $ export PY_NAME=$(python -c 'from sys import version_info as v; print("python%d.%d" % v[:2])') $ export PY_NUMPY_DIR=$(python -c 'import os.path, numpy.core; print(os.path.dirname(numpy.core.__file__))') $ cmake -DCMAKE_BUILD_TYPE=RELEASE \ -DCMAKE_INSTALL_PREFIX=/usr/local \ \ -DPYTHON2_EXECUTABLE=$(which python) \ -DPYTHON_INCLUDE_DIR=/usr/include/$PY_NAME \ -DPYTHON_INCLUDE_DIR2=/usr/include/aarch64-linux-gnu/$PY_NAME \ -DPYTHON_LIBRARY=/usr/lib/aarch64-linux-gnu/lib$PY_NAME.so \ -DPYTHON2_NUMPY_INCLUDE_DIRS=/usr/lib/$PY_NAME/dist-packages/numpy/core/include/ \ \ -DBUILD_DOCS=OFF \ -DBUILD_EXAMPLES=OFF \ -DBUILD_TESTS=OFF \ -DBUILD_PERF_TESTS=OFF \ \ -DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.0.1/modules \ ..
Compile OpenCV in the build folder:
$ make -j$(nproc --all)
Install OpenCV in the build folder:
$ sudo make install $ sudo ldconfig
Step 5: Test your building and installation
To confirm that you have install ed OpenCV correctly , try this:
rock@linux:~$ python Python 2.7.15rc1 (default, Nov 12 2018, 14:31:15) [GCC 7.3.0] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import cv2 >>> cv2.__version__ '4.0.1' >>>
Step6: Do the sample supported by OpenCV official
$ cd ~/opencv-4.0.1/samples/python $ python watershed.py
Finally, if you don't need the files in opencv-4.0.1 or opencv_contrib-4.0.1. You can remove them:
$ cd ~ $ rm -rf opencv-4.0.1 opencv_contrib-4.0.1 opencv_contrib.zip opencv.zip
If you follow the code exactly, you will find it working just fine.
Compile OpenCV (4.6.0)
Requirements
RAM >= 2G
Debian >= 10
Download Source Code
Dependencies
apt install build-essential cmake pkg-config ccache git apt install python2 python3 python3-pip apt install qt5-default apt install libpng-dev libjpeg-dev apt install libeigen3-dev apt install ffmpeg libavcodec-dev libavformat-dev libswscale-dev libavresample-dev apt install libgstreamer1.0-dev libgstreamermm-1.0-dev apt install libgtk-3-dev libgtkglext1-dev libgtkglextmm-x11-1.2-dev pip3 install numpy
Download `OpenBLAS` (Basic Linear Algebra Subprograms)
OpenBLAS : An optimized BLAS library()
# tar && cd project dir make sudo make install
- `qt5-default` may not be found in many apt source but there should be substitution
- `build-essential` `cmake` `pkg-config` `ccache` `git` essential to build
- `libpng-dev` `libjpeg-dev` image files surpport
- `libeigen3-dev` linear algebra surpport
- `ffmpeg` `libavcodec-dev` `libavformat-dev` `libswscale-dev` `libavresample-dev` video files surpport
- `libgstreamer1.0-dev` `libgstreamermm-1.0-dev` stream media surpport
- `libgtk-3-dev` `libgtkglext1-dev` `libgtkglextmm-x11-1.2-dev` GUI lib
Build
# tar && cd project dir mkdir -p build && cd build # generate makefile cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_VERBOSE_MAKEFILE=ON -DCMAKE_INSTALL_PREFIX=/opt/opencv -DWITH_OPENGL=ON -DOPENCV_ENABLE_NONFREE=ON -DWITH_QT=ON -DBUILD_TESTS=OFF -DBUILD_PERF_TESTS=OFF -DOPENCV_GENERATE_PKGCONFIG=ON -DWITH_1394=OFF -DBUILD_opencv_python2=OFF -DBUILD_opencv_python3=ON -DPYTHON3_LIBRARY=$(python3 -c "from distutils.sysconfig import get_config_var;from os.path import dirname,join ; print(join(dirname(get_config_var('LIBPC')),get_config_var('LDLIBRARY')))") -DPYTHON3_NUMPY_INCLUDE_DIRS=$(python3 -c "import numpy; print(numpy.get_include())") -DPYTHON3_PACKAGES_PATH=$(python3 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())")
- `WITH_OPENGL`=ON : enables OpenGL hardware acceleration surpport
- `CMAKE_BUILD_TYPE`=Release : or Debug if you want
- `CMAKE_VERBOSE_MAKEFILE`=ON : easy to find errors
- `OPENCV_ENABLE_NONFREE`=ON : Some algorithms included in the library are known to be protected by patents and are disabled by default
- `CMAKE_INSTALL_PREFIX`=/opt/opencv: To install produced binaries root location
- `WITH_QT`=ON : use Qt to draw a window
- `BUILD_TESTS`=OFF,`BUILD_PERF_TESTS`=OFF:shorten the build time
- `OPENCV_GENERATE_PKGCONFIG`=ON: enables `.pc` file generation along with standard CMake package
- For more information, OpenCV: OpenCV configuration options reference
result output may look like below:
-- General configuration for OpenCV 4.6.0 ===================================== -- Version control: unknown -- -- Platform: -- Timestamp: 2022-07-13T07:48:24Z -- Host: Linux 4.4.154-116-rockchip-g86a614bc15b3 aarch64 -- CMake: 3.13.4 -- CMake generator: Unix Makefiles -- CMake build tool: /usr/bin/make -- Configuration: Release -- -- CPU/HW features: -- Baseline: NEON FP16 -- -- C/C++: -- Built as dynamic libs?: YES -- C++ standard: 11 -- C++ Compiler: /usr/bin/c++ (ver 8.3.0) -- C++ flags (Release): -fsigned-char -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -DNDEBUG -- C++ flags (Debug): -fsigned-char -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -fvisibility=hidden -fvisibility-inlines-hidden -g -O0 -DDEBUG -D_DEBUG -- C Compiler: /usr/bin/cc -- C flags (Release): -fsigned-char -W -Wall -Wreturn-type -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -fvisibility=hidden -O3 -DNDEBUG -DNDEBUG -- C flags (Debug): -fsigned-char -W -Wall -Wreturn-type -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -fvisibility=hidden -g -O0 -DDEBUG -D_DEBUG -- Linker flags (Release): -Wl,--gc-sections -Wl,--as-needed -Wl,--no-undefined -- Linker flags (Debug): -Wl,--gc-sections -Wl,--as-needed -Wl,--no-undefined -- ccache: YES -- Precompiled headers: NO -- Extra dependencies: dl m pthread rt -- 3rdparty dependencies: -- -- OpenCV modules: -- To be built: calib3d core dnn features2d flann gapi highgui imgcodecs imgproc ml objdetect photo stitching video videoio -- Disabled: world -- Disabled by dependency: - -- Unavailable: java python2 python3 ts -- Applications: apps -- Documentation: NO -- Non-free algorithms: YES -- -- GUI: QT5 -- QT: YES (ver 5.11.3 ) -- QT OpenGL support: YES (Qt5::OpenGL 5.11.3) -- GTK+: YES (ver 3.24.5) -- GThread : YES (ver 2.58.3) -- GtkGlExt: YES (ver 1.2.0) -- OpenGL support: YES (/usr/lib/aarch64-linux-gnu/libGL.so /usr/lib/aarch64-linux-gnu/libGLU.so) -- VTK support: NO -- -- Media I/O: -- ZLib: /usr/lib/aarch64-linux-gnu/libz.so (ver 1.2.11) -- JPEG: /usr/lib/aarch64-linux-gnu/libjpeg.so (ver 62) -- WEBP: build (ver encoder: 0x020f) -- PNG: /usr/lib/aarch64-linux-gnu/libpng.so (ver 1.6.36) -- TIFF: /usr/lib/aarch64-linux-gnu/libtiff.so (ver 42 / 4.1.0) -- JPEG 2000: build (ver 2.4.0) -- OpenEXR: /usr/lib/aarch64-linux-gnu/libImath.so /usr/lib/aarch64-linux-gnu/libIlmImf.so /usr/lib/aarch64-linux-gnu/libIex.so /usr/lib/aarch64-linux-gnu/libHalf.so /usr/lib/aarch64-linux-gnu/libIlmThread.so (ver 2_2) -- HDR: YES -- SUNRASTER: YES -- PXM: YES -- PFM: YES -- -- Video I/O: -- FFMPEG: YES -- avcodec: YES (58.35.100) -- avformat: YES (58.20.100) -- avutil: YES (56.22.100) -- swscale: YES (5.3.100) -- avresample: YES (4.0.0) -- GStreamer: YES (1.14.4) -- v4l/v4l2: YES (linux/videodev2.h) -- -- Parallel framework: pthreads -- -- Trace: YES (with Intel ITT) -- -- Other third-party libraries: -- Lapack: YES (/opt/OpenBLAS/lib/libopenblas.so) -- Eigen: YES (ver 3.3.7) -- Custom HAL: YES (carotene (ver 0.0.1)) -- Protobuf: build (3.19.1) -- -- OpenCL: YES (no extra features) -- Include path: /home/rock/OpenCV/opencv-4.6.0/3rdparty/include/opencl/1.2 -- Link libraries: Dynamic load -- -- Python 3: -- Interpreter: /usr/bin/python3 (ver 3.7.3) -- Libraries: NO -- numpy: /home/rock/.local/lib/python3.7/site-packages/numpy/core/include (ver 1.16.2) -- install path: - -- -- Python (for build): /usr/bin/python2.7 -- -- Java: -- ant: NO -- JNI: NO -- Java wrappers: NO -- Java tests: NO -- -- Install to: /usr/local -- ----------------------------------------------------------------- -- -- Configuring done -- Generating done -- Build files have been written to: /home/rock/OpenCV/opencv-4.6.0/build
check the output,install dependencies and reuse the full `cmake` command to make item to **YES** if it interests you
eg. `OpenGL Support` and `OpenCL` should be **YES** if you want full hardware acceleration feature
After the generation, make :
make all #or make all -j$(grep -c ^processor /proc/cpuinfo) make install
Troubleshooting
- Post your issue at the forum: https://forum.radxa.com/c/dev