% 2017-0928    2012-0228
% h1P.m - compare histograms of 4 images of 256,32,16,8 levels,
%         respectively, by histogram equalization
m=512; n=512;
fin1=fopen('D23.raw','r');   % beach pebbles
I=fread(fin1,m*n,'uint8=>uint8'); fclose(fin1);
A=reshape(I,m,n); A=A';
% IA=double(I)/255.0; % Normalization
Z=A; H=zeros(256);
for i=1:m
  for j=1:n   g=1+Z(i,j);  H(g)=H(g)+1;  end
end
subplot(2,4,1)
imshow(A)
X=0:255;
subplot(2,4,5)
plot(X,H,'b-')
xlabel('256 possible levels')
%
% Histogram Equalization with 32 levels
Z=histeq(A,32); H=zeros(256);
for i=1:m
  for j=1:n   g=1+Z(i,j);  H(g)=H(g)+1;  end
end
subplot(2,4,2)
imshow(Z)
subplot(2,4,6)
plot(X,H,'b-')
xlabel('32 levels')
%
% Histogram Equalization with 16 levels
Z=histeq(A,16); H=zeros(256);
for i=1:m
  for j=1:n   g=1+Z(i,j);  H(g)=H(g)+1;  end
end
subplot(2,4,3)
imshow(Z)
subplot(2,4,7)
plot(X,H,'b-')
xlabel('16 levels')
% bar(H) ???
%
% Histogram Equalization with 8 levels
Z=histeq(A,8); H=zeros(256);
for i=1:m
  for j=1:n   g=1+Z(i,j);  H(g)=H(g)+1;  end
end
subplot(2,4,4)
imshow(Z)
subplot(2,4,8)
plot(X,H,'b-')
xlabel('8 levels')