iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
KernelsImpl.H
Go to the documentation of this file.
1 /*! @file
2  @author Unknown
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 namespace nrt
35 {
36 // ######################################################################
37 namespace KernelHelpers
38 {
39  // Helper function for binomialKernel():
40  double binomial(int n, int k);
41 }
42 
43 // ######################################################################
44 template<class T>
46 {
47  if(size == 0)
48  return Image<PixGray<T>>();
49 
51  int const N = size -1;
52  double const div = pow(2.0, double(N));
53 
54  // compute a series of N-choose-k, where N == (size-1), with k running
55  // from 0 to N, inclusive
56  for (int k = 0; k <= N; ++k)
57  result(k, 0) = PixGray<T>(KernelHelpers::binomial(N, k) / div);
58 
59  return result;
60 }
61 
62 // ######################################################################
63  template <class T>
64 Image<PixGray<T> > gaborKernel(const float stddev, const float period,
65  const float phase, const float theta,
66  const float bg, const float ampl)
67 {
68  // figure the proper size for the result
69  int size = int(ceil(stddev * sqrt(-2.0F * log(exp(-5.0F)))));
70 
71  Image<PixGray<T> > result(2 * size + 1, 2 * size + 1, ImageInitPolicy::None);
72 
73  // change the angles in degree to the those in radians:
74  float psi = M_PI / 180.0F * phase;
75 
76  float rtDeg = M_PI / 180.0F * theta;
77 
78  // calculate constants:
79  float omega = (2.0F * M_PI) / period;
80  float co = cos(rtDeg), si = sin(rtDeg);
81  float sigq = 2.0F * stddev * stddev;
82  auto aptr = result.pod_begin();
83  float a = float(ampl), b = float(bg);
84 
85  // compute gabor:
86  for (int y = -size; y <= size; y ++)
87  for (int x = -size; x <= size; x ++)
88  *aptr++ = T(a * cos(omega * (x * co + y * si) + psi) *
89  exp(-(x * x + y * y) / sigq) + b);
90  return result;
91 }
92 
93 // ######################################################################
94  template <class T>
95 Image<PixGray<T> > gaborKernel(const float scale, const float theta)
96 {
97  // figure the proper size for the result
98  int size = int(scale * 12+0.5f);
99 
100  Image<PixGray<T>> result(2 * size + 1, 2 * size + 1, ImageInitPolicy::None);
101 
102  // calculate constants:
103  float cosTheta = cos(theta + M_PI/2), sinTheta = sin(theta + M_PI/2);
104  float normConst = 50/M_PI/scale/scale;
105  auto aptr = result.pod_begin();
106 
107  // compute gabor:
108  for (int y = -size; y <= size; ++y)
109  for (int x = -size; x <= size; ++x)
110  {
111  if (x*x+y*y > size*size)
112  *aptr++ = T(); //zero out side the circle
113  else
114  {
115  float xx = (x*cosTheta + y * sinTheta)/scale;
116  float yy = (y*cosTheta - x * sinTheta)/scale;
117  *aptr++ = T(exp(-(4.0F*xx*xx+yy*yy)/100.0F)/normConst);
118  }
119  }
120 
121  return result;
122 }
123 
124 // ######################################################################
125 template <class T>
126 Image<PixGray<T>> morphologyKernel(int size, bool const disk)
127 {
128  if (size < 1) size = 1;
129 
130  if (disk)
131  {
132  Image<PixGray<T>> result(size, size, ImageInitPolicy::None);
133  auto aptr = result.begin();
134 
135  float const centerX = float(size - 1) * 0.5F;
136  float const centerY = centerX;
137  float const radiusD = centerX + 0.5f;
138  float const maxdiff = radiusD * radiusD;
139 
140  for (int y = 0; y < size; ++y)
141  {
142  float y2 = float(y) - centerY; y2 *= y2;
143  for (int x = 0; x < size; ++x)
144  {
145  float x2 = float(x) - centerX; x2 *= x2;
146  *aptr++ = (x2 + y2 <= maxdiff) ? std::numeric_limits<T>::max() : T();
147  }
148  }
149 
150  return result;
151  }
152  else
153  {
154  Image<PixGray<T>> result(Dims<int32>(size, size), PixGray<T>(std::numeric_limits<T>::max()));
155  return result;
156  }
157 }
158 
159 } // namespace nrt