zz:在android上编译opencv

身体很虚,转载一枚,等买了android手机来试一把。来源
点击下面链接看全文攻略。
Build platform: Ubuntu 9.10
Target platform: Android
Download and prepare OpenCV library source code.
1. Download the latest version of OpenCV (http://sourceforge.net/project/showfiles.php?group_id=22870).
2. As build platform is Linux, select linux version (for example OpenCV2.1.0.tat.bz).
3. Unpack somewhere to home dir.
Download and prepare cross-compiler
1. Download Android NDK 3 for Linux (http://developer.android.com/sdk/ndk/index.html)
2. Unpack it to ~/android_ndk_3/
3. Then run ~/android_ndk_3/build/host-setup.sh but first fix the error in line 119
Change
if [ "$result" = "Pass" ] ; then
to
if [ "$result" == "Pass" ] ; then
4. Do/install whatever needed to let host-setup.sh complete successful.
Create NDK project/Modify Makefiles
There is one big issue with NKD toolchein. It has trimmed stdc library which does not contain STL. Because of that some files (like cvkdtree.cpp in cv) can not be compiled since they use vector, list and other stuff. The solution is to compile STL from source code. In my OpenCV NDK project I used STL sources from uClibc (http://www.uclibc.org).
The simpliest way to start your OpenCV NDK project is to update hello-jni project with OpenCV source files.
The ~/android_ndk_3/apps/hello-jni/project/jni folder of hello-jni project may look like this
- cv
- hdr
- src
cvaux
- hdr
- src
cxcore
- hdr
- src
stl
- hdr
- src
Android.mk
hello-jni.c
The ~/android_ndk_3/apps/hello-jni/project/jni/Android.mk may looks like this
- APPS_PATH := $(call my-dir)############################
# stl
############################
include $(CLEAR_VARS)LOCAL_PATH := $(APPS_PATH)/stl/src
LOCAL_C_INCLUDES := $(APPS_PATH)/stl/hdrLOCAL_MODULE := stl
LOCAL_SRC_FILES := string.cpp algorithm.cpp char_traits.cpp iterator.cpp limits.cpp list.cpp vector.cppinclude $(BUILD_STATIC_LIBRARY)############################
# cxcore
############################
include $(CLEAR_VARS)LOCAL_PATH := $(APPS_PATH)/cxcore/src
LOCAL_C_INCLUDES := $(APPS_PATH)/cxcore/hdrLOCAL_CXXFLAGS := -DHAVE_CONFIG_HLOCAL_MODULE := cxcore
LOCAL_SRC_FILES := cxalloc.cpp cxarithm.cpp cxarray.cpp cxcmp.cpp cxconvert.cpp cxcopy.cpp cxdatastructs.cpp cxdrawing.cpp cxdxt.cpp cxerror.cpp cximage.cpp cxjacobieigens.cpp cxlogic.cpp cxlut.cpp cxmathfuncs.cpp cxmatmul.cpp cxmatrix.cpp cxmean.cpp cxmeansdv.cpp cxminmaxloc.cpp cxnorm.cpp cxouttext.cpp cxpersistence.cpp cxprecomp.cpp cxrand.cpp cxsumpixels.cpp cxsvd.cpp cxswitcher.cpp cxtables.cpp cxutils.cpp dummy.cppinclude $(BUILD_STATIC_LIBRARY)############################
# cv
############################
include $(CLEAR_VARS)LOCAL_PATH := $(APPS_PATH)/cv/src
LOCAL_C_INCLUDES := $(APPS_PATH)/cv/hdr $(APPS_PATH)/cxcore/hdr $(APPS_PATH)/stl/hdr
LOCAL_MODULE := cv
LOCAL_SRC_FILES := cvkdtree.cpp cvaccum.cpp cvadapthresh.cpp cvapprox.cpp cvcalccontrasthistogram.cpp cvcalcimagehomography.cpp cvcalibinit.cpp cvcalibration.cpp cvcamshift.cpp cvcanny.cpp cvcolor.cpp cvcondens.cpp cvcontours.cpp cvcontourtree.cpp cvconvhull.cpp cvcorner.cpp cvcornersubpix.cpp cvderiv.cpp cvdistransform.cpp cvdominants.cpp cvemd.cpp cvfeatureselect.cpp cvfilter.cpp cvfloodfill.cpp cvfundam.cpp cvgeometry.cpp cvhaar.cpp cvhistogram.cpp cvhough.cpp cvimgwarp.cpp cvinpaint.cpp cvkalman.cpp cvlinefit.cpp cvlkpyramid.cpp cvmatchcontours.cpp cvmoments.cpp cvmorph.cpp cvmotempl.cpp cvoptflowbm.cpp cvoptflowhs.cpp cvoptflowlk.cpp cvpgh.cpp cvposit.cpp cvprecomp.cpp cvpyramids.cpp cvpyrsegmentation.cpp cvrotcalipers.cpp cvsamplers.cpp cvsegmentation.cpp cvshapedescr.cpp cvsmooth.cpp cvsnakes.cpp cvstereobm.cpp cvstereogc.cpp cvsubdivision2d.cpp cvsumpixels.cpp cvsurf.cpp cvswitcher.cpp cvtables.cpp cvtemplmatch.cpp cvthresh.cpp cvundistort.cpp cvutils.cpp dummy.cpp
LOCAL_STATIC_LIBRARIES := cxcore stl
include $(BUILD_STATIC_LIBRARY)
############################
# cvaux
############################
include $(CLEAR_VARS)
LOCAL_PATH := $(APPS_PATH)/cvaux/src
LOCAL_C_INCLUDES := $(APPS_PATH)/cvaux/hdr $(APPS_PATH)/cv/hdr $(APPS_PATH)/cv/src $(APPS_PATH)/cxcore/hdr $(APPS_PATH)/stl/hdr
LOCAL_MODULE := cvaux
LOCAL_SRC_FILES := camshift.cpp cvaux.cpp cvauxutils.cpp cvbgfg_acmmm2003.cpp cvbgfg_codebook.cpp cvbgfg_common.cpp cvbgfg_gaussmix.cpp cvcalibfilter.cpp cvclique.cpp cvcorrespond.cpp cvcorrimages.cpp cvcreatehandmask.cpp cvdpstereo.cpp cveigenobjects.cpp cvepilines.cpp cvface.cpp cvfacedetection.cpp cvfacetemplate.cpp cvfindface.cpp cvfindhandregion.cpp cvhmm.cpp cvhmm1d.cpp cvhmmobs.cpp cvlcm.cpp cvlee.cpp cvlevmar.cpp cvlevmarprojbandle.cpp cvlevmartrif.cpp cvlines.cpp cvlmeds.cpp cvmat.cpp cvmorphcontours.cpp cvmorphing.cpp cvprewarp.cpp cvscanlines.cpp cvsegment.cpp cvsubdiv2.cpp cvtexture.cpp cvtrifocal.cpp cvvecfacetracking.cpp cvvideo.cpp decomppoly.cpp dummy.cpp enmin.cpp extendededges.cpp precomp.cpp vs/bgfg_estimation.cpp vs/blobtrackanalysis.cpp vs/blobtrackanalysishist.cpp vs/blobtrackanalysisior.cpp vs/blobtrackanalysistrackdist.cpp vs/blobtrackgen1.cpp vs/blobtrackgenyml.cpp vs/blobtrackingauto.cpp vs/blobtrackingcc.cpp vs/blobtrackingccwithcr.cpp vs/blobtrackingkalman.cpp vs/blobtrackinglist.cpp vs/blobtrackingmsfg.cpp vs/blobtrackingmsfgs.cpp vs/blobtrackpostprockalman.cpp vs/blobtrackpostproclinear.cpp vs/blobtrackpostproclist.cpp vs/enteringblobdetection.cpp vs/enteringblobdetectionreal.cpp vs/testseq.cpp
# failed to compile
#cv3dtracker.cpp
LOCAL_STATIC_LIBRARIES := cv cxcore stl
include $(BUILD_STATIC_LIBRARY)
The ~/android_ndk_3/apps/hello-jni/Application.mk file needs to be updated as follows
- APP_PROJECT_PATH := $(call my-dir)/project
APP_MODULES := stl cxcore cv cvaux hello-jni
To build the project go to ~/android_ndk_3 and type
- make APP=hello-jni
Of course there will be compile issues. Understand and fix them. Easiest cases are related to syntax mismatch between different compilers. In more complicated cases some code should be commented out. For example usage of libs with optimizations for Intel processor is not needed for ARM.
HighGui is also can be built but only partially. Simply remove files that causing problems from Android.mk. In my case the rest of files were enough to use cvLoadImage function for bmp file.
Running facedetect openCV example
There is no way to run native C code as separate application on Android. Instead native C functions can be called from Java apps. Because of that I made native function FaceDetect using OpenCV example application facedetect.c.
The declaration looks like this
- void
Java_com_example_hellojni_HelloJni_FaceDetect( JNIEnv* env, jobject thiz, jbyteArray jyuv_buff, int w, int h, jbyteArray jbgra_buff)
{
…
}
This function takes YUV_NV21 buffer (preview from camera captured by Java app), converts it to BGRA8888, searches the faces, draws circles around the faces and returns updated RGBA8888 buffer back to Java app. Java app can draw it on the screen.
实用,移动应用很靠谱~~~
非常有用,我前一阵子也想在android上编译opencv,没想到用这种方法。多谢了。
不客气。常来看看。
怕怕,opencv这么牛大的东西在Android上搞,不知道会有什么后果啊……
您好!看了这个感觉受益匪浅,但是有个问题,我们现在用的是NDK4,相比于NDK3有些变化,环境一直配不出来,不知道能不能提供点帮助
@cvchina
http://www.cnkinect.com 中国KINECT论坛