Sie sind auf Seite 1von 10

Automatic White Balance Algorithm

Pi19404
December 17, 2012

Contents

Contents
Automatic White Balance
0.1 0.2 0.3 0.4 0.5 Color Constancy . . . . . . . . . Gray world assumption . . . . normalized minkowski p-norm References . . . . . . . . . . . . Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

4
4 4 6 7 7

3 | 10

Automatic White Balance

Automatic White Balance


0.1 Color Constancy
Color constancy is a mechanism of detection of color indepdent of light source. The light source many introduce color casts in captured digital images To solve the colour constancy problem a standard method is to estimate the colour of the prevailing light and then, at the second stage, remove it. Once the color of light in indiviudal channels is obtained the each color pixel is normalized by a scaling factor . Two of the most commonly used simple techniques for estimating the colour of the light are the Grey-World and Max-RGB algorithms. These two methods will work well in practice if the average scene colour is grey or the maximum is white

0.2 Gray world assumption


The Gray World Assumption is a white balance method that assumes that your scene, on average, is a neutral gray. Grayworld assumption hold if we have a good distribution of colors in the scene. Assuming that we have a good distribution of colors in our scene,the average reected color is assumed to be the color of the light. Therefore, we can estimate the illuminant color cast by looking at the average color and comparing it to gray. Gray world algorithm produces an estimate of illuminant by computing the mean of each channel of the image. One of the methods of normalization is that The mean of the three compoments is used as illumination estimate of the image. To normalize the image of channel i avg ,the pixel value is scaled by s1 = avgi where avgi is the channel mean and avg is the illumination estimate . Another method of normalization is normalizing to the maximum channel by scaling by si max ( avgR , avgG , avgB ) ri = avgi

4 | 10

Attached are output of standard contrast stretching and present algorithm

(a) gray world normalization (b) gray world normalization method 1 method 2
Figure 1: Example 1

5 | 10

Automatic White Balance


0.3 normalized minkowski p-norm

Another variant to estimate illumination vector is to calculate the normalized minkowski p-norm for each color channel color constancy algorithm which is based on Minkowski norm - for each color channel, the Minkowski p-norm is calculated and the normalized result forms the estimated illumination vector.

ei =

1 N

1/p

pi
i

where summation if over all the pixels. Gray world algorithm is obtained by setting p=1 The shades of gray, is given by Finlayson and Trezzi concluded that using Minkowski norm with p = 6 gave the best estimation results on their data set.

(a) minkowski norm 6 normal- (b) minkowski norm 6 normalization method 1 ization method 2
Figure 2: Example 2

6 | 10

0.4 References
1. A New Color Balancing Method http://www.stanford.edu/class/ee368/Project_ 11/Reports/Cohen_A_New_Color_Balancing_Method.pdfauthor 2. http://research.edm.uhasselt.be/~oancuti/Underwater_CVPR_2012/CVPR_underwater_ final.pdf 3. http://www.imaging.org/IST/store/epub.cfm?abstrid=32117 4. https://ueaeprints.uea.ac.uk/23682/

0.5 Code
The code in included in the document however le can opened/saved by clicking on the icon

11

13

15

17

/ This i s a sample program t o demonstrate gray world a l g o r i t h m s using opencv Copyright (C) 2012 by p i 1 9 4 0 4 This program i s f r e e s o f t w a r e : you can r e d i s t r i b u t e i t and/or modify i t under t h e terms o f t h e GNU General P u b l i c L i c e n s e as published by t h e Free Software Foundation , e i t h e r v e r s i o n 3 o f t h e License , or ( a t your o p t i o n ) any l a t e r v e r s i o n . This program i s d i s t r i b u t e d i n t h e hope t h a t i t w i l l be u s e f u l , but WITHOUT ANY WARRANTY; without even t h e implied warranty o f MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See t h e GNU General P u b l i c L i c e n s e f o r more d e t a i l s . You should have r e c e i v e d a copy o f t h e GNU General P u b l i c L i c e n s e along with t h i s program . I f not , s e e < h t t p ://www. gnu . org/ l i c e n s e s / >. / / @ f i l e c o l o r _ b a l a n c e _ 2 . cpp @ b r i e f This i s a sample program t o demonstrate gray world a l g o r i t h m s @author p i 1 9 4 0 4 <pi19404@gmail . com> / # i n c l u d e " opencv2/highgui/highgui . hpp " # i n c l u d e " opencv2/imgproc/imgproc . hpp " # i n c l u d e <iostream > # i n c l u d e < s t d i o . h> using namespace s t d ; using namespace cv ;

19

21

23

25

27

29

31

33

7 | 10

Automatic White Balance


35

void gray_world ( Mat s r c 1 , f l o a t ml , f l o a t ma, f l o a t mb, i n t p ) { ma= 0 ; mb= 0 ; ml = 0 ; f o r ( i n t i = 0 ; i < s r c 1 . rows ; i ++) { f o r ( i n t j = 0 ; j < s r c 1 . c o l s ; j ++) { Vec3b v1= s r c 1 . at <cv : : Vec3b >( i , j ) ; f l o a t l c =pow( v1 . v a l [ 0 ] , p ) ; f l o a t ac=pow( v1 . v a l [ 1 ] , p ) ; f l o a t bc=pow( v1 . v a l [ 2 ] , p ) ; ma=ma+ac ; mb=mb+bc ; ml=ml+ l c ; } }

37

39

41

43

45

47

49

51

53

55

57

59

61

ma=pow ( ( f l o a t ) ma/( s r c 1 . c o l s s r c 1 . rows ) , ( f l o a t ) 1/p ) ; mb=pow ( ( f l o a t ) mb/( s r c 1 . c o l s s r c 1 . rows ) , ( f l o a t ) 1/p ) ; ml=pow ( ( f l o a t ) ml/( s r c 1 . c o l s s r c 1 . rows ) , ( f l o a t ) 1/p ) ; }

63

65

i n t main ( i n t argc , char argv [ ] ) {

67

69

71

73

i f ( a r g c <5 ) { c e r r << " Usage : c o l o r _ b a l a n c e 1 { i n p u t _ f i l e n a m e } o u t p u t f i l e norm ( > 0 ) norm_method ( 0 or 1 ) " << endl ; return 1; } namedWindow ( "AAA" , CV_WINDOW_AUTOSIZE ) ; s t r i n g f i l e =argv [ 1 ] ; Mat s r c ; s r c =cv : : imread ( f i l e . c _ s t r ( ) , 1 ) ; Mat d s t ; i f ( s r c . empty ( ) ) { c e r r << " Cannot load image " << " " << f i l e . c _ s t r ( ) << endl ; return 1; } else

75

77

79

81

83

85

87

8 | 10

c e r r << s r c . c o l s << " " << s r c . rows << " " ;


89

91

s r c . copyTo ( d s t ) ;

93

95

Mat s r c 1 ; s r c . copyTo ( s r c 1 ) ;

97

99

101

v e c t o r <Mat> b g r _ p l a n e s ;
103

105

s p l i t ( src , bgr_planes ) ; f l o a t ma=0 ,mb=0 ,ml = 0 ;

107

109

i n t p= a t o i ( argv [ 3 ] ) ; i n t m= a t o i ( argv [ 4 ] ) ; gray_world ( s r c ,&ml,&ma,&mb, p ) ;

111

113

115

117

119

f l o a t r =(ma+mb+ml ) / 3 ; i f (m==1) { r =(ma+mb+ml ) / 3 ; r =max (ma,mb) ; r =max ( r , ml ) ; }

121

123

125

127

129

131

cv : : Mat_<cv : : Vec3b > : : c o n s t _ i t e r a t o r i t = s r c 1 . begin <cv : : Vec3b > ( ) ; cv : : Mat_<cv : : Vec3b > : : c o n s t _ i t e r a t o r i t e n d = s r c 1 . end<cv : : Vec3b > ( ) ; cv : : Mat_<cv : : Vec3b > : : i t e r a t o r i t o u t = d s t . begin <cv : : Vec3b > ( ) ;

133

135

137

f o r ( ; i t ! = i t e n d ; ++ i t , ++ i t o u t ) {

139

cv : : Vec3b v1= i t ;
141

9 | 10

Automatic White Balance


f l o a t l =v1 . v a l [ 0 ] ; f l o a t a=v1 . v a l [ 1 ] ; f l o a t b=v1 . v a l [ 2 ] ; a=a ( r/ma) ; b= b ( r/mb) ; l = l ( r/ml ) ; i f ( a >255) a =255; i f ( b >255) b=255; i f ( l >255) l =255; v1 . v a l [ 0 ] = l ; v1 . v a l [ 1 ] = a ; v1 . v a l [ 2 ] = b ; i t o u t =v1 ;

143

145

147

149

151

153

155

157

159

161

163

165

167

169

171

// c v t C o l o r ( dst , dst , CV_Lab2BGR ) ; imwrite ( argv [ 2 ] , d s t ) ;

173

175

cv : : imshow ( " BBB " , d s t ) ; waitKey ( 0 ) ; return 0;

177

179

181

} Listing 1: A Simple Color Balance algorithm in opencv

10 | 10

Das könnte Ihnen auch gefallen