PICCANTE  0.4
The hottest HDR imaging library!
filter_bilateral_2df.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_BILATERAL_2DF_HPP
19 #define PIC_FILTERING_FILTER_BILATERAL_2DF_HPP
20 
21 #include "../base.hpp"
22 
23 #include "../util/std_util.hpp"
24 
25 #include "../util/array.hpp"
26 
27 #include "../util/precomputed_gaussian.hpp"
28 
29 #include "../filtering/filter.hpp"
30 
31 namespace pic {
32 
37 {
38 protected:
40 
42 
49  void ProcessBBox(Image *dst, ImageVec src, BBox *box);
50 
51 public:
56 
62  FilterBilateral2DF(float sigma_s, float sigma_r);
63 
65 
70  std::string signature()
71  {
72  return genBilString("F", sigma_s, sigma_r);
73  }
74 
83  static Image *execute(Image *imgIn, Image *imgOut,
84  float sigma_s, float sigma_r)
85  {
86  //filter
88  Image *out = filter.Process(Single(imgIn), imgOut);
89  return out;
90  }
91 };
92 
94 {
95  pg = NULL;
96 }
97 
98 PIC_INLINE FilterBilateral2DF::FilterBilateral2DF(float sigma_s, float sigma_r) : Filter()
99 {
100  //protected values are assigned/computed
101  this->sigma_s = sigma_s > 0.0f ? sigma_s : 1.0f;
102  this->sigma_r = sigma_r > 0.0f ? sigma_r : 0.01f;
103  this->sigma_r_sq_2 = this->sigma_r * this->sigma_r * 2.0f;
104 
105  //Precomputation of the Gaussian filter
107 }
108 
110 {
111  pg = delete_s(pg);
112 }
113 
115 {
116  int channels = dst->channels;
117 
118  //Filtering
119  Image *edge, *base;
120 
121  if(src.size() > 1) {
122  //Joint/Cross Bilateral Filtering
123  base = src[0];
124  edge = src[1];
125  } else {
126  base = src[0];
127  edge = src[0];
128  }
129 
130  for(int j = box->y0; j < box->y1; j++) {
131  for(int i = box->x0; i < box->x1; i++) {
132  //Convolution kernel
133  float *dst_data = (*dst)(i, j);
134 
135  float *data_edge = (*edge)(i, j);
136 
137  Arrayf::assign(0.0f, dst_data, channels);
138 
139  float sum = 0.0f;
140  for(int k = 0; k < pg->kernelSize; k++) {
141  int cj = j + k - pg->halfKernelSize;
142 
143  for(int l = 0; l < pg->kernelSize; l++) {
144  int ci = i + l - pg->halfKernelSize;
145 
146  //Spatial weight
147  float G1 = pg->coeff[k] * pg->coeff[l];
148 
149  //Range weight
150  float *cur_edge = (*edge)(ci, cj);
151 
152  float tmp = Arrayf::distanceSq(data_edge, cur_edge, edge->channels);
153  float G2 = expf(-tmp / sigma_r_sq_2);
154 
155  //Weight
156  float weight = G1 * G2;
157 
158  //filter
159  float *base_data_cur = (*base)(ci, cj);
160 
161  for(int m = 0; m < channels; m++) {
162  dst_data[m] += base_data_cur[m] * weight;
163  }
164 
165  sum += weight;
166  }
167  }
168 
169  //normalization
170  if(sum > 0.0f) {
171  Arrayf::div(dst_data, channels, sum);
172  } else {
173  float *base_data = (*base)(i, j);
174  Arrayf::assign(base_data, channels, dst_data);
175  }
176  }
177  }
178 }
179 
180 } // end namespace pic
181 
182 #endif /* PIC_FILTERING_FILTER_BILATERAL_2DF_HPP */
183 
void ProcessBBox(Image *dst, ImageVec src, BBox *box)
ProcessBBox.
Definition: filter_bilateral_2df.hpp:114
The BBox class manages the creation of bounding boxes for images.
Definition: bbox.hpp:29
int channels
Definition: image.hpp:80
T * delete_s(T *data)
delete_s
Definition: std_util.hpp:123
std::vector< Image * > ImageVec
ImageVec an std::vector of pic::Image.
Definition: image_vec.hpp:29
int x0
Definition: bbox.hpp:32
The Filter class.
Definition: filter.hpp:50
virtual Image * Process(ImageVec imgIn, Image *imgOut)
Process.
Definition: filter.hpp:390
float * coeff
Definition: precomputed_gaussian.hpp:67
The FilterBilateral2DF class.
Definition: filter_bilateral_2df.hpp:36
static Image * execute(Image *imgIn, Image *imgOut, float sigma_s, float sigma_r)
execute
Definition: filter_bilateral_2df.hpp:83
static T distanceSq(T *data0, T *data1, int n)
distanceSq
Definition: array.hpp:195
PrecomputedGaussian * pg
Definition: filter_bilateral_2df.hpp:41
int y0
Definition: bbox.hpp:32
std::string genBilString(std::string type, float sigma_s, float sigma_r)
genBilString
Definition: string.hpp:366
#define PIC_INLINE
Definition: base.hpp:33
The Image class stores an image as buffer of float.
Definition: image.hpp:60
float sigma_r_sq_2
Definition: filter_bilateral_2df.hpp:39
float sigma_s
Definition: filter_bilateral_2df.hpp:39
static void div(T *data, int size, T value)
div
Definition: array.hpp:353
The PrecomputedGaussian class.
Definition: precomputed_gaussian.hpp:30
int halfKernelSize
Definition: precomputed_gaussian.hpp:66
virtual void f(FilterFData *data)
f
Definition: filter_radial_basis_function.hpp:69
int kernelSize
Definition: precomputed_gaussian.hpp:66
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
static T * assign(T *data, int size, T *ret)
assign
Definition: array.hpp:464
float sigma_r
Definition: filter_bilateral_2df.hpp:39
FilterBilateral2DF()
FilterBilateral2DF.
Definition: filter_bilateral_2df.hpp:93
std::string signature()
signature
Definition: filter_bilateral_2df.hpp:70
~FilterBilateral2DF()
Definition: filter_bilateral_2df.hpp:109