camera_interface
AutoWhiteBalance.h
Go to the documentation of this file.
1 #ifndef AUTOWHITEBALANCE_H
2 #define AUTOWHITEBALANCE_H
3 
4 #include <cv.h>
5 
6 using namespace cv;
7 using namespace std;
8 
10  private:
11  void printScalar(Scalar scalar) {
12  cout << scalar[0] << ":" << scalar[1] << ":" << scalar[2] << endl;
13  }
14  public:
15  int *offsetLeft;
17  int channels;
18 
19  AutoWhiteBalancer(int channels) {
20  offsetLeft = new int[channels];
21  offsetRight = new int[channels];
22 
23  for(int channel=0;channel<channels;channel++) {
24  offsetLeft[channel] = 0;
25  offsetRight[channel] = 255;
26  }
27  }
28 
30  delete[] offsetRight;
31  delete[] offsetLeft;
32  }
33 
34  void applyCalibration(Mat image) {
35  Scalar left(offsetLeft[0], offsetLeft[1], offsetLeft[2]);
36  Scalar right(offsetRight[0], offsetRight[1], offsetRight[2]);
37  Scalar scale;
38  divide(Scalar(255,255,255), right-left, scale);
39  printScalar(right-left);
40  printScalar(scale);
41  Scalar shift = left;
42 
43  image = (image - shift);
44  multiply(image, scale, image);
45  }
46 };
47 
49  private:
50  static Mat getHistForChannel(Mat imageWithOneChannel) {
52  int histSize = 256;
53 
55  float range[] = { 0, 256 } ;
56  const float* histRange = { range };
57 
58  bool uniform = true; bool accumulate = false;
59 
60  Mat hist;
61 
63  calcHist( &imageWithOneChannel, 1, 0, Mat(), hist, 1, &histSize, &histRange, uniform, accumulate );
64 
65  return hist;
66  }
67  public:
69  AutoWhiteBalancer* awb = new AutoWhiteBalancer(image.channels());
70  vector<Mat> bgr_planes;
71  split(image, bgr_planes);
72  int size = image.rows * image.cols;
73 
74  for(int channel=0;channel < image.channels(); channel++) {
75  Mat hist = getHistForChannel(bgr_planes[channel]);
76 
77  double integralLeft = 0;
78  double integralRight = 0;
79  for(int i=0; i<hist.rows; ++i) {
80  float val = hist.at<float>(i)*100.0/size;
81  integralLeft += val;
82  if(integralLeft > 1) {
83  awb->offsetLeft[channel] = i;
84  break;
85  }
86  }
87  for(int i=hist.rows; i >= 0; --i) {
88  integralRight += hist.at<float>(i)*100.0/size;
89  if(integralRight > 1) {
90  awb->offsetRight[channel] = i;
91  break;
92  }
93  }
94  }
95  return awb;
96 
97  }
98 
99  Mat getHistogram(Mat src) {
101  vector<Mat> bgr_planes;
102  split( src, bgr_planes );
103 
104  Mat b_hist, g_hist, r_hist;
105 
106  b_hist = getHistForChannel(bgr_planes[0]);
107  g_hist = getHistForChannel(bgr_planes[1]);
108  r_hist = getHistForChannel(bgr_planes[2]);
109 
110 
111  // Draw the histograms for B, G and R
112  int histSize = 256;
113  int hist_w = 512; int hist_h = 400;
114  int bin_w = cvRound( (double) hist_w/histSize );
115 
116  Mat histImage( hist_h, hist_w, CV_8UC3, Scalar( 0,0,0) );
117 
119  normalize(b_hist, b_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
120  normalize(g_hist, g_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
121  normalize(r_hist, r_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
122 
124  for( int i = 1; i < histSize; i++ )
125  {
126  line( histImage, Point( bin_w*(i-1), hist_h - cvRound(b_hist.at<float>(i-1)) ) ,
127  Point( bin_w*(i), hist_h - cvRound(b_hist.at<float>(i)) ),
128  Scalar( 255, 0, 0), 2, 8, 0 );
129  line( histImage, Point( bin_w*(i-1), hist_h - cvRound(g_hist.at<float>(i-1)) ) ,
130  Point( bin_w*(i), hist_h - cvRound(g_hist.at<float>(i)) ),
131  Scalar( 0, 255, 0), 2, 8, 0 );
132  line( histImage, Point( bin_w*(i-1), hist_h - cvRound(r_hist.at<float>(i-1)) ) ,
133  Point( bin_w*(i), hist_h - cvRound(r_hist.at<float>(i)) ),
134  Scalar( 0, 0, 255), 2, 8, 0 );
135  }
136  return histImage;
137  }
138 };
139 
140 #endif /* end of include guard: AUTOWHITEBALANCE_H */
Definition: AutoWhiteBalance.h:9
int * offsetLeft
Definition: AutoWhiteBalance.h:15
Mat getHistogram(Mat src)
Definition: AutoWhiteBalance.h:99
void applyCalibration(Mat image)
Definition: AutoWhiteBalance.h:34
AutoWhiteBalancer(int channels)
Definition: AutoWhiteBalance.h:19
int channels
Definition: AutoWhiteBalance.h:17
Definition: AutoWhiteBalance.h:48
int * offsetRight
Definition: AutoWhiteBalance.h:16
static AutoWhiteBalancer * createAutoWhiteBalancer(Mat image)
Definition: AutoWhiteBalance.h:68
~AutoWhiteBalancer()
Definition: AutoWhiteBalance.h:29