File Exchange

## program Color balancing

version 1.0 (1.35 KB) by

A simple program for color balancing

3.11111
8 Ratings

Updated

A simple program for color balancing

Andrew Marchment

### Andrew Marchment (view profile)

Here is the code with the corrections Austin suggested 6 years ago:

%Program for white balancing%
%Author : Jeny Rajan
%usage : W=imbalance(im)
%im is the color image array to be corrected.
function W=wbalance(im)
im2=im;
im1=rgb2ycbcr(im);
Lu=im1(:,:,1);
Cb=im1(:,:,2);
Cr=im1(:,:,3);
[x y z]=size(im);
tst=zeros(x,y);
Mb=sum(sum(Cb));
Mr=sum(sum(Cr));
Mb=Mb/(x*y);
Mr=Mr/(x*y);
Db=sum(sum(Cb-Mb))/(x*y);
Dr=sum(sum(Cr-Mr))/(x*y);
cnt=1;
Ciny = zeros(1,x*y); %
for i=1:x
for j=1:y
b1=Cb(i,j)-(Mb+Db*sign(Mb));
b2=Cr(i,j)-(1.5*Mr+Dr*sign(Mr));
if (b1<(1.5*Db) & b2<(1.5*Dr))
Ciny(cnt)=Lu(i,j);
tst(i,j)=Lu(i,j);
cnt=cnt+1;
end
end
end
Ciny(cnt:end) = []; %
cnt=cnt-1;
iy=sort(Ciny,'descend');
nn=round(cnt/10);
Ciny2(1:nn)=iy(1:nn);
mn=min(Ciny2);
c=0;
for i=1:x
for j=1:y
if tst(i,j)<mn
tst(i,j)=0;
else
tst(i,j)=1;
c=c+1;
end
end
end
R=im(:,:,1);
G=im(:,:,2);
B=im(:,:,3);
R=double(R).*tst;
G=double(G).*tst;
B=double(B).*tst;
Rav=mean(mean(R));
Gav=mean(mean(G));
Bav=mean(mean(B));
Ymax=double(max(max(Lu)))/15;
Rgain=Ymax/Rav;
Ggain=Ymax/Gav;
Bgain=Ymax/Bav;
im(:,:,1)=im(:,:,1)*Rgain;
im(:,:,2)=im(:,:,2)*Ggain;
im(:,:,3)=im(:,:,3)*Bgain;
W=im;
figure,imshow(im2,[]),title('Original Image');
figure,imshow(im,[]),title('Corrected Image');

xu rui

### xu rui (view profile)

Matlab code could run,but the speed is ont fast.Maybe there are double for-loop in code.

Nicolas

### Nicolas (view profile)

The function is badly written. There are unused variables lying around, and the 2 for loops could be removed in favor of the usual Matlab tricks for computational efficiency.
The results are not that visually satisfying either - the color temperature is definitely off on some of the images I tried this function on.

hussain

### hussain (view profile)

hai , i am hussain
i need how to improve an image white balanced , and colour corrected....
plz help me

Yu

Changkaizhao

ji

very good

Austin

### Austin (view profile)

Thanks this was useful. This script is slow only because of the first double for-loop doing a dynamic memory allocation each time the condition is met. MATLAB is terrible with dynamic memory allocations. Simply add:

Ciny = zeros(1,x*y);

right before the outer for loop (for i=1:x), and then add:

Ciny(cnt:end) = [];

right at the end of the for loop (right above cnt=cnt-1; and then the sort) to get rid of the unused memory. This speeds up the code tremendously.

Thanks.

Tony

### Tony (view profile)

The program is good. But computationally intensive.

pupu QQ

### pupu QQ (view profile)

the effect is good enough,better than some comercial software in China ,for exmaple neo imaging.theory based on a taiwan paper on IEEE

Zhao Quanyou

program for this paper
A Novel Automatic White Balance Method
For Digital Still Cameras
good!

randy copl

Jacques Landry

No commenting of any kind in the file! No description! Mileading keywords. Color or white balances require a calibration file. This does not so is useless.