PICCANTE  0.4
The hottest HDR imaging library!
filter_kuwahara.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_KUWAHARA_HPP
19 #define PIC_FILTERING_FILTER_KUWAHARA_HPP
20 
21 #include "../filtering/filter.hpp"
22 
23 namespace pic {
24 
28 class FilterKuwahara: public Filter
29 {
30 protected:
31  unsigned int kernelSize;
32  unsigned int halfKernelSize;
33 
40  void ProcessBBox(Image *dst, ImageVec src, BBox *box)
41  {
42  int channels = dst->channels;
43 
44  Image *source = src[0];
45 
46  float *buf = new float[channels * 8];
47 
48  int start = 0;
49  float *m00 = &buf[start];
50  start += channels;
51  float *m10 = &buf[start];
52  start += channels;
53  float *m01 = &buf[start];
54  start += channels;
55  float *m11 = &buf[start];
56  start += channels;
57  float *s00 = &buf[start];
58  start += channels;
59  float *s10 = &buf[start];
60  start += channels;
61  float *s01 = &buf[start];
62  start += channels;
63  float *s11 = &buf[start];
64 
65  for(int m = box->z0; m < box->z1; m++) {
66  for(int j = box->y0; j < box->y1; j++) {
67  for(int i = box->x0; i < box->x1; i++) {
68  //1D Filtering
69  float mean = FLT_MAX;
70  int indx;
71  float tmpMean;
72 
73  float *tmpDst = (*dst)(i, j, m);
74 
75  //First block
76  BBox tmp00(i - halfKernelSize, i + 1, j - halfKernelSize, j + 1);
77  source->getMeanVal(&tmp00, m00);
78  source->getVarianceVal(m00, &tmp00, s00);
79 
80  tmpMean = 0.0f;
81 
82  for(int l = 0; l < channels; l++) {
83  tmpMean += s00[l];
84  }
85 
86  if(tmpMean < mean) {
87  mean = tmpMean;
88  indx = 0;
89  }
90 
91  //Second block
92  BBox tmp01(i, i + halfKernelSize, j - halfKernelSize, j + 1);
93  source->getMeanVal(&tmp01, m01);
94  source->getVarianceVal(m01, &tmp01, s01);
95 
96  tmpMean = 0.0f;
97 
98  for(int l = 0; l < channels; l++) {
99  tmpMean += s01[l];
100  }
101 
102  if(tmpMean < mean) {
103  mean = tmpMean;
104  indx = 1;
105  }
106 
107  //Third block
108  BBox tmp10(i - halfKernelSize, i + 1, j, j + halfKernelSize);
109  source->getMeanVal(&tmp10, m10);
110  source->getVarianceVal(m10, &tmp10, s10);
111 
112  tmpMean = 0.0f;
113 
114  for(int l = 0; l < channels; l++) {
115  tmpMean += s10[l];
116  }
117 
118  if(tmpMean < mean) {
119  mean = tmpMean;
120  indx = 2;
121  }
122 
123  //Fourth block
124  BBox tmp11(i, i + halfKernelSize, j, j + halfKernelSize);
125  source->getMeanVal(&tmp11, m11);
126  source->getVarianceVal(m11, &tmp11, s11);
127 
128  tmpMean = 0.0f;
129 
130  for(int l = 0; l < channels; l++) {
131  tmpMean += s11[l];
132  }
133 
134  if(tmpMean < mean) {
135  mean = tmpMean;
136  indx = 3;
137  }
138 
139  //final filtering
140  switch(indx) {
141  case 0: {
142  for(int l = 0; l < channels; l++) {
143  tmpDst[l] = m00[l];
144  }
145  }
146  break;
147 
148  case 1: {
149  for(int l = 0; l < channels; l++) {
150  tmpDst[l] = m01[l];
151  }
152  }
153  break;
154 
155  case 2: {
156  for(int l = 0; l < channels; l++) {
157  tmpDst[l] = m10[l];
158  }
159  }
160  break;
161 
162  case 3: {
163  for(int l = 0; l < channels; l++) {
164  tmpDst[l] = m11[l];
165  }
166  }
167  break;
168 
169  default: {
170  float *tmpSrc = (*source)(i, j, m);
171  for(int l = 0; l < channels; l++) {
172  tmpDst[l] = tmpSrc[l];
173  }
174  }break;
175  }
176  }
177  }
178  }
179 
180  delete[] buf;
181  }
182 
183 public:
189  {
191  }
192 
197  void update(int kernelSize)
198  {
199  if(kernelSize < 3) {
200  kernelSize = 3;
201  }
202 
203  this->kernelSize = kernelSize;
204  halfKernelSize = kernelSize >> 1;
205  }
206 
214  static Image *execute(Image *imgIn, Image *imgOut, int kernelSize)
215  {
216  FilterKuwahara filter(kernelSize);
217  return filter.Process(Single(imgIn), imgOut);
218  }
219 };
220 
221 } // end namespace pic
222 
223 #endif /* PIC_FILTERING_FILTER_KUWAHARA_HPP */
224 
The BBox class manages the creation of bounding boxes for images.
Definition: bbox.hpp:29
int channels
Definition: image.hpp:80
std::vector< Image * > ImageVec
ImageVec an std::vector of pic::Image.
Definition: image_vec.hpp:29
unsigned int halfKernelSize
Definition: filter_kuwahara.hpp:32
int x0
Definition: bbox.hpp:32
float * getMeanVal(BBox *box, float *ret)
getMeanVal computes the mean for the current Image.
void update(int kernelSize)
update
Definition: filter_kuwahara.hpp:197
FilterKuwahara(int kernelSize=3)
FilterKuwahara.
Definition: filter_kuwahara.hpp:188
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_kuwahara.hpp:40
int y0
Definition: bbox.hpp:32
The FilterKuwahara class.
Definition: filter_kuwahara.hpp:28
static Image * execute(Image *imgIn, Image *imgOut, int kernelSize)
execute
Definition: filter_kuwahara.hpp:214
The Image class stores an image as buffer of float.
Definition: image.hpp:60
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
unsigned int kernelSize
Definition: filter_kuwahara.hpp:31
int z0
Definition: bbox.hpp:32
float * getVarianceVal(float *meanVal, BBox *box, float *ret)
getVarianceVal computes the variance for the current Image.