原理见下图
——参考文献:自动白平衡算法的研究与实现 史榕(华东师范大学信息科学与技术学院,上海)
matlab代码:
%基于QCGP(QuadraticGW&PR)Combining 算法的白平衡处理程序close all;
clear;
clc;
I0=imread('lenna.bmp');
R0=I0(:,:,1);
G0=I0(:,:,2);
B0=I0(:,:,3);
%添加处理代码
subplot(331); %表示3(行数)*3(列数)的图像,1代表所画图形的序号
imshow(I0);
title('Original Image');
subplot(334);imhist(R0,256);title('R0');
subplot(335);imhist(G0,256);title('G0');
subplot(336);imhist(B0,256);title('B0');
%找到RGB最大值
[m,n]=size(R0);
Rmax=double(max(max(R0)));
Gmax=double(max(max(G0)));
Bmax=double(max(max(B0)));
%计算Kmax
Kmax=double((Rmax+Gmax+Bmax))/3;
R2max=(double(Rmax))^2;
G2max=(double(Gmax))^2;
B2max=(double(Bmax))^2;
%找到三分量均值
Rave=double(mean(R0(:)));
Gave=double(mean(G0(:)));
Bave=double(mean(B0(:)));
%计算Kave
Kave=double((Rave+Gave+Bave))/3;
R2ave=(double(Rave))^2;
G2ave=(double(Gave))^2;
B2ave=(double(Bave))^2;
%反解参数u'v'即xr1、xr2
Ar=[R2ave,Rave;R2max,Rmax];
B=[Kave;Kmax];
%计算R分量白平衡
xr=Ar\B;
xr(1);
xr(2);
for i=1:1:m
for j=1:1:n
R1(i,j)=xr(1)*(double(R0(i,j)))^2+xr(2)*double(R0(i,j));
end
end
R1=uint8(R1);
%计算G分量白平衡
Ag=[G2ave,Gave;G2max,Gmax];
xg=Ag\B;
xg(1);
xg(2);
for i=1:1:m
for j=1:1:n
G1(i,j)=xg(1)*(double(G0(i,j)))^2+xg(2)*double(G0(i,j));
end
end
G1=uint8(G1);
%计算B分量白平衡
Ab=[B2ave,Bave;B2max,Bmax];
xb=Ab\B;
xb(1);
xb(2);
for i=1:1:m
for j=1:1:n
B1(i,j)=xb(1)*(double(B0(i,j)))^2+xb(2)*double(B0(i,j));
end
end
B1=uint8(B1);
subplot(337);imhist(R1,256);title('R1');
subplot(338);imhist(G1,256);title('G1');
subplot(339);imhist(B1,256);title('B1');
%合成
I1(:,:,1)=R1;
I1(:,:,2)=G1;
I1(:,:,3)=B1;
subplot(333);imshow(I1);title('I1');
点击这里下载lenna图 lenna.bmp : http://download.csdn.net/detail/thunderclaus/5501199
效果如下