%Program for white balancing%
%Author : Jeny Rajan
%usage : W=imbalance(im)
%im is the color image array to be corrected.
% function W=wbalance(im)
%==========================Get file=============================
function W=awb_dtm(im)
Rorg=im(:,:,1);
Gorg=im(:,:,2);
Borg=im(:,:,3);
[x y z]=size(im);
im2=im;
im1=rgb2ycbcr(im);
%Lu=im1(:,:,1);
%Cb=im1(:,:,2);
%Cr=im1(:,:,3);
Lu= 0.299*double(Rorg) + 0.587*double(Gorg) + 0.114*double(Borg) + 16;
Cb=-0.169*double(Rorg) - 0.331*double(Gorg) + 0.500*double(Borg) + 128;
Cr= 0.500*double(Rorg) - 0.418*double(Gorg) - 0.082*double(Borg) + 128;
%Lu= 0.183*double(Rorg) + 0.624*double(Gorg) + 0.062*double(Borg) +16;
%Cb=-0.101*double(Rorg) - 0.338*double(Gorg) + 0.439*double(Borg) +128;
%Cr= 0.439*double(Rorg) - 0.399*double(Gorg) - 0.040*double(Borg) +128;
Lu_rsp = reshape(Lu, 1, x*y)';
R_rsp = reshape(Rorg, 1, x*y)';
G_rsp = reshape(Gorg, 1, x*y)';
B_rsp = reshape(Borg, 1, x*y)';
RGB_rsp = [R_rsp G_rsp B_rsp];
%======================Calc Mean-Valve============================
tst=zeros(x,y);
Mb=sum(sum(Cb));
Mr=sum(sum(Cr));
Mb=Mb/(x*y);
Mr=Mr/(x*y);
%======================Calc ave asb diff===========================
Db=sum(sum(abs(Cb-Mb)))/(x*y);
Dr=sum(sum(abs(Cr-Mr)))/(x*y);
%=====================White Region Collect=========================
b1=Cb-(Mb+Db*sign(Mb-128));
b2=Cr-(1.5*Mr+Dr*sign(Mr-128))+64;
b1=abs(b1);
b2=abs(b2);
b1_ref=1.5*Db;
b2_ref=1.5*Dr;
Lu_index=find(b1<b1_ref & b2<b2_ref);
Lu_index2=size(Lu_index);
Ciny=Lu_rsp(Lu_index,:);
cnt=Lu_index2(1);
iy=sort(Ciny,'descend');
nn=round(cnt/10);
Ciny2(1:nn)=iy(1:nn);
mn=min(Ciny2);
tst=zeros(x,y);
Rtst_rshp = reshape(tst, 1, x*y)';
Gtst_rshp = reshape(tst, 1, x*y)';
Btst_rshp = reshape(tst, 1, x*y)';
Rtst_rshp(Lu_index)=RGB_rsp(Lu_index,1);
Gtst_rshp(Lu_index)=RGB_rsp(Lu_index,2);
Btst_rshp(Lu_index)=RGB_rsp(Lu_index,3);
RGBtst_rshp =[Rtst_rshp Gtst_rshp Btst_rshp];
outR = reshape(Rtst_rshp, x, y);
outG = reshape(Gtst_rshp, x, y);
outB = reshape(Btst_rshp, x, y);
im3(:,:,1) = uint8(outR);
im3(:,:,2) = uint8(outG);
im3(:,:,3) = uint8(outB);
figure,imshow(im3,[]),title('Corrected Image');
Lu_index3=find(Lu_rsp>=mn);
Lu_index4=size(Lu_index3 );
tst=zeros(x,y);
tst_rshp = reshape(tst, 1, x*y)';
Rtst_rshp = reshape(tst, 1, x*y)';
Gtst_rshp = reshape(tst, 1, x*y)';
Btst_rshp = reshape(tst, 1, x*y)';
Rtst_rshp(Lu_index3)=RGB_rsp(Lu_index3,1);
Gtst_rshp(Lu_index3)=RGB_rsp(Lu_index3,2);
Btst_rshp(Lu_index3)=RGB_rsp(Lu_index3,3);
tst_rshp(Lu_index3)=1;
RGBtst_rshp =[Rtst_rshp Gtst_rshp Btst_rshp];
outR = reshape(Rtst_rshp, x, y);
outG = reshape(Gtst_rshp, x, y);
outB = reshape(Btst_rshp, x, y);
im3(:,:,1) = uint8(outR);
im3(:,:,2) = uint8(outG);
im3(:,:,3) = uint8(outB);
figure,imshow(im3,[]),title('Corrected Image');
c=Lu_index4(1);
tst=reshape(tst_rshp, x, y);
%======================White Point Adjust==============================
R=im(:,:,1);
G=im(:,:,2);
B=im(:,:,3);
R=double(R).*tst;
G=double(G).*tst;
B=double(B).*tst;
im3(:,:,1) = R;
im3(:,:,2) = G;
im3(:,:,3) = B;
figure,imshow(im3,[]),title('White Point Image');
%Rav=mean(mean(R));
%Gav=mean(mean(G));
%Bav=mean(mean(B));
Rav=sum(sum(double(R)))/c;
Gav=sum(sum(double(G)))/c;
Bav=sum(sum(double(B)))/c;
%Ymax=double(max(max(Lu)))/15;
Ymax=double(max(max(Lu)));
%Rgain=Ymax/Rav;
%Ggain=Ymax/Gav;
%Bgain=Ymax/Bav;
Rgain=Gav/Rav;
Ggain=1;
Bgain=Gav/Bav;
im(:,:,1)=im(:,:,1)*Rgain;
im(:,:,2)=im(:,:,2)*Ggain;
im(:,:,3)=im(:,:,3)*Bgain;
W=uint8(im);
figure,imshow(im2,[]),title('Original Image');
figure,imshow(im,[]),title('Corrected Image');