clear all;
close all;

[A,Amap] = imread('IMAGES2/night.tif'); % Refer to saved image
figure;
colormap(Amap);
image(A);
title('Original Image');

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

[newR,newG,newB] = mbal(r,g,b);
SRAbalanced = [newR,newG,newB];
SRAbalanced = scale(SRAbalanced,0,1);
myGamma = 2.7;
newMap = (SRAbalanced).^(1/myGamma);
figure; colormap(newMap); 
image(A);
title('SR color-balanced Image');

cd IMAGES2
tiffwrite(A,newMap,'balanced_night')
cd ..
