📜 ⬆️ ⬇️

Cross-compiling OpenCV 4 for Raspberry Pi and BeagleBone Black

Hello.

When I wanted to install OpenCV on my raspberry for one project, I was very disappointed with the result. It turned out that for new versions of Raspbian images with support for Python3.7 it is impossible to install the library from the repository with the familiar pip install command.

The reasons
pip install opencv-python returned an unexpected result for me: "Could not find a version that satisfies the requirement ...". Just recently, in this way I installed this library on my main computer, and the result was positive.

When I double-checked myself for curvature and made sure that I was still able to correctly type a command of 25 characters without typos, it was time to find out what else could affect the result.
')
The official opencv-python page at pypi.org has clarified a lot:
... Note that the wheel (especially manylinux) format does not currently support properly ARM architecture so there are no packages for ARM based platforms in PyPI. However, opencv-python packages for Raspberry Pi can be found from www.piwheels.org .

... Please note that the wheel format (especially manylinux) currently does not properly support the ARM architecture, so PyPI does not have packages for ARM-based platforms. However, opencv-python packages for the Raspberry Pi can be found at www.piwheels.org .
Ok ...

I checked that raspberry was pulling packets from the correct source, but the last block of the site put everything in its place:
Wheels provided support all Raspberry Pi models (Pi 3, Pi 2, Pi 1 and Pi Zero). Wheels are provided for Raspbian Jessie (Python 3.4) and Raspbian Stretch (Python 3.5). We plan to add support for Raspbian Buster (Python 3.7) in due course.

The provided wheels support all Raspberry Pi models (Pi 3, Pi 2, Pi 1 and Pi Zero. Wheel support Raspbian Jessie (Python 3.4) and Raspbian Stretch (Python 3.5). We plan to add support for Raspbian Buster (Python 3.7) in due time.
I was already ready for a sleepless night with compilation of OpenCV on a raspberry, but I remembered an article about cross-compiling a library (it was not for nothing that the teachers at the institute suggested we make a laboratory instead of manuals). Having slightly changed the parameters from the article (it was necessary to add import for python3 and check if all the libraries are for OpenCV 4.1.0), I could achieve the result I desired.

However, laziness took up, and I began to look for ready-made solutions to such a problem. So I came across this article. Following her, I collected everything the first time without dancing with tambourines and other shamanistic rituals. I think this article will be useful to others.

I present my free translation of the article “ Cross compiling OpenCV 4 for Raspberry Pi and BeagleBone Black ” by Paul Silisteanu. Perhaps someone will save minutes or hours that can be spent on a more interesting lesson than playing a detective story.

From myself, I added some notes to the translation, which I considered important: [ they are highlighted in the text ].

Enjoy reading!

Cross-compiling OpenCV 4 for Raspberry Pi and BeagleBone Black


In this article, I will show you how to install the latest version of OpenCV for Raspberry Pi and BeagleBone Black using cross-compilation. At the time of this writing [ its update at the time of translation is July 17, 2019 ], the latest version of OpenCV is 4.1.0, and the version shipped with standard OSs was 3.2.0. Of course, it is possible to compile OpenCV from source directly to Raspberry or BeagleBone Black, but this can take several hours. And cross-compiling OpenCV for armhf is a process lasting 20-30 minutes. And, depending on the speed of your computer, it may be even faster.

I recommend building on a Debian Buster virtual machine or in a Docker container so as not to clutter up your main OS. If you decide to install Buster on a virtual machine, be sure to use the minimum build [ I recommend doing just that - I checked it on my own skin ]. It is very important to start with a simple system, because we need to install the armhf executables and libraries. Using a minimal system, we avoid potential conflicts with native x86-64 versions.

The article describes working only on Raspberry PI with the Raspbian system, but if you use BeagleBone Black, everything should work exactly the same.

I assume that you already have a clean Raspbian installed. I used the image of the latest available desktop version. In fact, you can follow the article on Raspbian Lite, but I did all my tests using the GUI version.

First, let's make sure our virtual machine or container is updated:

sudo apt update sudo apt upgrade 

Next, you need to connect the armhf architecture.

 sudo dpkg --add-architecture armhf sudo apt update sudo apt install qemu-user-static 

At this point, you can install libraries and applications for armhf on your system and run them.

We are going to build OpenCV with support for Python and C ++. Let's install Python2 and Python3 , as well as the NumPy pact for them:

 sudo apt-get install python3-dev sudo apt-get install python3-numpy sudo apt-get install python-dev sudo apt-get install python-numpy 

We will also need libpython for armhf architecture:

 sudo apt-get install libpython2-dev:armhf sudo apt-get install libpython3-dev:armhf 

Next, we will install libgtk-3 to be able to write simple programs with a graphical interface. If you plan to use OpenCV exclusively in offline mode, you can safely ignore the following two libraries:

 $ sudo apt install libgtk-3-dev:armhf libcanberra-gtk3-dev:armhf 

We also need to install several other libraries necessary for OpenCV (support for various image and video formats):

 sudo apt install libtiff-dev:armhf zlib1g-dev:armhf sudo apt install libjpeg-dev:armhf libpng-dev:armhf sudo apt install libavcodec-dev:armhf libavformat-dev:armhf libswscale-dev:armhf libv4l-dev:armhf sudo apt-get install libxvidcore-dev:armhf libx264-dev:armhf 

Next, we are going to install cross-compilers for Debian, which can be used to create armhf binaries for the Raspberry Pi:

 sudo apt install crossbuild-essential-armhf sudo apt install gfortran-arm-linux-gnueabihf 

At the time of this writing, the toolkit above is based on GCC version 8.3, which is the same as in Raspbian.

Finally, we will install Cmake , git , pkg-config and wget:

 sudo apt install cmake git pkg-config wget 

Next, we can download the current version of OpenCV. I will show you how to install the full version of OpenCV (default and contrib libraries):

 cd ~ mkdir opencv_all && cd opencv_all wget -O opencv.tar.gz https://github.com/opencv/opencv/archive/4.1.0.tar.gz tar xf opencv.tar.gz wget -O opencv_contrib.tar.gz https://github.com/opencv/opencv_contrib/archive/4.1.0.tar.gz tar xf opencv_contrib.tar.gz rm *.tar.gz 

We need to temporarily change two system variables needed to successfully build GTK + support:

 export PKG_CONFIG_PATH=/usr/lib/arm-linux-gnueabihf/pkgconfig:/usr/share/pkgconfig export PKG_CONFIG_LIBDIR=/usr/lib/arm-linux-gnueabihf/pkgconfig:/usr/share/pkgconfig 

At the moment, we can use Cmake to generate OpenCV build scripts:

 cd opencv-4.1.0 mkdir build && cd build cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=/opt/opencv-4.1.0 \ -D CMAKE_TOOLCHAIN_FILE=../platforms/linux/arm-gnueabi.toolchain.cmake \ -D OPENCV_EXTRA_MODULES_PATH=~/opencv_all/opencv_contrib-4.1.0/modules \ -D OPENCV_ENABLE_NONFREE=ON \ -D ENABLE_NEON=ON \ -D ENABLE_VFPV3=ON \ -D BUILD_TESTS=OFF \ -D BUILD_DOCS=OFF \ -D PYTHON2_INCLUDE_PATH=/usr/include/python2.7 \ -D PYTHON2_LIBRARIES=/usr/lib/arm-linux-gnueabihf/libpython2.7.so \ -D PYTHON2_NUMPY_INCLUDE_DIRS=/usr/lib/python2/dist-packages/numpy/core/include \ -D PYTHON3_INCLUDE_PATH=/usr/include/python3.7m \ -D PYTHON3_LIBRARIES=/usr/lib/arm-linux-gnueabihf/libpython3.7m.so \ -D PYTHON3_NUMPY_INCLUDE_DIRS=/usr/lib/python3/dist-packages/numpy/core/include \ -D BUILD_OPENCV_PYTHON2=ON \ -D BUILD_OPENCV_PYTHON3=ON \ -D BUILD_EXAMPLES=OFF .. 

Description of parameters
CMAKE_INSTALL_PREFIX - Directory where the collected data will be located
CMAKE_TOOLCHAIN_FILE - Parameter showing Cmake what cross-compilation will be
OPENCV_EXTRA_MODULES_PATH - Path to additional OpenCV modules
OPENCV_ENABLE_NONFREE = ON - Include patented algorithms in the assembly
Please note that the use of SIFT and SURF algorithms is subject to patent restrictions. And if you are planning on assembling for commercial use, think about whether you need them and check out the limitations.
ENABLE_NEON = ON - Enable NEON instructions for optimization for ARM
ENABLE_VFPV3 = ON - Enable VFPv3-D32 instructions for optimization for ARM
BUILD_TESTS = OFF - Disable rules for building tests (it will be faster)
BUILD_DOCS = OFF - Disable rules for building documentation
PYTHON2_INCLUDE_PATH - Path to python2.7
PYTHON2_LIBRARIES - Path to the previously downloaded libpython2 library
PYTHON2_NUMPY_INCLUDE_DIRS - Path to NumPy for python2.7
PYTHON3_INCLUDE_PATH - Path to python3
PYTHON3_LIBRARIES - Path to the previously downloaded libpython3 library
PYTHON3_NUMPY_INCLUDE_DIRS - Path to NumPy for python3
BUILD_OPENCV_PYTHON2 = ON - Include rules for python2 in the assembly
BUILD_OPENCV_PYTHON3 = ON - Include python4 rules in the assembly
BUILD_EXAMPLES = OFF - Disable rules for assembling all examples

If you did not have errors, then as a result you will receive a Makefile in the assembly folder. Now we can start the actual build:

 make -j16 

[ The article indicates 16 parallel processes during assembly, but this parameter will need to be selected for your host OS ]

After the build phase is complete, we can install the library:

 sudo make install/strip 

Then we need to change the name of the library, which the installer mistakenly called the x86_64 library, although in fact it is armhf:

 cd /opt/opencv-4.1.0/lib/python3.7/dist-packages/cv2/python-3.7/ sudo cp cv2.cpython-37m-x86_64-linux-gnu.so cv2.so 

Let's compress the installation folder and save the archive in the home directory:

 cd /opt tar -cjvf ~/opencv-4.1.0-armhf.tar.bz2 opencv-4.1.0 cd ~ 

To make our life easier, I also prepared a simple pkg-config settings file called opencv.pc. You can download it this way:

 git clone https://gist.github.com/sol-prog/ed383474872958081985de733eaf352d opencv_cpp_compile_settings cd opencv_cpp_compile_settings cp opencv.pc ~ cd ~ 

[ Actually, it is not necessary to do this on the host machine. You can download the file directly to Raspberry. But the author wished to do it in this order ]

opencv.pc
Since the file is relatively small, let me insert it into the text of the article:

 libdir = /opt/opencv-4.1.0/lib includedir = /opt/opencv-4.1.0/include/opencv4 Name: OpenCV Description: OpenCV (Open Source Computer Vision Library) is an open source computer vision and machine learning software library. Version: 4.1.0 Libs: -L${libdir} -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_calib3d -lopencv_ccalib -lopencv_core -lopencv_datasets -lopencv_dnn_objdetect -lopencv_dnn -lopencv_dpm -lopencv_face -lopencv_features2d -lopencv_flann -lopencv_freetype -lopencv_fuzzy -lopencv_gapi -lopencv_hfs -lopencv_highgui -lopencv_imgcodecs -lopencv_img_hash -lopencv_imgproc -lopencv_line_descriptor -lopencv_ml -lopencv_objdetect -lopencv_optflow -lopencv_phase_unwrapping -lopencv_photo -lopencv_plot -lopencv_quality -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_shape -lopencv_stereo -lopencv_stitching -lopencv_structured_light -lopencv_superres -lopencv_surface_matching -lopencv_text -lopencv_tracking -lopencv_videoio -lopencv_video -lopencv_videostab -lopencv_xfeatures2d -lopencv_ximgproc -lopencv_xobjdetect -lopencv_xphoto Cflags: -I${includedir} -lopencv_bgsegm -lopencv_bioinspired -lopencv_calib3d -lopencv_ccalib -lopencv_core -lopencv_datasets -lopencv_dnn_objdetect -lopencv_dnn -lopencv_dpm -lopencv_face -lopencv_features2d -lopencv_flann -lopencv_freetype -lopencv_fuzzy -lopencv_gapi -lopencv_hfs -lopencv_highgui -lopencv_imgcodecs -lopencv_img_hash -lopencv_imgproc libdir = /opt/opencv-4.1.0/lib includedir = /opt/opencv-4.1.0/include/opencv4 Name: OpenCV Description: OpenCV (Open Source Computer Vision Library) is an open source computer vision and machine learning software library. Version: 4.1.0 Libs: -L${libdir} -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_calib3d -lopencv_ccalib -lopencv_core -lopencv_datasets -lopencv_dnn_objdetect -lopencv_dnn -lopencv_dpm -lopencv_face -lopencv_features2d -lopencv_flann -lopencv_freetype -lopencv_fuzzy -lopencv_gapi -lopencv_hfs -lopencv_highgui -lopencv_imgcodecs -lopencv_img_hash -lopencv_imgproc -lopencv_line_descriptor -lopencv_ml -lopencv_objdetect -lopencv_optflow -lopencv_phase_unwrapping -lopencv_photo -lopencv_plot -lopencv_quality -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_shape -lopencv_stereo -lopencv_stitching -lopencv_structured_light -lopencv_superres -lopencv_surface_matching -lopencv_text -lopencv_tracking -lopencv_videoio -lopencv_video -lopencv_videostab -lopencv_xfeatures2d -lopencv_ximgproc -lopencv_xobjdetect -lopencv_xphoto Cflags: -I${includedir} -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_shape -lopencv_stereo -lopencv_stitching -lopencv_structured_light -lopencv_superres -lopencv_surface_matching -lopencv_text -lopencv_tracking -lopencv_videoio -lopencv_video -lopencv_videostab -lopencv_xfeatures2d -lopencv_ximgproc -lopencv_xobjdetect libdir = /opt/opencv-4.1.0/lib includedir = /opt/opencv-4.1.0/include/opencv4 Name: OpenCV Description: OpenCV (Open Source Computer Vision Library) is an open source computer vision and machine learning software library. Version: 4.1.0 Libs: -L${libdir} -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_calib3d -lopencv_ccalib -lopencv_core -lopencv_datasets -lopencv_dnn_objdetect -lopencv_dnn -lopencv_dpm -lopencv_face -lopencv_features2d -lopencv_flann -lopencv_freetype -lopencv_fuzzy -lopencv_gapi -lopencv_hfs -lopencv_highgui -lopencv_imgcodecs -lopencv_img_hash -lopencv_imgproc -lopencv_line_descriptor -lopencv_ml -lopencv_objdetect -lopencv_optflow -lopencv_phase_unwrapping -lopencv_photo -lopencv_plot -lopencv_quality -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_shape -lopencv_stereo -lopencv_stitching -lopencv_structured_light -lopencv_superres -lopencv_surface_matching -lopencv_text -lopencv_tracking -lopencv_videoio -lopencv_video -lopencv_videostab -lopencv_xfeatures2d -lopencv_ximgproc -lopencv_xobjdetect -lopencv_xphoto Cflags: -I${includedir} 


Copy opencv-4.1.0-armhf.tar.bz2 and opencv.pc from your home folder into RPi.

In the next part of the article, I assume that you are already doing everything on your Raspberry Pi .

Make sure that your RPi has all the development libraries we used. As before, if you do not plan to use GTK +, ignore the first line of the following commands. Most of these libraries should already be installed if you are using the full version of Raspbian:

 sudo apt install libgtk-3-dev libcanberra-gtk3-dev sudo apt install libtiff-dev zlib1g-dev sudo apt install libjpeg-dev libpng-dev sudo apt install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev sudo apt-get install libxvidcore-dev libx264-dev 

Unzip and move the library to the / opt folder of your RPi:

 tar xfv opencv-4.1.0-armhf.tar.bz2 sudo mv opencv-4.1.0 /opt 

If you wish, you can delete the archive:

 rm opencv-4.1.0-armhf.tar.bz2 

Next, let's also move opencv.pc to a folder where pkg-config can find it:

 sudo mv opencv.pc /usr/lib/arm-linux-gnueabihf/pkgconfig 

So that the operating system can find the OpenCV libraries, we need to add them to the library path:

 echo 'export LD_LIBRARY_PATH=/opt/opencv-4.1.0/lib:$LD_LIBRARY_PATH' >> .bashrc source .bashrc 

Log out and log in or restart the Terminal.

Next, create some symbolic links that will allow Python to load the newly created libraries:

 sudo ln -s /opt/opencv-4.1.0/lib/python2.7/dist-packages/cv2 /usr/lib/python2.7/dist-packages/cv2 sudo ln -s /opt/opencv-4.1.0/lib/python3.7/dist-packages/cv2 /usr/lib/python3/dist-packages/cv2 

At this point, you can use the OpenCV library from C ++ or Python.
You can find several test programs in C ++ and Python in the repository for this article. You can download the code on your Pi using the commands:

 git clone https://github.com/sol-prog/raspberry-pi-opencv.git cd raspberry-pi-opencv/tests 

There are two standalone tests you can use even if you don’t have a display connected to your RPi: cli_cpp_test.cpp and cli_python_test.py. I also included two graphics tests that require display: gui_cpp_test.cpp and gui_python_test.py.

You can create and run C ++ tests as follows:

 g++ cli_cpp_test.cpp -o cli_cpp_test `pkg-config --cflags --libs opencv` ./cli_cpp_test 

And, if you have a display connected to your RPi:

 g++ gui_cpp_test.cpp -o gui_cpp_test `pkg-config --cflags --libs opencv` ./gui_cpp_test 

Here is a screenshot of the C ++ GUI test running on my Pi:



For Python tests, use:

 python3 cli_python_test.py 

or

 python3 gui_python_test.py 

As a note, it is also possible to cross-compile C ++ programs using OpenCV on your Debian x86-64 system and run binary on your RPi.

[ Further in the article, the author recommends reading Derek Molloy’s book on programming for RaspberryPi. You can find the referral link to the book in the original article ].
Link to the original article

A small addition from myself
I prepared a Dockerfile that will allow you to perform all the steps from the article without having to deploy a virtual machine.

Installation process:
Create a Dockerfile.

 mkdir opencv && cd opencv && mkdir armhf_opencv touch armhf_opencv/Dockerfile nano armhf_opencv/Dockerfile 

Next, we paste the code into the Dockerfile and after that we start the assembly of the image.

 docker image build armhf_opencv/ 

After the build is complete, you should see the identifier of the assembled image:

 >>> Successfully built babdc99ba2d8 

We use this identifier to set the tag for the image and run it:

 docker tag babdc99ba2d8 armhf_opencv:latest docker run armhf_opencv 

After starting the container, it is necessary to pump out the assembled archive and the opencv.pc file from it. To do this, we need its identifier:

 docker container ls --all >>> CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES >>> 369dff5a0a9b armhf_opencv "bash" 9 seconds ago Exited (0) 7 seconds ago affectionate_wu 

Copy the files we need to the current directory:

 docker cp 369dff5a0a9b:/root/opencv-4.1.0-armhf.tar.bz2 ./ docker cp 369dff5a0a9b:/root/opencv.pc ./ 

After that, I recommend deleting the image, since it takes up 2.5GB of space.

Building through docker on my i59600K with 16GB took about 30-40 minutes, and a lot of time was spent downloading data from the repository.

Further, the installation process on the Raspberry Pi is no different from that described in the article.

Dockerfile itself:

 FROM debian:buster USER root RUN apt update && apt upgrade RUN dpkg --add-architecture armhf && \ apt update && apt install -y qemu-user-static RUN apt install -y python3-dev python3-numpy python-dev python-numpy RUN apt install -y libpython2-dev:armhf libpython3-dev:armhf \ libgtk-3-dev:armhf libcanberra-gtk3-dev:armhf \ libtiff-dev:armhf zlib1g-dev:armhf \ libjpeg-dev:armhf libpng-dev:armhf \ libavcodec-dev:armhf libavformat-dev:armhf \ libswscale-dev:armhf libv4l-dev:armhf \ libxvidcore-dev:armhf libx264-dev:armhf RUN apt install -y crossbuild-essential-armhf gfortran-arm-linux-gnueabihf RUN apt install -y cmake git pkg-config wget RUN mkdir opencv_all && cd opencv_all && \ wget -O opencv.tar.gz https://github.com/opencv/opencv/archive/4.1.0.tar.gz && \ tar -xf opencv.tar.gz -C /tmp && \ wget -O opencv_contrib.tar.gz https://github.com/opencv/opencv_contrib/archive/4.1.0.tar.gz && \ tar -xf opencv_contrib.tar.gz -C /tmp && \ rm *.tar.gz && \ export PKG_CONFIG_PATH=/usr/lib/arm-linux-gnueabihf/pkgconfig:/usr/share/pkgconfig && \ export PKG_CONFIG_LIBDIR=/usr/lib/arm-linux-gnueabihf/pkgconfig:/usr/share/pkgconfig && \ cd /tmp/opencv-4.1.0 && mkdir build && cd build && \ cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=/opt/opencv-4.1.0 \ -D CMAKE_TOOLCHAIN_FILE=../platforms/linux/arm-gnueabi.toolchain.cmake \ -D OPENCV_EXTRA_MODULES_PATH=/tmp/opencv_contrib-4.1.0/modules \ -D OPENCV_ENABLE_NONFREE=ON \ -D ENABLE_NEON=ON \ -D ENABLE_VFPV3=ON \ -D BUILD_TESTS=OFF \ -D BUILD_DOCS=OFF \ -D PYTHON2_INCLUDE_PATH=/usr/include/python2.7 \ -D PYTHON2_LIBRARIES=/usr/lib/arm-linux-gnueabihf/libpython2.7.so \ -D PYTHON2_NUMPY_INCLUDE_DIRS=/usr/lib/python2/dist-packages/numpy/core/include \ -D PYTHON3_INCLUDE_PATH=/usr/include/python3.7m \ -D PYTHON3_LIBRARIES=/usr/lib/arm-linux-gnueabihf/libpython3.7m.so \ -D PYTHON3_NUMPY_INCLUDE_DIRS=/usr/lib/python3/dist-packages/numpy/core/include \ -D BUILD_OPENCV_PYTHON2=ON \ -D BUILD_OPENCV_PYTHON3=ON \ -D BUILD_EXAMPLES=OFF .. && \ make -j6 && make install/strip RUN cd /opt/opencv-4.1.0/lib/python3.7/dist-packages/cv2/python-3.7/ && \ cp cv2.cpython-37m-x86_64-linux-gnu.so cv2.so RUN cd /opt && tar -cjvf /root/opencv-4.1.0-armhf.tar.bz2 opencv-4.1.0 RUN git clone https://gist.github.com/sol-prog/ed383474872958081985de733eaf352d opencv_cpp_compile_settings && \ cd opencv_cpp_compile_settings && \ cp opencv.pc /root/ 

Source: https://habr.com/ru/post/461693/


All Articles