15 May 2011

OpenCV in Android

Using OpenCV in Android. This tutorial is tested under Ubuntu 10.04 + Android SDKr07 + Android NDKr4b.



Preparing the development environment

  • Download and install Android SDK. Details can be found here
  • Download eclipse and install the ADT plugin. Details can be found here
  • Download Androdi NDK. This tool is used to cross compile OpenCV source code to Android. Currently (NDK r4) only C is fully supported, so I can only use OpenCV 1.1 under Android. The laterst version of OpenCV uses lots of STL functions. 

Create the test project

  • Create a new Android project in eclipse. For example, called testOpenCV. Name the package name as: edu.stanford.android.
  • In the root directory of the project, create a new folder called jni and extract all files in android_opencv.tar.gz to this folder.
  • Run “$NDK/ndk-build” from your project directory. It will generate libopencv.so in the libs/armeabi folder.
  • Write Java code to use OpenCV functions. I have three java files which can be downloaded here. They are a little bit long so I do not want to paste the source code here. The general idea is that I use the intent to start the camera or gallery activities to get the image and send this image to OpenCV. After OpenCV finishes extracting SURF features, it send the processed image back to JVM. The interface between JVM and OpenCV is pretty simple: setSourceImage and getSourceImage.

Run the program

  • Press the menu button to select your camera to capture an image. The down-sampled image is saved in your gallery. Press the menu button again to select it from your gallery and features are automatically extracted.
  • Notice: if you select a large image in the gallery, the program may crash due to limited memory.
  • On my DROID phone, it usually takes 4 seconds to extract features from one image.

2 comments:

  1. the link(create test project-line2) is broken, please help. its urgent

    ReplyDelete
    Replies
    1. I did this long time ago, and presently I don't have the link. But as far as I know, you don't need this anymore. OpenCV provides examples which work on android as well. I wrote this when there was no help on opencv in android. Now it has become pretty developed. Try checking out the samples.

      Delete