ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
itkShapeLabelObject.h
Go to the documentation of this file.
1/*=========================================================================
2 *
3 * Copyright NumFOCUS
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 * https://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 "itkMath.h"
24#include "itkAffineTransform.h"
25#include "itkPrintHelper.h"
26
27namespace itk
28{
43template <typename TLabel, unsigned int VImageDimension>
44class ITK_TEMPLATE_EXPORT ShapeLabelObject : public LabelObject<TLabel, VImageDimension>
45{
46public:
47 ITK_DISALLOW_COPY_AND_MOVE(ShapeLabelObject);
48
56
58 itkNewMacro(Self);
59
61 itkOverrideGetNameOfClassMacro(ShapeLabelObject);
62
64
65 static constexpr unsigned int ImageDimension = VImageDimension;
66
67 using typename Superclass::IndexType;
68
69 using LabelType = TLabel;
70
71 using typename Superclass::LineType;
72
73 using typename Superclass::LengthType;
74
75 using typename Superclass::AttributeType;
76
78 static constexpr AttributeType NUMBER_OF_PIXELS = 100;
79
83 static constexpr AttributeType PHYSICAL_SIZE = 101;
84
88 static constexpr AttributeType CENTROID = 104;
89
90 static constexpr AttributeType BOUNDING_BOX = 105;
91
99
107 static constexpr AttributeType PERIMETER_ON_BORDER = 107;
108
112 static constexpr AttributeType FERET_DIAMETER = 108;
113
115 static constexpr AttributeType PRINCIPAL_MOMENTS = 109;
116
118 static constexpr AttributeType PRINCIPAL_AXES = 110;
119
123 static constexpr AttributeType ELONGATION = 111;
124
126 static constexpr AttributeType PERIMETER = 112;
127
128 static constexpr AttributeType ROUNDNESS = 113;
129
134
139
144
145 static constexpr AttributeType FLATNESS = 117;
146
148
149
153
154
163
164 static AttributeType
165 GetAttributeFromName(const std::string & s)
166 {
167 if (s == "NumberOfPixels")
168 {
169 return NUMBER_OF_PIXELS;
170 }
171 if (s == "PhysicalSize")
172 {
173 return PHYSICAL_SIZE;
174 }
175 else if (s == "Centroid")
176 {
177 return CENTROID;
178 }
179 else if (s == "BoundingBox")
180 {
181 return BOUNDING_BOX;
182 }
183 else if (s == "NumberOfPixelsOnBorder")
184 {
186 }
187 else if (s == "PerimeterOnBorder")
188 {
189 return PERIMETER_ON_BORDER;
190 }
191 else if (s == "FeretDiameter")
192 {
193 return FERET_DIAMETER;
194 }
195 else if (s == "PrincipalMoments")
196 {
197 return PRINCIPAL_MOMENTS;
198 }
199 else if (s == "PrincipalAxes")
200 {
201 return PRINCIPAL_AXES;
202 }
203 else if (s == "Elongation")
204 {
205 return ELONGATION;
206 }
207 else if (s == "Perimeter")
208 {
209 return PERIMETER;
210 }
211 else if (s == "Roundness")
212 {
213 return ROUNDNESS;
214 }
215 else if (s == "EquivalentSphericalRadius")
216 {
218 }
219 else if (s == "EquivalentSphericalPerimeter")
220 {
222 }
223 else if (s == "EquivalentEllipsoidDiameter")
224 {
226 }
227 else if (s == "Flatness")
228 {
229 return FLATNESS;
230 }
231 else if (s == "PerimeterOnBorderRatio")
232 {
233 return PERIMETER_ON_BORDER_RATIO;
234 }
235 else if (s == "OrientedBoundingBoxOrigin")
236 {
237 return ORIENTED_BOUNDING_BOX_ORIGIN;
238 }
239 else if (s == "OrientedBoundingBoxSize")
240 {
241 return ORIENTED_BOUNDING_BOX_SIZE;
242 }
243 // can't recognize the name
244 return Superclass::GetAttributeFromName(s);
245 }
246
247 static std::string
249 {
250 std::string name;
251 switch (a)
252 {
253 case NUMBER_OF_PIXELS:
254 name = "NumberOfPixels";
255 break;
256 case PHYSICAL_SIZE:
257 name = "PhysicalSize";
258 break;
259 case CENTROID:
260 name = "Centroid";
261 break;
262 case BOUNDING_BOX:
263 name = "BoundingBox";
264 break;
266 name = "NumberOfPixelsOnBorder";
267 break;
269 name = "PerimeterOnBorder";
270 break;
271 case FERET_DIAMETER:
272 name = "FeretDiameter";
273 break;
275 name = "PrincipalMoments";
276 break;
277 case PRINCIPAL_AXES:
278 name = "PrincipalAxes";
279 break;
280 case ELONGATION:
281 name = "Elongation";
282 break;
283 case PERIMETER:
284 name = "Perimeter";
285 break;
286 case ROUNDNESS:
287 name = "Roundness";
288 break;
290 name = "EquivalentSphericalRadius";
291 break;
293 name = "EquivalentSphericalPerimeter";
294 break;
296 name = "EquivalentEllipsoidDiameter";
297 break;
298 case FLATNESS:
299 name = "Flatness";
300 break;
302 name = "PerimeterOnBorderRatio";
303 break;
305 name = "OrientedBoundingBoxOrigin";
306 break;
308 name = "OrientedBoundingBoxSize";
309 break;
310 default:
311 // can't recognize the name
313 break;
314 }
315 return name;
316 }
317
319
321
323
325
326public:
328
330
332
335
336
337 const RegionType &
339 {
340 return m_BoundingBox;
341 }
342
343 void
345 {
346 m_BoundingBox = v;
347 }
348
349 [[nodiscard]] const double &
351 {
352 return m_PhysicalSize;
353 }
354
355 void
356 SetPhysicalSize(const double v)
357 {
358 m_PhysicalSize = v;
359 }
360
361 [[nodiscard]] const SizeValueType &
363 {
364 return m_NumberOfPixels;
365 }
366
367 void
369 {
371 }
372
373 const CentroidType &
375 {
376 return m_Centroid;
377 }
378
379 void
380 SetCentroid(const CentroidType & centroid)
381 {
382 m_Centroid = centroid;
383 }
384
385 [[nodiscard]] const SizeValueType &
390
391 void
396
397 [[nodiscard]] const double &
399 {
400 return m_PerimeterOnBorder;
401 }
402
403 void
404 SetPerimeterOnBorder(const double v)
405 {
407 }
408
409 [[nodiscard]] const double &
411 {
412 return m_FeretDiameter;
413 }
414
415 void
416 SetFeretDiameter(const double v)
417 {
418 m_FeretDiameter = v;
419 }
420
421 const VectorType &
423 {
424 return m_PrincipalMoments;
425 }
426
427 void
429 {
431 }
432
433 const MatrixType &
435 {
436 return m_PrincipalAxes;
437 }
438
439 void
441 {
442 m_PrincipalAxes = v;
443 }
444
445 [[nodiscard]] const double &
447 {
448 return m_Elongation;
449 }
450
451 void
452 SetElongation(const double v)
453 {
454 m_Elongation = v;
455 }
456
457 [[nodiscard]] const double &
459 {
460 return m_Perimeter;
461 }
462
463 void
464 SetPerimeter(const double v)
465 {
466 m_Perimeter = v;
467 }
468
469 [[nodiscard]] const double &
471 {
472 return m_Roundness;
473 }
474
475 void
476 SetRoundness(const double v)
477 {
478 m_Roundness = v;
479 }
480
481 [[nodiscard]] const double &
486
487 void
489 {
491 }
492
493 [[nodiscard]] const double &
498
499 void
504
505 const VectorType &
510
511 void
516
517 [[nodiscard]] const double &
519 {
520 return m_Flatness;
521 }
522
523 void
524 SetFlatness(const double v)
525 {
526 m_Flatness = v;
527 }
528
529 [[nodiscard]] const double &
534
535 void
537 {
539 }
540
541 const OrientedBoundingBoxPointType &
546
547 void
552
553 const OrientedBoundingBoxSizeType &
558
559 void
564
565
566 // some helper methods - not really required, but really useful!
567
569 const RegionType &
570 GetRegion() const
571 {
572 return m_BoundingBox;
573 }
574
575
578 const OrientedBoundingBoxDirectionType &
580 {
581 return this->GetPrincipalAxes();
582 }
583
594 OrientedBoundingBoxVerticesType
596 {
597 const MatrixType obbToPhysical(this->GetOrientedBoundingBoxDirection().GetTranspose());
598
599
601
602 // Use binary index to map the vertices of the OBB to an array. For
603 // example, in 2D, binary counting will give[0,0], [0,1], [1,0],
604 // [1,1], which corresponds to [minX,minY], [minX,maxY],
605 // [maxX,minY], [maxX,maxY].
606 for (unsigned int i = 0; i < OrientedBoundingBoxVerticesType::Length; ++i)
607 {
608 constexpr unsigned int msb = 1 << (ImageDimension - 1);
610 for (unsigned int j = 0; j < ImageDimension; ++j)
611 {
612 if (i & msb >> j)
613 {
614 offset[j] = m_OrientedBoundingBoxSize[j];
615 }
616 else
617 {
618 offset[j] = 0;
619 }
620 }
621 vertices[i] = m_OrientedBoundingBoxOrigin + obbToPhysical * offset;
622 }
623 return vertices;
624 }
625
629
635 {
636 typename AffineTransformType::MatrixType matrix;
637 typename AffineTransformType::OffsetType offset;
638 for (unsigned int i = 0; i < VImageDimension; ++i)
639 {
640 offset[i] = m_Centroid[i];
641 for (unsigned int j = 0; j < VImageDimension; ++j)
642 {
643 matrix[j][i] = m_PrincipalAxes[i][j]; // Note the transposition
644 }
645 }
646
648
649 result->SetMatrix(matrix);
650 result->SetOffset(offset);
651
652 return result;
653 }
654
659 AffineTransformPointer
661 {
662 typename AffineTransformType::MatrixType matrix;
663 typename AffineTransformType::OffsetType offset;
664 for (unsigned int i = 0; i < VImageDimension; ++i)
665 {
666 offset[i] = m_Centroid[i];
667 for (unsigned int j = 0; j < VImageDimension; ++j)
668 {
669 matrix[j][i] = m_PrincipalAxes[i][j]; // Note the transposition
670 }
671 }
672
674 result->SetMatrix(matrix);
675 result->SetOffset(offset);
676
678 result->GetInverse(inverse);
679
680 return inverse;
681 }
682
683 template <typename TSourceLabelObject>
684 void
685 CopyAttributesFrom(const TSourceLabelObject * src)
686 {
687 Superclass::template CopyAttributesFrom<TSourceLabelObject>(src);
688
689 m_BoundingBox = src->GetBoundingBox();
690 m_NumberOfPixels = src->GetNumberOfPixels();
691 m_PhysicalSize = src->GetPhysicalSize();
692 m_Centroid = src->GetCentroid();
693 m_NumberOfPixelsOnBorder = src->GetNumberOfPixelsOnBorder();
694 m_PerimeterOnBorder = src->GetPerimeterOnBorder();
695 m_FeretDiameter = src->GetFeretDiameter();
696 m_PrincipalMoments = src->GetPrincipalMoments();
697 m_PrincipalAxes = src->GetPrincipalAxes();
698 m_Elongation = src->GetElongation();
699 m_Perimeter = src->GetPerimeter();
700 m_Roundness = src->GetRoundness();
701 m_EquivalentSphericalRadius = src->GetEquivalentSphericalRadius();
702 m_EquivalentSphericalPerimeter = src->GetEquivalentSphericalPerimeter();
703 m_EquivalentEllipsoidDiameter = src->GetEquivalentEllipsoidDiameter();
704 m_Flatness = src->GetFlatness();
705 m_PerimeterOnBorderRatio = src->GetPerimeterOnBorderRatio();
706 m_OrientedBoundingBoxOrigin = src->GetOrientedBoundingBoxOrigin();
707 m_OrientedBoundingBoxSize = src->GetOrientedBoundingBoxSize();
708 }
709
710 template <typename TSourceLabelObject>
711 void
712 CopyAllFrom(const TSourceLabelObject * src)
713 {
714 itkAssertOrThrowMacro((src != nullptr), "Null Pointer");
715 this->template CopyLinesFrom<TSourceLabelObject>(src);
716 this->template CopyAttributesFrom<TSourceLabelObject>(src);
717 }
718
719protected:
721
722 {
723 m_Centroid.Fill(0);
724 m_PrincipalMoments.Fill(0);
725 m_PrincipalAxes.Fill(0);
729 }
730
731 void
732 PrintSelf(std::ostream & os, Indent indent) const override
733 {
734 Superclass::PrintSelf(os, indent);
735
736 os << indent << "BoundingBox: " << m_BoundingBox << std::endl;
737 print_helper::PrintNumericTrait(os, indent, "NumberOfPixels", m_NumberOfPixels);
738 os << indent << "PhysicalSize: " << m_PhysicalSize << std::endl;
739 print_helper::PrintNumericTrait(os, indent, "Centroid", m_Centroid);
740 print_helper::PrintNumericTrait(os, indent, "NumberOfPixelsOnBorder", m_NumberOfPixelsOnBorder);
741 os << indent << "PerimeterOnBorder: " << m_PerimeterOnBorder << std::endl;
742 os << indent << "FeretDiameter: " << m_FeretDiameter << std::endl;
743 os << indent << "PrincipalMoments: " << m_PrincipalMoments << std::endl;
744 os << indent << "PrincipalAxes: " << std::endl << m_PrincipalAxes;
745 os << indent << "Elongation: " << m_Elongation << std::endl;
746 os << indent << "Perimeter: " << m_Perimeter << std::endl;
747 os << indent << "Roundness: " << m_Roundness << std::endl;
748 os << indent << "EquivalentSphericalRadius: " << m_EquivalentSphericalRadius << std::endl;
749 os << indent << "EquivalentSphericalPerimeter: " << m_EquivalentSphericalPerimeter << std::endl;
750 os << indent << "EquivalentEllipsoidDiameter: " << m_EquivalentEllipsoidDiameter << std::endl;
751 os << indent << "Flatness: " << m_Flatness << std::endl;
752 os << indent << "PerimeterOnBorderRatio: " << m_PerimeterOnBorderRatio << std::endl;
753 print_helper::PrintNumericTrait(os, indent, "OrientedBoundingBoxSize", m_OrientedBoundingBoxSize);
754 print_helper::PrintNumericTrait(os, indent, "OrientedBoundingBoxOrigin", m_OrientedBoundingBoxOrigin);
755 }
756
757private:
767 double m_Elongation{};
768 double m_Perimeter{};
769 double m_Roundness{};
773 double m_Flatness{};
775
778};
779} // end namespace itk
780
781#endif
Matrix< double, Self::OutputSpaceDimension, Self::InputSpaceDimension > MatrixType
Simulate a standard C array with copy semantics.
An image region represents a structured region of data.
Control indentation during Print() invocation.
Definition itkIndent.h:51
Templated n-dimensional image to store labeled objects.
Definition itkLabelMap.h:70
itk::SizeValueType SizeValueType
LabelObjectLine< VImageDimension > LineType
void PrintSelf(std::ostream &os, Indent indent) const override
Index< VImageDimension > IndexType
unsigned int AttributeType
static std::string GetNameFromAttribute(const AttributeType &a)
void CopyLinesFrom(const TSourceLabelObject *src)
typename LineType::LengthType LengthType
A templated class holding a M x N size Matrix.
Definition itkMatrix.h:53
A templated class holding a geometric point in n-Dimensional space.
Definition itkPoint.h:54
const double & GetEquivalentSphericalPerimeter() const
void SetEquivalentEllipsoidDiameter(const VectorType &v)
Matrix< double, VImageDimension, VImageDimension > MatrixType
void CopyAllFrom(const TSourceLabelObject *src)
const VectorType & GetPrincipalMoments() const
void SetElongation(const double v)
AffineTransform< double, VImageDimension > AffineTransformType
const SizeValueType & GetNumberOfPixelsOnBorder() const
const double & GetPerimeter() const
const MatrixType & GetPrincipalAxes() const
void SetEquivalentSphericalRadius(const double v)
const RegionType & GetRegion() const
OrientedBoundingBoxVerticesType GetOrientedBoundingBoxVertices() const
const OrientedBoundingBoxPointType & GetOrientedBoundingBoxOrigin() const
void SetPrincipalMoments(const VectorType &v)
void SetPerimeterOnBorder(const double v)
const double & GetFlatness() const
void SetNumberOfPixels(const SizeValueType &v)
void SetNumberOfPixelsOnBorder(const SizeValueType &v)
const CentroidType & GetCentroid() const
static AttributeType GetAttributeFromName(const std::string &s)
FixedArray< OrientedBoundingBoxPointType, Math::UnsignedPower< unsigned int >(2, ImageDimension)> OrientedBoundingBoxVerticesType
const RegionType & GetBoundingBox() const
void SetPrincipalAxes(const MatrixType &v)
const double & GetEquivalentSphericalRadius() const
AffineTransformPointer GetPrincipalAxesToPhysicalAxesTransform() const
const double & GetFeretDiameter() const
void SetPerimeter(const double v)
void SetFeretDiameter(const double v)
const OrientedBoundingBoxSizeType & GetOrientedBoundingBoxSize() const
const OrientedBoundingBoxDirectionType & GetOrientedBoundingBoxDirection() const
const double & GetElongation() const
void SetOrientedBoundingBoxSize(const OrientedBoundingBoxSizeType &v)
void CopyAttributesFrom(const TSourceLabelObject *src)
const double & GetPhysicalSize() const
void SetPerimeterOnBorderRatio(const double v)
void SetBoundingBox(const RegionType &v)
LabelObject< LabelType, VImageDimension > Superclass
const double & GetPerimeterOnBorder() const
const SizeValueType & GetNumberOfPixels() const
const VectorType & GetEquivalentEllipsoidDiameter() const
void PrintSelf(std::ostream &os, Indent indent) const override
const double & GetPerimeterOnBorderRatio() const
typename AffineTransformType::Pointer AffineTransformPointer
void SetEquivalentSphericalPerimeter(const double v)
const double & GetRoundness() const
void SetFlatness(const double v)
void SetCentroid(const CentroidType &centroid)
AffineTransformPointer GetPhysicalAxesToPrincipalAxesTransform() const
void SetOrientedBoundingBoxOrigin(const OrientedBoundingBoxPointType &v)
static std::string GetNameFromAttribute(const AttributeType &a)
void SetPhysicalSize(const double v)
void SetRoundness(const double v)
Implements transparent reference counting.
A templated class holding a n-Dimensional vector.
Definition itkVector.h:63
Implements a weak reference to an object.
void PrintNumericTrait(std::ostream &os, const Indent &indent, const char *name, const T &value)
Print "<name>: <value>\n" indented, matching ITK's PrintSelf style.
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
unsigned long SizeValueType
Definition itkIntTypes.h:86