Saturday, September 11, 2010

Compile OpenCV for Android on Linux or Mac, high level hints

Android's market share is being the most growing among the Operating Systems for smartphones.

OpenCV is (from Wikipediaa computer vision library originally developed by Intel. It is free for use under the open source BSD license. The library is cross-platform. It focuses mainly on real-time image processing.


http://sourceforge.net/projects/opencvlibrary/

The initial revision of OpenCV was in C, with the latest revision C++ has been introduced in many sections of the code.

Android NDK is the developer toolkit to plug C and C++ code into an Android application, whose default implementation language is Java (which works upon the Dalvik virtual machine, created for Android).
It must be used together with the Android SDK, which is the development framework for you Android/Java applications.
Your C++ code should be used as a library which communicates (through JNI, Java Native Interface) to the Java layer.


The main problem in order to get OpenCV compiling on Android relies on the fact that the Android NDK doesn't provide a full standard C++ ("Minimal set of headers for C++ support"), for example exceptions can't be used in the Android C++. It's probably a way to discourage people from using C++ as a native language for the Android applications. Or maybe it's because Google doesn't like C++ exceptions, especially in an embedded system context.

Luckily, there are some guys who got full C++ working for Android, you just have to download it and compile.
To use that, you have to download a tweaked C++ Android NDK, from Crystax site:

What you finally need is also OpenCV, ported to Android

Now you have to install the "Crystax" Android NDK as it was the official one, following the related guide:

You have to compile OpenCV with the Android NDK, considering it as a NDK application (check the samples in the ndk directory). The OpenCV for Android zip already contains the files/scripts to be compiled in that way.
If you want to embed your custom C++ code in it, you can easily do that, modifying the Android.mk file and restructuring the hierarchy of the directories in your project.
In my case, I simply added a subdirectory to the OpenCV one, moved there all my source code, and compiled it together with OpenCV.

Final note: remember 
APP_ABI := armeabi armeabi-v7a
in the file Applications.mk if your mobile cpu supports it; your application will run much faster!


2 comments:

JD said...

Hi,
I read your article. I currently ported OpenCV to Android using the following link:

http://opencv.itseez.com/doc/tutorials/introduction/android_binary_package/android_binary_package.html

But I have to use some JNI mechnasim to convert OpenCV C/C++ code into Java which I don't like.

I currently use OpenCV 2.3. I am using Nvidia Tegra Android Development Package for android. Will your method work for OpenCV 2.3 also? If I follow your method can I directly execute my C/C++ OpenCV codes in Android??

Please get back to me ASAP.

Looking forward to your positive response.

Cheers,
Jayneil

Marco Bonifazi said...

I'm successfully using OpenCV 2.3 in Android and iOS on ARM cpu without any problems. I don't have any clue of other platforms, I guess that the portability is generally guaranteed but it depends on the feature your are working on.
For instance, I remember an issue on ARM cpu regarding cvLoadImage function which have been fixed later on.