PICCANTE  0.4
The hottest HDR imaging library!
filter_white_balance.hpp
Go to the documentation of this file.
1 /*
2 
3 PICCANTE
4 The hottest HDR imaging library!
5 http://vcg.isti.cnr.it/piccante
6 
7 Copyright (C) 2014
8 Visual Computing Laboratory - ISTI CNR
9 http://vcg.isti.cnr.it
10 First author: Francesco Banterle
11 
12 This Source Code Form is subject to the terms of the Mozilla Public
13 License, v. 2.0. If a copy of the MPL was not distributed with this
14 file, You can obtain one at http://mozilla.org/MPL/2.0/.
15 
16 */
17 
18 #ifndef PIC_FILTERING_FILTER_WHITE_BALANCE_HPP
19 #define PIC_FILTERING_FILTER_WHITE_BALANCE_HPP
20 
21 #include "../util/std_util.hpp"
22 
23 #include "../filtering/filter.hpp"
24 #include "../util/array.hpp"
25 
26 namespace pic {
27 
32 {
33 protected:
34 
35  float *white;
36  int nWhite;
37 
44  void ProcessBBox(Image *dst, ImageVec src, BBox *box)
45  {
46  if(white == NULL) {
47  return;
48  }
49 
50  int width = src[0]->width;
51  int channels = src[0]->channels;
52  float *data = src[0]->data;
53 
54  int transformChannels = MIN(channels, nWhite);
55 
56  for(int j = box->y0; j < box->y1; j++) {
57  int c = j * width;
58 
59  for(int i = box->x0; i < box->x1; i++) {
60  int indOut = c + i;
61  int ind = indOut * channels;
62 
63  for(int k = 0; k < transformChannels; k++) {
64  dst->data[ind + k] = data[ind + k] * white[k];
65  }
66  }
67  }
68  }
69 
70 public:
71 
77  {
78  white = NULL;
79  nWhite = -1;
80  }
81 
88  FilterWhiteBalance(float *white, unsigned int nWhite, bool bComputeScalingFactors)
89  {
90  this->white = NULL;
91  this->nWhite = -1;
92 
93  update(white, nWhite, bComputeScalingFactors);
94  }
95 
97  {
99  nWhite = -1;
100  }
101 
108  static float *getScalingFactors(float *white, int nWhite)
109  {
110  if(white == NULL || nWhite < 1) {
111  return NULL;
112  }
113 
114  float white_mean = Arrayf::sum(white, nWhite) / float(nWhite);
115 
116  float *out = new float[nWhite];
117 
118  for(int i = 0; i < nWhite; i++) {
119  if(white[i] > 0.0f) {
120  out[i] = white_mean / white[i];
121  } else {
122  out[i] = 1.0f;
123  }
124  }
125 
126  return out;
127  }
128 
135  void update(float *white, unsigned int nWhite, bool bComputeScalingFactors)
136  {
137  this->nWhite = nWhite;
138 
139  delete_vec_s(this->white);
140 
141  if(bComputeScalingFactors) {
142  this->white = getScalingFactors(white, nWhite);
143  } else {
144  this->white = new float[nWhite];
145  memcpy(this->white, white, sizeof(float) * nWhite);
146  }
147 
148  for(unsigned int i = 0; i < nWhite; i++) {
149  if(fabsf(this->white[i]) <= 1e-9f) {
150  this->white[i] = 1.0f;
151  }
152  }
153  }
154 
162  static Image* execute(Image *imgIn, float *white_color, Image *out)
163  {
164  if(imgIn == NULL || white_color == NULL) {
165  return NULL;
166  }
167 
168  FilterWhiteBalance flt_wb(white_color, imgIn->channels, true);
169  out = flt_wb.Process(Single(imgIn), out);
170 
171  return out;
172  }
173 
183  static Image* execute(Image *imgIn, int x, int y, bool bRobust = true, Image *out = NULL)
184  {
185  if(imgIn == NULL) {
186  return NULL;
187  }
188 
189  float *white_color = NULL;
190 
191  int patchSize = 5;
192 
193  if(!bRobust) {
194  white_color = (*imgIn)(x, y);
195  } else {
196  BBox patch(x - patchSize, x + patchSize, y - patchSize, y + patchSize);
197  white_color = imgIn->getMeanVal(&patch, NULL);
198  }
199 
200  out = execute(imgIn, white_color, out);
201 
202  if(bRobust) {
203  delete_vec_s(white_color);
204  }
205 
206  return out;
207  }
208 };
209 
210 } // end namespace pic
211 
212 #endif /* PIC_FILTERING_FILTER_WHITE_BALANCE_HPP */
213 
The BBox class manages the creation of bounding boxes for images.
Definition: bbox.hpp:29
float * data
data is the main buffer where pixel values are stored.
Definition: image.hpp:91
int channels
Definition: image.hpp:80
std::vector< Image * > ImageVec
ImageVec an std::vector of pic::Image.
Definition: image_vec.hpp:29
int x0
Definition: bbox.hpp:32
float * getMeanVal(BBox *box, float *ret)
getMeanVal computes the mean for the current Image.
The Filter class.
Definition: filter.hpp:50
virtual Image * Process(ImageVec imgIn, Image *imgOut)
Process.
Definition: filter.hpp:390
void ProcessBBox(Image *dst, ImageVec src, BBox *box)
ProcessBBox.
Definition: filter_white_balance.hpp:44
T * delete_vec_s(T *data)
delete_vec_s
Definition: std_util.hpp:138
float * white
Definition: filter_white_balance.hpp:35
FilterWhiteBalance()
FilterWhiteBalance.
Definition: filter_white_balance.hpp:76
int y0
Definition: bbox.hpp:32
static float * getScalingFactors(float *white, int nWhite)
getScalingFactors
Definition: filter_white_balance.hpp:108
static Image * execute(Image *imgIn, int x, int y, bool bRobust=true, Image *out=NULL)
execute
Definition: filter_white_balance.hpp:183
The FilterWhiteBalance class.
Definition: filter_white_balance.hpp:31
static T sum(T *data, int size)
sum
Definition: array.hpp:416
int nWhite
Definition: filter_white_balance.hpp:36
#define MIN(a, b)
Definition: math.hpp:69
The Image class stores an image as buffer of float.
Definition: image.hpp:60
static Image * execute(Image *imgIn, float *white_color, Image *out)
execute
Definition: filter_white_balance.hpp:162
virtual void f(FilterFData *data)
f
Definition: filter_radial_basis_function.hpp:69
void update(float *white, unsigned int nWhite, bool bComputeScalingFactors)
update
Definition: filter_white_balance.hpp:135
~FilterWhiteBalance()
Definition: filter_white_balance.hpp:96
PIC_INLINE ImageVec Single(Image *img)
Single creates an std::vector which contains img; this is for filters input.
Definition: image_vec.hpp:36
Definition: bilateral_separation.hpp:25
FilterWhiteBalance(float *white, unsigned int nWhite, bool bComputeScalingFactors)
FilterWhiteBalance.
Definition: filter_white_balance.hpp:88