ITK  4.2.0
Insight Segmentation and Registration Toolkit
itkStatisticsLabelObject.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef __itkStatisticsLabelObject_h
19 #define __itkStatisticsLabelObject_h
20 
21 #include "itkShapeLabelObject.h"
22 #include "itkHistogram.h"
23 
24 namespace itk
25 {
40 template< class TLabel, unsigned int VImageDimension >
41 class ITK_EXPORT StatisticsLabelObject:public ShapeLabelObject< TLabel, VImageDimension >
42 {
43 public:
47  typedef typename Superclass::LabelObjectType LabelObjectType;
51 
53  itkNewMacro(Self);
54 
57 
59 
60  itkStaticConstMacro(ImageDimension, unsigned int, VImageDimension);
61 
62  typedef typename Superclass::IndexType IndexType;
63 
65 
66  typedef TLabel LabelType;
67 
68  typedef typename Superclass::LineType LineType;
69 
70  typedef typename Superclass::LengthType LengthType;
71 
73 
75 
77 
79  itkStaticConstMacro(MINIMUM, AttributeType, 200);
80  itkStaticConstMacro(MAXIMUM, AttributeType, 201);
81  itkStaticConstMacro(MEAN, AttributeType, 202);
82  itkStaticConstMacro(SUM, AttributeType, 203);
83  itkStaticConstMacro(STANDARD_DEVIATION, AttributeType, 204);
84  itkStaticConstMacro(VARIANCE, AttributeType, 205);
85  itkStaticConstMacro(MEDIAN, AttributeType, 206);
86  itkStaticConstMacro(MAXIMUM_INDEX, AttributeType, 207);
87  itkStaticConstMacro(MINIMUM_INDEX, AttributeType, 208);
88  itkStaticConstMacro(CENTER_OF_GRAVITY, AttributeType, 209);
89 // itkStaticConstMacro(CENTRAL_MOMENTS, AttributeType, 210);
90  itkStaticConstMacro(WEIGHTED_PRINCIPAL_MOMENTS, AttributeType, 211);
91  itkStaticConstMacro(WEIGHTED_PRINCIPAL_AXES, AttributeType, 212);
92  itkStaticConstMacro(KURTOSIS, AttributeType, 213);
93  itkStaticConstMacro(SKEWNESS, AttributeType, 214);
94  itkStaticConstMacro(WEIGHTED_ELONGATION, AttributeType, 215);
95  itkStaticConstMacro(HISTOGRAM, AttributeType, 216);
96  itkStaticConstMacro(WEIGHTED_FLATNESS, AttributeType, 217);
97 
98  static AttributeType GetAttributeFromName(const std::string & s)
99  {
100  if ( s == "Minimum" )
101  {
102  return MINIMUM;
103  }
104  else if ( s == "Maximum" )
105  {
106  return MAXIMUM;
107  }
108  else if ( s == "Mean" )
109  {
110  return MEAN;
111  }
112  else if ( s == "Sum" )
113  {
114  return SUM;
115  }
116  else if ( s == "StandardDeviation" )
117  {
118  return STANDARD_DEVIATION;
119  }
120  else if ( s == "Variance" )
121  {
122  return VARIANCE;
123  }
124  else if ( s == "Median" )
125  {
126  return MEDIAN;
127  }
128  else if ( s == "MaximumIndex" )
129  {
130  return MAXIMUM_INDEX;
131  }
132  else if ( s == "MinimumIndex" )
133  {
134  return MINIMUM_INDEX;
135  }
136  else if ( s == "CenterOfGravity" )
137  {
138  return CENTER_OF_GRAVITY;
139  }
140  /*
141  else if( s == "CentralMoments" )
142  {
143  return CENTRAL_MOMENTS;
144  }
145  */
146  else if ( s == "WeightedPrincipalMoments" )
147  {
148  return WEIGHTED_PRINCIPAL_MOMENTS;
149  }
150  else if ( s == "WeightedPrincipalAxes" )
151  {
152  return WEIGHTED_PRINCIPAL_AXES;
153  }
154  else if ( s == "Kurtosis" )
155  {
156  return KURTOSIS;
157  }
158  else if ( s == "Skewness" )
159  {
160  return SKEWNESS;
161  }
162  else if ( s == "WeightedElongation" )
163  {
164  return WEIGHTED_ELONGATION;
165  }
166  else if ( s == "Histogram" )
167  {
168  return HISTOGRAM;
169  }
170  else if ( s == "WeightedFlatness" )
171  {
172  return WEIGHTED_FLATNESS;
173  }
174  // can't recognize the name
175  return Superclass::GetAttributeFromName(s);
176  }
177 
178  static std::string GetNameFromAttribute(const AttributeType & a)
179  {
180  switch ( a )
181  {
182  case MINIMUM:
183  return "Minimum";
184  break;
185  case MAXIMUM:
186  return "Maximum";
187  break;
188  case MEAN:
189  return "Mean";
190  break;
191  case SUM:
192  return "Sum";
193  break;
194  case STANDARD_DEVIATION:
195  return "StandardDeviation";
196  break;
197  case VARIANCE:
198  return "Variance";
199  break;
200  case MEDIAN:
201  return "Median";
202  break;
203  case MAXIMUM_INDEX:
204  return "MaximumIndex";
205  break;
206  case MINIMUM_INDEX:
207  return "MinimumIndex";
208  break;
209  case CENTER_OF_GRAVITY:
210  return "CenterOfGravity";
211  break;
212  /* case CENTRAL_MOMENTS:
213  return "CentralMoments";
214  break;*/
215  case WEIGHTED_PRINCIPAL_MOMENTS:
216  return "WeightedPrincipalMoments";
217  break;
218  case WEIGHTED_PRINCIPAL_AXES:
219  return "WeightedPrincipalAxes";
220  break;
221  case KURTOSIS:
222  return "Kurtosis";
223  break;
224  case SKEWNESS:
225  return "Skewness";
226  break;
227  case WEIGHTED_ELONGATION:
228  return "WeightedElongation";
229  break;
230  case HISTOGRAM:
231  return "Histogram";
232  break;
233  case WEIGHTED_FLATNESS:
234  return "WeightedFlatness";
235  break;
236  }
237  // can't recognize the name
238  return Superclass::GetNameFromAttribute(a);
239  }
240 
242 
243  typedef typename Superclass::CentroidType CentroidType;
244 
245  virtual void CopyAttributesFrom(const LabelObjectType *lo)
246  {
247  Superclass::CopyAttributesFrom(lo);
248 
249  // copy the data of the current type if possible
250  const Self *src = dynamic_cast< const Self * >( lo );
251  if ( src == NULL )
252  {
253  return;
254  }
255  m_Minimum = src->m_Minimum;
256  m_Maximum = src->m_Maximum;
257  m_Mean = src->m_Mean;
258  m_Sum = src->m_Sum;
259  m_StandardDeviation = src->m_StandardDeviation;
260  m_Variance = src->m_Variance;
261  m_Median = src->m_Median;
262  m_MaximumIndex = src->m_MaximumIndex;
263  m_MinimumIndex = src->m_MinimumIndex;
264  m_CenterOfGravity = src->m_CenterOfGravity;
265  // m_CentralMoments = src->m_CentralMoments;
266  m_WeightedPrincipalMoments = src->m_WeightedPrincipalMoments;
267  m_WeightedPrincipalAxes = src->m_WeightedPrincipalAxes;
268  m_Kurtosis = src->m_Kurtosis;
269  m_Skewness = src->m_Skewness;
270  m_WeightedElongation = src->m_WeightedElongation;
271  m_Histogram = src->m_Histogram;
272  m_WeightedFlatness = src->m_WeightedFlatness;
273  }
274 
275  const double & GetMinimum() const
276  {
277  return m_Minimum;
278  }
279 
280  void SetMinimum(const double & v)
281  {
282  m_Minimum = v;
283  }
284 
285  const double & GetMaximum() const
286  {
287  return m_Maximum;
288  }
289 
290  void SetMaximum(const double & v)
291  {
292  m_Maximum = v;
293  }
294 
295  const double & GetMean() const
296  {
297  return m_Mean;
298  }
299 
300  void SetMean(const double & v)
301  {
302  m_Mean = v;
303  }
304 
305  const double & GetSum() const
306  {
307  return m_Sum;
308  }
309 
310  void SetSum(const double & v)
311  {
312  m_Sum = v;
313  }
314 
315  const double & GetStandardDeviation() const
316  {
317  return m_StandardDeviation;
318  }
319 
320  void SetStandardDeviation(const double & v)
321  {
322  m_StandardDeviation = v;
323  }
324 
325  const double & GetVariance() const
326  {
327  return m_Variance;
328  }
329 
330  void SetVariance(const double & v)
331  {
332  m_Variance = v;
333  }
334 
335  const double & GetMedian() const
336  {
337  return m_Median;
338  }
339 
340  void SetMedian(const double & v)
341  {
342  m_Median = v;
343  }
344 
345  const IndexType & GetMaximumIndex() const
346  {
347  return m_MaximumIndex;
348  }
349 
350  void SetMaximumIndex(const IndexType & v)
351  {
352  m_MaximumIndex = v;
353  }
354 
355  const IndexType & GetMinimumIndex() const
356  {
357  return m_MinimumIndex;
358  }
359 
360  void SetMinimumIndex(const IndexType & v)
361  {
362  m_MinimumIndex = v;
363  }
364 
365  const PointType & GetCenterOfGravity() const
366  {
367  return m_CenterOfGravity;
368  }
369 
370  void SetCenterOfGravity(const PointType & v)
371  {
372  m_CenterOfGravity = v;
373  }
374 
375  /*
376  const MatrixType & GetCentralMoments() const
377  {
378  return m_CentralMoments;
379  }
380 
381  void SetCentralMoments( const MatrixType & v )
382  {
383  m_CentralMoments = v;
384  }*/
385 
386  const VectorType & GetWeightedPrincipalMoments() const
387  {
388  return m_WeightedPrincipalMoments;
389  }
390 
391  void SetWeightedPrincipalMoments(const VectorType & v)
392  {
393  m_WeightedPrincipalMoments = v;
394  }
395 
396  const MatrixType & GetWeightedPrincipalAxes() const
397  {
398  return m_WeightedPrincipalAxes;
399  }
400 
401  void SetWeightedPrincipalAxes(const MatrixType & v)
402  {
403  m_WeightedPrincipalAxes = v;
404  }
405 
406  const double & GetSkewness() const
407  {
408  return m_Skewness;
409  }
410 
411  void SetSkewness(const double & v)
412  {
413  m_Skewness = v;
414  }
415 
416  const double & GetKurtosis() const
417  {
418  return m_Kurtosis;
419  }
420 
421  void SetKurtosis(const double & v)
422  {
423  m_Kurtosis = v;
424  }
425 
426  const double & GetWeightedElongation() const
427  {
428  return m_WeightedElongation;
429  }
430 
431  void SetWeightedElongation(const double & v)
432  {
433  m_WeightedElongation = v;
434  }
435 
436  const HistogramType * GetHistogram() const
437  {
438  return m_Histogram;
439  }
440 
441  void SetHistogram(const HistogramType *v)
442  {
443  m_Histogram = v;
444  }
445 
446  const double & GetWeightedFlatness() const
447  {
448  return m_WeightedFlatness;
449  }
450 
451  void SetWeightedFlatness(const double & v)
452  {
453  m_WeightedFlatness = v;
454  }
455 
456  // some helper methods - not really required, but really useful!
460 
464  AffineTransformPointer GetWeightedPrincipalAxesToPhysicalAxesTransform() const
465  {
466  typename AffineTransformType::MatrixType matrix;
467  typename AffineTransformType::OffsetType offset;
468  for ( unsigned int i = 0; i < ImageDimension; i++ )
469  {
470  offset[i] = m_CenterOfGravity[i];
471  for ( unsigned int j = 0; j < ImageDimension; j++ )
472  {
473  matrix[j][i] = m_WeightedPrincipalAxes[i][j]; // Note the transposition
474  }
475  }
477 
478  AffineTransformPointer result = AffineTransformType::New();
479 
480  result->SetMatrix(matrix);
481  result->SetOffset(offset);
482 
483  return result;
484  }
485 
490  AffineTransformPointer GetPhysicalAxesToWeightedPrincipalAxesTransform(void) const
491  {
492  typename AffineTransformType::MatrixType matrix;
493  typename AffineTransformType::OffsetType offset;
494  for ( unsigned int i = 0; i < ImageDimension; i++ )
495  {
496  offset[i] = m_CenterOfGravity[i];
497  for ( unsigned int j = 0; j < ImageDimension; j++ )
498  {
499  matrix[j][i] = m_WeightedPrincipalAxes[i][j]; // Note the transposition
500  }
501  }
503 
504  AffineTransformPointer result = AffineTransformType::New();
505  result->SetMatrix(matrix);
506  result->SetOffset(offset);
507 
508  AffineTransformPointer inverse = AffineTransformType::New();
509  result->GetInverse(inverse);
510 
511  return inverse;
512  }
513 
514 protected:
516  {
517  m_Minimum = 0;
518  m_Maximum = 0;
519  m_Mean = 0;
520  m_Sum = 0;
521  m_StandardDeviation = 0;
522  m_Variance = 0;
523  m_Median = 0;
524  m_MaximumIndex.Fill(0);
525  m_MinimumIndex.Fill(0);
526  m_CenterOfGravity.Fill(0);
527  // m_CentralMoments.Fill(0);
528  m_WeightedPrincipalMoments.Fill(0);
529  m_WeightedPrincipalAxes.Fill(0);
530  m_Kurtosis = 0;
531  m_Skewness = 0;
532  m_WeightedElongation = 0;
533  m_Histogram = NULL;
534  m_WeightedFlatness = 0;
535  }
536 
537  void PrintSelf(std::ostream & os, Indent indent) const
538  {
539  Superclass::PrintSelf(os, indent);
540 
541  os << indent << "Minimum: " << m_Minimum << std::endl;
542  os << indent << "Maximum: " << m_Maximum << std::endl;
543  os << indent << "Mean: " << m_Mean << std::endl;
544  os << indent << "Sum: " << m_Sum << std::endl;
545  os << indent << "StandardDeviation: " << m_StandardDeviation << std::endl;
546  os << indent << "Variance: " << m_Variance << std::endl;
547  os << indent << "Median: " << m_Median << std::endl;
548  os << indent << "Skewness: " << m_Skewness << std::endl;
549  os << indent << "Kurtosis: " << m_Kurtosis << std::endl;
550  os << indent << "WeightedElongation: " << m_WeightedElongation << std::endl;
551  os << indent << "WeightedFlatness: " << m_WeightedFlatness << std::endl;
552  os << indent << "MaximumIndex: " << m_MaximumIndex << std::endl;
553  os << indent << "MinimumIndex: " << m_MinimumIndex << std::endl;
554  os << indent << "CenterOfGravity: " << m_CenterOfGravity << std::endl;
555  // os << indent << "CentralMoments: " << std::endl << m_CentralMoments;
556  os << indent << "WeightedPrincipalMoments: " << m_WeightedPrincipalMoments << std::endl;
557  os << indent << "WeightedPrincipalAxes: " << std::endl << m_WeightedPrincipalAxes;
558  os << indent << "Histogram: ";
559  if ( m_Histogram.IsNull() )
560  {
561  os << "NULL" << std::endl;
562  }
563  else
564  {
565  m_Histogram->Print(os, indent);
566  }
567  }
568 
569 private:
570  StatisticsLabelObject(const Self &); //purposely not implemented
571  void operator=(const Self &); //purposely not implemented
572 
573  double m_Minimum;
574  double m_Maximum;
575  double m_Mean;
576  double m_Sum;
578  double m_Variance;
579  double m_Median;
583  // MatrixType m_CentralMoments;
586  double m_Skewness;
587  double m_Kurtosis;
589 
591 
593 };
594 } // end namespace itk
595 
596 #endif
597