iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
StatisticsImpl.H
Go to the documentation of this file.
1 /*! @file
2  @author Daniel Parks <danielfp@usc.edu>
3  @copyright GNU Public License (GPL v3)
4  @section License
5  @verbatim
6  // ////////////////////////////////////////////////////////////////////////
7  // The iLab Neuromorphic Robotics Toolkit (NRT) //
8  // Copyright 2010-2012 by the University of Southern California (USC) //
9  // and the iLab at USC. //
10  // //
11  // iLab - University of Southern California //
12  // Hedco Neurociences Building, Room HNB-10 //
13  // Los Angeles, Ca 90089-2520 - USA //
14  // //
15  // See http://ilab.usc.edu for information about this project. //
16  // ////////////////////////////////////////////////////////////////////////
17  // This file is part of The iLab Neuromorphic Robotics Toolkit. //
18  // //
19  // The iLab Neuromorphic Robotics Toolkit is free software: you can //
20  // redistribute it and/or modify it under the terms of the GNU General //
21  // Public License as published by the Free Software Foundation, either //
22  // version 3 of the License, or (at your option) any later version. //
23  // //
24  // The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
25  // that it will be useful, but WITHOUT ANY WARRANTY; without even the //
26  // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
27  // PURPOSE. See the GNU General Public License for more details. //
28  // //
29  // You should have received a copy of the GNU General Public License //
30  // along with The iLab Neuromorphic Robotics Toolkit. If not, see //
31  // <http://www.gnu.org/licenses/>. //
32  // ////////////////////////////////////////////////////////////////////////
33  @endverbatim */
34 
35 #include <algorithm>
36 
37 // ######################################################################
38 template <class PixType> inline
40 {
41  auto maxEl = std::max_element(src.begin(),src.end());
42  size_t ind = std::distance(src.begin(),maxEl);
43  int x,y;
44  y = ind/src.width();
45  x = ind - src.width()*y;
46  return nrt::Point2D<int>(x,y);
47 }
48 
49 // ######################################################################
50 template <class PixType> inline
52 {
53  auto minEl = std::min_element(src.begin(),src.end());
54  size_t ind = std::distance(src.begin(),minEl);
55  int x,y;
56  y = ind/src.width();
57  x = ind - src.width()*y;
58  return nrt::Point2D<int>(x,y);
59 }
60 
61 // ######################################################################
62 template <NRT_PROMOTE_PIX_NO_DEFAULT> inline
63 DestType const nrt::sum(nrt::Image<PixType> const & im)
64 {
65  DestType ret(0);
66  for (PixType const & pix : im) ret += DestType(pix);
67  return ret;
68 }
69 
70 // ######################################################################
71 template <NRT_PROMOTE_PIX_NO_DEFAULT> inline
72 DestType const nrt::mean(nrt::Image<PixType> const & im)
73 {
74  if (im.size() == 0) return DestType(0);
75  return nrt::sum<promo>(im) / im.size();
76 }
77 
78 // ######################################################################
79 template <NRT_PROMOTE_PIX_NO_DEFAULT> inline
80 DestType const nrt::stdev(nrt::Image<PixType> const & im)
81 { return sqrt(nrt::variance<promo>(im).val()); }
82 
83 // ######################################################################
84 template <NRT_PROMOTE_PIX_NO_DEFAULT> inline
85 DestType const nrt::variance(nrt::Image<PixType> const & im)
86 {
87  if (im.size() == 0) return DestType(0);
88 
89  // We use the well-known formula: E[(X - E[X])^2] = E[X^2] - (E[X])^2
90  DestType s(0), ssq(0);
91  for (PixType const & pix : im) { DestType const p = DestType(pix); s += p; ssq += (p*p); }
92 
93  DestType avg = s / im.size();
94 
95  return (ssq / im.size()) - (avg * avg);
96 }
97 
98 // ######################################################################
99 template <class promo, class T> inline
100 typename nrt::promote<T, promo>::type const
102 {
103  typedef typename nrt::promote<T, promo>::type TF;
104  TF const zero(0);
105 
106  if (im1.dims() != im2.dims()) return zero;
107 
108  TF corr(0), norm1(0), norm2(0);
109  typename nrt::Image<nrt::PixGray<T> >::const_iterator i1 = im1.begin(), i2 = im2.begin(), stop = im1.end();
110 
111  while (i1 != stop) {
112  TF const v1 = TF(i1->val()), v2 = TF(i2->val());
113  corr += v1 * v2; norm1 += v1 * v1; norm2 += v2 * v2;
114  ++i1; ++i2;
115  }
116 
117  if (norm1 == zero) { if (norm2 == zero) return TF(1); else return zero; }
118  else if (norm2 == zero) return zero;
119 
120  return (corr / sqrt(norm1*norm2) + TF(1)) / TF(2);
121 }
122