ITK  5.0.0
Insight Segmentation and Registration Toolkit
itkShapeKeepNObjectsLabelMapFilter.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
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 itkShapeKeepNObjectsLabelMapFilter_h
19 #define itkShapeKeepNObjectsLabelMapFilter_h
20 
23 #include "itkProgressReporter.h"
24 
25 namespace itk
26 {
43 template< typename TImage >
44 class ITK_TEMPLATE_EXPORT ShapeKeepNObjectsLabelMapFilter:
45  public InPlaceLabelMapFilter< TImage >
46 {
47 public:
48  ITK_DISALLOW_COPY_AND_ASSIGN(ShapeKeepNObjectsLabelMapFilter);
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 
99  itkSetMacro(NumberOfObjects, SizeValueType);
100  itkGetConstReferenceMacro(NumberOfObjects, SizeValueType);
102 
107  itkGetConstMacro(Attribute, AttributeType);
108  itkSetMacro(Attribute, AttributeType);
110 
111  void SetAttribute(const std::string & s)
112  {
113  this->SetAttribute( LabelObjectType::GetAttributeFromName(s) );
114  }
115 
116 protected:
118  ~ShapeKeepNObjectsLabelMapFilter() override = default;
119 
120  void GenerateData() override;
121 
122  template< typename TAttributeAccessor >
123  void TemplatedGenerateData(const TAttributeAccessor &)
124  {
125  // Allocate the output
126  this->AllocateOutputs();
127 
128  ImageType *output = this->GetOutput();
129  ImageType *output2 = this->GetOutput(1);
130 
131  // set the background value for the second output - this is not done in the
132  // superclasses
133  output2->SetBackgroundValue( output->GetBackgroundValue() );
134 
135  using LabelObjectPointer = typename LabelObjectType::Pointer;
136  using VectorType = std::vector< LabelObjectPointer >;
137 
138  ProgressReporter progress( this, 0, 2 * output->GetNumberOfLabelObjects() );
139 
140  // get the label objects in a vector, so they can be sorted
141  VectorType labelObjects;
142  labelObjects.reserve( output->GetNumberOfLabelObjects() );
143  typename ImageType::Iterator it( output );
144  while ( ! it.IsAtEnd() )
145  {
146  labelObjects.push_back( it.GetLabelObject() );
147  progress.CompletedPixel();
148  ++it;
149  }
150 
151  // instantiate the comparator and sort the vector
152  if ( m_NumberOfObjects < output->GetNumberOfLabelObjects() )
153  {
154  auto end = labelObjects.begin() + m_NumberOfObjects;
155  if ( m_ReverseOrdering )
156  {
158  std::nth_element(labelObjects.begin(), end, labelObjects.end(), comparator);
159  }
160  else
161  {
163  std::nth_element(labelObjects.begin(), end, labelObjects.end(), comparator);
164  }
165  progress.CompletedPixel();
166 
167  // and remove the last objects of the map
168  for ( typename VectorType::const_iterator it2 = end;
169  it2 != labelObjects.end();
170  it2++ )
171  {
172  output2->AddLabelObject(*it2);
173  output->RemoveLabelObject(*it2);
174  progress.CompletedPixel();
175  }
176  }
177  }
178 
179  void PrintSelf(std::ostream & os, Indent indent) const override;
180 
182 
185 }; // end of class
186 } // end namespace itk
187 
188 #ifndef ITK_MANUAL_INSTANTIATION
189 #include "itkShapeKeepNObjectsLabelMapFilter.hxx"
190 #endif
191 
192 #endif
Light weight base class for most itk classes.
unsigned long SizeValueType
Definition: itkIntTypes.h:83
void TemplatedGenerateData(const TAttributeAccessor &)
Base class for filters that takes an image as input and overwrites that image as the output...
typename ImageType::LabelObjectType LabelObjectType
Implements progress tracking for a filter.
Keep N objects according to their shape attributes.
typename LabelObjectType::AttributeType AttributeType
Control indentation during Print() invocation.
Definition: itkIndent.h:49