How to compute the Cumulative Distribution Function of an image in MATLAB

David Norman picture David Norman · Aug 9, 2014 · Viewed 9.1k times · Source

I need to compute the Cumulative Distribution Function of an image. I normalized the values using the following code:

im = imread('cameraman.tif');
im_hist = imhist(im);
tf = cumsum(im_hist); %transformation function
tf_norm = tf / max(tf);
plot(tf_norm), axis tight

Also, when the CDF function is plotted, does the plot have to be somewhat a straight line which ideally should be a straight line to represent equal representation for pixel intensities?

Answer

cifz picture cifz · Aug 9, 2014

You can obtain a CDF very easily by:

A = imread('cameraman.tif');    
[histIM, bins] = imhist(A);
cdf = cumsum(counts) / sum(counts);
plot(cdf); % If you want to be more precise on the X axis plot it against bins

For the famous cameraman.tif it results in:

enter image description here

As for your second question. When the histogram is perfectly equalized (i.e. when at each intensity correspond roughly the same number of pixels) your CDF will look like a straight 45° line.

EDIT: Strictly speaking cumsum alone is not a proper CDF as a CDF describe a probability, hence it must obey probability axioms. In particular the first axiom of probability tell us that a probability value should lie in the range [0 ... 1] and cumsum alone does not guarantee that.