Prasanna Kulkarni

Compiling OpenCV C++ applications on Android platform (using NDK) & ARM (imx6) processor; without using eclipse or Android Studio IDE

Discussion created by Prasanna Kulkarni on Apr 18, 2019
Latest reply on Apr 28, 2019 by Prasanna Kulkarni

Objective :   I wanted to change my existing OpenCV applications which were written in cpp for desktop version (X_86) processor to an ARM Architecture (imx6). At the initial stage I was not interested to change my cpp applications to java and compile it on Android Studio IDE, Before developing an Android app I wanted to benchmark the imx6 device, is capable to run my OpenCV applications how much time it take to process the video frames etc. So retaining my cpp applications as it is, I wanted to cross compile it for ARM Arch. and run it on Android Device (without GUI ).


I have done this experiment on imx6dl board running marshmallow Android and kernel 4.x.


      I have followed many links and blogs to compile and build opencv cpp applications for ARM processor. One good link is  all credits goes to above mentioned blog author.

      This link is very good for beginners, till step no 8 it is very clearly written and I could follow it but Step no 9 onwards it is not properly written about where to keep demo applications, link & include libraries etc. when I tried step 9 in from that blog & tried to compile demo.cpp, It used to give an error *.hpp not found. I tried all the ways but was not able to solve.


          Then I thought that let me first build sample applications comes with opencv sdk. my approach is as shown below.


Build Set-up used


a. Ubuntu 14.04 and installed all the packages mentioned in  

     link. (link followed for only installation of dependent packages).

b. Download and install NDK r14b

c. Download and install OpenCV 3.4.1

d. Target armeabi-v7a (ARM based) & Android API -23

Steps 1-4 are required for first time setup

Step 1 : Download and Setup NDK :
unzip it and go inside ndk folder.
$  ./build/tools/ \ --arch arm \ --api 23 \ --install-dir /my-android-toolchain

Step 2 : Setup ANDROID_NDK Path

$ export ANDROID_STANDALONE_TOOLCHAIN=/my-android-toolchain/

Step 3 Download and Install OpenCV 3.4.1

$ cd opencv/

$ mkdir build

$ cd build

$ cmake -DCMAKE_TOOLCHAIN_FILE=../platforms/android/android.toolchain.cmake -DANDROID_STL=gnustl_shared -DBUILD_EXAMPLES=ON -DANDROID_NATIVE_API_LEVEL=23 ..

(-DBUILD_EXAMPLES=ON is important to build the samples).

$ make -j4


Step 4 Neon and VFPV3 optimizations (optional)


For ARM based devices, optimization i usually enable using: NEON VFPV3 Check out preamble of platforms/android/android.toolchain.cmake for various configuration options.


Step 5 Building your code Go to Working Diretory

$ cd path-to-opencv

Place your newly created cpp file in opencv/samples/cpp/

$ cd build/

$ cmake -DCMAKE_TOOLCHAIN_FILE=../platforms/android/android.toolchain.cmake -DANDROID_STL=gnustl_shared -DBUILD_EXAMPLES=ON -DANDROID_NATIVE_API_LEVEL=23 ..

$ make -j4


Step 6 Copying on android and running

$ cd path-to-opencv

$ adb push build/bin/example_cpp_your_program /system/xbin/

$ adb push build/libs/armeabi-v7a/ /system/lib/ ****(If Required)****

(for me while executing cpp binary it said library not found to link so)

$ adb shell

$ cd /system/xbin/

$ ./example_cpp_your_program

This method perfectly worked on my Android device without using Android Studio IDE.