Ver.1 is OBSOLETE and NOT MAINTAINED ANYMORE. Please use ver.2 instead.


This page explains how to use the 3-points version. For the general n-pts version, see matlab/demo.m in GitHub.

How to use the Matlab version with sample data


After unzipping the downloaded file, you will have the following files in a single directory named tnm.

Start Matlab, and change the working directory to the tnm directory. Run demo.m and you should see the following outputs and a pop-up window that visualizes the results.

> demo.m
Average reprojection error by TNM : 0.353531 pixel.

==== Parameters by TNM ====
R =
    0.9552   -0.0316   -0.2942
    0.0262    0.9994   -0.0221
    0.2947    0.0134    0.9555
T =
n1 =
n2 =
n3 =
d1 =
d2 =
d3 =

Brief descriptions of the code

tnm.m : top-level driver

The figure below illustrates the measurement model of our method.


In this figure, we use the following notations. Notice that \({}^Yx\) denotes \(x\) in \(Y\) coordinate system.

The goal is to estimate the relative rotation \(R\) and translation \(T\) between the camera \(C\) and the reference \(X\) which satisfy

\({}^Cp^i = R \cdot {}^Xp^i + T (i=1,2,3) \). (1)

by knowing \(q^i_j (i,j=1,2,3)\), the projections of \({}^Xp^i\) observed via three different mirrors \(\pi_j (j=1,2,3)\) of unknown positions. The orientation and the position of t he mirrors (the distances from the camera to the mirrors) are also estimated as a result. So the input / output of the above-mentioned tnm.m can be expressed as follows.


Our mirror-based calibration method consists of the following three steps, and we provide implementations corresponding to them.

  1. sub_p3p.m: P3P per mirrored image.

    • Input:
      • \({}^Xp^i (i=1,2,3)\) : Three reference points, and
      • \(q^i (i)\) : their projections observed via a mirror \(\pi\).
    • Output: Up to 4 possible solutions of \({}^Cp^i\)
  2. sub_tnm_orth.m: Unique solution selection using an orthogonality constraint.

    • Input: 64 sets of \({}^Cp^i_j (i,j=1,2,3)\).
    • Output: The set of \({}^Cp^i_j (i,j=1,2,3)\) which follows the orthogonality constraint best.
  3. sub_tnm_rt.m: Linear estimation of \(R\) and \(T\).
    • Input: \({}^Cp^i_j (i,j=1,2,3)\).
    • Output: \(R, T, n_j, d_j (j=1,2,3)\).

And in addition, we use the following sub-function for evaluation purpose.

How to use the OpenCV version with sample data


After unzipping the downloaded file, you will have the following files in a single directory named ./tnm-opencv.

This code requires OpenCV 2.3. We used libcv-dev package in Debian wheezy. For Debian/Ubuntu, try

 $ sudo apt-get -f install libcv-dev libcvaux-dev libhighgui-dev g++ make

to install requisite libraries and compilation tools, and then exec

 $ make

to compile the code.

For Visual C++ on Windows, please simply import, sub_solveP3P.h, and tnm.h into a new project, and compile it (you need to setup additional include path and libraries for Op enCV, of course).

Once compiled, run the binary (named demo) with no args in the ./tnm-opencv directory.

 $ ./demo
 loading 'data/input1.txt' = [263.854279, 284.595978;
   380.608337, 284.673645;
   261.375946, 355.315582]

 loading 'data/input2.txt' = [187.462204, 264.845764;
   302.664276, 261.313538;
   183.416656, 338.876984]

 loading 'data/input3.txt' = [393.80719, 301.828278;
   529.568542, 311.569794;
   391.23999, 374.259766]

 loading 'data/model.txt' = [0, 0, 0;
   175, 0, 0;
   0, 100, 0]

 loading 'data/camera.txt' = [487.910797, 0, 324.31308;
   0, 487.558441, 237.003937;
   0, 0, 1]

 Average reprojection error by TNM : 0.353531 pixels

 ==== Parameters by TNM ====

 R  = [0.9552278293542109, -0.0315692738655991, -0.2941822138995512;
   0.02622804982091903, 0.9994120031779081, -0.02208477544627536;
   0.294706436016986, 0.01338016634873504, 0.9554941589139341]

 T  = [71.67155976406129; 84.34036742140127; 120.2696306131992]

 n1 = [0.2679446927390354; 0.03066392138399924; -0.9629461903753189]

 n2 = [0.4356434955333516; 0.08437398428883026; -0.8961561111629551]

 n3 = [-0.04427539436835728; -0.01119106464381498; -0.9989566805050478]

 d1 = 386.23

 d2 = 355.048

 d3 = 404.707

This program automatically loads data from data/input{1,2,3}.txt, and then outputs the estimated parameters to stdout.

Brief descriptions of the code

The structure of the code is very straightforward. Please visit the main() function in first. The flow of main() is:

  1. load data from files (model.txt, input{1,2,3}.txt) by load() function defined in
  2. run calibration by tnm() function defined in tnm.h. What this function does inside is:
  3. call sub_solveP3P() defined in sub_solveP3P.h for each input data (= the Matlab function in tnm-matlab/sub_p3p.m),
  4. call sub_tnm_orth() defined in tnm.h (= the Matlab function in tnm-matlab/sub_tnm_orth.m), and
  5. call sub_tnm_rt() defined in tnm.h (= the Matlab function in tnm-matlab/sub_tnm_rt.m).
  6. run sub_reproj() defined in to evaluate the reprojection error (= the Matlab function in tnm-matlab/sub_reproj.m).

So to re-use the code for your own project, copy tnm.h and sub_solveP3P.h, and then use tnm() for calibration. To understand how to prepare the data, please consult the load() f unction in

How to use the code with your own data

To calibrate your own system, please follow the process below. In short, update data/model.txt and data/input{1,2,3}.txt, then run the program again.

  1. Suppose you have a reference object \(X\) and a camera \(C\).
    1. The intrinsic parameters of \(C\) should be provided. You can use OpenCV to estimate them.
  2. Capture three images of \(X\) as \(I_1, I_2, I_3\) via mirrors \(\pi_1, \pi_2, \pi_3\) under different poses.
  3. Detect three points of \(X\) for each of the images (\(I_1, I_2, I_3\)).
    1. Here we assume that the images are rectified (undistorted) using the intrinsic parameters before the detection.
    2. If you used a chessboard pattern as \(X\) and used OpenCV findChessboardCorners() to detect it in \(I_1, I_2, I_3\) automatically, you need to flip the detection result because the detector does not account for the observation via mirror.
  4. Store the data into data/model.txt and data/input{1,2,3}.txt.

    1. data/model.txt is a line-oriented plain text file each of lines represents the 3D position of a reference point in \(X\).
      0.000000 0.000000 0.000000
      175.000000 0.000000 0.000000
      0.000000 100.000000 0.000000
  5. data/input{1,2,3}.txt are also line-oriented plain text files, and each of lines represents the 2D projection of the corresponding 3D reference point in data/model.txt. For examp le, the first line below is the projection of the first reference point \((0,0,0)\) defined in the first line of data/model.txt.

    380.608337 284.673645
    263.854279 284.595978
    377.368225 350.688141
  6. Exec the demo program again, and you will get the result.