ITK  4.13.0
Insight Segmentation and Registration Toolkit
itkShapeLabelObject.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 itkShapeLabelObject_h
19 #define itkShapeLabelObject_h
20 
21 #include "itkLabelObject.h"
22 #include "itkLabelMap.h"
23 #include "itkAffineTransform.h"
24 
25 namespace itk
26 {
41 template< typename TLabel, unsigned int VImageDimension >
42 class ShapeLabelObject:public LabelObject< TLabel, VImageDimension >
43 {
44 public:
52 
54  itkNewMacro(Self);
55 
57  itkTypeMacro(ShapeLabelObject, LabelObject);
58 
60 
61  itkStaticConstMacro(ImageDimension, unsigned int, VImageDimension);
62 
63  typedef typename Superclass::IndexType IndexType;
64 
65  typedef TLabel LabelType;
66 
67  typedef typename Superclass::LineType LineType;
68 
70 
72 
74  itkStaticConstMacro(NUMBER_OF_PIXELS, AttributeType, 100);
75 
79  itkStaticConstMacro(PHYSICAL_SIZE, AttributeType, 101);
80 
84  itkStaticConstMacro(CENTROID, AttributeType, 104);
85 
86  itkStaticConstMacro(BOUNDING_BOX, AttributeType, 105);
87 
94  itkStaticConstMacro(NUMBER_OF_PIXELS_ON_BORDER, AttributeType, 106);
95 
103  itkStaticConstMacro(PERIMETER_ON_BORDER, AttributeType, 107);
104 
108  itkStaticConstMacro(FERET_DIAMETER, AttributeType, 108);
109 
111  itkStaticConstMacro(PRINCIPAL_MOMENTS, AttributeType, 109);
112 
114  itkStaticConstMacro(PRINCIPAL_AXES, AttributeType, 110);
115 
119  itkStaticConstMacro(ELONGATION, AttributeType, 111);
120 
122  itkStaticConstMacro(PERIMETER, AttributeType, 112);
123 
124  itkStaticConstMacro(ROUNDNESS, AttributeType, 113);
125 
129  itkStaticConstMacro(EQUIVALENT_SPHERICAL_RADIUS, AttributeType, 114);
130 
134  itkStaticConstMacro(EQUIVALENT_SPHERICAL_PERIMETER, AttributeType, 115);
135 
139  itkStaticConstMacro(EQUIVALENT_ELLIPSOID_DIAMETER, AttributeType, 116);
140 
141  itkStaticConstMacro(FLATNESS, AttributeType, 117);
142 
143  itkStaticConstMacro(PERIMETER_ON_BORDER_RATIO, AttributeType, 118);
144 
145 
148  itkStaticConstMacro(ORIENTED_BOUNDING_BOX_ORIGIN, AttributeType, 119);
149 
150 
158  itkStaticConstMacro(ORIENTED_BOUNDING_BOX_SIZE, AttributeType, 120);
159 
160  static AttributeType GetAttributeFromName(const std::string & s)
161  {
162  if ( s == "NumberOfPixels" )
163  {
164  return NUMBER_OF_PIXELS;
165  }
166  else if ( s == "PhysicalSize" )
167  {
168  return PHYSICAL_SIZE;
169  }
170  else if ( s == "Centroid" )
171  {
172  return CENTROID;
173  }
174  else if ( s == "BoundingBox" )
175  {
176  return BOUNDING_BOX;
177  }
178  else if ( s == "NumberOfPixelsOnBorder" )
179  {
181  }
182  else if ( s == "PerimeterOnBorder" )
183  {
184  return PERIMETER_ON_BORDER;
185  }
186  else if ( s == "FeretDiameter" )
187  {
188  return FERET_DIAMETER;
189  }
190  else if ( s == "PrincipalMoments" )
191  {
192  return PRINCIPAL_MOMENTS;
193  }
194  else if ( s == "PrincipalAxes" )
195  {
196  return PRINCIPAL_AXES;
197  }
198  else if ( s == "Elongation" )
199  {
200  return ELONGATION;
201  }
202  else if ( s == "Perimeter" )
203  {
204  return PERIMETER;
205  }
206  else if ( s == "Roundness" )
207  {
208  return ROUNDNESS;
209  }
210  else if ( s == "EquivalentSphericalRadius" )
211  {
213  }
214  else if ( s == "EquivalentSphericalPerimeter" )
215  {
217  }
218  else if ( s == "EquivalentEllipsoidDiameter" )
219  {
221  }
222  else if ( s == "Flatness" )
223  {
224  return FLATNESS;
225  }
226  else if ( s == "PerimeterOnBorderRatio" )
227  {
229  }
230  else if ( s == "OrientedBoundingBoxOrigin")
231  {
233  }
234  else if ( s == "OrientedBoundingBoxSize")
235  {
237  }
238  // can't recognize the name
240  }
241 
242  static std::string GetNameFromAttribute(const AttributeType & a)
243  {
244  std::string name;
245  switch ( a )
246  {
247  case NUMBER_OF_PIXELS:
248  name = "NumberOfPixels";
249  break;
250  case PHYSICAL_SIZE:
251  name = "PhysicalSize";
252  break;
253  case CENTROID:
254  name = "Centroid";
255  break;
256  case BOUNDING_BOX:
257  name = "BoundingBox";
258  break;
260  name = "NumberOfPixelsOnBorder";
261  break;
262  case PERIMETER_ON_BORDER:
263  name = "PerimeterOnBorder";
264  break;
265  case FERET_DIAMETER:
266  name = "FeretDiameter";
267  break;
268  case PRINCIPAL_MOMENTS:
269  name = "PrincipalMoments";
270  break;
271  case PRINCIPAL_AXES:
272  name = "PrincipalAxes";
273  break;
274  case ELONGATION:
275  name = "Elongation";
276  break;
277  case PERIMETER:
278  name = "Perimeter";
279  break;
280  case ROUNDNESS:
281  name = "Roundness";
282  break;
284  name = "EquivalentSphericalRadius";
285  break;
287  name = "EquivalentSphericalPerimeter";
288  break;
290  name = "EquivalentEllipsoidDiameter";
291  break;
292  case FLATNESS:
293  name = "Flatness";
294  break;
296  name = "PerimeterOnBorderRatio";
297  break;
299  name = "OrientedBoundingBoxOrigin";
300  break;
302  name = "OrientedBoundingBoxSize";
303  break;
304  default:
305  // can't recognize the name
307  break;
308  }
309  return name;
310  }
311 
313 
315 
317 
319 
320 
321 private:
322 
323  template <size_t VX, unsigned short VY>
324  struct IntegerPow
325  {
326  static const size_t Result = VX*IntegerPow<VX, VY-1>::Result;
327  };
328 
329  template <size_t VX>
330  struct IntegerPow<VX,0>
331  {
332  static const size_t Result = 1;
333  };
334 
335 public:
336 
338 
340 
342 
344 
345 
346  const RegionType & GetBoundingBox() const
347  {
348  return m_BoundingBox;
349  }
350 
351  void SetBoundingBox(const RegionType & v)
352  {
353  m_BoundingBox = v;
354  }
355 
356  const double & GetPhysicalSize() const
357  {
358  return m_PhysicalSize;
359  }
360 
361  void SetPhysicalSize(const double & v)
362  {
363  m_PhysicalSize = v;
364  }
365 
367  {
368  return m_NumberOfPixels;
369  }
370 
372  {
373  m_NumberOfPixels = v;
374  }
375 
376  const CentroidType & GetCentroid() const
377  {
378  return m_Centroid;
379  }
380 
381  void SetCentroid(const CentroidType & centroid)
382  {
383  m_Centroid = centroid;
384  }
385 
387  {
389  }
390 
392  {
394  }
395 
396  const double & GetPerimeterOnBorder() const
397  {
398  return m_PerimeterOnBorder;
399  }
400 
401  void SetPerimeterOnBorder(const double & v)
402  {
404  }
405 
406  const double & GetFeretDiameter() const
407  {
408  return m_FeretDiameter;
409  }
410 
411  void SetFeretDiameter(const double & v)
412  {
413  m_FeretDiameter = v;
414  }
415 
417  {
418  return m_PrincipalMoments;
419  }
420 
422  {
423  m_PrincipalMoments = v;
424  }
425 
426  const MatrixType & GetPrincipalAxes() const
427  {
428  return m_PrincipalAxes;
429  }
430 
431  void SetPrincipalAxes(const MatrixType & v)
432  {
433  m_PrincipalAxes = v;
434  }
435 
436  const double & GetElongation() const
437  {
438  return m_Elongation;
439  }
440 
441  void SetElongation(const double & v)
442  {
443  m_Elongation = v;
444  }
445 
446  const double & GetPerimeter() const
447  {
448  return m_Perimeter;
449  }
450 
451  void SetPerimeter(const double & v)
452  {
453  m_Perimeter = v;
454  }
455 
456  const double & GetRoundness() const
457  {
458  return m_Roundness;
459  }
460 
461  void SetRoundness(const double & v)
462  {
463  m_Roundness = v;
464  }
465 
466  const double & GetEquivalentSphericalRadius() const
467  {
469  }
470 
471  void SetEquivalentSphericalRadius(const double & v)
472  {
474  }
475 
476  const double & GetEquivalentSphericalPerimeter() const
477  {
479  }
480 
481  void SetEquivalentSphericalPerimeter(const double & v)
482  {
484  }
485 
487  {
489  }
490 
492  {
494  }
495 
496  const double & GetFlatness() const
497  {
498  return m_Flatness;
499  }
500 
501  void SetFlatness(const double & v)
502  {
503  m_Flatness = v;
504  }
505 
506  const double & GetPerimeterOnBorderRatio() const
507  {
509  }
510 
511  void SetPerimeterOnBorderRatio(const double & v)
512  {
514  }
515 
517  {
519  }
520 
522  {
524  }
525 
527  {
529  }
530 
532  {
534  }
535 
536 
537  // some helper methods - not really required, but really useful!
538 
540  const RegionType & GetRegion() const
541  {
542  return m_BoundingBox;
543  }
544 
545 
549  {
550  return this->GetPrincipalAxes();
551  }
552 
564  {
565 
568 
569  for( unsigned int i = 0; i < ImageDimension; ++i)
570  {
571  max[i] += m_OrientedBoundingBoxSize[i];
572  }
573 
575 
576  // Use binary index to map the vertices of the OBB to an array. For
577  // example, in 2D, binary counting will give[0,0], [0,1], [1,0],
578  // [1,1], which corresponds to [minX,minY], [minX,maxY],
579  // [maxX,minY], [maxX,maxY].
580  for (unsigned int i = 0; i < OrientedBoundingBoxVerticesType::Length; ++i)
581  {
582  const unsigned int msb = 1 << (ImageDimension-1);
583  for ( unsigned int j = 0; j < ImageDimension; j++ )
584  {
585  if (i & msb>>j)
586  {
587  vertices[i][j] = max[j];
588  }
589  else
590  {
591  vertices[i][j] = min[j];
592  }
593  }
594  }
595  return vertices;
596  }
597 
601 
606  {
607  typename AffineTransformType::MatrixType matrix;
608  typename AffineTransformType::OffsetType offset;
609  for ( unsigned int i = 0; i < VImageDimension; i++ )
610  {
611  offset[i] = m_Centroid[i];
612  for ( unsigned int j = 0; j < VImageDimension; j++ )
613  {
614  matrix[j][i] = m_PrincipalAxes[i][j]; // Note the transposition
615  }
616  }
618 
620 
621  result->SetMatrix(matrix);
622  result->SetOffset(offset);
623 
624  return result;
625  }
626 
632  {
633  typename AffineTransformType::MatrixType matrix;
634  typename AffineTransformType::OffsetType offset;
635  for ( unsigned int i = 0; i < VImageDimension; i++ )
636  {
637  offset[i] = m_Centroid[i];
638  for ( unsigned int j = 0; j < VImageDimension; j++ )
639  {
640  matrix[j][i] = m_PrincipalAxes[i][j]; // Note the transposition
641  }
642  }
644 
646  result->SetMatrix(matrix);
647  result->SetOffset(offset);
648 
650  result->GetInverse(inverse);
651 
652  return inverse;
653  }
654 
655  template< typename TSourceLabelObject >
656  void CopyAttributesFrom( const TSourceLabelObject * src )
657  {
658  Superclass::template CopyAttributesFrom<TSourceLabelObject>(src);
659 
660  m_BoundingBox = src->GetBoundingBox();
661  m_NumberOfPixels = src->GetNumberOfPixels();
662  m_PhysicalSize = src->GetPhysicalSize();
663  m_Centroid = src->GetCentroid();
664  m_NumberOfPixelsOnBorder = src->GetNumberOfPixelsOnBorder();
665  m_PerimeterOnBorder = src->GetPerimeterOnBorder();
666  m_FeretDiameter = src->GetFeretDiameter();
667  m_PrincipalMoments = src->GetPrincipalMoments();
668  m_PrincipalAxes = src->GetPrincipalAxes();
669  m_Elongation = src->GetElongation();
670  m_Perimeter = src->GetPerimeter();
671  m_Roundness = src->GetRoundness();
672  m_EquivalentSphericalRadius = src->GetEquivalentSphericalRadius();
673  m_EquivalentSphericalPerimeter = src->GetEquivalentSphericalPerimeter();
674  m_EquivalentEllipsoidDiameter = src->GetEquivalentEllipsoidDiameter();
675  m_Flatness = src->GetFlatness();
676  m_PerimeterOnBorderRatio = src->GetPerimeterOnBorderRatio();
677  m_OrientedBoundingBoxOrigin = src->GetOrientedBoundingBoxOrigin();
678  m_OrientedBoundingBoxSize = src->GetOrientedBoundingBoxSize();
679  }
680 
681  template< typename TSourceLabelObject >
682  void CopyAllFrom(const TSourceLabelObject *src)
683  {
684  itkAssertOrThrowMacro ( ( src != ITK_NULLPTR ), "Null Pointer" );
685  this->template CopyLinesFrom<TSourceLabelObject>( src );
686  this->template CopyAttributesFrom<TSourceLabelObject>( src );
687  }
688 
689 protected:
691  {
692  m_NumberOfPixels = 0;
693  m_PhysicalSize = 0;
694  m_Centroid.Fill(0);
697  m_FeretDiameter = 0;
700  m_Elongation = 0;
701  m_Perimeter = 0;
702  m_Roundness = 0;
706  m_Flatness = 0;
710  }
711 
712  void PrintSelf(std::ostream & os, Indent indent) const ITK_OVERRIDE
713  {
714  Superclass::PrintSelf(os, indent);
715 
716  os << indent << "NumberOfPixels: " << m_NumberOfPixels << std::endl;
717  os << indent << "PhysicalSize: " << m_PhysicalSize << std::endl;
718  os << indent << "Perimeter: " << m_Perimeter << std::endl;
719  os << indent << "NumberOfPixelsOnBorder: " << m_NumberOfPixelsOnBorder << std::endl;
720  os << indent << "PerimeterOnBorder: " << m_PerimeterOnBorder << std::endl;
721  os << indent << "PerimeterOnBorderRatio: " << m_PerimeterOnBorderRatio << std::endl;
722  os << indent << "Elongation: " << m_Elongation << std::endl;
723  os << indent << "Flatness: " << m_Flatness << std::endl;
724  os << indent << "Roundness: " << m_Roundness << std::endl;
725  os << indent << "Centroid: " << m_Centroid << std::endl;
726  os << indent << "BoundingBox: ";
727  m_BoundingBox.Print(os, indent);
728  os << indent << "EquivalentSphericalRadius: " << m_EquivalentSphericalRadius << std::endl;
729  os << indent << "EquivalentSphericalPerimeter: " << m_EquivalentSphericalPerimeter << std::endl;
730  os << indent << "EquivalentEllipsoidDiameter: " << m_EquivalentEllipsoidDiameter << std::endl;
731  os << indent << "PrincipalMoments: " << m_PrincipalMoments << std::endl;
732  os << indent << "PrincipalAxes: " << std::endl << m_PrincipalAxes;
733  os << indent << "FeretDiameter: " << m_FeretDiameter << std::endl;
734  os << indent << "m_OrientedBoundingBoxSize: " << m_OrientedBoundingBoxSize << std::endl;
735  os << indent << "m_OrientedBoundingBoxOrigin: " << m_OrientedBoundingBoxOrigin << std::endl;
736  }
737 
738 private:
739  ITK_DISALLOW_COPY_AND_ASSIGN(ShapeLabelObject);
740 
750  double m_Elongation;
751  double m_Perimeter;
752  double m_Roundness;
756  double m_Flatness;
758 
761 };
762 } // end namespace itk
763 
764 #endif
void SetPerimeter(const double &v)
Superclass::MatrixType MatrixType
Matrix< double, VImageDimension, VImageDimension > MatrixType
static const AttributeType PRINCIPAL_MOMENTS
Light weight base class for most itk classes.
static const AttributeType FERET_DIAMETER
void SetPhysicalSize(const double &v)
WeakPointer< const Self > ConstWeakPointer
Vector< double, VImageDimension > VectorType
OrientedBoundingBoxVerticesType GetOrientedBoundingBoxVertices() const
Superclass::LengthType LengthType
Superclass::LabelObjectType LabelObjectType
static Pointer New()
Point< double, VImageDimension > CentroidType
void SetOrientedBoundingBoxSize(const OrientedBoundingBoxSizeType &v)
Point< double, VImageDimension > OrientedBoundingBoxPointType
void SetEquivalentEllipsoidDiameter(const VectorType &v)
static const AttributeType BOUNDING_BOX
const double & GetRoundness() const
void SetElongation(const double &v)
void SetCentroid(const CentroidType &centroid)
void SetEquivalentSphericalRadius(const double &v)
void SetBoundingBox(const RegionType &v)
static const unsigned int ImageDimension
void SetFlatness(const double &v)
An image region represents a structured region of data.
static const AttributeType PHYSICAL_SIZE
static AttributeType GetAttributeFromName(const std::string &s)
virtual void Print(std::ostream &os, Indent indent=0) const
LabelMap< Self > LabelMapType
static const AttributeType NUMBER_OF_PIXELS_ON_BORDER
void SetEquivalentSphericalPerimeter(const double &v)
static const AttributeType FLATNESS
SizeValueType m_NumberOfPixelsOnBorder
const OrientedBoundingBoxPointType & GetOrientedBoundingBoxOrigin() const
static const AttributeType PERIMETER_ON_BORDER_RATIO
Implements a weak reference to an object.
static const AttributeType CENTROID
const OrientedBoundingBoxDirectionType & GetOrientedBoundingBoxDirection() const
const double & GetFlatness() const
LabelObjectLine< VImageDimension > LineType
void Fill(const ValueType &)
static const AttributeType NUMBER_OF_PIXELS
static const AttributeType EQUIVALENT_SPHERICAL_RADIUS
Simulate a standard C array with copy semnatics.
Definition: itkFixedArray.h:50
unsigned int AttributeType
static const AttributeType PRINCIPAL_AXES
static const unsigned int Length
Definition: itkFixedArray.h:54
Index< VImageDimension > IndexType
static std::string GetNameFromAttribute(const AttributeType &a)
static const AttributeType ORIENTED_BOUNDING_BOX_SIZE
void PrintSelf(std::ostream &os, Indent indent) const override
void SetNumberOfPixels(const SizeValueType &v)
Superclass::IndexType IndexType
void SetNumberOfPixelsOnBorder(const SizeValueType &v)
const OrientedBoundingBoxSizeType & GetOrientedBoundingBoxSize() const
const double & GetPhysicalSize() const
void CopyAttributesFrom(const TSourceLabelObject *src)
LineType::LengthType LengthType
const CentroidType & GetCentroid() const
ImageRegion< VImageDimension > RegionType
LabelObject< TLabel, VImageDimension > Superclass
void SetRoundness(const double &v)
static const AttributeType EQUIVALENT_ELLIPSOID_DIAMETER
const VectorType & GetPrincipalMoments() const
const RegionType & GetRegion() const
Superclass::AttributeType AttributeType
Superclass::LineType LineType
AffineTransformPointer GetPhysicalAxesToPrincipalAxesTransform(void) const
virtual void PrintSelf(std::ostream &os, Indent indent) const override
static AttributeType GetAttributeFromName(const std::string &s)
const double & GetEquivalentSphericalPerimeter() const
Vector< double, VImageDimension > OrientedBoundingBoxSizeType
const MatrixType & GetPrincipalAxes() const
A Label object to store the common attributes related to the shape of the object. ...
const double & GetFeretDiameter() const
OrientedBoundingBoxPointType m_OrientedBoundingBoxOrigin
const SizeValueType & GetNumberOfPixels() const
static const AttributeType PERIMETER
static const AttributeType ORIENTED_BOUNDING_BOX_ORIGIN
AffineTransformType::Pointer AffineTransformPointer
void SetOrientedBoundingBoxOrigin(const OrientedBoundingBoxPointType &v)
void SetPerimeterOnBorderRatio(const double &v)
const double & GetPerimeter() const
const double & GetEquivalentSphericalRadius() const
const VectorType & GetEquivalentEllipsoidDiameter() const
const double & GetPerimeterOnBorder() const
Superclass::OffsetType OffsetType
itk::SizeValueType SizeValueType
static const AttributeType PERIMETER_ON_BORDER
const double & GetElongation() const
The base class for the representation of an labeled binary object in an image.
static const AttributeType ROUNDNESS
const RegionType & GetBoundingBox() const
SmartPointer< const Self > ConstPointer
Control indentation during Print() invocation.
Definition: itkIndent.h:49
void Fill(const T &value)
Definition: itkMatrix.h:191
VectorType m_EquivalentEllipsoidDiameter
void SetPrincipalMoments(const VectorType &v)
FixedArray< OrientedBoundingBoxPointType, IntegerPow< 2, ImageDimension >::Result > OrientedBoundingBoxVerticesType
Templated n-dimensional image to store labeled objects.
Definition: itkLabelMap.h:70
void SetFeretDiameter(const double &v)
void SetPerimeterOnBorder(const double &v)
const SizeValueType & GetNumberOfPixelsOnBorder() const
void SetPrincipalAxes(const MatrixType &v)
const double & GetPerimeterOnBorderRatio() const
AffineTransform< double, VImageDimension > AffineTransformType
OrientedBoundingBoxSizeType m_OrientedBoundingBoxSize
static std::string GetNameFromAttribute(const AttributeType &a)
static const AttributeType EQUIVALENT_SPHERICAL_PERIMETER
void CopyAllFrom(const TSourceLabelObject *src)
MatrixType OrientedBoundingBoxDirectionType
AffineTransformPointer GetPrincipalAxesToPhysicalAxesTransform() const
SmartPointer< Self > Pointer
static const AttributeType ELONGATION