Histogram matching of two images using cdf

I have already calculated the cdf values of each image now i have to map the reference image values to target . I cannot use histogram equalization or interpolation I simply have to map them .

function S = histM(t, target)
if size(target,2)>1
%an image, replace the image with its cdf
target = getImageCDF(target);
end

%get the CDF for the input image
tcdf = getImageCDF(t);

% .... what shall be done here to map the values of reference to target

%subfunction: compute the CDF for an image that started as uint8
function cdf = getImageCDF(img)
bins = 0:255;
H = hist(img(:), bins);
Hmod = H + eps(sum(H));
cdf = [0, cumsum(Hmod)/sum(Hmod)];


First you'll have to compute the histogram of one of the two images.

H = hist(img(:), bins);


Next find the cdf of the image:

 cdf = [0, cumsum(Hmod)/sum(Hmod)];


Next you'll have to make the second image follow the exact same cdf of the first image. This is usually known as histogram specification. Here is the algorithm:

Say you have a 3 bit image, that is $8$ different intensity values $0 > 7$. Now as for the second image

Find it's cdf as well.

Now as seen in the following image. The $r_k$ is the pixel value of the second image.

$s_k$ is the cdf of the second image.

$z_k$ is the cdf of the firsk image (desired histogram).

$z_{k}*$ and $n$ are computed as the following:

$(z_k - s_k) >= 0$ for smallest value of $n \dots$ and $n = g_k$

How exactly: Let's compute $z_k*$

$z_k - s_k = 0 - 0.15 = -0.15 >! 0$ (not greater than zero) increase $z_k$ and keep $s_k$ as is until you satisify the criteria.

Until you reach: $0.15 - 0.15 = 0 >= 0$ (check) Then $z_k* = z_k$ $n = r_k$

Here are some illustration images:

• can you explain that with a matlab code . – Tanya May 12 '14 at 3:44