ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
itkShapeKeepNObjectsLabelMapFilter.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 itkShapeKeepNObjectsLabelMapFilter_h
19#define itkShapeKeepNObjectsLabelMapFilter_h
20
23#include "itkProgressReporter.h"
24
25namespace itk
26{
43template <typename TImage>
44class ITK_TEMPLATE_EXPORT ShapeKeepNObjectsLabelMapFilter : public InPlaceLabelMapFilter<TImage>
45{
46public:
47 ITK_DISALLOW_COPY_AND_MOVE(ShapeKeepNObjectsLabelMapFilter);
48
54
56 using ImageType = TImage;
57 using ImagePointer = typename ImageType::Pointer;
58 using ImageConstPointer = typename ImageType::ConstPointer;
59 using PixelType = typename ImageType::PixelType;
60 using IndexType = typename ImageType::IndexType;
61 using LabelObjectType = typename ImageType::LabelObjectType;
62
63 using AttributeType = typename LabelObjectType::AttributeType;
64
66 static constexpr unsigned int ImageDimension = TImage::ImageDimension;
67
69 itkNewMacro(Self);
70
72 itkOverrideGetNameOfClassMacro(ShapeKeepNObjectsLabelMapFilter);
73
74 /*itkConceptMacro(InputEqualityComparableCheck,
75 (Concept::EqualityComparable<InputImagePixelType>));
76 itkConceptMacro(IntConvertibleToInputCheck,
77 (Concept::Convertible<int, InputImagePixelType>));
78 itkConceptMacro(InputOStreamWritableCheck,
79 (Concept::OStreamWritable<InputImagePixelType>));*/
80
86 itkSetMacro(ReverseOrdering, bool);
87 itkGetConstReferenceMacro(ReverseOrdering, bool);
88 itkBooleanMacro(ReverseOrdering);
90
94 itkSetMacro(NumberOfObjects, SizeValueType);
95 itkGetConstReferenceMacro(NumberOfObjects, SizeValueType);
97
102 itkGetConstMacro(Attribute, AttributeType);
103 itkSetMacro(Attribute, AttributeType);
105
106 void
107 SetAttribute(const std::string & s)
108 {
109 this->SetAttribute(LabelObjectType::GetAttributeFromName(s));
110 }
111
112protected:
115
116 void
117 GenerateData() override;
118
119 template <typename TAttributeAccessor>
120 void
121 TemplatedGenerateData(const TAttributeAccessor &)
122 {
123 // Allocate the output
124 this->AllocateOutputs();
125
126 ImageType * output = this->GetOutput();
127 ImageType * output2 = this->GetOutput(1);
128
129 // set the background value for the second output - this is not done in the
130 // superclasses
131 output2->SetBackgroundValue(output->GetBackgroundValue());
132
133 using LabelObjectPointer = typename LabelObjectType::Pointer;
134 using VectorType = std::vector<LabelObjectPointer>;
135
136 ProgressReporter progress(this, 0, 2 * output->GetNumberOfLabelObjects());
137
138 // get the label objects in a vector, so they can be sorted
139 VectorType labelObjects;
140 labelObjects.reserve(output->GetNumberOfLabelObjects());
141 typename ImageType::Iterator it(output);
142 while (!it.IsAtEnd())
143 {
144 labelObjects.push_back(it.GetLabelObject());
145 progress.CompletedPixel();
146 ++it;
147 }
148
149 // instantiate the comparator and sort the vector
150 if (m_NumberOfObjects < output->GetNumberOfLabelObjects())
151 {
152 auto end = labelObjects.begin() + m_NumberOfObjects;
154 {
156 std::nth_element(labelObjects.begin(), end, labelObjects.end(), comparator);
157 }
158 else
159 {
161 std::nth_element(labelObjects.begin(), end, labelObjects.end(), comparator);
162 }
163 progress.CompletedPixel();
164
165 // and remove the last objects of the map
166 for (auto it2 = end; it2 != labelObjects.end(); ++it2)
167 {
168 output2->AddLabelObject(*it2);
169 output->RemoveLabelObject(*it2);
170 progress.CompletedPixel();
171 }
172 }
173 }
174
175 void
176 PrintSelf(std::ostream & os, Indent indent) const override;
177
179
182}; // end of class
183} // end namespace itk
184
185#ifndef ITK_MANUAL_INSTANTIATION
186# include "itkShapeKeepNObjectsLabelMapFilter.hxx"
187#endif
188
189#endif
OutputImageType * GetOutput()
void AllocateOutputs() override
Control indentation during Print() invocation.
Definition itkIndent.h:50
Implements progress tracking for a filter.
~ShapeKeepNObjectsLabelMapFilter() override=default
void PrintSelf(std::ostream &os, Indent indent) const override
virtual void SetAttribute(AttributeType _arg)
Implements transparent reference counting.
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
unsigned long SizeValueType
Definition itkIntTypes.h:86