Mastering OpenCV Android Application Programming - Sample Chapter
Mastering OpenCV Android Application Programming - Sample Chapter
ee
Sa
pl
He has a passion for programming and is always excited to try out new technologies.
His interests lie in computer vision, networks, and developing scalable systems.
He is an open source enthusiast and has contributed to libraries such as SimpleCV,
BinPy, and Krita.
When he is not working, he spends most of his time on Quora and Hacker
News. He loves to play basketball and ultimate frisbee. He can be reached at
[email protected].
Preface
This book will help you get started with OpenCV on the Android platform in no
time. It explains the various computer vision algorithms conceptually, as well as
their implementation on the Android platform. This book is an invaluable resource
if you are looking forward to implementing computer vision modules on new or
existing Android apps.
Preface
Chapter 7, Bringing Your Apps to Life with OpenCV Machine Learning, explains
how machine learning can be used in computer vision applications. In this
chapter, we take a look at some common machine learning algorithms and their
implementation in Android.
Chapter 8, Troubleshooting and Best Practices, covers some of the common errors and
issues that developers face while building their applications. It also unfolds some
good practices that can make the application more efficient.
Chapter 9, Developing a Document Scanning App, uses various algorithms that
have been explained across various chapters to build a complete system to scan
documents, regardless of what angle you click the image at.
Blurring
De-noising
Sharpening
At the end of this chapter, we will see how you can integrate OpenCV into your
existing Android applications.
Before we take a look at the various feature detection algorithms and their
implementations, let's first build a basic Android application to which we will
keep adding feature detection algorithms, as we go through this chapter.
[1]
Getting started
When we see an image, we perceive it as colors and objects. However, a computer
vision system sees it as a matrix of numbers (see the following image). These
numbers are interpreted differently, depending on the color model used. The
computer cannot directly detect patterns or objects in the image. The aim of
computer vision systems is to interpret this matrix of numbers as an object of a
particular type.
Setting up OpenCV
OpenCV is the short form of Open Source Computer Vision library. It is the most
widely used computer vision library. It is a collection of commonly used functions
that perform operations related to computer vision. OpenCV has been natively
written in C/C++, but has wrappers for Python, Java, and any JVM language,
which is designed to create the Java byte code, such as Scala and Clojure. Since most
of the Android app development is done in C++/Java, OpenCV has also been ported
as an SDK that developers can use to implement it in their apps and make them
vision enabled.
[2]
Chapter 1
We will now take a look at how to get started with setting up OpenCV for the
Android platform, and start our journey. We will use Android Studio as our IDE of
choice, but any other IDE should work just as well with slight modifications. Follow
these steps in order to get started:
1. Download Android Studio from https://developer.android.com/sdk/
and OpenCV4Android SDK from http://sourceforge.net/projects/
opencvlibrary/files/opencv-android/.
2. Extract the two files to a known location.
3. Create a normal Android Project and name it FirstOpenCVApp. Navigate to
File | Import.
4. Select the OpenCV_SDK_location/sdk/java/ directory.
5. Navigate to Build | Rebuild Project.
6. Navigate to File | Project Structure.
7. Add the OpenCV module to your app by selecting the app module in the left
column. Click on the green in the dependencies tab, and finally, select the
OpenCV module.
8. You are now ready to use OpenCV in your Android project. It should look
like this:
[3]
In the grayscale image, the numbers represent the intensity of that particular color.
They are represented on a scale of 0-255 when using integer representations, with 0
being pure black and 255 being pure white. If we use a floating point representation,
the pixels are represented on a scale of 0-1, with 0 being pure black and 1 being pure
white. In an RGB image in OpenCV, the first channel corresponds to blue color,
second channel corresponds to green color, and the third channel corresponds to
red color. Thus, each channel represents the intensity of any particular color. As we
know that red, green, and blue are primary colors, they can be combined in different
proportions to generate any color visible to the human eye. The following figure
shows the different colors and their respective RGB equivalents in an integer format:
[4]
Chapter 1
(a )
(b)
(d)
(c)
R =0 G =0 B=0
R =10 G = 20 B =100
R = 255 G = 255 B = 0
R = 255 G = 0 B = 0
Now that we have seen how an image is represented in computing terms, we will see
how we can modify the pixel values so that they need less computation time when
using them for the actual task at hand.
[5]
1) A,Bis location of
dst ( x,y ) =
anchor pixel
2) Range of i,jdepends on
anchor pixel
One of the most common uses of linear filtering is to remove the noise. Noise is the
random variation in brightness or color information in images. We use blurring
operations to reduce the noise in images.
[6]
Chapter 1
Add a new activity by right-clicking on the Java folder and navigate to New |
Activity. Then, select Blank Activity. Name the activity MainActivity.java and
the XML file activity_main.xml. Go to res/menu/menu_main.xml. Add an
item as follows:
<item android:id="@+id/action_load_image"
android:title="@string/action_load_image"
android:orderInCategory="1"
android:showAsAction="ifRoom" />
Since MainActivity is the activity that we will be using to perform our OpenCV
specific tasks, we need to instantiate OpenCV. Add this as a global member of
MainActivity.java:
private BaseLoaderCallback mOpenCVCallBack = new
BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS:
//DO YOUR WORK/STUFF HERE
break;
default:
super.onManagerConnected(status);
break;
}
[7]
[8]
Chapter 1
In the preceding code, MEAN_BLUR is a constant with value 1 that specifies the type of
operation that we want to perform.
Here we have added extra to the activity bundle. This is to differentiate which
operation we will be performing.
Open activity_main.xml. Replace everything with this code snippet. This
snippet adds two ImageView items: one for the original image and one for the
processed image:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/
android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:id="@+id/ivImage" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:id="@+id/ivImageProcessed" />
</LinearLayout>
[9]
Here, the Mat and ImageViews have been made global to the class so that we can
use them in other functions, without passing them as parameters. We will use the
ACTION_MODE variable to identify the required operation to be performed.
Now we will add the code to load an image from the gallery. For this, we will use
the menu button we created earlier. We will load the menu_main.xml file, when you
click on the menu button:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
Then we will add the listener that will perform the desired action when an action
item is selected. We will use Intent.ACTION_PICK to get an image from the gallery:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_load_image) {
Intent photoPickerIntent = new
Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent,
SELECT_PHOTO);
return true;
}
return super.onOptionsItemSelected(item);
}
[ 10 ]
Chapter 1
To apply mean blur to an image, we use the OpenCV provided function blur(). We
have used a 3 x 3 kernel for this purpose:
case HomeActivity.MEAN_BLUR:
Imgproc.blur(src, src, new Size(3,3));
break;
[ 11 ]
Now we will set this image in an ImageView to see the results of the operation:
Bitmap processedImage = Bitmap.createBitmap(src.cols(),
src.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(src, processedImage);
ivImage.setImageBitmap(selectedImage);
ivImageProcessed.setImageBitmap(processedImage);
Original Image (Left) and Image after applying Mean Blur (Right)
f ( x) =
2
( x ) / ( 2 2 )
1
e
2
[ 12 ]
Chapter 1
The anchor pixel is considered to be at (0, 0). As we can see, the pixels closer to the
anchor pixel are given a higher weightage than those further away from it. This is
generally the ideal scenario, as the nearby pixels should influence the result of a
particular pixel more than those further away. The Gaussian kernels of size 3, 5,
and 7 are shown in the following figure (image of 'Gaussian kernels' taken
from http://www1.adept.com/main/KE/DATA/ACE/AdeptSight_User/
ImageProcessing_Operations.html):
To use the Gaussian blur in your application, OpenCV provides a built-in function
called GaussianBlur. We will use this and get the following resulting image. We
will add a new case to the same switch block we used earlier. For this code, declare a
constant GAUSSIAN_BLUR with value 2:
case HomeActivity.GAUSSIAN_BLUR:
Imgproc.GaussianBlur(src, src, new Size(3,3), 0);
break;
[ 13 ]
To apply median blur in OpenCV, we use the built-in function medianBlur. As in the
previous cases, we have to add a button and add the OnClickListener functions.
We will add another case condition for this operation:
case HomeActivity.MEDIAN_BLUR:
Imgproc.medianBlur(src, src, 3);
break;
[ 14 ]
Chapter 1
-1
-1
-1
-1
[ 15 ]
Here we have given the image depth as 16SC1. This means that each pixel in our
image contains a 16-bit signed integer (16S) and the image has 1 channel (C1).
Now we will use the filter2D()function, which performs the actual convolution
when given the input image and a kernel. We will show the image in an ImageView.
We will add another case to the switch block created earlier:
Imgproc.filter2D(src, src, src.depth(), kernel);
Morphological operations
Morphological operations are a set of operations that process an image based on the
features of the image and a structuring element. These generally work on binary or
grayscale images. We will take a look at some basic morphological operations before
moving on to more advance ones.
Dilation
Dilation is a method by which the bright regions of an image are expanded. To
achieve this, we take a kernel of the desired size and replace the anchor pixel with
the maximum value overlapped by the kernel. Dilation can be used to merge objects
that might have been broken off.
[ 16 ]
Chapter 1
A binary image (left) and the result after applying dilation (right)
Erosion
Similarly, erosion is a method by which the dark regions of an image are expanded.
To achieve this, we take a kernel of the desired size and replace the anchor pixel by
the minimum value overlapped by the kernel. Erosion can be used to remove the
noise from images.
A binary image (left) and the result after applying erosion (right)
[ 18 ]
Chapter 1
Thresholding
Thresholding is the method of segmenting out sections of an image that we would
like to analyze. The value of each pixel is compared to a predefined threshold value
and based on this result, we modify the value of the pixel. OpenCV provides five
types of thresholding operations.
To perform thresholding, we will use the following code as a template and
change the parameters as per the kind of thresholding required. We need to replace
THRESH_CONSTANT with the constant for the required method of thresholding:
case HomeActivity.THRESHOLD:
Imgproc.threshold(src, src, 100, 255,
Imgproc.THRESH_CONSTANT);
break;
Here, 100 is the threshold value and 255 is the maximum value (the value of
pure white).
The constants are listed in the following table:
Thresholding
Method Name
Thresholding Function
Binary threshold
dst(x,y)
dst(x,y)
Threshold to zero
Truncate
Binary threshold,
inverted
dst(x,y)
0; if src(x,y)> thresh
maxVal; otherwise
Threshold to
zero, inverted
dst(x,y)
0; if src(x,y)> thresh
src(x,y); otherwise
[ 19 ]
Constant
THRESH_BINARY
THRESH_TOZERO
THRESH_TRUNC
THRESH_BINARY_INV
THRESH_TOZERO_INV
org/trunk/d7/d4d/tutorial_py_thresholding.html:
Adaptive thresholding
Setting a global threshold value may not be the best option when performing
segmentation. Lighting conditions affect the intensity of pixels. So, to overcome
this limitation, we will try to calculate the threshold value for any pixel based on its
neighboring pixels.
We will use three parameters to calculate the adaptive threshold of an image:
1. Adaptive method: The following are the two methods we will use:
neighboring pixels
[ 20 ]
Chapter 1
Original image (left) and image after applying Adaptive thresholding (right)
Here, the resulting image has a lot of noise present. This can be avoided by
applying a blurring operation before applying adaptive thresholding, so as to
smooth the image.
Summary
In this chapter, we have learnt how to get started with using OpenCV in your
Android project. Then we looked at different filters in image processing, especially
linear filters, and how they can be implemented on an Android device. These filters
will later form the basis of any computer vision application that you try to build. In
the following chapters, we will look at more complex image filters, and also see how
to extract information from the images in the form of edges, corners, and the like.
[ 21 ]
www.PacktPub.com
Stay Connected: