This tutorial presents an implementation in C.Bibby's ECCV2008 paper [1] .

The foreground and background pixels are determined by image segmentation. In my implementation, levelsets are used for solving the problem. (I've attached a matlab file, showing you how levelsets works in a simple way.) Besides, I guest you need to read [2] for implementing the registration code. They got very good explanations and matlab code [3] on their project homepage. Try the code from "forward addiction" first, it starts from the very beginning of image warping, make you gets a good understanding on gradient descent algorithms. Then you can go on with their "inverse compositional" algorithm, which is really efficient.

Level sets should not be initialized by Gaussian smoothing of manually thresholded binary image, but instead, it is recommended to initialized it with distance transformed signed distance function (SDF), like:

```
nr=size(img,1);
nc=size(img,2);
bw=zeros(nr,nc);
bw(floor(nr*.25):floor(nr*.75),floor(nc*.25):floor(nc*.75))=1;
phi=bwdist(bw);
phi=phi-bwdist(1-bw);
```

in C/C++, this can be

```
void icvInitializeLevelSet(CvMat * mask, CvMat * phi)
{
assert(mask->cols==phi->cols);
assert(mask->rows==phi->rows);
int nr=mask->rows,nc=mask->cols;
assert(CV_MAT_TYPE(phi->type)==CV_32F);
CvMat * distmap = cvCreateMat(nr, nc, CV_32F);
CvMat * tmp = cvCreateMat(nr, nc, CV_8U);
cvCmpS(mask,0,tmp,CV_CMP_EQ);
cvDistTransform(tmp,phi);
cvDistTransform(mask,distmap);
cvSub(distmap,phi,phi);
cvReleaseMat(&tmp);
cvReleaseMat(&distmap);
}
```

To calculate histogram in MATLAB, you need the 'imhist' function. In RGB color space, what you need to construct is 3D volume of accumulated values of images pixels that belongs to the range. Say you wanted to calculate histogram of a RGB color image, where image pixels range from 0 to 255, you want them categorized into 8x8x8 volume. Each voxel contains accumulated values in 0~31,32~63 and so on. Also, RED pixels are calculated in x-axis, GREEN pixels in y, BLUE pixel in z-axis. Therefore, they finally form a volume of histogram values. You can try it first in gray-scale images first.

*to be continued...*

[1] | Bibby, Charles, and Ian Reid. "Robust real-time visual tracking using pixel-wise posteriors." Computer Vision–ECCV 2008. Springer Berlin Heidelberg, 2008. 831-844. |

[2] | http://www.ri.cmu.edu/research_project_detail.html?project_id=515&menu_id=261 |

[3] | Baker, Simon, and Iain Matthews. "Lucas-kanade 20 years on: A unifying framework." International Journal of Computer Vision 56.3 (2004): 221-255. |