% MATLAB code for Histogram equalisation
% function to return resultant
% matrix and summary
function [f,r]=HistEq(k,max)
Freq=zeros(1,max);
[x,y]=size(k);
% Calculate frequency of each
% intensity value.
for i=1:x
for j=1:y
Freq(k(i,j)+1)=Freq(k(i,j)+1)+1;
end
end
% Calculate PDF for each intensity value.
PDF=zeros(1,max);
Total=x*y;
for i=1:max
PDF(i)=Freq(i)/Total;
end
% Calculate the CDF for each intensity value.
CDF=zeros(1,max);
CDF(1)=PDF(1);
for i=2:max
CDF(i)=CDF(i-1)+PDF(i);
end
% Multiply by Maximum intensity value
% and round off the result.
Result=zeros(1,max);
for i=1:max
Result(i)=uint8(CDF(i)*(max-1));
end
% Compute the Equalized image/matrix.
mat=zeros(size(k));
for i=1:x
for j=1:y
mat(i,j)=Result(k(i,j)+1);
end
end
f=mat;
r=Result;
end
% Utility code here.
k=[0, 1, 1, 3, 4;
7, 2, 5, 5, 7;
6, 3, 2, 1, 1;
1, 4, 4, 2, 1];
[new_matrix, summary]=HistEq(k,8);