% histogram  equalization of an image
clear 
x=[13 14 2 14;
10 2 5 9;
15 15 3 15;
15 8 13 1] % 4x4 4-bit input image
L=16; % set number of gray levels
[R,C]=size(x); % dimensions of the image
RC=R*C; % number of pixels in the image
his=zeros(1,L); % compute the histogram
for k=1:R
  for m=1:C
    his(x(k,m)+1) = his(x(k,m)+1) + 1;
  end
end
pr=his/RC; % normalized histogram
cump=cumsum(pr) % cumulative histogram
%
heq=round(cump*(L-1)); % equalized histogram
for k=1:R  % compute histogram equalized image
  for m=1:C
    xeq(k,m)=heq(x(k,m)+1);
  end
end
xeq % histogram equalized image






