Structural deformable models
SensorSet.h
Go to the documentation of this file.
1 /* Sensor -*- C++ -*- */
2 #ifndef _SENSORSET_H_
3 #define _SENSORSET_H_
4 
5 #include <math.h>
6 #include <complex>
7 #include "Sensor.h"
8 #include "Fourier.h"
9 #include "DMatrix.h"
10 
11 //usable Sensor implementations
12 
14 class IntensitySensor : public Sensor {
15 public:
16  std::ostream& print(std::ostream& os) const {
17  Sensor::print(os) << "i";
18  return os;
19  }
20 protected:
22  float calcValue(int x, int y) const {
23  return source->getValue(x,y);
24  }
25 };
26 
28 class PPIntensitySensor : public PPSensor {
29 protected:
31  float calcValue(int x, int y) const {
32  return source->getValue(x,y);
33  }
34 };
35 
37 class MCIntensitySensor : public PPSensor {
38 public:
39  int getNChannels() const { return 1; }
40  std::ostream& print(std::ostream& os) const {
41  Sensor::print(os) << "m ";
42  for(dword i=0; i<cweights.size(); i++) { //source->getNChannels()
43  if(i) os << " ";
44  os << cweights[i];
45  }
46  return os;
47  }
48 protected:
50  float calcValue(int x, int y) const {
51  float result = 0.0f;
52  const std::vector<float> mv = source->getMValue(x,y);
53  assert(mv.size() == cweights.size());
54  for(dword i=0; i<mv.size(); i++) { //source->getNChannels()
55  float weight = cweights[i];
56  if(weight!=0.0f) result += mv[i]*weight;
57  }
58  return result;
59  }
60 };
61 
63 class MCGSensor : public PPSensor {
64 public:
66  { m_AddSkip = 1; }
67  int getNChannels() const { return 1; }
68  std::ostream& print(std::ostream& os) const {
69  Sensor::print(os)
70  << "M";
71  return os;
72  }
73 protected:
75  float calcValue(int x, int y) const {
76  float result = 0.0f;
77  const std::vector<float> mv = source->getMValue(x,y);
78  for(std::vector<float>::const_iterator v=mv.begin();
79  v != mv.end(); v++)
80  result+=*v;
81  return result;
82  }
83  virtual Point calcGradient(int x, int y) const {
84  std::vector<float> c = source->getMValue(x,y);
85  std::vector<float> r = source->getMValue(x+1,y);
86  std::vector<float> b = source->getMValue(x,y+1);
87  Point2D g(0,0);
88  for(int i=0; i<source->getNChannels(); i++)
89  {
90  register float dx = r[i]-c[i];
91  g.x += dx*dx;
92  register float dy = b[i]-c[i];
93  g.y += dy*dy;
94  }
95  g.x = sqrt(g.x);
96  g.y = sqrt(g.y);
97  return g;
98  }
99 };
100 
102 class CRSensor : public PPSensor {
103 public:
105  { m_AddSkip = 1; }
106  int getNChannels() const { return 1; }
107  std::ostream& print(std::ostream& os) const {
108  Sensor::print(os)
109  << "r";
110  return os;
111  }
112 protected:
114  float calcValue(int x, int y) const {
115  float result = 0.0f;
116  std::vector<float> mv = source->getMValue(x,y);
117 #define SATURATION
118 #ifdef CHROMITHING
119  float g = 0;
120  for(std::vector<float>::const_iterator v=mv.begin();
121  v != mv.end(); v++)
122  g+=*v;
123  g/=mv.size();
124  for(std::vector<float>::const_iterator v=mv.begin();
125  v != mv.end(); v++)
126  {
127  register float d = g-*v;
128  result += d*d;
129  }
130  return sqrt(result);
131 #elif defined(SATURATION)
132  float mi = std::numeric_limits<float>::max();
133  float ma = std::numeric_limits<float>::min();
134  for(std::vector<float>::const_iterator v=mv.begin();
135  v != mv.end(); v++)
136  {
137  if(*v>ma) ma = *v;
138  if(*v<mi) mi = *v;
139  }
140  return ma>0.00001 ? (ma-mi)/ma : 0;
141 #endif
142 
143 #undef SATURATION
144 #undef CHROMITHING
145  }
146 };
147 
150 public:
151  SmoothIntensitySensor(float _scale=0);
152  bool performUpdate();
153  //dword getFilterSize() const {return smoothflt.getSizeX();}
154  std::ostream& print(std::ostream& os) const {
155  Sensor::print(os)
156  << "g " << getScale();
157  return os;
158  }
159 
160 protected:
161  void calcAllValues();
162  float calcValue(int x, int y) const {
163  return source->getValue(x,y);
164  }
165  void updateScale();
166 protected:
170 };
171 
173 class GradMagSensor : public PPSensor {
174 public:
176  {
178  togglePP(PPSensor::PP_FORCE);
179  m_AddSkip = 1;
180  performUpdate();
181  };
182  bool performUpdate() {
183  if(isModified(UPD_DATA)) {
184  m_Skip = source->getSkip()+m_AddSkip;
185  }
186  return PPSensor::performUpdate();
187  }
188  std::ostream& print(std::ostream& os) const {
189  Sensor::print(os)
190  << "d";
191  return os;
192  }
193 protected:
195  float calcValue(int x, int y) const {
196  if(!isValid(x,y)) return 0;
197  else return source->getGradient(x,y).norm();
198  }
199 };
200 
202 class CornerSensor : public PPSensor {
203 public:
205  {
207  togglePP(PPSensor::PP_FORCE);
208  m_AddSkip = 3;
209  };
210 
211  bool performUpdate() {
212  if(isModified(UPD_DATA)) {
213  m_Skip = source->getSkip()+m_AddSkip;
214  }
215  return PPSensor::performUpdate();
216  }
217  std::ostream& print(std::ostream& os) const {
218  Sensor::print(os)
219  << "c";
220  return os;
221  }
222 protected:
224  float calcValue(int x, int y) const {
225  if(!isValid(x,y)) return 0;
226  float value = source->getValue(x,y);
227  if(value==0) return 0;
228  Point c=source->getGradient(x,y);
229  Point ddx = source->getGradient(x+1,y)-c;
230  Point ddy = source->getGradient(x,y+1)-c;
231 //#define SS_GRADIENT_WEIGHTED_DETERMINANT
232 #ifdef SS_GRADIENT_WEIGHTED_DETERMINANT
233  float n1 = ddx.normalize();
234  float n2 = ddy.normalize();
235  float det = ddx.x*ddy.y-ddx.y*ddy.x;
236  det *= min(n1,n2);
237 #else
238  float det = ddx.x*ddy.y-ddx.y*ddy.x;
239 #endif
240  if(det<0) return 0; // repelling regions are no good
241  float ret=det*value;
242  return ret;
243 /* using eigenvalues
244 //cout << det;
245 //cout << ddx << endl << ddy << endl;
246 float p = (ddx.x+ddy.y)/2;
247 float d = sqrt(p*p-ddx.x*ddy.y+ddx.y*ddy.x);
248 //cout << "p=" << p << " d=" << d<<endl;
249 float e1 = p+d;
250 float e2 = p-d;
251 //cout << "e1="<<e1<<" e2="<<e2<<endl;
252 //cout << "---------------------" << endl;
253 return e1*e2;
254 */
255  }
256 };
257 
259 class CombiSensor : public PPSensor {
260 public:
261  CombiSensor(int nchannels=0);
262  virtual ~CombiSensor();
263 
264 //virtual functions
265  int getNChannels() const { return sources.size(); }
266  int getSkip() const {
267  int m_Skip = 0;
268  for(std::vector<sensor_ptr>::const_iterator s = sources.begin();
269  s!=sources.end(); s++)
270  if((*s)->getSkip() > m_Skip) m_Skip = (*s)->getSkip();
271  return m_Skip;
272  }
273 
274  void changeSource(sensor_cptr _source);
275  std::ostream& print(std::ostream& os) const;
276  std::ostream& hprint(std::ostream &os, SensorCollection *sc) const;
277 //member functions
278  void clearSources();
279  void setNSources(int n);
280  void setSource(sensor_ptr _source, float weight, int id);
281  void setSource(sensor_ptr _source, float weight)
282  { setSource(_source, weight, sources.size()); }
283  Sensor& assign(const Sensor& rhs);
284  void normalizeInput(bool doit=true) { m_NormalizeInput = doit; }
285  bool isInputNormalized() const { return m_NormalizeInput; }
286 protected:
287  std::vector<sensor_ptr> sources;
289 
291  float calcValue(int x, int y) const {
292  float result = 0.0f;
293  std::vector<sensor_ptr>::const_iterator s = sources.begin();
294  std::vector<float>::const_iterator w = cweights.begin();
295  if(m_NormalizeInput)
296  for(; s != sources.end(); s++,w++) {
297  result += (*s)->getWeightedValue(x,y)*(*w);
298  }
299  else
300  for(; s != sources.end(); s++,w++) {
301  result += (*s)->getValue(x,y)*(*w);
302  }
303  return result;
304  }
305 
306  std::vector<float> calcMValue(int x, int y) const {
307  std::vector<float> vv(getNChannels());
308  std::vector<float>::iterator v = vv.begin();
309  std::vector<sensor_ptr>::const_iterator s = sources.begin();
310  std::vector<float>::const_iterator w = cweights.begin();
311  if(m_NormalizeInput)
312  for(; v != vv.end(); v++, s++,w++)
313  *v = (*s)->getWeightedValue(x,y)*(*w);
314  else
315  for(; v != vv.end(); v++, s++,w++)
316  *v = (*s)->getValue(x,y)*(*w);
317  return vv;
318  }
319 
320  //void calcMinMax();
321 };
322 
324 class MahalSensor : public PPSensor {
325 public:
326  MahalSensor(const std::string& fname="")
327  {
328  m_AddSkip = 1;
329  loadConfig(fname);
330  }
331  int getNChannels() const { return 1; }
332  Sensor& assign(const Sensor& rhs);
333  std::ostream& print(std::ostream& os) const {
334  Sensor::print(os)
335  << "C " << m_Filename;
336  return os;
337  }
338  bool loadConfig(const std::string& fname = "");
339  bool saveConfig(const std::string& fname = "") const;
340  void setCovi(const DMatrix<float>& icov) { this->icov = icov; }
341  void setMean(const DMatrix<float>& mean) { this->m = mean; }
342  const DMatrix<float>& getICov() const { return icov; }
343  const DMatrix<float>& getMean() const { return m; }
344  const std::string& getFilename() const { return m_Filename; }
345 protected:
347  float calcValue(int x, int y) const {
348  std::vector<float> mv = source->getMValue(x,y);
349  DMatrix<float> v(1,std::min(mv.size(),size_t(3)),&mv.front());
350  v -= m;
351  DMatrix<float> vt(v); vt.transpose();
352  float d = (vt.mulRight(icov.mulRight(v))).at(0,0);
353  return exp(-0.5*d);
354  }
356  mutable std::string m_Filename;
357 };
358 
360 class MappingSensor : public PPSensor {
361 public:
362  enum MappingTypes {MS_IDENTITY, MS_CLAMPLU, MS_CLAMPL, MS_CLAMPU,
363  MS_GAUSSNORM, MS_MGAUSSNORM, MS_MCLAMPU, MS_BIAS,
364  MS_LAST};
365 
366  MappingSensor(const std::string& mapn="")
367  : m_MappingID()
368  {
369  m_AddSkip = 1;
370  setMapping(mapn);
371  }
372  int getNChannels() const { return 1; }
373  Sensor& assign(const Sensor& rhs);
374  std::ostream& print(std::ostream& os) const;
375  const char* getMappingName() const
376  { return s_MappingNames[m_MappingID]; }
377  dword getMappingID() const { return m_MappingID; }
378  dword setMappingID(dword id);
379  dword setMapping(const std::string& mname);
380  bool readParams(std::istream& is);
381  bool performUpdate();
382 protected:
383  static const char* s_MappingNames[];
385  virtual float calcValue(int x, int y) const {
386  float v = source->getValue(x,y);
387  switch(m_MappingID) {
388 // case MS_IDENTITY:
389 // break;
390  case MS_CLAMPL:
391  if(v < m_Param[0]) v = m_Param[0];
392  break;
393  case MS_CLAMPU:
394  if(v > m_Param[0]) v = m_Param[0];
395  break;
396  case MS_CLAMPLU:
397  if(v < m_Param[0]) v = m_Param[0];
398  else if(v > m_Param[1]) v = m_Param[1];
399  break;
400  case MS_MCLAMPU:
401  if(v > m_Param[1]) v = m_Param[1];
402  break;
403  case MS_GAUSSNORM:
404  v = (v-m_Param[1])*m_Param[2]+0.5;
405  break;
406  case MS_MGAUSSNORM:
407  if(v>0.f) v = (v-m_Param[1])*m_Param[2];
408  break;
409  case MS_BIAS:
410  //if(v>0.f) {
411  v = (v-m_Param[1])*m_Param[2];
412  v = erf(v)*0.5+0.5;
413  //} else v = 0.f;
414  break;
415  }
416  return v;
417  }
418 
419 protected:
421  std::vector<float> m_Param;
422 };
423 
424 
425 #endif
MT & transpose()
Definition: DMatrix.h:260
dword m_MappingID
Definition: SensorSet.h:420
const std::string & getFilename() const
Definition: SensorSet.h:344
std::ostream & print(std::ostream &os) const
Definition: SensorSet.h:16
bool isModified(dword mask=UPD_ALL) const
Definition: Sensor.h:108
#define NULL
Definition: simpletypes.h:9
virtual std::ostream & hprint(std::ostream &os, SensorCollection *sc) const
Definition: Sensor.cpp:222
virtual Point calcGradient(int x, int y) const
Definition: SensorSet.h:83
const DMatrix< float > & getICov() const
Definition: SensorSet.h:342
std::vector< sensor_ptr > sources
Definition: SensorSet.h:287
int getNChannels() const
Definition: SensorSet.h:39
std::ostream & print(std::ostream &os) const
Definition: SensorSet.h:107
int getNChannels() const
Definition: SensorSet.h:265
bool m_NormalizeInput
Definition: SensorSet.h:288
float calcValue(int x, int y) const
Definition: SensorSet.h:224
const char * getMappingName() const
Definition: SensorSet.h:375
int m_AddSkip
Definition: Sensor.h:183
virtual void changeSource(sensor_cptr _source)
Definition: Sensor.cpp:96
MCGSensor(sensor_cptr _source=NULL)
Definition: SensorSet.h:65
float y
Definition: Point.h:224
bool isValid(int x, int y) const
Definition: Sensor.cpp:197
virtual int getNChannels() const
Definition: Sensor.h:123
std::vector< float > cweights
multi-channel weights (&#39;color&#39;)
Definition: Sensor.h:174
int getNChannels() const
Definition: SensorSet.h:67
int getSkip() const
Definition: SensorSet.h:266
Definition: Sensor.h:21
float calcValue(int x, int y) const
Computes saturation or some other colour indicator.
Definition: SensorSet.h:347
Image< double > m_SFilter
Definition: SensorSet.h:168
std::ostream & print(std::ostream &os) const
Definition: SensorSet.h:68
float calcValue(int x, int y) const
Computes a scalar product between the multi-channel intensitis and the cweights vector.
Definition: SensorSet.h:50
std::shared_ptr< Sensor > sensor_ptr
Definition: types_fwd.h:15
virtual bool performUpdate()
Definition: Sensor.cpp:123
bool isInputNormalized() const
Definition: SensorSet.h:285
dword getMappingID() const
Definition: SensorSet.h:377
float calcValue(int x, int y) const
Computes a scalar product between the multi-channel intensities and the cweights vector.
Definition: SensorSet.h:291
std::ostream & print(std::ostream &os) const
Definition: SensorSet.h:217
std::ostream & print(std::ostream &os) const
Definition: SensorSet.h:154
MahalSensor(const std::string &fname="")
Definition: SensorSet.h:326
std::ostream & print(std::ostream &os) const
Definition: SensorSet.h:333
float normalize()
normalizes the vector; returns old norm
Definition: Point.h:175
std::vector< float > calcMValue(int x, int y) const
Definition: SensorSet.h:306
virtual float calcValue(int x, int y) const
Computes saturation or some other colour indicator.
Definition: SensorSet.h:385
CRSensor()
Definition: SensorSet.h:104
void enableUpdate(dword udMask)
Definition: Sensor.h:113
void setSource(sensor_ptr _source, float weight)
Definition: SensorSet.h:281
float calcValue(int x, int y) const
Definition: SensorSet.h:162
std::ostream & print(std::ostream &os) const
Definition: SensorSet.h:188
int getNChannels() const
Definition: SensorSet.h:106
bool performUpdate()
Definition: SensorSet.h:211
unsigned long dword
Definition: simpletypes.h:6
std::ostream & print(std::ostream &os) const
Definition: SensorSet.h:40
void setCovi(const DMatrix< float > &icov)
Definition: SensorSet.h:340
float getScale() const
Definition: Sensor.h:101
virtual bool performUpdate()
Definition: Sensor.cpp:306
bool performUpdate()
Definition: SensorSet.h:182
float calcValue(int x, int y) const
Computes a scalar product between the multi-channel intensities and the cweights vector.
Definition: SensorSet.h:75
std::string m_Filename
Definition: SensorSet.h:356
int getNChannels() const
Definition: SensorSet.h:331
const DMatrix< float > & getMean() const
Definition: SensorSet.h:343
void setMean(const DMatrix< float > &mean)
Definition: SensorSet.h:341
float calcValue(int x, int y) const
Definition: SensorSet.h:31
float mean
Definition: Sensor.h:177
std::shared_ptr< const Sensor > sensor_cptr
Definition: types_fwd.h:17
float calcValue(int x, int y) const
Definition: SensorSet.h:195
DMatrix< float > m
Definition: SensorSet.h:355
void normalizeInput(bool doit=true)
Definition: SensorSet.h:284
Definition: Point.h:16
T & at(dword x, dword y)
Definition: DMatrix.h:46
float calcValue(int x, int y) const
Computes saturation or some other colour indicator.
Definition: SensorSet.h:114
std::vector< float > m_Param
Definition: SensorSet.h:421
float calcValue(int x, int y) const
Definition: SensorSet.h:22
sensor_cptr source
Definition: Sensor.h:170
int m_Skip
Definition: Sensor.h:182
Image< std::complex< double > > m_FFilter
Definition: SensorSet.h:169
virtual Sensor & assign(const Sensor &rhs)
Definition: Sensor.cpp:54
float x
Definition: Point.h:224
MappingSensor(const std::string &mapn="")
Definition: SensorSet.h:366
int getNChannels() const
Definition: SensorSet.h:372
virtual std::ostream & print(std::ostream &os) const
Definition: Sensor.cpp:213
MT mulRight(const MT &rhs) const
Definition: DMatrix.h:149
DMatrix< T > & sqrt(DMatrix< T > &mat)
Definition: DMatrixUtil.h:81