{"id":116,"date":"2010-04-22T16:01:50","date_gmt":"2010-04-22T06:01:50","guid":{"rendered":"http:\/\/www.computer-vision-software.com\/blog\/?p=116"},"modified":"2010-04-23T20:22:40","modified_gmt":"2010-04-23T10:22:40","slug":"android","status":"publish","type":"post","link":"http:\/\/www.computer-vision-software.com\/blog\/2010\/04\/android\/","title":{"rendered":"Compiling OpenCV for Android using NDK 3"},"content":{"rendered":"<p><a onclick=\"javascript:pageTracker._trackPageview('\/outgoing\/www.computer-vision-software.com\/blog\/wp-content\/uploads\/2010\/04\/Picture-18.jpg');\"  href=\"http:\/\/www.computer-vision-software.com\/blog\/wp-content\/uploads\/2010\/04\/Picture-18.jpg\"><\/a><\/p>\n<p>Build platform: Ubuntu 9.10<br \/>\nTarget platform: Android<\/p>\n<p><strong>Download and prepare OpenCV library source code. <\/strong><\/p>\n<p><strong>1. <\/strong>Download the latest version of OpenCV (<a onclick=\"javascript:pageTracker._trackPageview('\/outgoing\/sourceforge.net\/project\/showfiles.php?group_id=22870');\"  href=\"http:\/\/sourceforge.net\/project\/showfiles.php?group_id=22870\" target=\"_blank\">http:\/\/sourceforge.net\/project\/showfiles.php?group_id=22870<\/a>).<\/p>\n<p>2. As build platform is Linux, select linux version (for example<em> OpenCV2.1.0.tat.bz<\/em>).<\/p>\n<p><!--more--><\/p>\n<p><strong>3. <\/strong>Unpack somewhere to home dir.<\/p>\n<p><strong>Download and prepare cross-compiler<\/strong><\/p>\n<p><strong><span style=\"font-weight: normal;\">1. Download Android NDK 3 for Linux (<a onclick=\"javascript:pageTracker._trackPageview('\/outgoing\/developer.android.com\/sdk\/ndk\/index.html');\"  href=\"http:\/\/developer.android.com\/sdk\/ndk\/index.html\" target=\"_blank\">http:\/\/developer.android.com\/sdk\/ndk\/index.html<\/a>)<\/span><\/strong><\/p>\n<p><strong><span style=\"font-weight: normal;\">2. Unpack it to <em>~\/android_ndk_3\/<\/em><\/span><\/strong><\/p>\n<p><strong><span style=\"font-weight: normal;\"><em><span style=\"font-style: normal;\"><strong>3. <\/strong>Then run <em>~\/android_ndk_3\/build\/host-setup.sh<\/em> but first fix the error in line 119<\/span><\/em><\/span><\/strong><\/p>\n<p><strong><span style=\"font-weight: normal;\"><em><span style=\"font-style: normal;\">Change<\/span><\/em><\/span><\/strong><\/p>\n<p><strong><span style=\"font-weight: normal;\"><em><span style=\"font-style: normal;\">if [ &#8220;$result&#8221; = &#8220;Pass&#8221; ] ; then<\/span><\/em><\/span><\/strong><\/p>\n<p><strong><span style=\"font-weight: normal;\"><em><span style=\"font-style: normal;\">to<\/span><\/em><\/span><\/strong><\/p>\n<p><strong><span style=\"font-weight: normal;\"><em><span style=\"font-style: normal;\">if [ &#8220;$result&#8221; == &#8220;Pass&#8221; ] ; then<\/span><\/em><\/span><\/strong><\/p>\n<p><strong><span style=\"font-weight: normal;\"><em><span style=\"font-style: normal;\"><strong>4. <\/strong>Do\/install whatever needed to let host-setup.sh complete successful.<\/span><\/em><\/span><\/strong><\/p>\n<p><strong>Create NDK project\/Modify Makefiles<\/strong><\/p>\n<p>There is one big issue with NKD toolchein. It has trimmed stdc library which does not contain STL. Because of that some files (like <em>cvkdtree.cpp<\/em> 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 (<a onclick=\"javascript:pageTracker._trackPageview('\/outgoing\/www.uclibc.org');\"  href=\"http:\/\/www.uclibc.org\" target=\"_blank\">http:\/\/www.uclibc.org<\/a>).<\/p>\n<p>The simpliest way to start your OpenCV NDK project is to update <em>hello-jni<\/em> project with OpenCV source files.<\/p>\n<p>The <em>~\/android_ndk_3\/apps\/hello-jni\/project\/jni<\/em> folder of hello-jni project may look like this<\/p>\n<ol>cv<br \/>\n&#8211; hdr<br \/>\n&#8211; src<br \/>\ncvaux<br \/>\n&#8211; hdr<br \/>\n&#8211; src<br \/>\ncxcore<br \/>\n&#8211; hdr<br \/>\n&#8211; src<br \/>\nstl<br \/>\n&#8211; hdr<br \/>\n&#8211; src<br \/>\nAndroid.mk<br \/>\nhello-jni.c<\/ol>\n<p>The <em>~\/android_ndk_3\/apps\/hello-jni\/project\/jni\/Android.mk<\/em> may looks like this<\/p>\n<ol>APPS_PATH := $(call my-dir)############################<br \/>\n# stl<br \/>\n############################<br \/>\ninclude $(CLEAR_VARS)LOCAL_PATH := $(APPS_PATH)\/stl\/src<br \/>\nLOCAL_C_INCLUDES := $(APPS_PATH)\/stl\/hdrLOCAL_MODULE := stl<br \/>\nLOCAL_SRC_FILES := string.cpp algorithm.cpp char_traits.cpp iterator.cpp limits.cpp list.cpp vector.cppinclude $(BUILD_STATIC_LIBRARY)############################<br \/>\n# cxcore<br \/>\n############################<br \/>\ninclude $(CLEAR_VARS)LOCAL_PATH := $(APPS_PATH)\/cxcore\/src<br \/>\nLOCAL_C_INCLUDES := $(APPS_PATH)\/cxcore\/hdrLOCAL_CXXFLAGS := -DHAVE_CONFIG_HLOCAL_MODULE := cxcore<br \/>\nLOCAL_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)############################<br \/>\n# cv<br \/>\n############################<br \/>\ninclude $(CLEAR_VARS)<\/p>\n<p>LOCAL_PATH := $(APPS_PATH)\/cv\/src<br \/>\nLOCAL_C_INCLUDES := $(APPS_PATH)\/cv\/hdr $(APPS_PATH)\/cxcore\/hdr $(APPS_PATH)\/stl\/hdr<\/p>\n<p>LOCAL_MODULE := cv<br \/>\nLOCAL_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<\/p>\n<p>LOCAL_STATIC_LIBRARIES := cxcore stl<\/p>\n<p>include $(BUILD_STATIC_LIBRARY)<\/p>\n<p>############################<br \/>\n# cvaux<br \/>\n############################<br \/>\ninclude $(CLEAR_VARS)<\/p>\n<p>LOCAL_PATH := $(APPS_PATH)\/cvaux\/src<br \/>\nLOCAL_C_INCLUDES := $(APPS_PATH)\/cvaux\/hdr $(APPS_PATH)\/cv\/hdr $(APPS_PATH)\/cv\/src $(APPS_PATH)\/cxcore\/hdr $(APPS_PATH)\/stl\/hdr<\/p>\n<p>LOCAL_MODULE := cvaux<br \/>\nLOCAL_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<\/p>\n<p># failed to compile<br \/>\n#cv3dtracker.cpp<\/p>\n<p>LOCAL_STATIC_LIBRARIES := cv cxcore stl<\/p>\n<p>include $(BUILD_STATIC_LIBRARY)<\/ol>\n<p>The <em>~\/android_ndk_3\/apps\/hello-jni\/Application.mk<\/em> file needs to be updated as follows<\/p>\n<ol>APP_PROJECT_PATH := $(call my-dir)\/project<br \/>\nAPP_MODULES \u00a0 \u00a0 \u00a0:= stl cxcore cv cvaux hello-jni<\/ol>\n<p>To build the project go to <em>~\/android_ndk_3<\/em> and type<\/p>\n<ol>make APP=hello-jni<\/ol>\n<p>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.<\/p>\n<p>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.<\/p>\n<p><strong>Running facedetect openCV example<\/strong><\/p>\n<p>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.<\/p>\n<p>The declaration looks like this<\/p>\n<ol>void<br \/>\nJava_com_example_hellojni_HelloJni_FaceDetect( JNIEnv* env, jobject thiz, jbyteArray jyuv_buff, int w, int h, jbyteArray jbgra_buff)<br \/>\n{<br \/>\n&#8230;<br \/>\n}<\/ol>\n<p>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.<\/p>\n<p style=\"text-align: center;\"><a onclick=\"javascript:pageTracker._trackPageview('\/outgoing\/www.computer-vision-software.com\/blog\/wp-content\/uploads\/2010\/04\/Picture-18.jpg');\"  href=\"http:\/\/www.computer-vision-software.com\/blog\/wp-content\/uploads\/2010\/04\/Picture-18.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" title=\"Picture 18\" src=\"http:\/\/www.computer-vision-software.com\/blog\/wp-content\/uploads\/2010\/04\/Picture-18-300x225.jpg\" alt=\"\" width=\"300\" height=\"225\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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).<\/p>\n","protected":false},"author":35,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[84],"tags":[69,7,29,70,6],"class_list":["post-116","post","type-post","status-publish","format-standard","hentry","category-opencv","tag-android","tag-arm","tag-face-detection","tag-ndk","tag-opencv"],"_links":{"self":[{"href":"http:\/\/www.computer-vision-software.com\/blog\/wp-json\/wp\/v2\/posts\/116","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.computer-vision-software.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.computer-vision-software.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.computer-vision-software.com\/blog\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"http:\/\/www.computer-vision-software.com\/blog\/wp-json\/wp\/v2\/comments?post=116"}],"version-history":[{"count":0,"href":"http:\/\/www.computer-vision-software.com\/blog\/wp-json\/wp\/v2\/posts\/116\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.computer-vision-software.com\/blog\/wp-json\/wp\/v2\/media?parent=116"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.computer-vision-software.com\/blog\/wp-json\/wp\/v2\/categories?post=116"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.computer-vision-software.com\/blog\/wp-json\/wp\/v2\/tags?post=116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}