David Norman
David Norman

Reputation: 301

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

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?

Upvotes: 1

Views: 11245

Answers (2)

cifz
cifz

Reputation: 1078

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.

Upvotes: 3

Juan David
Juan David

Reputation: 2797

function icdf = imgcdf(img)
% Author: Javier Montoya ([email protected]).
%         http://www.lis.ic.unicamp.br/~jmontoya
%
% IMGCDF calculates the Cumulative Distribution Function of image I.
% Input parameters:
%    img: image I (passed as a bidimensional matrix).
% Ouput parameters:
%    icdf: cumulative distribution function.
%
% See also: IMGHIST
%
% Usage:
%    I    = imread('tire.tif');
%    icdf = imgcdf(I);
%    figure; stem(icdf); title('Cumulative Distribution Function (CDF)');

   if exist('img', 'var') == 0
      error('Error: Specify an input image.');
   end

   icdf    = [];
   ihist   = imghist(img);
   maxgval = 255;
   icdf    = zeros(1,maxgval);

   icdf(1)= ihist(1);
   for i=2:1:maxgval+1
      icdf(i) = ihist(i) + icdf(i-1);
   end
end

Its not my code but it works for me! Also check the cdf function in the statistics toolbox

Upvotes: 0

Related Questions