StudioW Tech Munch

hacks, musings and tutorials on embedded systems and engineering

How to cross compile OpenCV for armbian with gtk

1 year ago · 4 MIN READ
#arm-linux 

OpenCV is a large and well developed, cross platform machine vision library. Today we will take a look at how to cross compile OpenCV for 32 bit ARM platform.

The board tested was a NanoPi M1, which is based on the Allwinner H3 SoC, and this method should work for any armbian installation on an armhf SoC. (If you are not using a hard-float installation, you must install different cross compiling toolchain - see the "Toolchain" section below.)

Prerequisites:

  • Ubuntu Xenial 16.04 on x64_86 host (or virtualbox of it on Windows)
  • An instance of Armbian running
  • SSH access to the instance

Step 1) Toolchain on cross build machine In the terminal, type

sudo apt-get install gcc-arm-linux-gnueabihf g++-arm-linux-gnueabihf

Note: if you're using a 64 bit board or a board without hardware float, you will need to use a different toolchain.

Step 2) Install cross pkg-config on cross build machine

sudo apt-get install pkg-config-arm-linux-gnueabihf

Step 3) Find out the armbian machine's IP address on armbian machine

sudo ifconfig

Step 4) mount armbian root filesystem to a folder in your home folder on cross build machine

mkdir /home/{your_username}/armbian/mntsysroot

sudo sshfs {armbian_username}@{your.armbian.machine.ip}:/ /home/{your_username}/armbian/mntsysroot/ -o transform_symlinks -o allow_other

Step 5) Create symlinks to from armbian sysroot to toolchain search dirs on cross build machine These are the places that arm-linux-gnueabihf looks in for header files and shared objects in the linking process. Try typing

arm-linux-gnueabihf-gcc -print-search-dirs to see for yourself.

We will link from the armbian sysroot to these (in a safe way) on cross build machine

sudo ln -s /home/{your_username}/armbian/mntsysroot/usr/lib/arm-linux-gnueabihf/ /usr/lib/arm-linux-gnueabihf

sudo ln -s /home/{your_username}/armbian/mntsysroot/lib/arm-linux-gnueabihf/ /lib/arm-linux-gnueabihf

sudo ln -s /home/{your_username}/armbian/mntsysroot/usr/share /usr/share/arm-linux-gnueabihf

sudo ln -s /home/{your_username}/armbian/mntsysroot/usr/include/arm-linux-gnueabihf /usr/include/arm-linux-gnueabihf

Step 6)Install required packages on armbian machine on armbian machine

sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev libgtk2.0-dev libatlas-base-dev gfortran python2.7-dev python3-dev ffmpeg

Step 7) test package config working "arm-linux-gnueabihf-pkg-config" is a wrapper script around the original pkgconfig utility, and looks in /usr/lib/arm-linux-gnueabihf/pkgconfig and a couple of other folders for files with ".pc" extension, which describe where to find libraries and header files of your armbian sysroot (remember we linked some folders before). Test it with:

arm-linux-gnueabihf-pkg-config --list-all

arm-linux-gnueabihf-pkg-config --libs gtk+-2.0

arm-linux-gnueabihf-pkg-config --cflags gtk+-2.0

The first command should list most of the libraries installed on the armbian system. The second one will output the "-L" and "-l" flags, and the paths will be wrong. The third one will say something like "xcbproto not found". Now try:

export PKG_CONFIG_SYSROOT_DIR=/home/{your_username}/armbian/mntsysroot

export PKG_CONFIG_PATH=/usr/share/arm-linux-gnueabihf/pkgconfig:/home/{your_username}/armbian/mntsysroot/usr/lib/pkgconfig

arm-linux-gnueabihf-pkg-config --libs gtk+-2.0

arm-linux-gnueabihf-pkg-config --cflags gtk+-2.0

They should now be showing the correct paths.

Step 8) Download opencv source on cross build machine

cd ~/armbian

git clone https://github.com/opencv/opencv.git

git checkout 3.1.0

Step 9) Modify the OpenCV cmake cross toolchain file

cd ~/armbian/opencv/platforms/linux

gedit arm-gnueabi.toolchain.cmake

Add the following lines to the beginning of the file:

set(ENV{PKG_CONFIG_PATH} "/usr/share/arm-linux-gnueabihf/pkgconfig:/home/{your_username}/armbian/mntsysroot/usr/lib/pkgconfig")

set(ENV{PKG_CONFIG_SYSROOT_DIR} "/home/{your_username}/armbian/mntsysroot")

set(PKG_CONFIG_EXECUTABLE "/usr/bin/arm-linux-gnueabihf-pkg-config")

set(ENV{LD_LIBRARY_PATH} "/home/{your_username}/armbian/mntsysroot/usr/lib")

set(ENV{C_INCLUDE_PATH} "/home/{your_username}/armbian/mntsysroot/usr/include")

set(ENV{CPLUS_INCLUDE_PATH} "/home/{your_username}/armbian/mntsysroot/usr/include")

and change line that sets the CMAKE variable ARM_LINUX_SYSROOT (line 23 for me) to

set(ARM_LINUX_SYSROOT "/usr/arm-linux-gnueabihf" CACHE PATH "ARM cross compilation system root")

Note: If you're using any other toolchain, you may need to specify CMAKE_C_COMPILER and CMAKE_CXX_COMPILER paths.

Step 10) Let's generate makefiles. Go to the directory and open cmake-gui. You will need to install it with

sudo apt-get install cmake cmake-gui

Now, make a build folder inside the opencv folder:

cd ~/armbian/opencv

mkdir build && cd build

cmake-gui

This will launch a window. Point the source directory to your ~/armbian/opencv directory and build directory to ~/armbian/opencv/build.

Click "Configure" and select "use cross compiling toolchain file" and browse to the arm-gnueabi.toolchain.cmake file that we modified earlier.

After it goes through the tests, you may get some warnings about precompiled headers. So go to the list of Cmake variables in the cmake-gui window, and change the following:

ENABLE_PRECOMPILED_HEADERS=OFF (uncheck the box) WITH_1394=OFF (uncheck the box)

Optionally, if your arm processor supports it: ENABLE_NEON=ON (check the box) ENABLE_VFPV3=ON (check the box)

Click "Generate"

Now scroll down and you should see GTK detected. Also, for videoio, you're going to need v4l2 and ffmpeg to get any useful work done, so make sure these are installed on the armbian machine, and they should be detected by arm-linux-gnueabihf-pkg-config on your cross-build machine automatically.

ffmpegon.png gtkon.png

Now close the cmake-gui window and type

cd ~/armbian/opencv/build

make -j4

If you get errors, like I did for the videoio module, it's because the linker cannot find either a header file(.h) or shared library (.so). I had to manually adjust "~/armbian/opencv/build/modules/videoio/CMakeFiles/opencv_videoio.dir/flags.make" and add the following include flag to the "CXX_INCLUDES =" list.

-I/home/{your_username}/armbian/mntsysroot/usr/include

So that it looks like CXX_INCLUDES = -I/home/{your_username}/armbian/ope....{bla bla bla bla bla bla} .. .. ....imgcodecs/include -I/home/{your_username}/armbian/mntsysroot/usr/include

Step 11) Install into the armbian machine

on cross build machine

cd ~/armbian/opencv/build

make install

Now open the /opencv/build/install/lib/pkgconfig/opencv.pc file and change the line

prefix=/home/isuru/armbian/opencv/build/install

to

prefix=/usr

now copy the "install" folder on to a USB drive, including all its subfolders and files (copying with sshfs didn't work for me).

on armbian machine copy all folders in "install folder" to your armbian machine's /usr folder (you will find similarly named folders there). This will copy the opencv header and shared library files to your armbian filesystem.

Step 12) Check libs and cflags with pkg-config on cross build machine

arm-linux-gnueabihf-pkg-config --libs --cflags opencv

on armbian machine

pkg-config --libs --cflags opencv

I owe partial credits and many saved hours to:

Hertaville how to qt Qt Raspberry pi beginners guide Nadeem openCV guide

···

Isuru Walpola

Mechanical engineer who codes, and dabbles in electronics.
comments powered by Disqus


Proudly powered by Canvas · Sign In