CSCI1290 : Final project

OpenCV with Android/Java

sbnguyen


Goals

The main goal of this project is to explore the idea of using Android for future CS129. I want to do this for various reasons:

So for this project I set for myself these goals: I will go through my work in these order:


Approaches and evaluation

Android in general

Pros:

Cons:

Android OpenCV

Website: http://opencv.willowgarage.com/wiki/Android

This is the official Android support of OpenCV. Things are build using Android NDK, JNI and Swig.

Pros:

Cons:

After spending a few hours trying to get my SWIG config file to work, I scrapped the whole things. If it's that hard just to get a sample program working then it's not going to be much better than the N900. You have to define your method 3 times: in Java, in C++ and in SWIG. That's not fun.

JavaCV

Website: http://code.google.com/p/javacv/

This is the Java interface for OpenCV and is built on top of JavaCPP (which is created by the same guy). It works with Android and has instruction for it

Pros:

Cons:

Right now this is the most promissing approach and is the only approach that I got a working Android program. It took me about 2 full days from downloading JavaCV to get a Desktop version of Exposure Fusion working by porting C++ code from Project 5 to Java.

JavaCPP

Website: http://code.google.com/p/javacpp/

The base for JavaCV and from the same guy. JavaCPP is the interface for using C++ codes in Java. It's still JNI deep down but you don't really have to write a separate JNI class or SWIG files. What you have to do is decorate your Java file and declaration of native method and JavaCPP will generate the code for you.

Pros:

Cons:

I got the exposure fusion desktop version to work with only the addition of about 20 lines of C++ code. Getting this additional build step to work with Android doesn't seem to be too bad but I don't have to do that. I think a better documentation is better than handing in something that works but cannot be modified or replicated.

Android NDK Native activity

Android NDK now support native Activity which means you can write your apps entirely in C++. I haven't had the chance to try it but it seems feasible. No documentation from OpenCV Android though.

Pros:

Cons:


Tutorial

I will go through step by step how to setup a project that have both Android and Desktop version in Eclipse. To do this the easiest way is to create 2 separate projects: a Java one and an Android one because you cannot realy have 2 build procedure for a project and some platform specific code. Symlink will be utilized to share common code. My example is in code/javacv/ExposureFusion and code/javacpp/ExposureFusion

JavaCV

Your project now should look like this:

Note a few things:

Now try running my code and be amazed ;).

JavaCPP

  • Create Java project
  • Add JAR library
  • Add additional builder:
    • Copy the ant build file build.xml from my project to the new project
    • Right click project -> Properties
    • Builders -> New -> Ant Builder
    • Set Buildfile = ${build_project}/build.xml
    • Done
  • The ant build file will call javacpp.jar if needed everytime you try to build your project to generate JNI file. Modify it to suit your config. You are welcome.

Android result

Nothing really impressive here. I tried both different exposure time and flash/noflash


FAQ/Tips

Here are some stuffs that I think would be helpful if you want to do any of these

  • Download source code for Javacv, Javacpp and Android and attach it to your library. It can be helpful reading through source code to figure out what's going on
  • Pointer.position() is equivalent to pointer manipulation in C++. Pointer is a wrapper around memory address and p.position(x) is x bytes after p
  • I would add more if I'm not brain dead right now ...


Rants

  • If you are Travis I think you know what I went through for this project
  • If you are not Travis and you think I am a slacker and did nothing for the final project, please talk to Travis :(
  • Thank you for the class. I really enjoyed it except for the phone