SoftPOSIT in C/Fortran

NOTE: Please link to this page via PURL: http://purl.org/hanshuo/softposit

Introduction

This page describes a C/Fortran implementation of the SoftPOSIT algorithm. It is heavily based on the original MATLAB script developed by Prof. Daniel DeMenthon.

Download

You can obtain the latest copy from GitHub:

$ git clone git://github.com:hanshuo/softposit.git

If you do not have git installed, download and install it from the Git official website.

Installation Requirement

To install, you will need:

  • A Fortran 90 compiler. Under Linux, gfortran (contained in the GNU Compiler Collection) is recommended.
  • (Optional) A C compiler, if you need the C implementation. Under Linux, gcc is recommended.

The installation has only been tested under Ubuntu Linux 10.04 (Lucid), with gfortran and gcc. The Intel MKL version under test is 10.1.1.019 (other 10.x versions should be fine).

gcc and gfortran

For Ubuntu Linux, you can get gcc and gfortran by running:

$ apt-get install build-essential

Intel MKL

The Intel MKL is free under Linux for non-commercial use. You can obtain a copy from the official website.

You will also need to compile the Fortran 95 interfaces to BLAS and LAPACK (part of the Intel MKL). Before you start, you may want to read the “Fortran 95 Interfaces to LAPACK and BLAS” section in the Intel MKL User's guide. Below is the instruction for gfortran under Linux. If you are using other Fortran compilers and/or platforms, refer to the Intel MKL User's Guide and modify the makefiles as appropriate.

Install Fortran 95 interface to BLAS/LAPACK for gfortran

In the following, replace <MKL_DIR> with the MKL installation directory, e.g. /opt/intel/mkl/10.1.1.019.

BLAS:

$ cp softposit/imkl_install/blas95/makefile <MKL_DIR>/interfaces/blas95/
$ cd <MKL_DIR>/interfaces/blas95
$ make lib32
$ cp *.mod <MKL_DIR>/lib/include/

This will create the following a new file named libmkl_blas95.a under <MKL_DIR>/lib/32/, and three new files under <MKL_DIR>/include: mkl77_blas.mod, mkl95_precision.mod, mkl95_blas.mod.

LAPACK:

$ cp softposit/imkl_install/lapack95/makefile <MKL_DIR>/interfaces/lapack95/
$ cd <MKL_DIR>/interfaces/lapack95
$ make lib32
$ cp *.mod <MKL_DIR>/lib/include/

This will create a new file named libmkl_lapack95.a under <MKL_DIR>/lib/32/, and four new files under <MKL_DIR>/lib/include: mkl77_lapack.mod, mkl77_lapack1.mod, mkl95_precision.mod, mkl95_lapack.mod.

Usage

In C/C++, the SoftPOSIT algorithm can be called by:

softposit_(float* rot, float* trans, int* foundPose, int* imagePts, float* worldPts, \ 
           int* nbImagePts, int* nbWorldPts, float* beta0, float* noiseStd, \
           float* initRot, float* initTrans, float* focalLength, int* center);             

Inputs:

  • imagePts: nx2, image points coordinates
  • worldPts: mx3, world points coordinates
  • nbImagePts: number of image points
  • nbWorldPts: number of world points
  • beta0, noiseStd: parameters used in SoftPOSIT (see the MATLAB code for detail)
  • initRot: 3×3, initial guess of the rotation matrix
  • initTrans: 3×1, initial guess of the translation vector
  • focalLength: focal length of the camera
  • center: 2×1, coordinate of the image plane center

Outputs:

  • rot: 3×3, rotation matrix
  • trans: 3×1, translation vector
  • foundPose: if a proper pose is found, foundPose = 1, otherwise foundPose = 0

Note: All matrices are in column-major order. For example, the following 5×2 matrix

imagePts = 612  117
           486  145
           567  206
           476  234
           441  329

should be defined in C/C++ as

int imagePts[] = {612, 486, 567, 476, 441, 117, 145, 206, 234, 329};

Example

First set the following environment variables (replace the string for MKL_DIR with the correct MKL installation directory):

$ export MKL_DIR=/opt/intel/mkl/10.1.1.019
$ export LD_LIBRARY_PATH=$MKL_DIR/lib/32:$LD_LIBRARY_PATH

Then compile and run the C demo program:

$ cd softposit/src/demo
$ make
$ ./demo_c

You should be able to see the same result as running demo.m in MATLAB. For other demos in C++ or Fortran, refer to the README file therein.

Comments and Feedbacks?

Comments and feedbacks are very welcome. You can reach me at hanshuo_at_caltech_dot_edu.