OpenCv on Android

OpenCV (Open Source Computer Vision) is a library of programming functions mainly aimed at real-time computer vision, written in C++.

OpenCV for Android SDK comes with handy Android (java) wrappers so you can concentrate on writing java and let the wrappers take care of communicating with the underlying native (C++) OpenCV libraries. That’s great if you want to keep the C++ abstracted as far away as possible and happy with the version of OpenCV libraries shipped with the sdk. But if you want to customize OpenCV in any way or build a different version from source code, then it seems you have to look further than the sdk which doesn’t seem to come with the necessary build files.

Building C++ projects for android requires installing not just Android Studio, but also the Android NDK (don’t forget LLDB for debugging and CMake which is essentially a makefile generator).

Unfortunately, Android libc (Bionic) has very limited functionality (which differs from one Android version to another), so developers have been forced to take into account all its peculiarities, detecting in run-time which specific Android version the application is running on and applying specific workarounds. A better alternative is to use CrystaX NDK, a drop in replacement for the Android NDK. With Crystax developers can forget about such problems, since CrystaX NDK provides complementary libcrystax, which is used to hide the differences between Android versions or even re-implement many of libc functions. Most importantly, with the help of libcrystax, the application behaves the same on all Android devices regardless of Android version. So downloading CrystaX and configuring Android Studio to use this instead (Module Settings -> Project Structure -> SDK Location) is recommended. As a bonus you also get pre-built boost libraries out of the box.

Build OpenCV from source for Android:

The detailed instructions here are apply to a development machine running Linux, but the procedure should be very similar on Windows/MacOS(X)

  • Clone the main OpenCV repository from GitHub:
    git clone https://github.com/opencv/opencv.git {path_to_local_clone_of_opencv_repo}/opencv_github
  • Change to the opencv platforms dir
    cd {path_to_local_clone_of_opencv_repo}/opencv_github/platforms
  • Add android’s cmake to your PATH environment variable:
    export PATH={path_to_local_android_installation}/android/sdk/cmake/3.6.3155560/bin/:$PATH
    Note: You may need to modify the cmake version number depending on your android installation
  • Point ANDROID_NDK to the install location:
    export ANDROID_NDK={path_to_local_android_installation}/android/sdk/ndk-bundle/
    or
    export ANDROID_NDK={path_to_local_crystax_installation}/crystax/crystax-ndk-10.3.2
  • Configure the build
    sh ./scripts/cmake_android_arm.sh .
    This creates a build directory and runs a CMake command to populate the directory with a particular configuration of OpenCV.
    Note:
    (i) You can edit this script if you want a different configuration. eg “cmake -DBUILD_SHARED_LIBS=ON….” if you want shared libs, instead of the default dynamic libs.
    (ii) Call one of the other scripts instead if you want to build for a different cpu architecture (abi) eg to build instead for x86:
    "./scripts/cmake_android_x86.sh"
  • Start the build
    cd build_android_arm
    make -j8

    Note: The –j8 flag specifies that the make command will use 8 threads. You could specify a different number if you have reason for doing so.

You should now have the freshly built OpenCV libraries in
{path_to_local_clone_of_opencv_repo}/opencv_github/platforms//build_android_arm/lib/armeabi-v7a/

Note: After the first build cmake caches options in the following location:
{path_to_local_clone_of_opencv_repo}/opencv_github/platforms/build_android_arm/CMakeCache.txt
You can peruse and edit these (and then repeat the previous 2 steps), if you want to perform any customizations of the build.