OpenCV4Android SDK

This tutorial was designed to help you with installation and configuration of OpenCV4Android SDK.

This guide was written with MS Windows 7 in mind, though it should work with GNU Linux and Apple MacOS as well.

This tutorial assumes you have the following installed and configured:

  • JDK

  • Android SDK and NDK

  • Eclipse IDE

  • ADT and CDT plugins for Eclipse

If you need help with anything of the above, you may refer to our Introduction into Android Development guide.

If you encounter any error after thoroughly following these steps, feel free to contact us via OpenCV4Android discussion group or OpenCV Q&A forum. We’ll do our best to help you out.

General info

OpenCV4Android SDK package enables development of Android applications with use of OpenCV library.

The structure of package contents looks as follows:

OpenCV-2.4.3-android-sdk
|_ apk
|   |_ OpenCV_2.4.3_binary_pack_XXX.apk
|   |_ OpenCV_2.4.3_Manager.apk
|
|_ doc
|_ samples
|_ sdk
|    |_ etc
|    |_ java
|    |_ native
|          |_ 3rdparty
|          |_ jni
|          |_ libs
|               |_ armeabi
|               |_ armeabi-v7a
|               |_ x86
|
|_ license.txt
|_ README.android
  • sdk folder contains OpenCV API and libraries for Android:

  • sdk/java folder contains an Android library Eclipse project providing OpenCV Java API that can be imported into developer’s workspace;

  • sdk/native folder contains OpenCV C++ headers (for JNI code) and native Android libraries (*.so and *.a) for ARM-v5, ARM-v7a and x86 architectures;

  • sdk/etc folder contains Haar and LBP cascades distributed with OpenCV.

  • apk folder contains Android packages that should be installed on the target Android device to enable OpenCV library access via OpenCV Manager API (see details below).

    On production devices that have access to Google Play Market (and internet) these packages will be installed from Market on the first start of an application using OpenCV Manager API. But dev kits without Market or internet require this packages to be installed manually. (Install the Manager.apk and the corresponding binary_pack.apk depending on the device CPU, the Manager GUI provides this info).

    Note: installation from internet is the preferable way since we may publish updated versions of this packages on the Market.

  • samples folder contains sample applications projects and their prebuilt packages (APK). Import them into Eclipse workspace (like described below) and browse the code to learn possible ways of OpenCV use on Android.

  • doc folder contains various OpenCV documentation in PDF format. It’s also available online at http://docs.opencv.org.

    Note: the most recent docs (nightly build) are at http://docs.opencv.org/trunk/. Generally, it’s more up-to-date, but can refer to not-yet-released functionality.

Starting version 2.4.3 OpenCV4Android SDK uses OpenCV Manager API for library initialization. OpenCV Manager is an Android service based solution providing the following benefits for OpenCV applications developers:

  • Compact apk-size, since all applications use the same binaries from Manager and do not store native libs within themselves;

  • Hardware specific optimizations are automatically enabled on all supported platforms;

  • Automatic updates and bug fixes;

  • Trusted OpenCV library source. All packages with OpenCV are published on Google Play;

For additional information on OpenCV Manager see the:

Tegra Android Development Pack users

You may have used Tegra Android Development Pack (TADP) released by NVIDIA for Android development environment setup.

Beside Android development tools the TADP 2.0 includes OpenCV4Android SDK 2.4.2, so it can be already installed in your system and you can skip to running the face-detection sample.

More details regarding TADP can be found in the Introduction into Android Development guide.

Manual OpenCV4Android SDK setup

Get the OpenCV4Android SDK

  1. Go to the OpenCV dowload page on SourceForge and download the latest available version. Currently it’s OpenCV-2.4.3-android-sdk.zip

  2. Create a new folder for Android with OpenCV development. For this tutorial I have unpacked OpenCV to the C:\Work\OpenCV4Android\ directory.

    Note

    Better to use a path without spaces in it. Otherwise you may have problems with ndk-build.

  3. Unpack the OpenCV package into the chosen directory.

    You can unpack it using any popular archiver (e.g with 7-Zip):

    Exploring OpenCV package with 7-Zip

    On Unix you can use the following command:

    unzip ~/Downloads/OpenCV-2.4.3-android-sdk.zip
    

Open OpenCV library and samples in Eclipse

  1. Start Eclipse and choose your workspace location.

    We recommend to start working with OpenCV for Android from a new clean workspace. A new Eclipse workspace can for example be created in the folder where you have unpacked OpenCV4Android SDK package:

    Choosing C:\Work\android-opencv\ as workspace location
  2. Import OpenCV library and samples into workspace.

    OpenCV library is packed as a ready-for-use Android Library Project. You can simply reference it in your projects.

    Each sample included into the OpenCV-2.4.3-android-sdk.zip is a regular Android project that already references OpenCV library. Follow the steps below to import OpenCV and samples into the workspace:

    • Right click on the Package Explorer window and choose Import... option from the context menu:

      Select Import... from context menu
    • In the main panel select General ‣ Existing Projects into Workspace and press Next button:

      General > Existing Projects into Workspace
    • In the Select root directory field locate your OpenCV package folder. Eclipse should automatically locate OpenCV library and samples:

      Locate OpenCV library and samples
    • Click Finish button to complete the import operation.

    After clicking Finish button Eclipse will load all selected projects into workspace. Numerous errors will be indicated:

    Confusing Eclipse screen with numerous errors

    However, all these errors are only false-alarms!

    Just give a minute to Eclipse to complete initialization.

    In some cases these errors disappear after Project ‣ Clean... ‣ Clean all ‣ OK or after pressing F5 (for Refresh action) when selecting error-label-marked projects in Package Explorer.

    Sometimes more advanced manipulations are required:

    The provided projects are configured for API 11 target (and API 9 for the library) that can be missing platform in your Android SDK. After right click on any project select Properties and then Android on the left pane. Click some target with API Level 11 or higher:

    Updating target

    Eclipse will rebuild your workspace and error icons will disappear one by one:

    After small help Eclipse removes error icons!

    Once Eclipse completes build you will have the clean workspace without any build errors:

    OpenCV package imported into Eclipse

Running OpenCV Samples

At this point you should be able to build and run the samples. Keep in mind, that face-detection, Tutorial 3 and Tutorial 4 include some native code and require Android NDK and CDT plugin for Eclipse to build working applications. If you haven’t installed these tools see the corresponding section of Introduction into Android Development.

Also, please consider that Tutorial 0 and Tutorial 1 samples use Java Camera API that definitelly accessible on emulator from the Android SDK. Other samples use OpenCV Native Camera which may not work with emulator.

Note

Recent Android SDK tools, revision 19+ can run ARM v7a OS images but they available not for all Android versions.

Well, running samples from Eclipse is very simple:

  • Connect your device with adb tool from Android SDK or create an emulator with camera support.

  • Select project you want to start in Package Explorer and just press Ctrl + F11 or select option Run ‣ Run from the main menu, or click Run button on the toolbar.

    Note

    Android Emulator can take several minutes to start. So, please, be patient.

  • On the first run Eclipse will ask you about the running mode for your application:

    Run sample as Android Application
  • Select the Android Application option and click OK button. Eclipse will install and run the sample.

    Chances are that on the first launch you will not have the OpenCV Manager package installed. In this case you will see the following message:

    You will see this message if you have no OpenCV Manager installed

    To get rid of the message you will need to install OpenCV Manager and the appropriate OpenCV binary pack. Simply tap Yes if you have Google Play Market installed on your device/emulator. It will redirect you to the corresponding page on Google Play Market.

    If you have no access to the Market, which is often the case with emulators - you will need to install the packages from OpenCV4Android SDK folder manually. Open the console/terminal and type in the following two commands:

    1
    2
    <Android SDK path>/platform-tools/adb install <OpenCV4Android SDK path>/apk/OpenCV_2.4.3_Manager.apk
    <Android SDK path>/platform-tools/adb install <OpenCV4Android SDK path>/apk/OpenCV_2.4.3_binary_pack_armv7a.apk
    

    If you’re running Windows, that will probably look like this:

    Run these commands in the console to install OpenCV Manager

    When done, you will be able to run OpenCV samples on your device/emulator seamlessly.

  • Here is Tutorial 2 - Use OpenCV Camera sample, running on top of stock camera-preview of the emulator.

    Tutorial 1 Basic - 1. Add OpenCV - running Canny

What’s next

Now, when you have your instance of OpenCV4Adroid SDK set up and configured, you may want to proceed to using OpenCV in your own application. You can learn how to do that in a separate Android development with OpenCV tutorial.