
%This compares the results of histogram equalization on a color checkerboard image
%using (a) RGB color representation and (a) YIQ color representation. Different colors.
%Colors are randomly generated, so they don't match figures in the text.

%Generate 8X8 color checkerboard with each color random strength between 0 and 0.7:
clear;X=0.7*rand(8,8,3);figure,image(X),axis off,title('Original dark checkerboard')
X=round(X*64)+1;%multiply by 64 and round to integer values.
%Perform histogram equalization on each RGB color component.
X1=X(:,:,1);PX1=hist(X1(:),0.5+[0:63]);FX1=[0 cumsum(PX1)];Y1=round(FX1(X1(:)))+1;
X2=X(:,:,2);PX2=hist(X2(:),0.5+[0:63]);FX2=[0 cumsum(PX2)];Y2=round(FX2(X2(:)))+1;
X3=X(:,:,3);PX3=hist(X3(:),0.5+[0:63]);FX3=[0 cumsum(PX3)];Y3=round(FX3(X3(:)))+1;
YY1=reshape(Y1,8,8);YY2=reshape(Y2,8,8);YY3=reshape(Y3,8,8);%reshape to 8X8 images.
YY(:,:,1)=YY1;YY(:,:,2)=YY2;YY(:,:,3)=YY3;YY=(YY-1)/63;%YY1=red;YY2=green;YY3=blue.
figure,imagesc(YY),axis off,title('Histogram equalization using RGB')

%Transform checkerboard image from RGB (X) to YIQ (Z) representation using Eq. (10.4):
A=[.299,.587,.114;.596,-.275,-.321;.212,-.523,.311];B=inv(A);
Z(:,:,1)=A(1,1)*X(:,:,1)+A(1,2)*X(:,:,2)+A(1,3)*X(:,:,3);
Z(:,:,2)=A(2,1)*X(:,:,1)+A(2,2)*X(:,:,2)+A(2,3)*X(:,:,3);
Z(:,:,3)=A(3,1)*X(:,:,1)+A(3,2)*X(:,:,2)+A(3,3)*X(:,:,3);

%Perform histogram equalization on only Y component in YIQ color representation:
Z1=Z(:,:,1);PZ1=hist(Z1(:),0.5+[0:255]);FZ1=[0 cumsum(PZ1)];ZZ1=round(FZ1(round(Z1(:))))+1;
ZZ2=reshape(ZZ1,8,8); %Transform from YIQ (Z) back to RGB (W) for display:
W(:,:,1)=B(1,1)*ZZ2(:,:,1)+B(1,2)*Z(:,:,2)+B(1,3)*Z(:,:,3);
W(:,:,2)=B(2,1)*ZZ2(:,:,1)+B(2,2)*Z(:,:,2)+B(2,3)*Z(:,:,3);
W(:,:,3)=B(3,1)*ZZ2(:,:,1)+B(3,2)*Z(:,:,2)+B(3,3)*Z(:,:,3);
%Now linearly scale so 0<W<1 (required by imagesc for color images):
W=(W-min(min(min(W))))/(max(max(max(W)))-min(min(min(W))));
figure,imagesc(W),axis off,title('Histogram equalization using YIQ')



