Hugintrunk  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
HDRUtils.h
Go to the documentation of this file.
1 // -*- c-basic-offset: 4 -*-
24 #ifndef _VIGRA_EXT_HDRUTILS_H
25 #define _VIGRA_EXT_HDRUTILS_H
26 
27 #include "ROIImage.h"
28 #include "utils.h"
29 #include "lut.h"
30 
31 
32 namespace vigra_ext {
33 
34 
35 template<class VALUETYPE=vigra::RGBValue<float> >
37 {
38 public:
39  typedef VALUETYPE argument_type;
40  typedef VALUETYPE first_argument_type;
41  typedef VALUETYPE second_argument_type;
42  typedef VALUETYPE result_type;
43 
44  typedef typename vigra::NumericTraits<VALUETYPE> Traits;
45  typedef typename Traits::RealPromote real_type;
46 
48  {
49  reset();
50  }
51 
52  void reset ()
53  {
54  result = vigra::NumericTraits<real_type>::zero();
55  weight = 0;
56 
57  maxComp = DBL_MIN;
58  minComp = DBL_MAX;
59  maxW= 0;
60  minW= 1;
61  }
62 
63  // add a new measurement for the current pixel
64  template<class T, class M>
65  void operator() (T const &v, M const &m)
66  {
67  // normalize to 0..1
68  double nm = m / (double)vigra_ext::LUTTraits<M>::max();
69 
70  // use a gaussian weight function.
71 // double w = (nm - 0.5)/0.18;
72 // w = exp( -w*w);
73  // a simple triangular function should also work ok
74  double w = 0.5-fabs(nm-0.5);
75 
76  result += w*v;
77  weight += w;
78 
79  // store minimum and maximum weight and pixel values
80  if (nm > maxW) {
81  maxW = nm;
82  }
83  if ( nm < minW) {
84  minW = nm;
85  }
86 
87  double cmax = getMaxComponent(v);
88 
89  if (cmax > maxComp)
90  {
91  maxComp = cmax;
92  maxValue = v;
93  }
94  if (cmax < minComp)
95  {
96  minComp = cmax;
97  minValue = v;
98  }
99  }
100 
103  {
104  double eps = 1e-7;
105  // heuristics to deal with over and underexposed images.
106  if (minW > (1.0-eps) && maxW > (1.0-eps)) {
107  // all pixels overexposed, just use brightest value
108  return maxValue;
109  } else if (minW < eps && maxW < eps) {
110  // all pixels underexposed. use darkest value
111  return minValue;
112  }
113  if (weight > 0)
114  return result/weight;
115  else
116  return result;
117  }
118 
119 protected:
121  double weight;
122 
124  double maxComp;
126  double minComp;
127  double maxW;
128  double minW;
129 };
130 
131 #if 0
132 
138 template <class In>
139 double calcSigmoidHDRWeight(In val)
140 {
141  // normalize to 0..1
142  double x = getMaxComponent(val)/((double) vigra_ext::LUTTraits<In>::max());
143 
144  // sigomid function between -6 ... 6
145  x = (x-0.5)*12;
146 
147  // calculate sigmoidal stuff
148  double y = 1.0 / ( 1.0 + exp(-x));
149 
150  double min = 1.0 / ( 1.0 + exp(+6.0));
151  double max = 1.0 / ( 1.0 + exp(-6.0));
152  return (y - min)/(max-min);
153 }
154 
155 
160 struct HDRWeightFunctor
161 {
162  HDRWeightFunctor()
163  {
164  m_lut.resize(256);
165  std::cerr << "w = [";
166  for (int i=0;i<256;i++) {
167  // gaussian
168  float x = (i/255.0f - 0.5f)/0.18f;
169  vigra::UInt8 w = std::min(utils::ceili(255*exp( -x*x)), 255);
170 
171  // hat function
172  //vigra::UInt8 w = std::max( utils::ceili( (1-pow((2.0*i/255.0)-1.0, 4.0) )*255), 1);
173  m_lut[i] = w;
174  std::cerr << (int) w << " ";
175  }
176  }
177 
178  template <class T>
179  vigra::UInt8 operator()(vigra::RGBValue<T> rgb) const
180  {
181  // mean is not so good since it can create overexposed
182  // pixels in the image.
183  //T x = (rgb[0] + rgb[1] + rgb[2])/T(3);
184 
185  T x = std::max(rgb[0], std::max(rgb[1], rgb[2]));
186  return operator()(x);
187  }
188 
189  vigra::UInt8 operator()(float x) const
190  {
191  x = x*255;
192  vigra::UInt8 i = 0;
193  if (x> 0 || x <=255)
194  i = (vigra::UInt8) x;
195  return m_lut[i];
196  }
197 
198 
199  vigra::UInt8 operator()(vigra::UInt16 x) const
200  {
201  x <<= 8;
202  return m_lut[x];
203  }
204 
205  vigra::UInt8 operator()(vigra::UInt8 x) const
206  {
207  return m_lut[x];
208  }
209 
210  vigra::ArrayVector<vigra::UInt8> m_lut;
211 };
212 
213 #endif
214 
215 
216 } //namespace
217 #endif //_H
V getMaxComponent(vigra::RGBValue< V > const &v)
get the maximum component of a vector (also works for single pixel types...)
Definition: utils.h:268
Traits::RealPromote real_type
Definition: HDRUtils.h:45
vigra::NumericTraits< VALUETYPE > Traits
Definition: HDRUtils.h:44
functions to manage ROI&#39;s
int ceili(double x)
Definition: hugin_math.h:60
static T max(T x, T y)
Definition: svm.cpp:65
real_type operator()() const
return the result
Definition: HDRUtils.h:102
static T min(T x, T y)
Definition: svm.cpp:62