ITK  5.3.0
Insight Toolkit
itkShapeRelabelLabelMapFilter.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  * 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 itkShapeRelabelLabelMapFilter_h
19 #define itkShapeRelabelLabelMapFilter_h
20 
22 #include "itkLabelObject.h"
24 #include "itkProgressReporter.h"
25 
26 namespace itk
27 {
44 template <typename TImage>
45 class ITK_TEMPLATE_EXPORT ShapeRelabelLabelMapFilter : public InPlaceLabelMapFilter<TImage>
46 {
47 public:
48  ITK_DISALLOW_COPY_AND_MOVE(ShapeRelabelLabelMapFilter);
49 
55 
57  using ImageType = TImage;
58  using ImagePointer = typename ImageType::Pointer;
59  using ImageConstPointer = typename ImageType::ConstPointer;
60  using PixelType = typename ImageType::PixelType;
61  using IndexType = typename ImageType::IndexType;
62  using LabelObjectType = typename ImageType::LabelObjectType;
63 
64  using AttributeType = typename LabelObjectType::AttributeType;
65 
67  static constexpr unsigned int ImageDimension = TImage::ImageDimension;
68 
70  itkNewMacro(Self);
71 
74 
75 #ifdef ITK_USE_CONCEPT_CHECKING
76  // Begin concept checking
77 /* itkConceptMacro(InputEqualityComparableCheck,
78  (Concept::EqualityComparable<InputImagePixelType>));
79  itkConceptMacro(IntConvertibleToInputCheck,
80  (Concept::Convertible<int, InputImagePixelType>));
81  itkConceptMacro(InputOStreamWritableCheck,
82  (Concept::OStreamWritable<InputImagePixelType>));*/
83 // End concept checking
84 #endif
85 
91  itkSetMacro(ReverseOrdering, bool);
92  itkGetConstReferenceMacro(ReverseOrdering, bool);
93  itkBooleanMacro(ReverseOrdering);
95 
100  itkGetConstMacro(Attribute, AttributeType);
101  itkSetMacro(Attribute, AttributeType);
102  void
103  SetAttribute(const std::string & s)
104  {
105  this->SetAttribute(LabelObjectType::GetAttributeFromName(s));
106  }
108 
109 protected:
111  ~ShapeRelabelLabelMapFilter() override = default;
112 
113  void
114  GenerateData() override;
115 
116  template <typename TAttributeAccessor>
117  void
118  TemplatedGenerateData(const TAttributeAccessor &)
119  {
120  // Allocate the output
121  this->AllocateOutputs();
122 
123  ImageType * output = this->GetOutput();
124 
125  using LabelObjectPointer = typename LabelObjectType::Pointer;
126  using VectorType = std::vector<LabelObjectPointer>;
127 
128  ProgressReporter progress(this, 0, 2 * output->GetNumberOfLabelObjects());
129 
130  // Get the label objects in a vector, so they can be sorted
131  VectorType labelObjects;
132  labelObjects.reserve(output->GetNumberOfLabelObjects());
133  for (typename ImageType::Iterator it(output); !it.IsAtEnd(); ++it)
134  {
135  labelObjects.push_back(it.GetLabelObject());
136  progress.CompletedPixel();
137  }
138 
139  // Instantiate the comparator and sort the vector
140  if (m_ReverseOrdering)
141  {
142  std::sort(labelObjects.begin(),
143  labelObjects.end(),
145  }
146  else
147  {
148  std::sort(labelObjects.begin(),
149  labelObjects.end(),
151  }
152  // progress.CompletedPixel();
153 
154  // and put back the objects in the map
155  output->ClearLabels();
157  typename VectorType::const_iterator it2 = labelObjects.begin();
158  while (it2 != labelObjects.end())
159  {
160  // Avoid the background label if it is used
161  if (label == output->GetBackgroundValue())
162  {
163  label++;
164  }
165  (*it2)->SetLabel(label);
166  output->AddLabelObject(*it2);
167 
168  // Go to the next label
169  label++;
170  progress.CompletedPixel();
171 
172  ++it2;
173  }
174  }
175 
176  void
177  PrintSelf(std::ostream & os, Indent indent) const override;
178 
180 
182 }; // end of class
183 } // end namespace itk
184 
185 #ifndef ITK_MANUAL_INSTANTIATION
186 # include "itkShapeRelabelLabelMapFilter.hxx"
187 #endif
188 
189 #endif
itk::ShapeRelabelLabelMapFilter::ImageType
TImage ImageType
Definition: itkShapeRelabelLabelMapFilter.h:57
itk::ShapeRelabelLabelMapFilter::m_Attribute
AttributeType m_Attribute
Definition: itkShapeRelabelLabelMapFilter.h:181
itk::ShapeRelabelLabelMapFilter::ImageConstPointer
typename ImageType::ConstPointer ImageConstPointer
Definition: itkShapeRelabelLabelMapFilter.h:59
itk::ShapeRelabelLabelMapFilter::PixelType
typename ImageType::PixelType PixelType
Definition: itkShapeRelabelLabelMapFilter.h:60
itk::ShapeRelabelLabelMapFilter::SetAttribute
void SetAttribute(const std::string &s)
Definition: itkShapeRelabelLabelMapFilter.h:103
itk::ShapeRelabelLabelMapFilter::AttributeType
typename LabelObjectType::AttributeType AttributeType
Definition: itkShapeRelabelLabelMapFilter.h:64
itk::InPlaceLabelMapFilter
Base class for filters that takes an image as input and overwrites that image as the output.
Definition: itkInPlaceLabelMapFilter.h:83
itk::GTest::TypedefsAndConstructors::Dimension2::VectorType
ImageBaseType::SpacingType VectorType
Definition: itkGTestTypedefsAndConstructors.h:53
itkProgressReporter.h
itk::Functor::LabelObjectComparator
Definition: itkLabelObjectAccessors.h:65
itk::SmartPointer< Self >
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itkShapeLabelObjectAccessors.h
itk::Functor::LabelObjectReverseComparator
Definition: itkLabelObjectAccessors.h:84
itk::GTest::TypedefsAndConstructors::Dimension2::IndexType
ImageBaseType::IndexType IndexType
Definition: itkGTestTypedefsAndConstructors.h:50
itk::ShapeRelabelLabelMapFilter::m_ReverseOrdering
bool m_ReverseOrdering
Definition: itkShapeRelabelLabelMapFilter.h:179
itk::LightObject
Light weight base class for most itk classes.
Definition: itkLightObject.h:59
itk::ShapeRelabelLabelMapFilter
Relabels objects according to their shape attributes.
Definition: itkShapeRelabelLabelMapFilter.h:45
itkInPlaceLabelMapFilter.h
itkLabelObject.h
itk::ShapeRelabelLabelMapFilter::IndexType
typename ImageType::IndexType IndexType
Definition: itkShapeRelabelLabelMapFilter.h:61
itk::NumericTraits::ZeroValue
static T ZeroValue()
Definition: itkNumericTraits.h:148
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::ShapeRelabelLabelMapFilter::ImagePointer
typename ImageType::Pointer ImagePointer
Definition: itkShapeRelabelLabelMapFilter.h:58
itk::ProgressReporter
Implements progress tracking for a filter.
Definition: itkProgressReporter.h:60
itk::ShapeRelabelLabelMapFilter::TemplatedGenerateData
void TemplatedGenerateData(const TAttributeAccessor &)
Definition: itkShapeRelabelLabelMapFilter.h:118
itk::ShapeRelabelLabelMapFilter::LabelObjectType
typename ImageType::LabelObjectType LabelObjectType
Definition: itkShapeRelabelLabelMapFilter.h:62