Monday, October 21, 2013

How to Use Ceres-Solver Library for Android-NDK

After using Levenberg-Marquardt optimization library from Lourakis in Android-NDK, this time I tried another library that offers non-linear least square solver called Ceres-Solver Library from Google.

From the specification and the demo, it is clear that this library shows impressive result (google map demo). And most importantly, this library already put one folder JNI inside it. It means we can quickly use it for our Android application.

For augmented reality application, I use ceres-solver for minimizing the reprojection error of the camera pose that is produced by our algorithm (refer this work for the detail). The result is comparable with the result I produced in the last post using Levenberg-Marquardt library. The good point is this solver offers sparse solver as well, and as written in the tutorial, it can be applied for solving the bundle adjustment step, which is suitable for our purpose.

The step for using ceres-solver for android-ndk:
  1. download the library from (eigen library is necessary as well)
  2. go to the jni folder after extracting it
  3. build the library using ndk-build. it will yield libceres.a
  4. grab the libceres.a
  5. we will use the libceres.a as a static library for our project
Now, in our project, let's use the libceres.a and this is my that uses libceres.a

  LOCAL_PATH := $(call my-dir)

  include $(CLEAR_VARS)
  LOCAL_MODULE    := ceres
  LOCAL_SRC_FILES := libceres.a

  include $(CLEAR_VARS)

  LOCAL_C_INCLUDES += ceres-solver-1.7.0/include
  LOCAL_C_INCLUDES += ceres-solver-1.7.0/internal/ceres/miniglog

  LOCAL_MODULE    := myproject
  LOCAL_SRC_FILES := jni_part.cpp  
  LOCAL_LDLIBS +=  -llog -ldl 

Don't forget to change the path of the ceres-solver and eigen includes path.

In my error project, I use the linear-solver type DENSE_SCHUR as shown in the bundle adjustment tutorial. I also tried to change using SPARSE_NORMAL_CHOLESKY that can run faster but less accurate because I guess our problem is not dealing with sparse data. This solver can help to produce the camera pose even in the extreme tilt.

This is the snapshot of my implementation of shape tracking in android using ceres-solver. The camera pose can be computed as shown by the red outline in the images. Tested using Nexus 7.

Shape Tracking for Augmented Reality on Android Tablet

Shape Tracking for Augmented Reality on Android Tablet


  1. Hi,

    I am developing an app with ceres on Android. I'm using cmake. I have built ceres and lapack, blas (comes with levmar). I have static libs of them. However I could not compile them all in Android side. I got undefined reference for 2 functions inside lapack and blas(dgels_and dsyrk_). I have added include files. The functions are there but linker could not link it. Could you please help me with the issue?

  2. Hi, just want to know where do we put the ectracted lib before go to the jni folder? thanks!