% Fig 3.19 
% Transformation function for histogram equalization
% Dr. W.D.Pan, UAH

clear all;
close all;
I = imread('pout.tif');
L = 256;
K = (L-1)/ numel(I);
[counter, bin] = imhist(I);

% Original mapping
origf = bin;

for i = 1: length(bin)
    if counter(i) > 0
        origf(i) = bin(i);
    else
        origf(i) = 0;
    end
end

plot(origf);

% Mapping after equalization
transf = bin;

sum = 0;
for i = 1: length(bin)
    sum = sum + counter(i);
    transf(i) = uint8(round(K * sum));
end
hold on;
plot(transf,'r');

