Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I integrate OpenCV 4.0 into a pure C++ Android NDK project?

What are the steps necessary for me to integrate,

the latest version of OpenCV

into a pure C++(No Java Code) Android NDK project, such as Android NDK Google tutorial:

Endless-Tunnel ?

official documentation for android integration refers to much older version :OpenCV-2.4 and folder structures are no longer the same.

I am using Android Studio on Linux.

All help is highly Appreciated.

like image 296
Sadern Alwis Avatar asked Mar 03 '19 09:03

Sadern Alwis


People also ask

Can we use OpenCV for Android app?

OpenCv4Android is available as a SDK with a set of samples and Javadoc documentation for OpenCV Java API. It also contains prebuilt apk-files, which you can run on your device instantly.

What is the difference between Android SDK and Android ndk?

Android provides Native Development Kit (NDK) to support native development in C/C++, besides the Android Software Development Kit (Android SDK) which supports Java.

What compiler does Android ndk use?

Code written in C/C++ can be compiled to ARM, or x86 native code (or their 64-bit variants) using the Android Native Development Kit (NDK). The NDK uses the Clang compiler to compile C/C++.


1 Answers

  1. Download opencv Android package (e.g. opencv-4.0.1-android-sdk) and unpack to, say, ~/android.

  2. To the bottom of CMakeLists.txt, add

    set( OpenCV_DIR "~/android/OpenCV-android-sdk/sdk/native/jni" )
    find_package( OpenCV REQUIRED )
    target_link_libraries(game opencv_java)
    

The package will define the following variables:

OpenCV_LIBS : The list of all imported targets for OpenCV modules.

OpenCV_INCLUDE_DIRS : The list of OpenCV include directories. With CMake >= 2.8.11 you don't even need to write

include_directories(${OpenCV_INCLUDE_DIRS})

This version of prebuilt OpenCV SDK defines also

OpenCV_VERSION : The version of this OpenCV build: "4.0.1"

OpenCV_ANDROID_NATIVE_API_LEVEL : Minimum required level of Android API: "16".

This means that your app manifest needs minSdkVersion 16 or higher (the original sample needs a fix here).

Instead of the shared library that contains all OpenCV functionality, you can use static libraries (opencv_imgcodecs, opencv_stitching, et al). These static libraries assume the default ANDROID_STL=c++_static.

For best results, use NDK r.18 or r.19.

UPDATE: NDK r.21 works well for opencv 4.3.0.

like image 192
Alex Cohn Avatar answered Sep 28 '22 02:09

Alex Cohn