function y=GWbalance(indexImage)

% Perform grayworld assumtion color balancing.

r=indexImage(:,1);
g=indexImage(:,2);
b=indexImage(:,3);

avgR = mean(r);
avgG = mean(g);
avgB = mean(b);
avgRGB = [avgR avgG avgB];
grayValue = (avgR + avgG + avgB)/3
scaleValue = grayValue./avgRGB

newI(:,1) = scaleValue(1) * r;
newI(:,2) = scaleValue(2) * g;
newI(:,3) = scaleValue(3) * b;

y=newI;
