36 #ifndef VIGRA_HISTOGRAM_HXX
37 #define VIGRA_HISTOGRAM_HXX
40 #include "array_vector.hxx"
81 vigra_precondition(mi < ma,
82 "HistogramOptions::setMinMax(): min < max required.");
92 vigra_precondition(c > 0,
93 "HistogramOptions::setBinCount(): binCount > 0 required.");
102 "HistogramOptions::regionAutoInit(): you must not call setMinMax() when auto initialization is desired.");
111 "HistogramOptions::globalAutoInit(): you must not call setMinMax() when auto initialization is desired.");
124 template <
class DataType,
class BinType>
130 double scale_, scaleInverse_;
133 HistogramView(DataType
const & min, DataType
const & max,
int binCount,
134 BinType * bins = 0,
int stride = 1)
139 scale_(double(binCount) / (max - min)),
140 scaleInverse_(1.0 / scale_)
143 HistogramView & setData(BinType * bins ,
int stride = 1)
150 HistogramView & reset()
153 for(
int k=0; k<size_; ++k)
154 *(bins_ +k*stride_) = BinType();
158 void getBinCenters(ArrayVector<DataType> * centers)
const
160 double invScale = 1.0 / scale_;
161 for(
int k=0; k < size_; ++k)
163 (*centers)[k] = mapItemInverse(k + 0.5) ;
177 BinType
const & operator[](
int k)
const
179 return *(bins_ + k*stride_);
182 double mapItem(DataType
const & d)
const
184 return scale_ * (d - offset_);
187 DataType mapItemInverse(
double d)
const
189 return DataType(d * scaleInverse_ + offset_);
192 void add(DataType
const & d, BinType weight = NumericTraits<BinType>::one())
194 get(int(mapItem(d))) += weight;
199 BinType &
get(
int index)
205 return *(bins_ + index*stride_);
210 class TrapezoidKernel
213 typedef T value_type;
215 T operator[](
double f)
const
218 return 0.5*(f + 1.5);
220 return 0.5*(1.5 - f);
224 double radius()
const
229 T findMaximum(
double l,
double c,
double r)
const
231 double curv = -2.0*c + r + l;
234 double extr = 0.5*(l-r) / curv;
251 bool findMode(
double l,
double c,
double r,
double * m)
const
253 double curv = -2.0*c + r + l;
256 *m = 0.5*(l-r) / curv;
257 if(*m < -0.5 || *m > 0.5)
263 template <
class DataType,
class KernelType>
264 class KernelHistogramView
265 :
public HistogramView<DataType, typename KernelType::value_type>
272 typedef typename KernelType::value_type BinType;
273 typedef HistogramView<DataType, BinType> BaseType;
275 KernelHistogramView(DataType
const & min, DataType
const & max,
int binCount,
276 BinType * bins = 0,
int stride = 1)
277 : BaseType(min, max, binCount, bins, stride),
278 radius_(kernel_.radius()-0.5)
281 void add(DataType
const & d, BinType weight = NumericTraits<BinType>::one())
283 double mapped = this->mapItem(d);
284 double f = mapped -
std::floor(mapped) - kernel_.radius();
285 int center = int(mapped);
287 for(
int k=center+radius_; k>=center-radius_; --k, f += 1.0)
289 this->
get(k) += weight*kernel_[f];
293 DataType findMode()
const
295 double mmax = 0, vmax = 0, m;
297 for(
int k=0; k<this->size(); ++k)
302 double c = (*this)[k];
303 double r = k < this->size() - 1
306 if(kernel_.findMode(l, c, r, &m))
308 double v = l*kernel_[m+1.0] + c*kernel_[m] + r*kernel_[m-1.0];
316 return this->mapItemInverse(mmax);
319 template <
class Array>
320 void findModes(Array * modes)
323 for(
int k=0; k<this->size(); ++k)
328 double c = (*this)[k];
329 double r = k < this->size() - 1
332 if(kernel_.findMode(l, c, r, &m))
334 double v = l*kernel_[m+1.0] + c*kernel_[m] + r*kernel_[m-1.0];
335 modes->push_back(std::make_pair(this->mapItemInverse(m + k + 0.5), v));
341 template <
class DataType,
class BinType>
343 :
public HistogramView<DataType, BinType>
346 typedef HistogramView<DataType, BinType> BaseType;
347 ArrayVector<BinType> data_;
350 Histogram(DataType
const & min, DataType
const & max,
int binCount,
351 BinType * bins = 0,
int stride = 1)
352 : BaseType(min, max, binCount),
355 this->setData(&data_[0]);
358 Histogram
const & reset()
360 this->setData(&data_[0]);
368 #endif // VIGRA_HISTOGRAM_HXX
HistogramOptions & regionAutoInit()
Definition: histogram.hxx:99
HistogramOptions & setBinCount(int c)
Definition: histogram.hxx:90
HistogramOptions()
Definition: histogram.hxx:71
bool validMinMax() const
Definition: histogram.hxx:118
int binCount
Total number of bins in the histogram.
Definition: histogram.hxx:60
void add(FixedPoint< IntBits1, FracBits1 > l, FixedPoint< IntBits2, FracBits2 > r, FixedPoint< IntBits3, FracBits3 > &result)
addition with enforced result type.
Definition: fixedpoint.hxx:561
HistogramOptions & setMinMax(double mi, double ma)
Definition: histogram.hxx:79
HistogramOptions & globalAutoInit()
Definition: histogram.hxx:108
bool local_auto_init
If true, range mapping bounds are defined by minimum and maximum of the data.
Definition: histogram.hxx:63
double maximum
Upper bound for linear range mapping from values to indices.
Definition: histogram.hxx:57
int floor(FixedPoint< IntBits, FracBits > v)
rounding down.
Definition: fixedpoint.hxx:667
double minimum
Lower bound for linear range mapping from values to indices.
Definition: histogram.hxx:54
Set histogram options.
Definition: histogram.hxx:49