Book Image

Android Application Programming with OpenCV 3

By : Joseph Howse
Book Image

Android Application Programming with OpenCV 3

By: Joseph Howse

Overview of this book

<p>Android Application Programming with OpenCV 3 is a practical, hands-on guide to computer vision and mobile app development. It shows how to capture, manipulate, and analyze images while building an application that combines photography and augmented reality. To help the reader become a well-rounded developer, the book covers OpenCV (a computer vision library), Android SDK (a mobile app framework), OpenGL ES (a 3D graphics framework), and even JNI (a Java/C++ interoperability layer).</p> <p>Now in its second edition, the book offers thoroughly reviewed code, instructions, and explanations. It is fully updated to support OpenCV 3 and Android 5, as well as earlier versions. Although it focuses on OpenCV's Java bindings, this edition adds an extensive chapter on JNI and C++, so that the reader is well primed to use OpenCV in other environments.</p>
Table of Contents (13 chapters)
Android Application Programming with OpenCV 3
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Setting up a development environment


We are going to install various components of a development environment separately and configure them to work together. Broadly, this task has the following two stages:

  1. Set up a general-purpose Android development environment.

  2. Set up OpenCV for use in this environment. We may use a prepackaged, preconfigured version of OpenCV, or alternatively, we may configure and build OpenCV from source.

Let's start by looking at the setup steps for a general-purpose Android development environment. We will not delve into very much detail here because good instructions are available at the given links and, as an Android or Java developer, you have probably been through similar steps before.

Tip

If you already have an Android development environment or another Java development environment and you just want to add components to it, some of the following steps might not apply to you.

Here are the steps:

  1. Download and install Oracle JDK 7 from http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html. Alternatively, on Debian or Ubuntu, install Oracle JDK 7 from the WebUpd8 PPA, as described at https://launchpad.net/~webupd8team/+archive/ubuntu/java. Although most Linux distributions have OpenJDK in their standard repositories, Oracle JDK is recommended instead for Android development.

  2. Download Eclipse and unzip it to any destination, which we will refer to as <eclipse>. Many up-to-date Eclipse distributions are available at http://www.eclipse.org/downloads/. Of these, Eclipse IDE for Java Developers is a good choice as a foundation for an Android development environment.

  3. We now need to set up Android SDK and the ADT plugin for Eclipse. Go to http://developer.android.com/sdk/index.html#Other and download SDK Tools Only. Install or unzip it to any destination, which we will refer to as <android_sdk>. Open Eclipse and install the ADT plugin according to the official instructions at http://developer.android.com/sdk/installing/installing-adt.html. Restart Eclipse. A window, Welcome to Android Development, should appear. Click on Use Existing SDKs, browse to <android_sdk>, and click on Next. Close Eclipse.

  4. From the Eclipse menu system, navigate to Windows | Android SDK Manager. Select and install additional SDK packages according to the official instructions at https://developer.android.com/sdk/installing/adding-packages.html. In particular, we will need the most recent versions of the following packages: the latest Android API, such as Android 5.1.1 (API 22), Android SDK Tools, Android SDK Platform-tools, Android SDK Build Tools, and Android Support Library. After the packages are installed, close Eclipse.

  5. If we are using Windows, download and install Cygwin from http://cygwin.com/install.html.

  6. Download Android NDK from http://developer.android.com/tools/sdk/ndk/index.html. Unzip it to any destination, which we will refer to as <android_ndk>.

  7. Edit your system's Path (in Windows) or PATH (in Mac, Linux, or other Unix-like systems) to include <android_sdk>/platform-tools, <android_sdk>/tools, and <android_ndk>. Also, create an environment variable named NDKROOT and set its value to <android_ndk>. (If you are unsure how to edit Path, PATH or other environment variables, see the tips in the boxes on this page and next page.)

    Tip

    Editing environment variables on Windows

    The system's Path variable and other environment variables can be edited in the Environment Variables window of the Control Panel.

    On Windows Vista/7/8, open the Start menu and launch the Control Panel. Now, go to System and Security | System | Advanced system settings. Click on the Environment Variables button.

    On Windows XP, open the Start menu and go to Control Panel | System. Click on the Advanced tab. Click on the Environment Variables button.

    Now, under System variables, select an existing environment variable, such as Path, and click on the Edit button. Alternatively, make a new environment variable by clicking on the New button. Edit the variable's name and value as needed. For example, if we want to add C:\android-sdk\platform-tools and C:\android-sdk\tools to Path, we should append ;C:\android-sdk\platform-tools;C:\android-sdk\tools to the existing value of Path. Note the use of semicolons as separators.

    To apply the changes, click on all the OK buttons until we are back in the main window of the Control Panel. Now, log out and log in again.

    Tip

    Editing environment variables on Mac

    Edit ~/.profile.

    To append to an existing environment variable in ~/.profile, add a line such as export PATH=$PATH:~/android-sdk/platform-tools:~/android-sdk/tools. This example appends ~/android-sdk/platform-tools and ~/android-sdk/tools to PATH. Note the use of colons as separators.

    To create a new environment variable in ~/.profile, add a line such as export NDKROOT=~/android-ndk.

    Save your changes, log out, and log in again.

    Editing environment variables on Linux

    Edit either ~/.profile (as described previously for Mac) or ~/.pam_environment (as described next). Note that ~/.profile and ~/.pam_environment use slightly different formats for variables.

    To append to an existing environment variable in ~/.pam_environment, add a line such as PATH DEFAULT=${PATH}:~/android-sdk/platform-tools:~/android-sdk/tools. This example appends ~/android-sdk/platform-tools and ~/android-sdk/tools to PATH. Note the use of colons as separators.

    To create a new environment variable in ~/.pam_environment., add a line such as NDKROOT DEFAULT=~/android-ndk.

    Save your changes, log out, and log in again.

Now we have an Android development environment, but we still need OpenCV. We may choose to download a prebuilt version of OpenCV, or we may build it from source. These options are discussed in the following two subsections.

Getting prebuilt OpenCV4Android

The prebuilt versions of OpenCV4Android can be downloaded from http://sourceforge.net/projects/opencvlibrary/files/opencv-android/. Look for files that have opencv-android in the name, such as OpenCV-3.0.0-android-sdk.zip (the latest version at the time of writing). Download the latest version and unzip it to any destination, which we will refer to as <opencv>.

Building OpenCV4Android from source

Alternatively, the process for building OpenCV4Android from trunk (the latest, unstable source code) is documented at http://code.opencv.org/projects/opencv/wiki/Building_OpenCV4Android_from_trunk. For a summary of the process, continue reading this section. Otherwise, skip ahead to Building the OpenCV samples with Eclipse, later in this chapter.

Tip

Since trunk contains the latest, unstable source code, there is no guarantee that the build process will succeed. You may need to do your own troubleshooting if you want to build from trunk.

To build OpenCV from source, we need the following additional software:

  • Git: This is a Source Control Management (SCM) tool, which we will use to obtain OpenCV's source code. On Windows or Mac, download and install Git from http://git-scm.com/. On Linux, install it using your package manager. For example, on Debian or Ubuntu, open Terminal and run $ sudo apt-get install git-core.

  • CMake: This is a set of build tools. On Windows or Mac, download and install CMake from http://www.cmake.org/cmake/resources/software.html. On Linux, install it using your package manager. For example, on Debian or Ubuntu, open Terminal and run $ sudo apt-get install cmake.

  • Apache Ant 1.8.0 or greater: This is a set of build tools for Java. On Linux, just install Ant using your package manager. For example, on Debian or Ubuntu, open Terminal and run $ sudo apt-get install ant. On Windows or Mac, download Ant from http://ant.apache.org/bindownload.cgi and unzip it to any destination, which we will refer to as <ant>. Make the following changes to your environment variables:

    • Add <ant>/bin to Path (Windows) or PATH (Unix).

    • Create a variable ANT_HOME with the value <ant>.

  • Python 2.6 or greater (but not 3.0 or greater): This is a scripting language that is used by some of the OpenCV build scripts. An appropriate version of Python comes preinstalled on Mac and most Linux systems, including Debian and Ubuntu. On Windows, download and install Python from http://www.python.org/getit/. If you have installed multiple versions of Python on your system, ensure that an installation of Python 2.6 or greater (but not 3.0 or greater) is the only one in Path (Windows) or PATH (Unix). The OpenCV build scripts do not run properly with Python 3.0 or greater.

Once we have these prerequisites, we can download the OpenCV source code to any location, which we will refer to as <opencv_source>. Then, we can build it using an included script. Specifically, we should take the following steps:

On Windows, open Git Bash (Git's command prompt). On Mac, Debian, Ubuntu, or other Unix-like systems, open Terminal (or another command-line shell).

Run these commands:

$ git clone git://code.opencv.org/opencv.git <opencv_source>
$ cd <opencv_source>/platforms
$ sh ./scripts/cmake_android_arm.sh
$ cd build_android_arm
$ make -j8

The –j8 flag specifies that the make command will use 8 threads, which is typically a good number for a quad-core processor. For a dual-core processor, a better choice might be the –j4 flag (4 threads).

If all goes well, we should get a build of OpenCV4Android in <opencv_source>/platforms/build_android_arm. We can move it elsewhere if we wish. We will refer to its final location as <opencv>.

You might wonder what the cmake_android_arm.sh build script is doing. Actually, it just creates a build directory and runs a CMake command to populate the directory with a particular configuration of OpenCV. Here are the entire contents of the script file:

#!/bin/sh
cd `dirname $0`/..

mkdir -p build_android_arm
cd build_android_arm

cmake -DCMAKE_BUILD_WITH_INSTALL_RPATH=ON - DCMAKE_TOOLCHAIN_FILE=../android/android.toolchain.cmake $@ ../..

Advanced users, who are familiar with CMake, might want to copy and modify this script to create a custom configuration of OpenCV. Refer to the code in <opencv_source>/CMakeLists.txt for definitions of OpenCV's CMake options.

Note

The preceding steps use the cmake_android_arm.sh script to produce an OpenCV4Android build for ARM, which is the architecture of most Android phones and tablets. Alternatively, you can use the cmake_android_x86.sh script for x86 or the cmake_android_mips.sh script for MIPS. Note that the name of the build directory also changes according to the architecture.