21 #ifndef mia_core_histogram_hh 22 #define mia_core_histogram_hh 31 #include <boost/type_traits.hpp> 72 size_t index(T x)
const;
79 T
value(
size_t k)
const;
111 size_t index(
unsigned char x)
const;
114 unsigned char value(
size_t k)
const;
131 template <
typename Feeder>
139 typedef std::pair<typename Feeder::value_type, size_t>
value_type;
141 typedef std::pair<typename Feeder::value_type, typename Feeder::value_type>
range_type;
163 void push(
typename Feeder::value_type x);
170 void push(
typename Feeder::value_type x,
size_t count);
178 template <
typename Iterator>
179 void push_range(Iterator begin, Iterator end);
185 const_iterator begin()
const;
188 const_iterator end()
const;
191 size_t operator [] (
size_t idx)
const;
198 const value_type at(
size_t idx)
const;
202 typename Feeder::value_type median()
const;
205 typename Feeder::value_type MAD()
const;
208 double average()
const;
211 double deviation()
const;
214 double excess_kurtosis()
const;
217 double skewness()
const;
225 range_type get_reduced_range(
double remove)
const;
228 std::vector<size_t> m_histogram;
233 template <
typename T>
238 m_step(( double(max) - double(min) ) / double(bins - 1)),
239 m_inv_step(double(bins - 1) / (double(max) - double(min)))
243 template <
typename T>
249 template <
typename T>
252 double val = floor(m_inv_step * (x - m_min) + 0.5);
260 template <
typename T>
263 return k * m_step + m_min;
287 template <
typename Feeder>
290 m_histogram(f.
size()),
295 template <
typename Feeder>
297 m_feeder(org.m_feeder),
298 m_histogram(m_feeder.
size()),
301 size_t n = (size_t)(org.m_n * (1.0 - perc));
304 while (n > m_n && i < m_histogram.size()) {
305 m_n += org.m_histogram[i];
306 m_histogram[i] = org.m_histogram[i];
312 template <
typename Feeder>
315 return m_histogram.size();
318 template <
typename Feeder>
322 ++m_histogram[m_feeder.index(x)];
325 template <
typename Feeder>
326 template <
typename Iterator>
335 template <
typename Feeder>
339 m_histogram[m_feeder.index(x)] += count;
342 template <
typename Feeder>
345 return m_histogram.begin();
348 template <
typename Feeder>
351 return m_histogram.end();
354 template <
typename Feeder>
357 assert(idx < m_histogram.size());
358 return m_histogram[idx];
361 template <
typename Feeder>
364 float n_2 = m_n / 2.0f;
368 sum += m_histogram[k++];
370 return m_feeder.value(k > 0 ? k-1 : k);
373 template <
typename Feeder>
376 typedef typename Feeder::value_type T;
382 for (
size_t k = 0; k <
size(); ++k) {
383 T v = m_feeder.value(k);
384 help.
push(v > m ? v - m : m -v, m_histogram[k]);
390 template <
typename Feeder>
393 if (idx < m_histogram.size())
394 return value_type(m_feeder.value(idx), m_histogram[idx]);
399 template <
typename Feeder>
405 for (
size_t i = 0; i <
size(); ++i) {
407 sum += value.first * value.second;
412 template <
typename Feeder>
420 for (
size_t i = 0; i <
size(); ++i) {
421 const auto value =
at(i);
422 sum1 += value.first * value.second;
423 sum2 += value.first * value.first * value.second;
424 double h = (value.first - mu);
427 sum3 += h * value.second;
430 double sigma2 = (sum2 - sum1 * sum1 / m_n) / m_n;
431 double mu4 = sum3 / m_n;
432 return mu4 / (sigma2 * sigma2) - 3;
435 template <
typename Feeder>
443 for (
size_t i = 0; i <
size(); ++i) {
444 const auto value =
at(i);
445 sum1 += value.first * value.second;
446 sum2 += value.first * value.first * value.second;
447 double h = (value.first - mu);
448 sum3 += h * h * h * value.second;
451 double sigma2 = (sum2 - sum1 * sum1 / m_n) / m_n;
452 double mu4 = sum3 / m_n;
453 return mu4 / (sigma2 * sqrt(sigma2));
456 template <
typename Feeder>
463 for (
size_t i = 0; i <
size(); ++i) {
465 sum += value.first * value.second;
466 sum2 += value.first * value.first * value.second;
468 return sqrt((sum2 - sum * sum / m_n) / (m_n - 1));
471 template <
typename Feeder>
475 assert(
remove >= 0.0 &&
remove < 49.0);
476 long remove_count =
static_cast<long>(
remove * m_n / 100.0);
478 range_type result(m_feeder.value(0), m_feeder.value(m_histogram.size() - 1));
480 if (remove_count > 0) {
482 long counted_pixels_low = 0;
484 while (counted_pixels_low < remove_count && low_end < (
long)m_histogram.size())
485 counted_pixels_low += m_histogram[++low_end];
487 result.first = m_feeder.value(low_end);
489 long high_end = m_histogram.size();
490 long counted_pixels_high = 0;
491 while (counted_pixels_high <= remove_count && high_end > 0)
492 counted_pixels_high += m_histogram[--high_end];
493 cvdebug() <<
" int range = " << low_end <<
", " << high_end <<
" removing " << remove_count <<
" pixels at each end\n";
494 result.second = m_feeder.value(high_end);
THistogram(const Feeder &f)
specialization of the THistogramFeeder for unsigned byte input data
range_type get_reduced_range(double remove) const
const_iterator begin() const
double excess_kurtosis() const
#define NS_MIA_BEGIN
conveniance define to start the mia namespace
T value_type
typedef for generic programming
std::pair< typename Feeder::value_type, size_t > value_type
A type for the value-index pair.
a simple histogram that uses an instance of THistogramFeeder as input converter
THistogramFeeder(T min, T max, size_t bins)
THistogramFeeder< unsigned char > CUBHistogramFeeder
typedef for the unsigned byte histogram feeder specialization
A class to normalize and quantizize input data to a given histogram range with its given number of bi...
std::vector< size_t >::const_iterator const_iterator
STL iterator.
const_iterator end() const
size_t operator[](size_t idx) const
Feeder::value_type MAD() const
const value_type at(size_t idx) const
std::pair< typename Feeder::value_type, typename Feeder::value_type > range_type
void push(typename Feeder::value_type x)
void push_range(Iterator begin, Iterator end)
unsigned char value_type
typedef for generic programming
Feeder::value_type median() const
#define NS_MIA_END
conveniance define to end the mia namespace