In this tutorial, we will create a new OpenCV project in Android Studio. Since you have everything downloaded already, we are ready to move on. By the end of this tutorial, you will have a new project set up, which you can use to build your projects upon. So lets do some CV!
Note: This tutorial has been tested on Android Studio 1.5 and 2.0
In short, the steps are, (2) Create simple project. (3) Import OpenCV SDK as a Module in Android Studio. (4) Set OpenCV Version. in Project (5) Fix Library association in project. (6) Done! Now actual OpenCV code can be written in future. The longer and detailed guided tutorial is given below.
I beg you to open Android studio.
Create a new project
I named it “OpenCV_Test”. (It could be different in the following GIF)
Minimum SDK is “API 19: Android 4.4 (KitKat).
Default Activity is “Basic Activity” (easily changeable later on)
Activity name is “MainActivity” (i.e. default settings)
I have shown the entire process in this GIF.
After pressing Finish, Gradle will perform some processes and eventually, you will be presented with your Android project.
Now, we can start importing OpenCV into the project.
Import the OpenCV for Android SDK module in Android Studio.
To import the OpenCV SDK as a module in your project, go to File>New>Import Module.
Then Give path to your OpenCV as ..\OpenCV-android-sdk\sdk\java
You can chose to modify your module’s name.
Press Next and Finish the Dialogue.
The entire process is given here:
Note that in the end, there are some errors in the code as well as the console. This is because you need to fix the Version number and link the libraries with your own project. This is shown in the next two steps.
In this step, we will fix the Android Project SDK information in the OpenCV build.gradle file.
In project explorer, switch to Project view.
Go to OpenCVLibrary300 > build.gradle file and open it.
I have updated the contents of the file with the following code
Note that the android project’s SDK information can be obtained from the project’s own build.gradle file.
Once the Android SDK settings are updated, press “Try Again” button.
This entire process is given below:
Now. we need to fix library association.
Looking good so far! We now need to make sure that the OpenCV for Android libraries are associated with our Android Studio project. Currently, Android Studio will not recognize the OpenCV code that we type in our files. We fix this as follows.
Go to File>Project Structure.
When window opens, under Modules, select app.
Select Module Dependencies.
(In my case) Select :openCVLibrary300.
Press Ok and Ok.
After a few seconds, the libraries will be associated with your Android project.
This entire process is shown here:
You can now write more OpenCV code in future in the same project.
We did it! Project is ready for future OpenCV development.
This concludes this tutorial. You are now ready to start making your own Android Studio Project that consists of OpenCV SDK features limited only by your imagination. In the next tutorial, I will SHOW CAMERA on android app screen using OpenCV. Make sure that you read it. Do not forget to SHARE this post and write your opinions, ideas and views in the COMMENT section below.
Thanks for reading :). Please like my Facebook page and follow my Twitter for more tutorials.
In the previous tutorial, you learnt about the file structure of OpenCV SDK for Android. Now, we will learn the packages which are available for us to use in OpenCV SDK for Android. At the end of this tutorial, you will definitely know what OpenCV for android has to offer.
Each packages has java classes which contain functions which you use.
First of all, lets find the packages in the sdk folder manually. On your computer, once you have downloaded the OpenCV SDK, you can browse to the following address in order to find all the OpenCV for Android classes.
There, you will find the following packages (OpenCV 3.0):
Also, when you have created an OpenCV project in Android Studio, you will find the packages in the project browser like this:
Now, I will explain to you the purpose of each and every package so that you learn about OpenCV without any effort :D. You won’t regret coming on my blog 🙂
Note that each package can be imported like this as I told before:
OpenCV interaction with Android platform
This package has functions which can allow you to implement an OpenCV project in your Android device. This package handles interactions between OpenCV and Your phone.
Project 3D coordinates of objects in a scene.
“calib3d” stands for Camera Calibration and 3D Reconstruction. This package has functions which can determine 3D coordinates of objects. So lets say you have a box in a picture. calib3D functions can project 3D coordinates of that box if you provide “intrinsic” and “extrinsic” coordinates.
There are functions to estimate intrinsic and extrinsic parameters 🙂
Brain of OpenCV. Does all what OpenCV does elsewhere.
“core” is the core package of OpenCV. This package contains functions which provide the base functionality of OpenCV. This include mathematical and matrix operations, algorithmic procedures and much more.
This library is not responsible for image processing. That library is called “imgproc” and it is discussed below.
Provides basic information about current OpenCV SDK installation in your project
Basically, if in your project, you wish to obrain basic information about your OpenCV SDK such as its packages, version, SDK path, and list of libraries.
Moreover, you can specify a specific version of OpenCV that user’s phone should download from play store. Pretty cool, right?
Detect and track features in a a 2D test image/frame
You will use this library to detect features, draw them and track them on the screen.
Read/write images from your disc
Well, imagine you wish to work on an image that is in your disk instead of receiving it from camera. In this case, you will use this library. Its functions allow you to do the following:
Read single image
Read multiple images
Write an image (output)
Read specific formats of images in different ways to suit your project and purpose (encode/decode)
Image Processing. DUH!
Whether you have loaded an image, or are using camera as input, you will be needing OpenCV to process the image. Well, “imgproc” is the library that has all the functions that you can use, manipulate and exploit to your liking.
This library has functions and classes which allow you to blur image, draw shapes, compare shapes, write text on image, apply enhancement functions and much, much, much more!
This library also has functions which detect points of interest of your liking.
Machine Learning (Artificial Intelligence)
You can train OpenCV to do do Computer Vision according to your requirements. All you need to do is to teach it.
This library has classes and functions that train OpenCV to perform computer vision job for you 🙂
Detect features or an object
This library has functions that give you the ability to implement detection of a specific custom-defined object in an image or camera feed.
Ever heard of Haar like feature detection? You can do that in this library.
This library handles Image enhancement.
Want to set ISO? Exposure setting? contrast? Saturation? hue? Shit? Well, this library has all these functionalities and much more! You can calibrate so much.
One interesting feature is de-noising which will let you play with noise in your image ;D. Useful, right?
Compatibility converters to support different formats of data
This library has functions that convert image data from one mathematical format into another. For example, you can convert from one matrix format into another for image processing using a specifically designed code. It is a life saver.
This library will help you to perform video processing. You can track objects in a video, subtract background, apply video filters and much more.
Interact with video files
Using this library, you can open video files, open different format of video files and work with them.
This tutorial was brief and was supposed to give you the starting knowledge in order to grow your knowledge about OpenCV by yourself. Let me know in the comments what you think. I will be glad to answer. Now proceed to hte next tutorial 🙂
In this tutorial, you will learn about the file structure of OpenCV SDK for Android. The information here is very short and lacks details. My purpose here is to provide just enough information to get going with the rest of the tutorials. I highly recommend reading more about the folders, algorithms, libraries and classes on your own.
So you have downloaded SDK but you may be curious what the SDK files do. Lets see to that now. You can skip this tutorial if you are interested in going directly to the development.
When you extract the “OpenCV-3.0.0-android-sdk-1.zip” from the precious tutorial, you will get a folder /OpenCV-android-sdk/.
Inside the /OpenCV-android-sdk/ folder, you will see:
Lets look at these in detail.
This folder contains OpenCV manager API for different android device architectures. Chose the .apk file which is supported by your android device. Or simply download one from Google Play Store because that way, you will be downloading the apk file that is supported by your device/
This folder contains sample android apps that you can install into your android device to test features of OpenCV. Also note that the source code of each sample is included in the /samples/ folder. It is a good place to start.
THE BRAINS OF OpenCV is here. I will explain about this folder below.
Well, it can’t be said more precisely than what is written there as “By downloading, copying, installing or using the software you agree to this license.If you do not agree to this license, do not download, install,
copy or use the software.”
Contains a link to online documentation, resources and feedback. Quiet handy!
Now that the root file structure of OpenCV is out of the way, we can now take a look at the SDK folder contents to give you the idea of what exactly what it and its contents do.
The /sdk/ folder contains the OpenCV API and libraries that will be used in your android project. You need these to perform all the functionalities that OpenCV offers in order to help you perform your Computer Vision related job.
Inside the SDK folder, in my case, there are three folders:
Lets look at the contents of SDK folder here:
This folder contains the “memory” of OpenCV. Memory being like the brain-memory :D. See, as the time passed, the geniuses who made OpenCV for us to feast upon collected data and fine-tuned it for algorithms to use. That data is kept here.
For example, face detection requires some data which is compared with the picture snapped by your x-megapixel camera. That data is kept inside this folder.
HAAR or HAAR-like-features is a folder where you put post-OpenCV-training data.
In this folder, you can find data files which contain data generated by training OpenCV in order to detect face, eyes, nose etc detection.
You too can create such data files if you wish to detect something such as legs, airplane, cracks on wall (edge detection), pacman, ****, *******, *******888*88* detection 😛
Lets say you wish to train OpenCV to detect face of a “sick” person. So you take pictures of a (e.g.) 1000 sick people (positive images) and then resize those picture to a same small size that is easy to process in bulk :D. Now you will also need pictures of people who are NOT sick. Now, you will train OpenCV on that load of 1000 pictures plus the people who are not sick. This data is put in this folder to be used.
HAAR algorithm is very accurate but is slower as compared to LBP.
LBP stands for local binary pattern. This method is unique in a way that instead of using generated data to detect features (as was the case of HAAR), the LBP takes a pixel and finds the intensity of its neighbor pixels.
So lets say there is a pixel. Practically, each pixel has eight neighbor pixels, so our pixel also has eight surrounding pixels. Now, for each pixel, a binary value is obtained. The value of binary number depends on the comparison between the center pixel and its test-neighbor pixel.
If pixel intensity is greater than center pixel, then value is 1.
If pixel intensity is lesser than center pixel, then value is 0.
LBP algorithm is very fast, but least accurate.
When creating a new Android project, you can import the OpenCV Java Api from this folder.
Contains settings that are implemented when importing the OpenCV for android Java API.
Contains generated files. For example, R.java, when generated is put here.
This folder contains the documentation of OpenCV for Android. In my case, it is the documentation for the version 3.0.0
All the classes of OpenCV for Android are explained here. So if you ever find yourself in trouble, or wish to learn more, visit this folder.
Standard Android project folder that contains resources to be used in an android project. The resource file that came in my case contains camera information.
The /etc/ folder was the memory of OpenCV. But this folder is the actual brain of OpenCV. This folder contains the classes that perform all the functionalities of OpenCV in Android. Classes are written in Java.
Root files are self explainatory. If not, then what on Earth are you doing on this post? You should learn Android and then come here.
Exactly this folder contains the classes. Core, mathematical operation algorithm, training and all the shit that OpenCV gets done for you, is actually here. Every improvement in OpenCV is included here. Please respect the contents of this folder if you use OpenCV.
This folder contains C++ .h (header) files and native libraries for multiple android architectures.
This is it for this tutorial. I have explained to you the structure of the OpenCV for Android SDK file structure. In the next tutorial, I will explin to you about the classes available in OpenCV for Android SDK which you can use.