Getting Emgu CV for Android

Differences between Emgu CV for Android and Desktop version

The System.Drawing namespace is provided by Mono.Android package.

Mono for Android use "Android.Graphic.Bitmap" instead of "System.Drawing.Bitmap".

The Emgu.CV.GPU namespace is only provided for compilation against shared code developed for desktop system that optionally use GPU computation. The function GpuInvoke.HasCuda will always return false on Android. If you are doing a fresh development for Android you can skip this reference.

Creating a Emgu CV for Android Project

We will show you how to create a new MonoTouch Project for iOS.

First, download Emgu CV for Android and extract it to a local drive. We are going to use Visual Studio 2010 with Mono for Android plugin. Open the file "Solution/Android/Emgu.CV.Android.sln". You will find an Android solution with some sample projects. In this case we will walk you through creating a new SURF detector sample.

Add a new Android Application project and we will name the project SURFFeature

Add references to the project. The following references are needed for SURFFeature:

Emgu.Util.Android, Emgu.CV.Android, Emgu.CV.GPU.Android, OpenTK

We need Emgu.CV.GPU.Android here because we are going to share the engine source code with Windows and Linux, which can take advantage of GPU if CUDA compatible one is presented. If you do not plan to reuse source code that take advantage of GPU on desktop/server platform, you can skip this reference. We will also need to reference OpenTK because this reference provide System.Drawing namespace on Mono for Android.

Now create a folder call lib. Under which create the folders: armeabi, armeabi-v7a and x86.

Right click on the newly created armeabi subfolder, Add->Existing Items, browse to the libs\armeabi folder under the Emgu CV for Android package. Select all the .so file and Click "Add as Link". All the .so files will be added to the project. Now highlight all files, go to the "property" panel, select "AndroidNativeLibrary" as Build Action.

Right click on the armeabi-v7a subfolder, Add->Existing Items, browse to the libs\armeabi-v7a folder under the Emgu CV for Android package. Select all the .so file and Click "Add as Link". All the .so files will be added to the project. Now highlight all files, go to the "property" panel, select "AndroidNativeLibrary" as Build Action.

Right click on the x86 subfolder, Add->Existing Items, browse to the libs\x86 folder under the Emgu CV for Android package. Select all the .so file and Click "Add as Link". All the .so files will be added to the project. Now highlight all files, go to the "property" panel, select "AndroidNativeLibrary" as Build Action.

Right click on the SURFFeature project and select Property. Here we can check all the Supported architectures: armeabi, armeabi-v7 and x86.

Create the Android GUI with a single button and an image view.

Add the two files "box.png" and "box_in_scene.png" as Android Assets.

Add the "DrawMatches.cs" file from the Windows SURFFeature example to the project, we are going to re-use the same code for Android.

Now go to the Activity1.cs file and we will implement the GUI components. In the top of the source code, add the following using statements:

base.OnCreate(bundle);// Set our view from the "main" layout resourceSetContentView(Resource.Layout.Main);// Get our button from the layout resource,// and attach an event to itButtonbutton=FindViewById<Button>(Resource.Id.MatchButton);ImageViewimageView=FindViewById<ImageView>(Resource.Id.ResultImageView);TextViewmessageView=FindViewById<TextView>(Resource.Id.MessageView);button.Click+=delegate{longtime;using(Image<Gray,Byte>box=newImage<Gray,byte>(Assets,"box.png"))using(Image<Gray,Byte>boxInScene=newImage<Gray,byte>(Assets,"box_in_scene.png"))using(Image<Bgr,Byte>result=DrawMatches.Draw(box,boxInScene,outtime)){messageView.Text=String.Format("Matched in {0} milliseconds.",time);imageView.SetImageBitmap(result.ToBitmap());}};

We are ready to test the SURFFeature program on Simulator. Just hit "F5" and the program will be build, deploy and run on the simulator. The result of our program will be displayed.

Now we are ready to test it on a real device. I am using a Google Nexus S and I connect my cell phone to the my work station, deploy the program in release mode on the device and here it is: