Friday 8 January 2016

Android OpenCV Setup


There is a lot of crazy stuff that one can do with the power of smartphone. One among them is Computer Vision, making the phones able to capture, analyses, modify images in real time gives the device a an ability to interpret the images and take decisions based up on them. Some examples of it would be Face detections, gesture detection etc.Now comes the question.
How can I do it on my android phone?
Well the best way to implement Computer Vision On android is using OpenCV Library for Android.You can learn more about OpenCV here.

 I'm going to help you learn how to use OpenCV library  for android within your Android Studio project. To begin with the tutorial I assume you already have a working Android Studio with the SDK and NDK setup.

1. Downloading OpenCv4android.
The first thing you need to do is download the latest stable opencv4android by clicking here.  I have downloaded the version 3.0 as shown in the following image. Notice the url and the version displayed in the image. Download the one compatible for your system(i.e Android)

 

Once you have downloaded the zip extract it to a specific location in you computer. Now open the folder you have extracted you will see a sdk,  samples folder and some other folders and files.

2. Compiling the OpenCv SDK.
As  OpenCv is written in C,C++ one has to compile it before use on the local machine thus now we have  compile the SDK  to do so make a copy of the java folder(which is in SDK folder) to another location and rename it to OpenCv.
Now go to Android studio Create a New project then go to file->new-> import module
select the folder we just renamed if every thing goes well you will be able to see something like the following image


Before you import make sure android NDK is configured properly to check that you can go to 
File-> Project Structure.. then when a new window pops up click on SDK Location in Left side panel as in the following image

 

check the path in the android ndk location refers to the right directory. Now in the same window click on the app module and check whether the OpenCv dependence is added to it as in the following image

 

 if this how it looks you are ready to start using the OpenCv library. If you dont see this that ok you can  add it using the + sign or you can go to the app.gradle file and in dependencies you can write the following in build.gradle(app)

dependencies {
       .... 
     compile project(':opencv')
}
 
make sure that the build and target version of the app and OpenCv Module must be Same you can check them in the gradle files.

3. Starting With Code
Now as we are all set with the setup :) we will start with the code. In the main activity class of the app we created all you have to do is write the following code in order to initilize the OpenCv library .
____________________________________________________________________________________
 
private BaseLoaderCallback  mLoaderCallback = new BaseLoaderCallback(this) {
    @Override    public void onManagerConnected(int status) {
        switch (status) {
            case LoaderCallbackInterface.SUCCESS:
            {
                Log.i(TAG, "OpenCV loaded successfully");
            } break;
            default:
            {
                super.onManagerConnected(status);
            } break;
        }
    }
};

@Override public void onResume()
{
    super.onResume();
    if (!OpenCVLoader.initDebug()) {
        Log.d(TAG, "Using OpenCV Manager for initialization");
        OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_0_0, this, mLoaderCallback);
    } else {
        //...
    }
}
 
___________________________________________________________________________________

If this code does not show any error you good to go and run the app. As the app starts it will show you a popup message stating that the OpenCv manager is not present and would you like to download it say yes and it will take you to play store download it and your app will run sucessfully.
you can check the logcat for the log entries we made in the functions.

Now you can also import the samples provided in the Samples folder downloaded along with the OpenCv SDK and play arround with it. for more tutorials on OpenCv visit this link.

Using the Async Initialization(the method mentioned above) is a recommended way but there is a another way as well Static Initialization of OpenCv which is my Next post.

Please do leave a comment if you felt it was helpful.

No comments:

Post a Comment