PICCANTE  0.4
The hottest HDR imaging library!
sub_sample_stack.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_ALGORITHMS_SUB_SAMPLE_STACK_HPP
19 #define PIC_ALGORITHMS_SUB_SAMPLE_STACK_HPP
20 
21 #include "../util/math.hpp"
22 #include "../util/std_util.hpp"
23 
24 #include "../image.hpp"
25 #include "../image_vec.hpp"
26 #include "../point_samplers/sampler_random.hpp"
27 #include "../histogram.hpp"
28 
29 namespace pic {
30 
35 {
36 protected:
37 
43  {
44  #ifdef PIC_DEBUG
45  printf("Computing histograms...");
46  #endif
47 
49 
50  int c = 0;
51  for(int j = 0; j < channels; j++) {
52  for(int i = 0; i < exposures; i++) {
53  h[c].calculate(stack[i], VS_LDR, 256, NULL, j);
54  h[c].cumulativef(true);
55  c++;
56  }
57  }
58 
59  #ifdef PIC_DEBUG
60  printf("Ok\n");
61  #endif
62 
63  total = this->nSamples * this->channels * this->exposures;
64  samples = new int[total];
65 
66  #ifdef PIC_DEBUG
67  printf("Sampling...");
68  #endif
69 
70  float div = float(nSamples - 1);
71  c = 0;
72  for(int k = 0; k < channels; k++) {
73  for(int i = 0; i < nSamples; i++) {
74 
75  float u = float(i) / div;
76 
77  for(int j = 0; j < exposures; j++) {
78 
79  int ind = k * exposures + j;
80 
81  float *bin_c = h[ind].getCumulativef();
82 
83  float *ptr = std::upper_bound(&bin_c[0], &bin_c[0]+256, u);
84 
85  samples[c] = CLAMPi((int)(ptr - bin_c), 0, 255);
86  c++;
87  }
88  }
89  }
90 
91  #ifdef PIC_DEBUG
92  printf("Ok\n");
93  #endif
94 
95  delete[] h;
96  }
97 
104  {
105  int width = stack[0]->width;
106  int height = stack[0]->height;
107 
108  Vec<2, int> vec(width, height);
109 
110  RandomSampler<2> *sampler = new RandomSampler<2>(sub_type, vec, nSamples, 1, 0);
111 
112  #ifdef PIC_DEBUG
113  int oldNSamples = nSamples;
114  #endif
115 
116  this->nSamples = sampler->getSamplesPerLevel(0);
117 
118  total = this->nSamples * this->channels * this->exposures;
119  samples = new int[total];
120 
121  #ifdef PIC_DEBUG
122  printf("--subSample samples: %d \t \t old samples: %d\n", nSamples, oldNSamples);
123  #endif
124 
125  int c = 0;
126 
127  for(int k = 0; k < channels; k++) {
128  for(int i = 0; i < nSamples; i++) {
129 
130  int x, y;
131  sampler->getSampleAt(0, i, x, y);
132 
133  for(int j = 0; j < exposures; j++) {
134  float fetched = (*stack[j])(x, y)[k];
135  float tmp = lround(fetched * 255.0f);
136  samples[c] = CLAMPi(int(tmp), 0, 255);
137  c++;
138  }
139  }
140  }
141 
142  delete sampler;
143  }
144 
146  int channels;
147  int nSamples;
148  int total;
149  int *samples;
150 
151 public:
152 
157  {
158  total = 0;
159  exposures = 0;
160  channels = 0;
161  nSamples = 0;
162  samples = NULL;
163  }
164 
166  {
167  release();
168  }
169 
173  void release()
174  {
175  exposures = 0;
176  channels = 0;
177  nSamples = 0;
178  total = 0;
179 
181  }
182 
190  void execute(ImageVec &stack, int nSamples, float alpha = 0.f, bool bSpatial = false, SAMPLER_TYPE sub_type = ST_MONTECARLO_S)
191  {
192  release();
193 
194  if(!((stack.size() > 1 && (nSamples > 1)))) {
195  return;
196  }
197 
198  this->nSamples = nSamples;
199  this->channels = stack[0]->channels;
200  this->exposures = int(stack.size());
201 
202  if(bSpatial) {
203  sampleSpatial(stack, sub_type);
204  } else {
205  sampleGrossberg(stack);
206  }
207 
208  if (alpha < 0.f || alpha > 1.f)
209  alpha = 0.f;
210  else if (alpha > 0.5f)
211  alpha = 1.f - alpha;
212 
213  if(alpha > 0.f && alpha <= 0.5f) {
214  float t_min_f = alpha;
215  float t_max_f = 1.0f - t_min_f;
216 
217  int t_min = int(t_min_f * 255.0f);
218  int t_max = int(t_max_f * 255.0f);
219 
220  for(int i = 0; i < total; i++) {
221  if(samples[i] < t_min || samples[i] > t_max) {
222  samples[i] = -1;
223  }
224  }
225  }
226  }
227 
232  int *get()
233  {
234  return samples;
235  }
236 
241  int getNSamples() const
242  {
243  return nSamples;
244  }
245 
249  void print()
250  {
251  for(int i = 0; i < total; i++) {
252  printf("%d\n", samples[i]);
253  }
254 
255  }
256 };
257 
258 } // end namespace pic
259 
260 #endif /* PIC_ALGORITHMS_SUB_SAMPLE_STACK_HPP */
261 
int total
Definition: sub_sample_stack.hpp:148
int getSamplesPerLevel(int level)
getSamplesPerLevel
float * cumulativef(bool bNormalized)
cumulativef computes the cumulative Histogram.
Definition: histogram.hpp:440
void calculate(Image *imgIn, VALUE_SPACE type=VS_LIN, int nBin=256, BBox *box=NULL, int channel=0)
calculate computes the histogram of an input image. In the case of LDR images, they are ssumed to be ...
Definition: histogram.hpp:213
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
The Histogram class is a class for creating, managing, loading, and saving histogram for an Image...
Definition: histogram.hpp:37
PIC_INLINE long lround(double x)
lround rounds double numbers properly.
Definition: math.hpp:229
float * getCumulativef()
getCumulativef this function returns the cumulative Histogram. Note that cumulativef needs to be comp...
Definition: histogram.hpp:459
~SubSampleStack()
Definition: sub_sample_stack.hpp:165
void getSampleAt(int level, int i, int &x, int &y)
getSampleAt
Definition: point_samplers.hpp:51
Definition: histogram.hpp:31
int * samples
Definition: sub_sample_stack.hpp:149
The SubSampleStack class.
Definition: sub_sample_stack.hpp:34
int channels
Definition: sub_sample_stack.hpp:146
void sampleGrossberg(ImageVec &stack)
sampleGrossberg creates a low resolution version of the stack using Grossberg and Nayar sampling...
Definition: sub_sample_stack.hpp:42
void sampleSpatial(ImageVec &stack, SAMPLER_TYPE sub_type=ST_MONTECARLO_S)
sampleSpatial creates a low resolution version of the stack.
Definition: sub_sample_stack.hpp:103
SAMPLER_TYPE
Definition: point_samplers.hpp:51
SubSampleStack()
SubSampleStack.
Definition: sub_sample_stack.hpp:156
void execute(ImageVec &stack, int nSamples, float alpha=0.f, bool bSpatial=false, SAMPLER_TYPE sub_type=ST_MONTECARLO_S)
execute
Definition: sub_sample_stack.hpp:190
#define CLAMPi(x, a, b)
Definition: math.hpp:81
Definition: bilateral_separation.hpp:25
int nSamples
Definition: sub_sample_stack.hpp:147
The RandomSampler class.
Definition: sampler_random.hpp:43
int exposures
Definition: sub_sample_stack.hpp:145
void release()
release
Definition: sub_sample_stack.hpp:173
void print()
print
Definition: sub_sample_stack.hpp:249
The Vec class.
Definition: vec.hpp:35
int getNSamples() const
getNSamples
Definition: sub_sample_stack.hpp:241