Quantcast
Channel: devahead BLOG » Android
Viewing all articles
Browse latest Browse all 5

Building OpenCV for Android with libjpeg-turbo

$
0
0

Nowadays, OpenCV offers a way to install a common shared library on an Android device and use it in multiple apps without the need to build every single app with OpenCV inside it: this is called OpenCV Manager. In this way the total size of the apps is definitely smaller. Anyway, you might need to tweak OpenCV or, like I’m showing here, work with libjpeg-turbo instead of the standard libjpeg used by OpenCV, so you might need to build your own copy of OpenCV (useful also for debugging purposes).

As you know, building your own copy means having multiple choices on compilation options (for example the target Android platform) and multiple choices mean a lot of headaches while trying to setup everything correctly and finding the right options that will work for us. This process will probably be no exception, especially if you’ll also need to build libjpeg-turbo, but the good news is that you can do it! You’re a software developer for a reason, so you’re used to all of this stuff and sure this won’t scare you right? It’s time to start now!

In this tutorial I’m going to work with the following environment:

You first need to have all the necessary tools installed (Android SDK and NDK, CMake, Apache Ant, Python, Java JDK) paying attention to the versions of some of them: the NDK must be 32bit otherwise you’re going to have compilation problems with some ASM files inside OpenCV and your build will fail; Python must be 2.x and not 3.x due to compatibility problems with the 3.x version.

You need to have libjpeg-turbo compiled for Android and you can find out how to do it following my other tutorial Building libjpeg-turbo for Android.

Once you’re ready with all the tools, you can download the Linux version of OpenCV (we need the Linux version because we have to compile it), then decompress the opencv-2.4.5.tar.gz file to have the root of OpenCV in C:\OpenCV.

Make a copy of C:\OpenCV\android\scripts\wincfg.cmd.tmpl and name it wincfg.cmd keeping it in the scripts folder. Change the content of the wincfg.cmd file setting all the required variables. Here is an example:

SET ANDROID_NDK=C:\AndroidSDK\ndk32bit
SET CMAKE_EXE=C:\AndroidSDK\cmake-win32-x86\bin\cmake.exe
SET MAKE_EXE=%ANDROID_NDK%\prebuilt\windows\bin\make.exe
SET ANDROID_SDK=C:\AndroidSDK
SET ANT_DIR=C:\AndroidSDK\apache-ant
SET JAVA_HOME=C:\PROGRA~1\Java\jdk1.6.0_26
SET PYTHON_EXECUTABLE=C:\Python\python.exe
SET ANDROID_ABI=armeabi-v7a with NEON
SET BUILD_DIR=build_neon
SET ANDROID_NATIVE_API_LEVEL=8

NOTE: even though the PYTHON_EXECUTABLE variable is not present in the wincfg.cmd.tmpl template file, you need to set it anyway.

Now you’ve got a few choices to build OpenCV: it can be the release or the debug version, it can be built with the standard libjpeg or with libjpeg-turbo. We’re going to see what is needed for the different options. What you need in every case is to open a command prompt in Windows and go to the C:\OpenCV\android folder as we’ll launch our commands from there. At the end of every build, the compiled files can be found in C:\OpenCV\android\build_neon\lib\armeabi-v7a.


Release version

With the standard libjpeg

Type

scripts\cmake_android.cmd

in the command prompt and execute it.

With libjpeg-turbo

Rename the C:\OpenCV\3rdparty\libjpeg folder to libjpeg_ORI (just to keep a copy of it), create a new libjpeg folder.

You should have the empty folder C:\OpenCV\3rdparty\libjpeg. You need to do a couple of things now: copy the libjpeg-turbo include files and copy the compiled version of libjpeg-turbo inside the new folder.

If you’ve built libjpeg-turbo following my tutorial Building libjpeg-turbo for Android, you can go to /home/andrea/libjpeg-turbo-1.2.90/build/arm_neon/installdir and copy the content of the include folder inside C:\OpenCV\3rdparty\libjpeg, then you can copy inside the same folder also the libjpeg.a that you find in /home/andrea/libjpeg-turbo-1.2.90/build/arm_neon/libs. Now you should have all the include files (.h) and the compiled libjpeg-turbo (.a) inside C:\OpenCV\3rdparty\libjpeg.

Go back to your Windows command prompt and from the C:\OpenCV\android folder type

scripts\cmake_android.cmd -DUSE_NEON=ON -DWITH_JPEG=ON -DBUILD_JPEG=OFF -DJPEG_INCLUDE_DIR=C:\OpenCV\3rdparty\libjpeg -DJPEG_LIBRARY=C:\OpenCV\3rdparty\libjpeg\libjpeg.a

and execute it.


Debug version

With the standard libjpeg

Type

scripts\cmake_android.cmd -DCMAKE_BUILD_TYPE=Debug

in the command prompt and execute it.

With libjpeg-turbo

Perform all the needed steps like in the release build of OpenCV, but the command to execute in the Windows command prompt this time is

scripts\cmake_android.cmd -DCMAKE_BUILD_TYPE=Debug -DUSE_NEON=ON -DWITH_JPEG=ON -DBUILD_JPEG=OFF -DJPEG_INCLUDE_DIR=C:\OpenCV\3rdparty\libjpeg -DJPEG_LIBRARY=C:\OpenCV\3rdparty\libjpeg\libjpeg.a


Using the compiled OpenCV in Android

OK, now you’ve got your personal build of OpenCV. How can you use it in your own Android app? I’m going to quickly show how to include the compiled version in the native side of your app. You’ll then be able to use the C++ interface of OpenCV through the include files available at OpenCV-2.4.5-android-sdk\sdk\native\jni\include\opencv2.

First of all you need to download the Android package from http://www.opencv.org. Once you have it ready, unzip the file, replace the content of OpenCV-2.4.5-android-sdk\sdk\native\libs\armeabi-v7a with C:\OpenCV\android\build_neon\lib\armeabi-v7a and replace OpenCV-2.4.5-android-sdk\sdk\native\3rdparty\libs\armeabi-v7a\liblibjpeg.a with the compiled libjpeg-turbo C:\OpenCV\3rdparty\libjpeg\libjpeg.a. You need to make sure that you rename the libjpeg-turbo library libjpeg.a to liblibjpeg.a to replace the original libjpeg in the OpenCV-2.4.5-android-sdk\sdk\native\3rdparty\libs\armeabi-v7a folder.

Now you’re ready to link the library to your own Android app. To do this, edit the Android.mk file in your jni folder and add the following lines:

OPENCV_PATH := /your/custom/path/OpenCV-2.4.5-android-sdk/sdk/native/jni
OPENCV_INSTALL_MODULES := on
OPENCV_CAMERA_MODULES := off
OPENCV_LIB_TYPE := STATIC
include $(OPENCV_PATH)/OpenCV.mk

A quick note: you might need to add nonfree to the OPENCV_MODULES:= line in your OpenCV.mk to load also the nonfree module, but beware of the licensing it requires.


Viewing all articles
Browse latest Browse all 5

Latest Images

Trending Articles



Latest Images