ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
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;
59 using ImagePointer = typename ImageType::Pointer;
60 using ImageConstPointer = typename ImageType::ConstPointer;
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 /*itkConceptMacro(InputEqualityComparableCheck,
77 (Concept::EqualityComparable<InputImagePixelType>));
78 itkConceptMacro(IntConvertibleToInputCheck,
79 (Concept::Convertible<int, InputImagePixelType>));
80 itkConceptMacro(InputOStreamWritableCheck,
81 (Concept::OStreamWritable<InputImagePixelType>));*/
82
88 itkSetMacro(ReverseOrdering, bool);
89 itkGetConstReferenceMacro(ReverseOrdering, bool);
90 itkBooleanMacro(ReverseOrdering);
92
97 itkGetConstMacro(Attribute, AttributeType);
98 itkSetMacro(Attribute, AttributeType);
99 void
100 SetAttribute(const std::string & s)
101 {
102 this->SetAttribute(LabelObjectType::GetAttributeFromName(s));
103 }
104
105
106protected:
108 ~ShapeRelabelLabelMapFilter() override = default;
109
110 void
111 GenerateData() override;
112
113 template <typename TAttributeAccessor>
114 void
115 TemplatedGenerateData(const TAttributeAccessor &)
116 {
117 // Allocate the output
118 this->AllocateOutputs();
119
120 ImageType * output = this->GetOutput();
121
122 using LabelObjectPointer = typename LabelObjectType::Pointer;
123 using VectorType = std::vector<LabelObjectPointer>;
124
125 ProgressReporter progress(this, 0, 2 * output->GetNumberOfLabelObjects());
126
127 // Get the label objects in a vector, so they can be sorted
128 VectorType labelObjects;
129 labelObjects.reserve(output->GetNumberOfLabelObjects());
130 for (typename ImageType::Iterator it(output); !it.IsAtEnd(); ++it)
131 {
132 labelObjects.push_back(it.GetLabelObject());
133 progress.CompletedPixel();
134 }
135
136 // Instantiate the comparator and sort the vector
138 {
139 std::sort(labelObjects.begin(),
140 labelObjects.end(),
142 }
143 else
144 {
145 std::sort(labelObjects.begin(),
146 labelObjects.end(),
148 }
149 // progress.CompletedPixel();
150
151 // and put back the objects in the map
152 output->ClearLabels();
153 PixelType label{};
154 auto it2 = labelObjects.begin();
155 while (it2 != labelObjects.end())
156 {
157 // Avoid the background label if it is used
158 if (label == output->GetBackgroundValue())
159 {
160 ++label;
161 }
162 (*it2)->SetLabel(label);
163 output->AddLabelObject(*it2);
164
165 // Go to the next label
166 ++label;
167 progress.CompletedPixel();
168
169 ++it2;
170 }
171 }
172
173 void
174 PrintSelf(std::ostream & os, Indent indent) const override;
175
177
179}; // end of class
180} // end namespace itk
181
182#ifndef ITK_MANUAL_INSTANTIATION
183# include "itkShapeRelabelLabelMapFilter.hxx"
184#endif
185
186#endif
OutputImageType * GetOutput()
void AllocateOutputs() override
Control indentation during Print() invocation.
Definition itkIndent.h:50
Implements progress tracking for a filter.
~ShapeRelabelLabelMapFilter() override=default
virtual void SetAttribute(AttributeType _arg)
void PrintSelf(std::ostream &os, Indent indent) const override
void TemplatedGenerateData(const TAttributeAccessor &)
typename LabelObjectType::AttributeType AttributeType
Implements transparent reference counting.
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....