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