Emgu CV for Android

From Emgu CV: OpenCV in .NET (C#, VB, C++ and more)
Jump to navigation Jump to search

Prerequisite

You will need Xamarin Android Bussiness (or Enterprise) version for development. Click here to purchase a license or download an evaluation copy.

Getting Emgu CV for Android

Emgu CV for Android commercial license 3.0 is available for purchase from here. It is also available as part of the Multi-platform License 3.0.

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.

EmguCvAndroidSolution.png

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

EmguCvAndroidCreateProject.png

  • 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.

EmguCvAndroidProjectReference.png

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

EmguCvAndroidCreateLibFolders.png

  • 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.

EmguCvAndroidAddSoFiles.png

  • 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.

EmguCvAndroidProjectProperties.png

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

EmguCvAndroidCreateGUI.png

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

EmguCvAndroidAddAssets.png

  • 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:
using System;
using System.IO;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Graphics;

using Emgu.CV;
using Emgu.CV.Structure;

Add the following code in the body of the OnCreate function:

         base.OnCreate(bundle);

         // Set our view from the "main" layout resource
         SetContentView(Resource.Layout.Main);

         // Get our button from the layout resource,
         // and attach an event to it
         Button button = FindViewById<Button>(Resource.Id.MatchButton);
         ImageView imageView = FindViewById<ImageView>(Resource.Id.ResultImageView);
         TextView messageView = FindViewById<TextView>(Resource.Id.MessageView);

         button.Click += delegate 
         {
            long time;
            using (Image<Gray, Byte> box = new Image<Gray,byte>(Assets, "box.png"))
            using (Image<Gray, Byte> boxInScene = new Image<Gray, byte>(Assets, "box_in_scene.png"))
            using (Image<Bgr, Byte> result = DrawMatches.Draw(box, boxInScene, out time))
            {
               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.

MonoAndroidSURFFeatureResultSimulator.png

  • 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:

MonoAndroidSURFFeatureResultNexusS.jpg