ITK  5.1.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:
45  ITK_DISALLOW_COPY_AND_ASSIGN(ShapeLabelObject);
46 
54 
56  itkNewMacro(Self);
57 
59  itkTypeMacro(ShapeLabelObject, LabelObject);
60 
62 
63  static constexpr unsigned int ImageDimension = VImageDimension;
64 
65  using IndexType = typename Superclass::IndexType;
66 
67  using LabelType = TLabel;
68 
69  using LineType = typename Superclass::LineType;
70 
72 
74 
76  static constexpr AttributeType NUMBER_OF_PIXELS = 100;
77 
81  static constexpr AttributeType PHYSICAL_SIZE = 101;
82 
86  static constexpr AttributeType CENTROID = 104;
87 
88  static constexpr AttributeType BOUNDING_BOX = 105;
89 
96  static constexpr AttributeType NUMBER_OF_PIXELS_ON_BORDER = 106;
97 
105  static constexpr AttributeType PERIMETER_ON_BORDER = 107;
106 
110  static constexpr AttributeType FERET_DIAMETER = 108;
111 
113  static constexpr AttributeType PRINCIPAL_MOMENTS = 109;
114 
116  static constexpr AttributeType PRINCIPAL_AXES = 110;
117 
121  static constexpr AttributeType ELONGATION = 111;
122 
124  static constexpr AttributeType PERIMETER = 112;
125 
126  static constexpr AttributeType ROUNDNESS = 113;
127 
132 
137 
142 
143  static constexpr AttributeType FLATNESS = 117;
144 
145  static constexpr AttributeType PERIMETER_ON_BORDER_RATIO = 118;
146 
147 
151 
152 
161 
162  static AttributeType GetAttributeFromName(const std::string & s)
163  {
164  if ( s == "NumberOfPixels" )
165  {
166  return NUMBER_OF_PIXELS;
167  }
168  else if ( s == "PhysicalSize" )
169  {
170  return PHYSICAL_SIZE;
171  }
172  else if ( s == "Centroid" )
173  {
174  return CENTROID;
175  }
176  else if ( s == "BoundingBox" )
177  {
178  return BOUNDING_BOX;
179  }
180  else if ( s == "NumberOfPixelsOnBorder" )
181  {
183  }
184  else if ( s == "PerimeterOnBorder" )
185  {
186  return PERIMETER_ON_BORDER;
187  }
188  else if ( s == "FeretDiameter" )
189  {
190  return FERET_DIAMETER;
191  }
192  else if ( s == "PrincipalMoments" )
193  {
194  return PRINCIPAL_MOMENTS;
195  }
196  else if ( s == "PrincipalAxes" )
197  {
198  return PRINCIPAL_AXES;
199  }
200  else if ( s == "Elongation" )
201  {
202  return ELONGATION;
203  }
204  else if ( s == "Perimeter" )
205  {
206  return PERIMETER;
207  }
208  else if ( s == "Roundness" )
209  {
210  return ROUNDNESS;
211  }
212  else if ( s == "EquivalentSphericalRadius" )
213  {
215  }
216  else if ( s == "EquivalentSphericalPerimeter" )
217  {
219  }
220  else if ( s == "EquivalentEllipsoidDiameter" )
221  {
223  }
224  else if ( s == "Flatness" )
225  {
226  return FLATNESS;
227  }
228  else if ( s == "PerimeterOnBorderRatio" )
229  {
231  }
232  else if ( s == "OrientedBoundingBoxOrigin")
233  {
235  }
236  else if ( s == "OrientedBoundingBoxSize")
237  {
239  }
240  // can't recognize the name
242  }
243 
244  static std::string GetNameFromAttribute(const AttributeType & a)
245  {
246  std::string name;
247  switch ( a )
248  {
249  case NUMBER_OF_PIXELS:
250  name = "NumberOfPixels";
251  break;
252  case PHYSICAL_SIZE:
253  name = "PhysicalSize";
254  break;
255  case CENTROID:
256  name = "Centroid";
257  break;
258  case BOUNDING_BOX:
259  name = "BoundingBox";
260  break;
262  name = "NumberOfPixelsOnBorder";
263  break;
264  case PERIMETER_ON_BORDER:
265  name = "PerimeterOnBorder";
266  break;
267  case FERET_DIAMETER:
268  name = "FeretDiameter";
269  break;
270  case PRINCIPAL_MOMENTS:
271  name = "PrincipalMoments";
272  break;
273  case PRINCIPAL_AXES:
274  name = "PrincipalAxes";
275  break;
276  case ELONGATION:
277  name = "Elongation";
278  break;
279  case PERIMETER:
280  name = "Perimeter";
281  break;
282  case ROUNDNESS:
283  name = "Roundness";
284  break;
286  name = "EquivalentSphericalRadius";
287  break;
289  name = "EquivalentSphericalPerimeter";
290  break;
292  name = "EquivalentEllipsoidDiameter";
293  break;
294  case FLATNESS:
295  name = "Flatness";
296  break;
298  name = "PerimeterOnBorderRatio";
299  break;
301  name = "OrientedBoundingBoxOrigin";
302  break;
304  name = "OrientedBoundingBoxSize";
305  break;
306  default:
307  // can't recognize the name
309  break;
310  }
311  return name;
312  }
313 
315 
317 
319 
321 
322 
323 private:
324 
325  template <size_t VX, unsigned short VY>
326  struct IntegerPow
327  {
328  static const size_t Result = VX*IntegerPow<VX, VY-1>::Result;
329  };
330 
331  template <size_t VX>
332  struct IntegerPow<VX,0>
333  {
334  static constexpr size_t Result = 1;
335  };
336 
337 public:
338 
340 
342 
344 
346 
347 
348  const RegionType & GetBoundingBox() const
349  {
350  return m_BoundingBox;
351  }
352 
353  void SetBoundingBox(const RegionType & v)
354  {
355  m_BoundingBox = v;
356  }
357 
358  const double & GetPhysicalSize() const
359  {
360  return m_PhysicalSize;
361  }
362 
363  void SetPhysicalSize(const double & v)
364  {
365  m_PhysicalSize = v;
366  }
367 
369  {
370  return m_NumberOfPixels;
371  }
372 
374  {
375  m_NumberOfPixels = v;
376  }
377 
378  const CentroidType & GetCentroid() const
379  {
380  return m_Centroid;
381  }
382 
383  void SetCentroid(const CentroidType & centroid)
384  {
385  m_Centroid = centroid;
386  }
387 
389  {
391  }
392 
394  {
396  }
397 
398  const double & GetPerimeterOnBorder() const
399  {
400  return m_PerimeterOnBorder;
401  }
402 
403  void SetPerimeterOnBorder(const double & v)
404  {
406  }
407 
408  const double & GetFeretDiameter() const
409  {
410  return m_FeretDiameter;
411  }
412 
413  void SetFeretDiameter(const double & v)
414  {
415  m_FeretDiameter = v;
416  }
417 
419  {
420  return m_PrincipalMoments;
421  }
422 
424  {
425  m_PrincipalMoments = v;
426  }
427 
428  const MatrixType & GetPrincipalAxes() const
429  {
430  return m_PrincipalAxes;
431  }
432 
433  void SetPrincipalAxes(const MatrixType & v)
434  {
435  m_PrincipalAxes = v;
436  }
437 
438  const double & GetElongation() const
439  {
440  return m_Elongation;
441  }
442 
443  void SetElongation(const double & v)
444  {
445  m_Elongation = v;
446  }
447 
448  const double & GetPerimeter() const
449  {
450  return m_Perimeter;
451  }
452 
453  void SetPerimeter(const double & v)
454  {
455  m_Perimeter = v;
456  }
457 
458  const double & GetRoundness() const
459  {
460  return m_Roundness;
461  }
462 
463  void SetRoundness(const double & v)
464  {
465  m_Roundness = v;
466  }
467 
468  const double & GetEquivalentSphericalRadius() const
469  {
471  }
472 
473  void SetEquivalentSphericalRadius(const double & v)
474  {
476  }
477 
478  const double & GetEquivalentSphericalPerimeter() const
479  {
481  }
482 
483  void SetEquivalentSphericalPerimeter(const double & v)
484  {
486  }
487 
489  {
491  }
492 
494  {
496  }
497 
498  const double & GetFlatness() const
499  {
500  return m_Flatness;
501  }
502 
503  void SetFlatness(const double & v)
504  {
505  m_Flatness = v;
506  }
507 
508  const double & GetPerimeterOnBorderRatio() const
509  {
511  }
512 
513  void SetPerimeterOnBorderRatio(const double & v)
514  {
516  }
517 
519  {
521  }
522 
524  {
526  }
527 
529  {
531  }
532 
534  {
536  }
537 
538 
539  // some helper methods - not really required, but really useful!
540 
542  const RegionType & GetRegion() const
543  {
544  return m_BoundingBox;
545  }
546 
547 
551  {
552  return this->GetPrincipalAxes();
553  }
554 
566  {
567 
570 
571  for( unsigned int i = 0; i < ImageDimension; ++i)
572  {
573  max[i] += m_OrientedBoundingBoxSize[i];
574  }
575 
577 
578  // Use binary index to map the vertices of the OBB to an array. For
579  // example, in 2D, binary counting will give[0,0], [0,1], [1,0],
580  // [1,1], which corresponds to [minX,minY], [minX,maxY],
581  // [maxX,minY], [maxX,maxY].
582  for (unsigned int i = 0; i < OrientedBoundingBoxVerticesType::Length; ++i)
583  {
584  const unsigned int msb = 1 << (ImageDimension-1);
585  for ( unsigned int j = 0; j < ImageDimension; j++ )
586  {
587  if (i & msb>>j)
588  {
589  vertices[i][j] = max[j];
590  }
591  else
592  {
593  vertices[i][j] = min[j];
594  }
595  }
596  }
597  return vertices;
598  }
599 
603 
608  {
609  typename AffineTransformType::MatrixType matrix;
610  typename AffineTransformType::OffsetType offset;
611  for ( unsigned int i = 0; i < VImageDimension; i++ )
612  {
613  offset[i] = m_Centroid[i];
614  for ( unsigned int j = 0; j < VImageDimension; j++ )
615  {
616  matrix[j][i] = m_PrincipalAxes[i][j]; // Note the transposition
617  }
618  }
620 
622 
623  result->SetMatrix(matrix);
624  result->SetOffset(offset);
625 
626  return result;
627  }
628 
634  {
635  typename AffineTransformType::MatrixType matrix;
636  typename AffineTransformType::OffsetType offset;
637  for ( unsigned int i = 0; i < VImageDimension; i++ )
638  {
639  offset[i] = m_Centroid[i];
640  for ( unsigned int j = 0; j < VImageDimension; j++ )
641  {
642  matrix[j][i] = m_PrincipalAxes[i][j]; // Note the transposition
643  }
644  }
646 
648  result->SetMatrix(matrix);
649  result->SetOffset(offset);
650 
652  result->GetInverse(inverse);
653 
654  return inverse;
655  }
656 
657  template< typename TSourceLabelObject >
658  void CopyAttributesFrom( const TSourceLabelObject * src )
659  {
660  Superclass::template CopyAttributesFrom<TSourceLabelObject>(src);
661 
662  m_BoundingBox = src->GetBoundingBox();
663  m_NumberOfPixels = src->GetNumberOfPixels();
664  m_PhysicalSize = src->GetPhysicalSize();
665  m_Centroid = src->GetCentroid();
666  m_NumberOfPixelsOnBorder = src->GetNumberOfPixelsOnBorder();
667  m_PerimeterOnBorder = src->GetPerimeterOnBorder();
668  m_FeretDiameter = src->GetFeretDiameter();
669  m_PrincipalMoments = src->GetPrincipalMoments();
670  m_PrincipalAxes = src->GetPrincipalAxes();
671  m_Elongation = src->GetElongation();
672  m_Perimeter = src->GetPerimeter();
673  m_Roundness = src->GetRoundness();
674  m_EquivalentSphericalRadius = src->GetEquivalentSphericalRadius();
675  m_EquivalentSphericalPerimeter = src->GetEquivalentSphericalPerimeter();
676  m_EquivalentEllipsoidDiameter = src->GetEquivalentEllipsoidDiameter();
677  m_Flatness = src->GetFlatness();
678  m_PerimeterOnBorderRatio = src->GetPerimeterOnBorderRatio();
679  m_OrientedBoundingBoxOrigin = src->GetOrientedBoundingBoxOrigin();
680  m_OrientedBoundingBoxSize = src->GetOrientedBoundingBoxSize();
681  }
682 
683  template< typename TSourceLabelObject >
684  void CopyAllFrom(const TSourceLabelObject *src)
685  {
686  itkAssertOrThrowMacro ( ( src != nullptr ), "Null Pointer" );
687  this->template CopyLinesFrom<TSourceLabelObject>( src );
688  this->template CopyAttributesFrom<TSourceLabelObject>( src );
689  }
690 
691 protected:
693  {
694  m_NumberOfPixels = 0;
695  m_PhysicalSize = 0;
696  m_Centroid.Fill(0);
699  m_FeretDiameter = 0;
702  m_Elongation = 0;
703  m_Perimeter = 0;
704  m_Roundness = 0;
708  m_Flatness = 0;
712  }
713 
714  void PrintSelf(std::ostream & os, Indent indent) const override
715  {
716  Superclass::PrintSelf(os, indent);
717 
718  os << indent << "NumberOfPixels: " << m_NumberOfPixels << std::endl;
719  os << indent << "PhysicalSize: " << m_PhysicalSize << std::endl;
720  os << indent << "Perimeter: " << m_Perimeter << std::endl;
721  os << indent << "NumberOfPixelsOnBorder: " << m_NumberOfPixelsOnBorder << std::endl;
722  os << indent << "PerimeterOnBorder: " << m_PerimeterOnBorder << std::endl;
723  os << indent << "PerimeterOnBorderRatio: " << m_PerimeterOnBorderRatio << std::endl;
724  os << indent << "Elongation: " << m_Elongation << std::endl;
725  os << indent << "Flatness: " << m_Flatness << std::endl;
726  os << indent << "Roundness: " << m_Roundness << std::endl;
727  os << indent << "Centroid: " << m_Centroid << std::endl;
728  os << indent << "BoundingBox: ";
729  m_BoundingBox.Print(os, indent);
730  os << indent << "EquivalentSphericalRadius: " << m_EquivalentSphericalRadius << std::endl;
731  os << indent << "EquivalentSphericalPerimeter: " << m_EquivalentSphericalPerimeter << std::endl;
732  os << indent << "EquivalentEllipsoidDiameter: " << m_EquivalentEllipsoidDiameter << std::endl;
733  os << indent << "PrincipalMoments: " << m_PrincipalMoments << std::endl;
734  os << indent << "PrincipalAxes: " << std::endl << m_PrincipalAxes;
735  os << indent << "FeretDiameter: " << m_FeretDiameter << std::endl;
736  os << indent << "m_OrientedBoundingBoxSize: " << m_OrientedBoundingBoxSize << std::endl;
737  os << indent << "m_OrientedBoundingBoxOrigin: " << m_OrientedBoundingBoxOrigin << std::endl;
738  }
739 
740 private:
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
const VectorType & GetPrincipalMoments() const
const RegionType & GetBoundingBox() const
void SetPerimeter(const double &v)
static constexpr AttributeType FERET_DIAMETER
Light weight base class for most itk classes.
void SetPhysicalSize(const double &v)
static Pointer New()
void SetOrientedBoundingBoxSize(const OrientedBoundingBoxSizeType &v)
void SetEquivalentEllipsoidDiameter(const VectorType &v)
const double & GetFeretDiameter() const
LabelObjectLine< VImageDimension > LineType
unsigned int AttributeType
void SetElongation(const double &v)
void SetCentroid(const CentroidType &centroid)
const VectorType & GetEquivalentEllipsoidDiameter() const
void SetEquivalentSphericalRadius(const double &v)
const OrientedBoundingBoxSizeType & GetOrientedBoundingBoxSize() const
const double & GetEquivalentSphericalRadius() const
static constexpr AttributeType PERIMETER
Matrix< double, VImageDimension, VImageDimension > MatrixType
void SetBoundingBox(const RegionType &v)
Index< VImageDimension > IndexType
void SetFlatness(const double &v)
An image region represents a structured region of data.
const double & GetPhysicalSize() const
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkArray.h:26
static AttributeType GetAttributeFromName(const std::string &s)
void SetEquivalentSphericalPerimeter(const double &v)
SizeValueType m_NumberOfPixelsOnBorder
const SizeValueType & GetNumberOfPixelsOnBorder() const
static constexpr AttributeType PERIMETER_ON_BORDER
Implements a weak reference to an object.
static constexpr AttributeType ELONGATION
const double & GetFlatness() const
void PrintSelf(std::ostream &os, Indent indent) const override
void Fill(const ValueType &)
static constexpr AttributeType ORIENTED_BOUNDING_BOX_ORIGIN
const MatrixType & GetPrincipalAxes() const
typename Superclass::OffsetType OffsetType
static constexpr AttributeType PHYSICAL_SIZE
Simulate a standard C array with copy semantics.
Definition: itkFixedArray.h:52
static constexpr AttributeType EQUIVALENT_ELLIPSOID_DIAMETER
static constexpr AttributeType ORIENTED_BOUNDING_BOX_SIZE
AffineTransformPointer GetPrincipalAxesToPhysicalAxesTransform() const
static constexpr unsigned int ImageDimension
SmartPointer< Self > Pointer
itk::SizeValueType SizeValueType
static constexpr AttributeType PRINCIPAL_AXES
static std::string GetNameFromAttribute(const AttributeType &a)
const OrientedBoundingBoxDirectionType & GetOrientedBoundingBoxDirection() const
const double & GetPerimeterOnBorder() const
void PrintSelf(std::ostream &os, Indent indent) const override
static constexpr AttributeType NUMBER_OF_PIXELS_ON_BORDER
void SetNumberOfPixels(const SizeValueType &v)
void SetNumberOfPixelsOnBorder(const SizeValueType &v)
static constexpr AttributeType BOUNDING_BOX
void CopyAttributesFrom(const TSourceLabelObject *src)
const double & GetPerimeter() const
typename Superclass::MatrixType MatrixType
const SizeValueType & GetNumberOfPixels() const
static constexpr AttributeType NUMBER_OF_PIXELS
void SetRoundness(const double &v)
static AttributeType GetAttributeFromName(const std::string &s)
const OrientedBoundingBoxPointType & GetOrientedBoundingBoxOrigin() const
A Label object to store the common attributes related to the shape of the object.
const CentroidType & GetCentroid() const
OrientedBoundingBoxPointType m_OrientedBoundingBoxOrigin
AffineTransformPointer GetPhysicalAxesToPrincipalAxesTransform() const
static constexpr unsigned int Length
Definition: itkFixedArray.h:56
void SetOrientedBoundingBoxOrigin(const OrientedBoundingBoxPointType &v)
typename LineType::LengthType LengthType
OrientedBoundingBoxVerticesType GetOrientedBoundingBoxVertices() const
void SetPerimeterOnBorderRatio(const double &v)
static constexpr AttributeType PERIMETER_ON_BORDER_RATIO
static constexpr AttributeType PRINCIPAL_MOMENTS
The base class for the representation of an labeled binary object in an image.
const double & GetElongation() const
static constexpr AttributeType EQUIVALENT_SPHERICAL_PERIMETER
typename AffineTransformType::Pointer AffineTransformPointer
Control indentation during Print() invocation.
Definition: itkIndent.h:49
void Fill(const T &value)
Definition: itkMatrix.h:187
const double & GetEquivalentSphericalPerimeter() const
VectorType m_EquivalentEllipsoidDiameter
void SetPrincipalMoments(const VectorType &v)
Templated n-dimensional image to store labeled objects.
Definition: itkLabelMap.h:70
void SetFeretDiameter(const double &v)
void SetPerimeterOnBorder(const double &v)
static constexpr AttributeType CENTROID
const double & GetRoundness() const
void SetPrincipalAxes(const MatrixType &v)
OrientedBoundingBoxSizeType m_OrientedBoundingBoxSize
static std::string GetNameFromAttribute(const AttributeType &a)
virtual void Print(std::ostream &os, Indent indent=0) const
static constexpr AttributeType FLATNESS
typename Superclass::AttributeType AttributeType
static constexpr AttributeType EQUIVALENT_SPHERICAL_RADIUS
void CopyAllFrom(const TSourceLabelObject *src)
static constexpr AttributeType ROUNDNESS
const RegionType & GetRegion() const
const double & GetPerimeterOnBorderRatio() const