Question of unary term (data term) of the graph cut method I am trying to apply graph cut method for my segmentation task. I found some example codes at
Graph_Cut_Demo.
Part of the codes are showing below
img = im2double( imread([ImageDir 'cat.jpg']) ); 
[ny,nx,nc] = size(img); 
d = reshape( img, ny*nx, nc );  
k = 2; % number of clusters 
[l0 c] = kmeans( d, k ); 
l0 = reshape( l0, ny, nx ); 

% For each class, the data term Dc measures the distance of 
% each pixel value to the class prototype. For simplicity, standard 
% Euclidean distance is used. Mahalanobis distance (weighted by class 
% covariances) might improve the results in some cases.  Note that the 
% image intensity values are in the [0,1] interval, which provides 
% normalization.   
 
Dc = zeros( ny, nx, k ); 
for i = 1:k 
  dif = d - repmat( c(i,:), ny*nx,1 ); 
  Dc(:,:,i) = reshape( sum(dif.^2,2), ny, nx ); 
end 

It seems that the method used k-means clustering to initialise the graph and get the data term Dc. However, I don't understand how they calculate this data term. Why they use
dif = d - repmat( c(i,:), ny*nx,1 ); 

In the comments thy said the data term Dc measures the distance of each pixel value to the class prototype. What is the class prototype, and why it can be determined by k-means label?
In another implementation Graph_Cut_Demo2, it used
% calculate the data cost per cluster center
Dc = zeros([sz(1:2) k],'single');
for ci=1:k
    % use covariance matrix per cluster
    icv = inv(cov(d(l0==ci,:)));    
    dif = d- repmat(c(ci,:), [size(d,1) 1]);
    % data cost is minus log likelihood of the pixel to belong to each
    % cluster according to its RGB value
    Dc(:,:,ci) = reshape(sum((dif*icv).*dif./2,2),sz(1:2));
end

This confused me a lot. Why they calculate the covariance matrix and how they formed the data term using minus log likelihood? Any papers or descriptions available for these implementation?
 A: Graph cuts can optimize discrete (energy) functions of the form:
$E(\ell) = \sum_iD_i(\ell_i) + \sum_{i,j}S_{i,j}(\ell_i, \ell_j)$
where the $\ell_i$ assign a label to the i-th pixel, e.g. in your case they tell you which cluster, that was previously identified by KMeans, the pixel should belong to.
It seems that your code uses Gaussian potentials as data terms. As energy functions can be related to the negative log of probability distributions, the Gaussian potential also appears in the log domain in your graph cuts method, i.e. 
$D_i(c) = -\log(\mathcal{N}(I_i | \mu_c, \Sigma_c)) = (I_i - \mu_c)^T \Sigma_c^{-1}(I_i - \mu_c) + \text{const}$
So basically you want to penalize quadratic differences from the pixel color to the mean color of the assigned cluster. The constant term does not depend on the cluster assignment and hence can be ignored.
The first piece of code uses the identity matrix for each $\Sigma_c$ and the second piece of code estimates $\Sigma_c$ from all datapoints clustered together by KMeans. The latter is done to account for clusters where the pixel colors are not spread evenly around the mean color in all directions. This picture illustrates this http://mathworks.com/matlabcentral/fileexchange/screenshots/6502/original.jpg.
