% histogram  specification  
clear  
xref=[13,14,2,14;
    10,2,5,9;
    15,15,3,15;
    15,8,13,1];% ref image
xin=[11,13,0,13;
    7,0,2,5;
    15,15,1,15;
    15,4,11,0]; % input image
[R,C]=size(xin); % image dimensions
L=16 % set the number of gray levels
%
his=zeros(1,L); % cumulative histogram of ref
for k=1:R
  for m=1:C
    his(xref(k,m)+1) = his(xref(k,m)+1) + 1;
  end
end
pr_ref=his/sum(his);
c_ref=cumsum(pr_ref)
%
his=zeros(1,L);% cumulative histogram of input
for k=1:R
  for m=1:C
     his(xin(k,m)+1) = his(xin(k,m)+1) + 1;
  end
end
pr_in=his/sum(his);
c_in=cumsum(pr_in)
num=[];
%
for k=0:L-1 % map of g levels from input to output
  n=0;
    while (c_ref(n+1)+2*eps < c_in(k+1))  
    n=n+1;  
  end
  num=[num n];
end
num
for k=1:R % output image
  for m=1:C
    xo(k,m) = num(xin(k,m) + 1);
  end
end
xo
%%%%%%%%%%





