Histogram matching of two images using cdf
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 &gt; 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) &gt;= 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 &gt;! 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 &gt;= 0$ (check)
Then $z_k* = z_k$
$n = r_k $



Here are some illustration images: