彩色图像白平衡原理与程序(Matlab)
先贴Matlab代码,然后补理论
1 View Code 2 %Program for white balancing% 3 %Author : Jeny Rajan 4 %usage : W=imbalance(im) 5 %im is the color image array to be corrected. 6 7 function W=wbalance(im) 8 im2=im; 9 im1=rgb2ycbcr(im); 10 Lu=im1(:,:,1); 11 Cb=im1(:,:,2); 12 Cr=im1(:,:,3); 13 [x y z]=size(im); 14 Ciny = zeros(1,x*y); 15 tst=zeros(x,y); 16 Mb=sum(sum(Cb)); 17 Mr=sum(sum(Cr)); 18 Mb=Mb/(x*y); 19 Mr=Mr/(x*y); 20 Db=sum(sum(Cb-Mb))/(x*y); 21 Dr=sum(sum(Cr-Mr))/(x*y); 22 cnt=1; 23 24 for i=1:x 25 for j=1:y 26 b1=Cb(i,j)-(Mb+Db*sign(Mb)); 27 b2=Cr(i,j)-(1.5*Mr+Dr*sign(Mr)); 28 if (b1<(1.5*Db) && b2<(1.5*Dr))% & changed 29 Ciny(cnt)=Lu(i,j); 30 tst(i,j)=Lu(i,j); 31 cnt=cnt+1; 32 end 33 end 34 end 35 Ciny(cnt:end) = []; 36 cnt=cnt-1; 37 iy=sort(Ciny,'descend'); 38 nn=round(cnt/10); 39 Ciny2(1:nn)=iy(1:nn); 40 mn=min(Ciny2); 41 c=0; 42 for i=1:x 43 for j=1:y 44 if tst(i,j)<mn 45 tst(i,j)=0; 46 else 47 tst(i,j)=1; 48 c=c+1; 49 end 50 end 51 end 52 R=im(:,:,1); 53 G=im(:,:,2); 54 B=im(:,:,3); 55 R=double(R).*tst; 56 G=double(G).*tst; 57 B=double(B).*tst; 58 Rav=mean(mean(R)); 59 Gav=mean(mean(G)); 60 Bav=mean(mean(B)); 61 Ymax=double(max(max(Lu)))/15; 62 Rgain=Ymax/Rav; 63 Ggain=Ymax/Gav; 64 Bgain=Ymax/Bav; 65 im(:,:,1)=im(:,:,1)*Rgain; 66 im(:,:,2)=im(:,:,2)*Ggain; 67 im(:,:,3)=im(:,:,3)*Bgain; 68 W=im;