Tutorial on porting OpenCV(revision:5578) to android in Windows Environment

Software Prerequisites

  • svn client (eg.Tortoise SVN)
·         cmake tool http://www.cmake.org/
·         Android NDK (revision r5 or newer) http://developer.android.com/sdk/ndk/index.html
·         OpenCV trunk https://code.ros.org/svn/opencv/trunk/opencv

     Additional requirements for Java wrapper and Android samples

·         SWIG http://www.swig.org/
·         Android SDK http://developer.android.com/sdk/index.html
·         ant http://ant.apache.org

     Windows additional prerequisites

At the moment OpenCV cross compilation under cygwin is not supported.
However you can cross-compile OpenCV on Windows using native port of make.
These two are tested to work for OpenCV:
·         http://code.google.com/p/mingw-and-ndk/ (see "make-3.7z" under Downloads section)

Downloading OpenCV

Install the Tortoise SVN software on your machine, select a preferable directory and right click to check out.
·         Enter the URL with https://code.ros.org/svn/opencv/trunk/opencv and press ok

OpenCV build

By default OpenCV and the most of Android samples are configured for modern ARM-v7a architecture. 
If you want to compile OpenCV for emulator or older device, or want to build with NEON support please read the Android targets section for instructions.

     Windows (native)

Define the following environment variables:
  • ANDROID_NDK - full path to the Android NDK. This path should not contain spaces, and tailing slash.
  • CMAKE_EXE - full path to cmake executable (including executable name).
  • MAKE_EXE - full path to native port of make (including executable name). This path should not contain spaces.
Instead of modifying your environment you can create file opencv\android\scripts\wincfg.cmd and put all settings to this file. OpenCV provides template for this file named opencv\android\scripts\wincmd.cfg.tmpl. Please note that settings from wincfg.cmd takes precedence over global environment variables.
Then open Windows console (cmd.exe) and go to opencv\android folder. Run script cmake_android.cmd from scripts folder:
cd opencv\android\
scripts\cmake_android.cmd
When script finishes you will have OpenCV compiled.

Troubleshooting

First, make sure that paths to OpenCV, Android NDK and make.exe do not contain spaces and other suspicious symbols.
If make fails with message similar to
*** target pattern contains no `%'.  Stop.
Then you probably use cygwin tools instead of their native ports. Remove cygwin from your PATH, delete build directory and rebuild.

Building console "Hello World" with OpenCV

This application is not usual Android application. This is a unix console which may be used as a template for development. Console application is much more convenient for development, debugging and performance optimization stages. Then, if you are finished with your CV algorithm, you can switch to normal Android development with Java + NDK (see the android-opencv and OpenCV_SAMPLE sections below).

     Prerequisities

First of all you need an Android device with root privileges. The goal is to have ability to run console applications on Android device using adb shell. Even chmod is not available by default (for example on Nexus One). You have the following options:
  • Use emulator
·         Enable root access to your device. Use google to find instructions for your device.
  • Use developer device, for example NVidia Tegra devkits have root access by default.
If the device allows you to do chmod and run executables, then follow the instructions below.
Second, you need OpenCV compiled for your device. By default this sample is configured for emulator. Read Android targets to know how to build OpenCV for emulator.
Also you need some free space on sdcard to run the HelloAndroid application - it will write small image to /mnt/sdcard. Please note, that even with emulator you need a sdcard. You can create it with AVD Manager from Android SDK.

     Windows

Set the same environment variables as needed for OpenCV build (variables set in \opencv\android\scripts\wincfg.cmd work as well).
If you are using 
wincfg.cmd file then ensure that ARM_TARGET and BUILD_DIR are set to correct values. Use the following values for emulator:
SET BUILD_DIR=build_armeabi
SET ARM_TARGET=armeabi
When all variables are set run the commands
cd opencv\android\apps\HelloAndroid
cmake_android.cmd
open CMakeLists.txt and find line :
SET( OpenCV_DIR ${CMAKE_SOURCE_DIR}/../../build )
Delete the cache in this line
Connect to your device with adb tool from Android SDK and run the application
Edit run.cmd line:
%adb% push .\bin\%OUT_DIR%\%PROJECT_NAME% /data/bin/sample/%PROJECT_NAME% || GOTO end
to
%adb% push .\build_armeabi\%PROJECT_NAME% /data/bin/sample/%PROJECT_NAME% || GOTO end

Next, go to opencv\android\apps\HelloAndroid\build_armeabi directory and change the opencv filename to HelloAndroid

Now execute the following command in command prompt:

run.cmd
If you can see HelloAndroid.png in your current directory and Hello Android! message in console - you've arrived.

Troubleshooting

If you see error message similar to
ld: cannot find -lopencv_contrib
or (on Windows)
ld.exe: cannot find -lopencv_contrib230
It means that cmake failed to find OpenCV compiled for Android. Check that all paths are set to correct values, remove build directory and try to rebuild the application.

Building the android-opencv project

The android-opencv project is a shared OpenCV library for Android. It contains some java bindings and a java/jni camera client. It is required to build the most of Android samples.

     Prerequisities

You need OpenCV compiled for Android.

     Windows

In addition to variables set for OpenCV build set the following environment variables (or put them to opencv\android\scripts\wincfg.cmd)
  • ANDROID_SDK - full path to Android SDK.
  • SWIG_DIR - full path to SWIG.
  • ANT_DIR - full path to Apache Ant.
  • JAVA_HOME - full path to JDK. Both x86 and x64 work for OpenCV. But note that Android SDK installer requires 32 bit JRE. At the same time if you download zip with SDK, you can work with 64 bit JRE/JDK without 32 bit version installed.
All paths should be specified without tailing slash.
Next go to android-opencv folder and run build script:
cd android-opencv
cmake_andoid.cmd

Troubleshooting

If android helper tool complains (when you run project_create.sh script on linux/Mac or cmake_andoid.cmd on Windows) then you have not installed API level 7 package to your Android SDK. You can either install it or update project settings to use one of API levels you have.
Put your target API level to default.properties file and rerun the script. android-opnecv supports target-7 or higher.
To get full list of installed API targets you can use the command
android list targets

Building a sample OpenCV_SAMPLE

     Windows

Go to OpenCV_SAMPLE directory and execute the build and cmake:
cd OpenCV_SAMPLE
..\..\scripts\build.cmd
cmake_andoid.cmd
Recreate build.xml file for the project:
set path=%PATH%;C:\dev\android-sdk-windows\tools 
android update project --name OpenCV_SAMPLE --path .
Plug in you device to compile and install the project:
path=%PATH%;C:\dev\apache-ant-1.8.2\bin
ant clean
ant compile
ant install
Screenshot of my computer with canny filter:

     Android hardware targets

You may wish to build OpenCV and samples for multiple hardware targets.
To do so, edit the name of file wincfg.cmd.tmpl to wincfg.cmd. Next open the file and Uncomment the line for the chosen device.
Just change the cmake cache ARM_TARGET to either: "armeabi" "armeab-v7a" "armeab-v7a with NEON"
You may install each of these to the toolchain/NDK, and they should be linked against properly via way of the android-cmake toolchain.

     Android API levels

You may wish to build OpenCV and samples for different level of Android native API.
Just change the cmake cache ANDROID_API_LEVEL to appropriative value.
The following levels are currently supported:
Platform Version
SDK API Level
NDK API Level
Android 3.1
12
9
Android 3.0
11
Android 2.3.4
10
Android 2.3.3
Android 2.3
9
Android 2.2
8
8
Android 2.1
7
5
Android 2.0.1
6
Android 2.0
5
Android 1.6
4
4
Android 1.5
3
3
Android 1.1
2
-
Android 1.0
1

If there are any doubts or hesitation or suggestion, feel free to write to jasongoh1987@gmail.com and i will update the blog from time to time. Thank You.
Taken and edited from http://opencv.willowgarage.com/
Credits to Andrey Kamaev

Comments

  1. Hi,

    I'm trying to build opencv for android as well but I'm facing an error when running HelloAndroid. I can run the "cmake-android.cmd" successfully but when come to "run.cmd" it show an error like below

    cannot stat '/bin/armeabi/HelloAndroid":No such file or directory

    Can u help me on this? Am i missing any step in front? by the way, I'm using opencv revision 5886 cz i cant download 5578 as u mention in tis tutorial.

    thanks,
    wei

    ReplyDelete
  2. Hi wei,

    I miss 1 extra step on the blog, first u will need to do the following:
    right click and edit the following run.cmd line:
    %adb% push .\bin\%OUT_DIR%\%PROJECT_NAME% /data/bin/sample/%PROJECT_NAME% || GOTO end
    to
    %adb% push .\build_armeabi\%PROJECT_NAME% /data/bin/sample/%PROJECT_NAME% || GOTO end

    Next, go to opencv\android\apps\HelloAndroid\build_armeabi directory and change the opencv filename to HelloAndroid. Thank you.

    ReplyDelete
  3. Hi,

    Thanks for ur respond, I can successfully run HelloAndroid already. But I facing problem again in running d android-opencv project. When I try to run d "cmake-android.cmd", it failed and state that build.xml doesn't exist. I search the whole directory and can't find the build.xml.

    Can u help me on tis.

    Thanks,
    wei

    ReplyDelete
  4. Hi,

    Thanks for ur tutorial. I solve d issue already. I forget to change the android version in default.properties.

    thanks
    wei.

    ReplyDelete
  5. Hi,

    Sorry to bother again,probably my laz question. I don't quite understand on the last part of ur tutorial, d OPENCV-Sample project. When I try to run "..\..\scripts\build.cmd", it show fail to update android project because didnt provide argument for '--name'.

    However, I still can run d next step without error if I skip tis step. And I not sure bout create xml for tis project step. Is it I create a new xml or I put in the exist xml. Can provide a more detail instruction on the final 2 step.

    thanks,
    wei

    ReplyDelete
  6. The following lines are to recreate xml file for the opencv_sample project:

    set path=%PATH%;C:\dev\android-sdk-windows\tools
    android update project --path .

    after recreating the xml file, follow the subsequent instruction and the program will be installed to your android device.

    ReplyDelete
  7. Hello,
    First thanx for this tutorial.
    Im trying to run the OpenCV_SAMPLE in windows.
    So far i have complete successfully the previous steps but now i dont know how to run in the device this example.. the ant commands i think is only for linux..

    ReplyDelete
  8. Oops. Ok i found it. I have set the enviromental variable for the ANT. Another thing if you can help.. I want to run the CVCamera sample but it dont have a cmake file. Is there anyway to do it?
    Thanx

    ReplyDelete
  9. copy the following into cmake_andoid.cmd in the same directory of CVCamera:

    @ECHO OFF
    SETLOCAL
    PUSHD %~dp0
    SET PROJECT_NAME=CVCamera
    CALL ..\..\scripts\build.cmd %*
    POPD
    ENDLOCAL

    then execute it from command prompt follow by ant compile, ant install =)

    ReplyDelete

Post a Comment

Popular posts from this blog

Simplex implementation in Java with sample code

Tutorial on Connecting Java application to IBM DB2 Express-C edition (Windows)