%
% colorH3.m 
% 2019-1125 Read a Color Image and print the (R,G,B) color histograms
% 
% m=100; n=100; npixel=m*n;
% I=imread('Nijubashi.jpg');  % (m * n) * 3 = (R,G,B)
% m=960; n=960; npixel=m*n;
I=imread('Nijubashi.jpg');  % (m * n) * 3 = (R,G,B)
[m, N]=size(I); n=N/3;      % [m, N, n]
npixel=m*n;
R=I(:,:,1);  G=I(:,:,2);  B=I(:,:,3);
hR=zeros(256);  hG=zeros(256);  hB=zeros(256);
for i=1:m
    for j=1:n
        r=1+R(i,j); g=1+G(i,j); b=1+B(i,j);
        hR(r)=hR(r)+1; 
        hG(g)=hG(g)+1;
        hB(b)=hB(b)+1;
    end
end
for k=1:256
   hR(k)=100.0*(hR(k)/npixel);
   hG(k)=100.0*(hG(k)/npixel);
   hB(k)=100.0*(hB(k)/npixel);
end
subplot(2,2,1)
imshow(I)
subplot(2,2,3)
L=0:255;
plot(L,hR,'r-',L,hG,'g-',L,hB,'b-')
title('RGB Histograms of 2448 \times 3264 image')
xlabel('Intensity Levels')
ylabel('Percentage %')
%
% 2nd Color Image Picture
% 
m=960; n=960; npixel=m*n;
I=imread('machupicchu01.jpg');  % (m * n) * 3 = (R,G,B)
I2=I;
R=I(:,:,1);  G=I(:,:,2);  B=I(:,:,3);
hR=zeros(256);  hG=zeros(256);  hB=zeros(256);
for i=1:m
    for j=1:n
        r=1+R(i,j); g=1+G(i,j); b=1+B(i,j);
        hR(r)=hR(r)+1; 
        hG(g)=hG(g)+1;
        hB(b)=hB(b)+1;
    end
end
for k=1:256
   hR(k)=100.0*(hR(k)/npixel);
   hG(k)=100.0*(hG(k)/npixel);
   hB(k)=100.0*(hB(k)/npixel);
end
subplot(2,2,2)
imshow(I2)
subplot(2,2,4)
L=0:255;
plot(L,hR,'r-',L,hG,'g-',L,hB,'b-')
title('RGB Histograms of 960 \times 960 image')
xlabel('Intensity Levels')
ylabel('Percentage %')