1 #ifndef AUTOWHITEBALANCE_H
2 #define AUTOWHITEBALANCE_H
11 void printScalar(Scalar scalar) {
12 cout << scalar[0] <<
":" << scalar[1] <<
":" << scalar[2] << endl;
20 offsetLeft =
new int[channels];
21 offsetRight =
new int[channels];
23 for(
int channel=0;channel<channels;channel++) {
24 offsetLeft[channel] = 0;
25 offsetRight[channel] = 255;
35 Scalar left(offsetLeft[0], offsetLeft[1], offsetLeft[2]);
36 Scalar right(offsetRight[0], offsetRight[1], offsetRight[2]);
38 divide(Scalar(255,255,255), right-left, scale);
39 printScalar(right-left);
43 image = (image - shift);
44 multiply(image, scale, image);
50 static Mat getHistForChannel(Mat imageWithOneChannel) {
55 float range[] = { 0, 256 } ;
56 const float* histRange = { range };
58 bool uniform =
true;
bool accumulate =
false;
63 calcHist( &imageWithOneChannel, 1, 0, Mat(), hist, 1, &histSize, &histRange, uniform, accumulate );
70 vector<Mat> bgr_planes;
71 split(image, bgr_planes);
72 int size = image.rows * image.cols;
74 for(
int channel=0;channel < image.channels(); channel++) {
75 Mat hist = getHistForChannel(bgr_planes[channel]);
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;
82 if(integralLeft > 1) {
87 for(
int i=hist.rows; i >= 0; --i) {
88 integralRight += hist.at<
float>(i)*100.0/size;
89 if(integralRight > 1) {
101 vector<Mat> bgr_planes;
102 split( src, bgr_planes );
104 Mat b_hist, g_hist, r_hist;
106 b_hist = getHistForChannel(bgr_planes[0]);
107 g_hist = getHistForChannel(bgr_planes[1]);
108 r_hist = getHistForChannel(bgr_planes[2]);
113 int hist_w = 512;
int hist_h = 400;
114 int bin_w = cvRound( (
double) hist_w/histSize );
116 Mat histImage( hist_h, hist_w, CV_8UC3, Scalar( 0,0,0) );
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() );
124 for(
int i = 1; i < histSize; i++ )
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 );
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