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
26namespace itk
27{
45template <typename TImage>
46class ITK_TEMPLATE_EXPORT ShapeRelabelLabelMapFilter : public InPlaceLabelMapFilter<TImage>
47{
48public:
49 ITK_DISALLOW_COPY_AND_MOVE(ShapeRelabelLabelMapFilter);
50
56
58 using ImageType = TImage;
61 using PixelType = typename ImageType::PixelType;
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 }
110protected:
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
Base class for filters that takes an image as input and overwrites that image as the output.
Control indentation during Print() invocation.
Definition: itkIndent.h:50
Light weight base class for most itk classes.
Implements progress tracking for a filter.
Relabels objects according to their shape attributes.
~ShapeRelabelLabelMapFilter() override=default
void PrintSelf(std::ostream &os, Indent indent) const override
typename ImageType::ConstPointer ImageConstPointer
void TemplatedGenerateData(const TAttributeAccessor &)
typename ImageType::LabelObjectType LabelObjectType
typename ImageType::PixelType PixelType
typename ImageType::IndexType IndexType
typename LabelObjectType::AttributeType AttributeType
SmartPointer< const Self > ConstPointer
SmartPointer< Self > Pointer
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....