在OpenCV4Android中没有nonfree module,因此也就没有了SURF和SIFT组件。但是我们可以通过OpenCV for Windows的nonfree module开源代码通过NDK将其编译为Android可以使用的.so库文件,然后通过JNI技术,将该.so文件挂载到JNI的库中。
参考网上两篇博客:
http://blog.csdn.net/mfe10714022/article/details/50215131
http://stackoverflow.com/questions/28510045/undefined-reference-to-cvinitmodule-nonfree-in-android
具体做法如下:
My development environment is set up as follows:
- android-ndk-r10d (install path:
D:\adt-bundle-windows-x86_64-20140702\android-ndk-r10d\
) - OpenCV-2.4.10-android-sdk (install path:
D:\CODE\OpenCV-2.4.10-android-sdk\
), Download link - OpenCV-2.4.10 (install path:
D:\CODE\OpenCV-2.4.10\
), Download link
Building the nonfree module
-
We actually only need to copy a few files from
OpenCV-2.4.10
source code toOpenCV-2.4.10-android-sdk
, namely:
Copy the nonfree folder fromOpenCV-2.4.10\sources\modules\nonfree\include\opencv2\
toOpenCV-2.4.10-android-sdk\sdk\native\jni\include\opencv2
. -
Create a folder to hold our new project for
libnonfree.so
. Here, I call itlibnonfree
. Create ajni
folder underlibnonfree
. Copy the following files fromOpenCV-2.4.10\sources\modules\nonfree\src
tolibnonfree\jni\
folder:- nonfree_init.cpp
- precomp.hpp
- sift.cpp (use the original file)
- surf.cpp (use the original file)
-
Building
libnonfree.so
:
CreateAndroid.mk
andApplication.mk
scripts. ThisAndroid.mk
is used to buildlibnonfree.so
.- Application.mk
- Android.mk (you should modify
OPENCV_PATH
where yourOpenCV-2.4.10-android-sdk
is)
cd
into the project folderlibnonfree
and typendk-build
to build thelibnonfree.so
.
So far, you have got libnonfree.so
along with libopencv_java.so
and libgnustl_shared.so
in libnonfree\libs\armeabi-v7a
folder.
You can easily build any SIFT or SURF applications using those libraries. If you want to use SIFT and SURF in JAVA code in your Android application, you only need to write JNI interfaces for the functions you want to use.
Building a sample application
-
Create a project folder call
libnonfree_demo
. Create ajni
folder inside the project folder. Then copylibnonfree.so
along withlibopencv_java.so
andlibgnustl_shared.so
intojni
. -
Create a nonfree_jni.cpp in
jni
. It is simple SIFT test program. It basically reads an image and detects the keypoints, then extracts feature descriptors, finally draws the keypoints to an output image. -
Create
Android.mk
andApplication.mk
insidejni
:- Application.mk
- Android.mk (you should modify
OPENCV_PATH
where yourOpenCV-2.4.10-android-sdk
is)
cd
into the project folderlibnonfree_demo
and typendk-build
to build thelibnonfree_demo.so
.
At this point you can easily extend the sample app with your SVMDetector
. Just copy the source and include files int to the folder libnonfree_demo\jni
and add cpp files to LOCAL_SRC_FILES
in Android.mk
.
The whole source can be downloaded from: https://github.com/bkornel/opencv_android_nonfree.
Original source from: http://web.guohuiwang.com/technical-notes/sift_surf_opencv_android
在编译的时候会报错:注释掉precomp.hpp一下几行:
1.#include "cvconfig.h"
2.
#ifdef HAVE_OPENCV_OCL
# include "opencv2/nonfree/ocl.hpp"
# include "opencv2/ocl/private/util.hpp"
#endif