简体   繁体   中英

How to use OpenCV C++ applications on Android (using NDK) & ARM processor

I want to change my existing OpenCV applications which were written in cpp for desktop version (X_86) processor to an ARM (processor) Architecture . At the initial stage I am not interested to change my cpp applications to java and compile it on Android Studio IDE. Before developing an Android app I want to benchmark the ARM processor. Whether it is capable to run my OpenCV applications, how much time it takes to process the video frames etc. So retaining my cpp applications as it is, I want to cross compile it for ARM Arch. and run it on Android Device (without GUI). how to do this?

I have followed many links and blogs to compile and build opencv cpp applications for ARM processor. One good link is

https://www.learnopencv.com/install-opencv-on-android-tiny-and-optimized/

all credits goes to above mentioned blog author.

This link is very good for beginners, till step no 8 it is very clearly written and I could follow it but Step no 9 onwards it is not properly written about where to keep demo application,link,include libraries. It used to give an error *.hpp not found. I tried all the ways but not able to solve. Then I thought that let me first build sample applications comes with opencv sdk. my approach is as shown below.

Build Set-up used

a. Ubuntu 14.04 and installed all the packages mentioned in https://www.learnopencv.com/install-opencv3-on-ubuntu/ link.

b.Download and install NDK r14b

c. Download and install OpenCV 3.4.1

d. Target armeabi-v7a (ARM based) & Android API 23

Steps 1-4 are required for first time setup

Step 1 : Download and Setup NDK unzip it and go to NDK folder

$ ./build/tools/make_standalone_toolchain.py \\ --arch arm \\ --api 23 \\ --install-dir /tmp/my-android-toolchain

Step 2 : Setup ANDROID_NDK Path

$ export ANDROID_STANDALONE_TOOLCHAIN=/tmp/my-android-toolchain/

Step 3 Download and Install OpenCV 3.4.1

$ cd opencv/

$ mkdir build

$ cd build

$ cmake -DCMAKE_TOOLCHAIN_FILE=../platforms/android/android.toolchain.cmake -DANDROID_STL=gnustl_shared -DBUILD_EXAMPLES=ON -DANDROID_NATIVE_API_LEVEL=23 ..

( -DBUILD_EXAMPLES=ON is important to build the samples).

$ make -j4

Step 4 Neon and VFPV3 optimizations (optional)

For ARM based devices, optimization i usually enable using: NEON VFPV3 Check out preamble of platforms/android/android.toolchain.cmake for various configuration options.

Step 5 Building your code Go to Working Diretory

$ cd path-to-opencv

Place your newly created cpp file in opencv/samples/cpp/

$ cd build/

$ cmake -DCMAKE_TOOLCHAIN_FILE=../platforms/android/android.toolchain.cmake -DANDROID_STL=gnustl_shared -DBUILD_EXAMPLES=ON -DANDROID_NATIVE_API_LEVEL=23 ..

$ make -j4

Step 6 Copying on android and running

$ cd path-to-opencv

$ adb push build/bin/example_cpp_your_program /system/xbin/

$ adb push build/libs/armeabi-v7a/libgnustl_shared.so /system/lib/ ****(If Required)****

(for me while executing cpp binary it said library not found to link so)

$ adb shell

$ cd /system/xbin/

$ ./example_cpp_your_program

This method perfectly worked on my Android device without using Android Studio IDE.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM