Hugin trunk 0.1
Loading...
Searching...
No Matches
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
32namespace vigra_ext {
33
34
35template<class VALUETYPE=vigra::RGBValue<float> >
37{
38public:
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
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
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
119protected:
121 double weight;
122
124 double maxComp;
126 double minComp;
127 double maxW;
128 double minW;
129};
130
131#if 0
138template <class In>
139double calcSigmoidHDRWeight(In val)
140{
141 // normalize to 0..1
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
160struct HDRWeightFunctor
161{
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
Traits::RealPromote real_type
Definition HDRUtils.h:45
real_type operator()() const
return the result
Definition HDRUtils.h:102
vigra::NumericTraits< VALUETYPE > Traits
Definition HDRUtils.h:44
V getMaxComponent(vigra::RGBValue< V > const &v)
get the maximum component of a vector (also works for single pixel types...)
Definition utils.h:268
std::vector< deghosting::BImagePtr > threshold(const std::vector< deghosting::FImagePtr > &inputImages, const double threshold, const uint16_t flags)
Threshold function used for creating alpha masks for images.
Definition threshold.h:41
functions to manage ROI's