set(HAVE_${__modname}OFFCACHEINTERNAL"Module ${__modname} can not be built in current configuration")
set(OPENCV_MODULE_${__modname}_LOCATION"${CMAKE_CURRENT_SOURCE_DIR}"CACHEINTERNAL"Location of ${__modname} module sources")
set(OPENCV_MODULES_DISABLED_FORCE"${OPENCV_MODULES_DISABLED_FORCE}"CACHEINTERNAL"List of OpenCV modules which can not be build in current configuration")
This tutorial is created to help you use OpenCV library within your Android project.
This tutorial has been created to help you use OpenCV library within your Android project.
This guide was written with Windows 7 in mind, though it should work with any other OS supported by OpenCV4Android SDK.
This guide was written with Windows 7 in mind, though it should work with any other OS supported by
OpenCV4Android SDK.
This tutorial assumes you have the following installed and configured:
@ -23,41 +23,57 @@ This tutorial assumes you have the following installed and configured:
If you need help with anything of the above, you may refer to our :ref:`android_dev_intro` guide.
This tutorial also assumes you have OpenCV4Android SDK already installed on your development machine and OpenCV Manager on your testing device correspondingly. If you need help with any of these, you may consult our :ref:`O4A_SDK` tutorial.
This tutorial also assumes you have OpenCV4Android SDK already installed on your development
machine and OpenCV Manager on your testing device correspondingly. If you need help with any of
these, you may consult our :ref:`O4A_SDK` tutorial.
If you encounter any error after thoroughly following these steps, feel free to contact us via
`OpenCV4Android <https://groups.google.com/group/android-opencv/>`_ discussion group or OpenCV
`Q&A forum <http://answers.opencv.org>`_ . We'll do our best to help you out.
If you encounter any error after thoroughly following these steps, feel free to contact us via `OpenCV4Android <https://groups.google.com/group/android-opencv/>`_ discussion group or OpenCV `Q&A forum <http://answers.opencv.org>`_ . We'll do our best to help you out.
Using OpenCV library within your Android project
Using OpenCV Library Within Your Android Project
================================================
In this section we will explain how to make some existing project to use OpenCV.
Starting with 2.4.2 release for Android, *OpenCV Manager* is used to provide apps with the best available version of OpenCV.
You can get more information here: :ref:`Android_OpenCV_Manager` and in these `slides <https://docs.google.com/a/itseez.com/presentation/d/1EO_1kijgBg_BsjNp2ymk-aarg-0K279_1VZRcPplSuk/present#slide=id.p>`_.
Starting with 2.4.2 release for Android, *OpenCV Manager* is used to provide apps with the best
available version of OpenCV.
You can get more information here: :ref:`Android_OpenCV_Manager` and in these
Using async initialization is a **recommended** way for application development. It uses the OpenCV Manager to access OpenCV libraries externally installed in the target system.
Using async initialization is a **recommended** way for application development. It uses the OpenCV
Manager to access OpenCV libraries externally installed in the target system.
#. Add OpenCV library project to your workspace. Use menu
:guilabel:`File -> Import -> Existing project in your workspace`.
#. Add OpenCV library project to your workspace. Use menu :guilabel:`File -> Import -> Existing project in your workspace`,
press :guilabel:`Browse` button and locate OpenCV4Android SDK (:file:`OpenCV-2.4.3-android-sdk/sdk`).
Press :guilabel:`Browse` button and locate OpenCV4Android SDK
(:file:`OpenCV-2.4.3-android-sdk/sdk`).
..image:: images/eclipse_opencv_dependency0.png
:alt:Add dependency from OpenCV library
:align:center
#. In application project add a reference to the OpenCV Java SDK in :guilabel:`Project -> Properties -> Android -> Library -> Add` select ``OpenCV Library - 2.4.3``.
#. In application project add a reference to the OpenCV Java SDK in
In most cases OpenCV Manager may be installed automatically from Google Play. For such case, when Google Play is not available, i.e. emulator, developer board, etc, you can
install it manually using adb tool. See :ref:`manager_selection` for details.
In most cases OpenCV Manager may be installed automatically from Google Play. For the case, when
Google Play is not available, i.e. emulator, developer board, etc, you can install it manually
using adb tool. See :ref:`manager_selection` for details.
There is a very base code snippet implementing the async initialization. It shows basic principles. See the "15-puzzle" OpenCV sample for details.
There is a very base code snippet implementing the async initialization. It shows basic principles.
See the "15-puzzle" OpenCV sample for details.
..code-block:: java
:linenos:
@ -97,35 +113,48 @@ There is a very base code snippet implementing the async initialization. It show
}
}
It this case application works with OpenCV Manager in asynchronous fashion. ``OnManagerConnected`` callback will be called in UI thread, when initialization finishes.
Please note, that it is not allowed to use OpenCV calls or load OpenCV-dependent native libs before invoking this callback.
It this case application works with OpenCV Manager in asynchronous fashion. ``OnManagerConnected``
callback will be called in UI thread, when initialization finishes. Please note, that it is not
allowed to use OpenCV calls or load OpenCV-dependent native libs before invoking this callback.
Load your own native libraries that depend on OpenCV after the successful OpenCV initialization.
Default BaseLoaderCallback implementation treat application context as Activity and calls Activity.finish() method to exit in case of initialization failure.
To override this behavior you need to override finish() method of BaseLoaderCallback class and implement your own finalization method.
Default ``BaseLoaderCallback`` implementation treat application context as Activity and calls
``Activity.finish()`` method to exit in case of initialization failure. To override this behavior
you need to override ``finish()`` method of ``BaseLoaderCallback`` class and implement your own
finalization method.
Application development with static initialization
Application Development with Static Initialization
According to this approach all OpenCV binaries are included into your application package. It is designed mostly for development purposes.
This approach is deprecated for the production code, release package is recommended to communicate with OpenCV Manager via the async initialization described above.
According to this approach all OpenCV binaries are included into your application package. It is
designed mostly for development purposes. This approach is deprecated for the production code,
release package is recommended to communicate with OpenCV Manager via the async initialization
described above.
#. Add the OpenCV library project to your workspace the same way as for the async initialization above.
Use menu :guilabel:`File -> Import -> Existing project in your workspace`, push :guilabel:`Browse` button and select OpenCV SDK path (:file:`OpenCV-2.4.3-android-sdk/sdk`).
#. Add the OpenCV library project to your workspace the same way as for the async initialization
above. Use menu :guilabel:`File -> Import -> Existing project in your workspace`,
press :guilabel:`Browse` button and select OpenCV SDK path
(:file:`OpenCV-2.4.3-android-sdk/sdk`).
..image:: images/eclipse_opencv_dependency0.png
:alt:Add dependency from OpenCV library
:align:center
#. In the application project add a reference to the OpenCV4Android SDK in :guilabel:`Project -> Properties -> Android -> Library -> Add` select ``OpenCV Library - 2.4.3``;
#. In the application project add a reference to the OpenCV4Android SDK in
#. If your application project **doesn't have a JNI part**, just copy the corresponding OpenCV native libs from :file:`<OpenCV-2.4.3-android-sdk>/sdk/native/libs/<target_arch>` to your project directory to folder :file:`libs/<target_arch>`.
#. If your application project **doesn't have a JNI part**, just copy the corresponding OpenCV
native libs from :file:`<OpenCV-2.4.3-android-sdk>/sdk/native/libs/<target_arch>` to your
project directory to folder :file:`libs/<target_arch>`.
In case of the application project **with a JNI part**, instead of manual libraries copying you need to modify your ``Android.mk`` file:
add the following two code lines after the ``"include $(CLEAR_VARS)"`` and before ``"include path_to_OpenCV-2.4.3-android-sdk/sdk/native/jni/OpenCV.mk"``
In case of the application project **with a JNI part**, instead of manual libraries copying you
need to modify your ``Android.mk`` file:
add the following two code lines after the ``"include $(CLEAR_VARS)"`` and before
@ -145,12 +174,14 @@ This approach is deprecated for the production code, release package is recommen
OPENCV_INSTALL_MODULES:=on
include ../../sdk/native/jni/OpenCV.mk
After that the OpenCV libraries will be copied to your application :file:`libs` folder during the JNI part build.
After that the OpenCV libraries will be copied to your application :file:`libs` folder during
the JNI build.v
Eclipse will automatically include all the libraries from the :file:`libs` folder to the application package (APK).
Eclipse will automatically include all the libraries from the :file:`libs` folder to the
application package (APK).
#. The last step of enabling OpenCV in your application is Java initialization code before call to OpenCV API.
It can be done, for example, in the static section of the ``Activity`` class:
#. The last step of enabling OpenCV in your application is Java initialization code before calling
OpenCV API. It can be done, for example, in the static section of the ``Activity`` class:
..code-block:: java
:linenos:
@ -161,7 +192,8 @@ This approach is deprecated for the production code, release package is recommen
}
}
If you application includes other OpenCV-dependent native libraries you should load them **after** OpenCV initialization:
If you application includes other OpenCV-dependent native libraries you should load them
**after** OpenCV initialization:
..code-block:: java
:linenos:
@ -175,39 +207,45 @@ This approach is deprecated for the production code, release package is recommen
}
}
Native/C++
----------
To build your own Android application, which uses OpenCV from native part, the following steps should be done:
To build your own Android application, using OpenCV as native part, the following steps should be
taken:
#. You can use an environment variable to specify the location of OpenCV package or just hardcode absolute or relative path in the :file:`jni/Android.mk` of your projects.
#. You can use an environment variable to specify the location of OpenCV package or just hardcode
absolute or relative path in the :file:`jni/Android.mk` of your projects.
#. The file :file:`jni/Android.mk` should be written for the current application using the common rules for this file.
#. The file :file:`jni/Android.mk` should be written for the current application using the common
rules for this file.
For detailed information see the Android NDK documentation from the Android NDK archive, in the file
include C:\Work\OpenCV4Android\OpenCV-2.4.3-android-sdk\sdk\native\jni\OpenCV.mk
should be inserted into the :file:`jni/Android.mk` file **after** the line
Should be inserted into the :file:`jni/Android.mk` file **after** this line:
..code-block:: make
include $(CLEAR_VARS)
#. Several variables can be used to customize OpenCV stuff, but you **don't need** to use them when your application uses the `async initialization` via the `OpenCV Manager` API.
#. Several variables can be used to customize OpenCV stuff, but you **don't need** to use them when
your application uses the `async initialization` via the `OpenCV Manager` API.
Note: these variables should be set **before** the ``"include .../OpenCV.mk"`` line:
..note:: These variables should be set **before** the ``"include .../OpenCV.mk"`` line:
..code-block:: make
OPENCV_INSTALL_MODULES:=on
Copies necessary OpenCV dynamic libs to the project ``libs`` folder in order to include them into the APK.
Copies necessary OpenCV dynamic libs to the project ``libs`` folder in order to include them
into the APK.
..code-block:: make
@ -219,7 +257,8 @@ To build your own Android application, which uses OpenCV from native part, the f
OPENCV_LIB_TYPE:=STATIC
Perform static link with OpenCV. By default dynamic link is used and the project JNI lib depends on ``libopencv_java.so``.
Perform static linking with OpenCV. By default dynamic link is used and the project JNI lib
depends on ``libopencv_java.so``.
#. The file :file:`Application.mk` should exist and should contain lines:
@ -228,139 +267,250 @@ To build your own Android application, which uses OpenCV from native part, the f
APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
Also the line like this one:
Also, the line like this one:
..code-block:: make
APP_ABI := armeabi-v7a
should specify the application target platforms.
Should specify the application target platforms.
In some cases a linkage error (like ``"In function 'cv::toUtf16(std::basic_string<...>... undefined reference to 'mbstowcs'"``) happens
when building an application JNI library depending on OpenCV.
The following line in the :file:`Application.mk` usually fixes it:
In some cases a linkage error (like ``"In function 'cv::toUtf16(std::basic_string<...>...
undefined reference to 'mbstowcs'"``) happens when building an application JNI library,
depending on OpenCV. The following line in the :file:`Application.mk` usually fixes it:
..code-block:: make
APP_PLATFORM := android-9
#. Either use :ref:`manual <NDK_build_cli>```ndk-build`` invocation or :ref:`setup Eclipse CDT Builder <CDT_Builder>` to build native JNI lib before Java part [re]build and APK creation.
#. Either use :ref:`manual <NDK_build_cli>```ndk-build`` invocation or
:ref:`setup Eclipse CDT Builder <CDT_Builder>` to build native JNI lib before (re)building the Java
part and creating an APK.
"Hello OpenCV" Sample
=====================
Hello OpenCV Sample
===================
Here are basic steps to guide you trough the creation process of a simple OpenCV-centric application.
It will be capable of accessing camera output, processing it and displaying the result.
Here are basic steps to guide you trough the process of creating a simple OpenCV-centric
application. It will be capable of accessing camera output, processing it and displaying the
result.
#. Open Eclipse IDE, create a new clean workspace, create a new Android project (:guilabel:`File -> New -> Android Project`).
#. Open Eclipse IDE, create a new clean workspace, create a new Android project
:menuselection:`File --> New --> Android Project`.
#. Set name, target, package and ``minSDKVersion`` accordingly.
#. Add the following permissions to the ``AndroidManifest.xml`` file:
#. Create a new class :menuselection:`File -> New -> Class`. Name it for example:
@ -36,8 +36,8 @@ Finds edges in an image using the [Canny86]_ algorithm.
..seealso:::ocv:func:`Canny`
ocl::BruteForceMatcher_OCL
--------------------------
ocl::BruteForceMatcher_OCL_base
-------------------------------
..ocv:class:: ocl::BruteForceMatcher_OCL_base
Brute-force descriptor matcher. For each descriptor in the first set, this matcher finds the closest descriptor in the second set by trying each one. This descriptor matcher supports masking permissible matches between descriptor sets. ::
@ -162,7 +162,7 @@ Finds the best match for each descriptor from a query set with train descriptors
Downloads matrices obtained via :ocv:func:`ocl::BruteForceMatcher_OCL_base::matchSingle` or :ocv:func:`ocl::BruteForceMatcher_OCL_base::matchCollection` to vector with :ocv:class:`DMatch`.
..ocv:function:: void bilateralFilter(const oclMat &src, oclMat &dst, int d, double sigmaColor, double sigmaSpave, int borderType=BORDER_DEFAULT)
..ocv:function:: void ocl::bilateralFilter(const oclMat &src, oclMat &dst, int d, double sigmaColor, double sigmaSpave, int borderType=BORDER_DEFAULT)
:param src:The source image
@ -147,7 +147,7 @@ ocl::copyMakeBorder
--------------------
Returns void
..ocv:function:: void copyMakeBorder(const oclMat &src, oclMat &dst, int top, int bottom, int left, int right, int boardtype, const Scalar &value = Scalar())
..ocv:function:: void ocl::copyMakeBorder(const oclMat &src, oclMat &dst, int top, int bottom, int left, int right, int boardtype, const Scalar &value = Scalar())
:param src:The source image
@ -165,7 +165,7 @@ ocl::dilate
------------------
Returns void
..ocv:function:: void dilate( const oclMat &src, oclMat &dst, const Mat &kernel, Point anchor = Point(-1, -1), int iterations = 1, int borderType = BORDER_CONSTANT, const Scalar &borderValue = morphologyDefaultBorderValue())
..ocv:function:: void ocl::dilate( const oclMat &src, oclMat &dst, const Mat &kernel, Point anchor = Point(-1, -1), int iterations = 1, int borderType = BORDER_CONSTANT, const Scalar &borderValue = morphologyDefaultBorderValue())
:param src:The source image
@ -187,7 +187,7 @@ ocl::erode
------------------
Returns void
..ocv:function:: void erode( const oclMat &src, oclMat &dst, const Mat &kernel, Point anchor = Point(-1, -1), int iterations = 1, int borderType = BORDER_CONSTANT, const Scalar &borderValue = morphologyDefaultBorderValue())
..ocv:function:: void ocl::erode( const oclMat &src, oclMat &dst, const Mat &kernel, Point anchor = Point(-1, -1), int iterations = 1, int borderType = BORDER_CONSTANT, const Scalar &borderValue = morphologyDefaultBorderValue())
:param src:The source image
@ -209,7 +209,7 @@ ocl::morphologyEx
------------------
Returns void
..ocv:function:: void morphologyEx( const oclMat &src, oclMat &dst, int op, const Mat &kernel, Point anchor = Point(-1, -1), int iterations = 1, int borderType = BORDER_CONSTANT, const Scalar &borderValue = morphologyDefaultBorderValue())
..ocv:function:: void ocl::morphologyEx( const oclMat &src, oclMat &dst, int op, const Mat &kernel, Point anchor = Point(-1, -1), int iterations = 1, int borderType = BORDER_CONSTANT, const Scalar &borderValue = morphologyDefaultBorderValue())
:param m:The destination matrix. If it does not have a proper size or type before the operation, it will be reallocated
@ -19,11 +19,11 @@ Returns void
The method converts source pixel values to the target datatype. saturate cast is applied in the end to avoid possible overflows. Supports CV_8UC1, CV_8UC4, CV_32SC1, CV_32SC4, CV_32FC1, CV_32FC4.
:param magnitude:The source floating-point array of magnitudes of 2D vectors. It can be an empty matrix (=Mat()) - in this case the function assumes that all the magnitudes are =1. If it's not empty, it must have the same size and same type as angle
:param x:The floating-point array of x-coordinates of the vectors
@ -313,11 +323,11 @@ ocl::flip
------------------
Returns void
..ocv:function:: void flip(const oclMat &src, oclMat &dst, int flipCode)
..ocv:function:: void ocl::flip( const oclMat& a, oclMat& b, int flipCode )
:param src:Source image.
:param a:Source image.
:param dst:Destination image
:param b:Destination image
:param flipCode:Specifies how to flip the array: 0 means flipping around the x-axis, positive (e.g., 1) means flipping around y-axis, and negative (e.g., -1) means flipping around both axes.