Megatron300
Megatron300

Reputation: 239

Using OpenCV nonfree module in Android

I'm trying to use the nonfree module with OpenCV on Android. I'm following this answer https://stackoverflow.com/a/28557686/6126070 but I'm having trouble understanding it since this is my first application using Android and NDK.

Right now, OpenCV (without nonfree) is working on my application and I'm using it in C++ code with NDK and JNI. What I'm having trouble with is editing my current Android.mk and Application.mk files to thoses in the answer to compile nonfree.

Here is my Android.mk and Application.mk files with the structure of my project.

Android.mk :

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

#opencv
OPENCVROOT:= C:\OpenCV-2.4.10-android-sdk
OPENCV_CAMERA_MODULES:=on
OPENCV_INSTALL_MODULES:=on
OPENCV_LIB_TYPE:=SHARED
include ${OPENCVROOT}/sdk/native/jni/OpenCV.mk

LOCAL_SRC_FILES := com_example_adrien_ndkopencvtest4_OpencvNativeClass.cpp


LOCAL_LDLIBS += -llog
LOCAL_MODULE := MyOpencvLibs 


include $(BUILD_SHARED_LIBRARY)

Application.mk :

APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := armeabi-v7a
APP_PLATFORM := android-16

Project structure : enter image description here

My project crashes with the line #include <opencv2/nonfree/nonfree.hpp> in my jni .h file so that's why I'm trying to import nonfree module.

So as I said, I'm trying to implement the answer linked above but I'm having trouble.

In the answer :

Building the nonfree module :

Step 1 : I'm copying the files just fine.

Step 2 : I don't understand where I'm supposed to create this folder, in my application or in my computer ?

Step 3 : Here is the big problem, I don't know how I can merge the Android.mk and Application.mk files provided in the answer with mine. Furthermore, I don't understand the line "cd into the project folder libnonfree and type ndk-build to build the libnonfree.so."

For the "Building a sample appliaction" I haven't been to this part yet but I supposed I don't need to do it I can just keep using OpenCV with my application except the #include <opencv2/nonfree/nonfree.hpp> line will work.

I tried to make this question clear, if you need more information I will gladly edit it.

Upvotes: 1

Views: 639

Answers (1)

Beatrice Lin
Beatrice Lin

Reputation: 1496

Step2: Create folder under your jniLibs file or put 4 file directly.

Step3:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

#opencv
OPENCVROOT:= C:\OpenCV-2.4.10-android-sdk
OPENCV_CAMERA_MODULES:=on
OPENCV_INSTALL_MODULES:=on
OPENCV_LIB_TYPE:=SHARED
include ${OPENCVROOT}/sdk/native/jni/OpenCV.mk

LOCAL_SRC_FILES := com_example_adrien_ndkopencvtest4_OpencvNativeClass.cpp


LOCAL_LDLIBS += -llog
LOCAL_MODULE := MyOpencvLibs 


include $(BUILD_SHARED_LIBRARY)


include $(CLEAR_VARS)

LOCAL_C_INCLUDES:= ${OPENCVROOT}/sdk/native/jni/include
LOCAL_MODULE    := nonfree
LOCAL_CFLAGS    := -Werror -O3 -ffast-math
LOCAL_LDLIBS    += -llog

# for 2.4.8, delete the line precomp.cpp \
LOCAL_SRC_FILES := nonfree_init.cpp \
sift.cpp \
surf.cpp
include $(BUILD_SHARED_LIBRARY)

and you can search how to add External tool in android studio (ndk-build)

Upvotes: 1

Related Questions