Orfeo Toolbox  4.0
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 >
45  public InPlaceLabelMapFilter< TImage >
46 {
47 public:
53 
55  typedef TImage ImageType;
56  typedef typename ImageType::Pointer ImagePointer;
57  typedef typename ImageType::ConstPointer ImageConstPointer;
58  typedef typename ImageType::PixelType PixelType;
59  typedef typename ImageType::IndexType IndexType;
60  typedef typename ImageType::LabelObjectType LabelObjectType;
61 
62  typedef typename LabelObjectType::AttributeType AttributeType;
63 
65  itkStaticConstMacro(ImageDimension, unsigned int, TImage::ImageDimension);
66 
68  itkNewMacro(Self);
69 
72 
73 #ifdef ITK_USE_CONCEPT_CHECKING
74  // Begin concept checking
75 /* itkConceptMacro(InputEqualityComparableCheck,
76  (Concept::EqualityComparable<InputImagePixelType>));
77  itkConceptMacro(IntConvertibleToInputCheck,
78  (Concept::Convertible<int, InputImagePixelType>));
79  itkConceptMacro(InputOStreamWritableCheck,
80  (Concept::OStreamWritable<InputImagePixelType>));*/
81 // End concept checking
82 #endif
83 
89  itkSetMacro(ReverseOrdering, bool);
90  itkGetConstReferenceMacro(ReverseOrdering, bool);
91  itkBooleanMacro(ReverseOrdering);
92 
96  itkSetMacro(NumberOfObjects, SizeValueType);
97  itkGetConstReferenceMacro(NumberOfObjects, SizeValueType);
98 
103  itkGetConstMacro(Attribute, AttributeType);
104  itkSetMacro(Attribute, AttributeType);
105 
106  void SetAttribute(const std::string & s)
107  {
108  this->SetAttribute( LabelObjectType::GetAttributeFromName(s) );
109  }
110 
111 protected:
114 
115  void GenerateData();
116 
117  template< typename TAttributeAccessor >
118  void TemplatedGenerateData(const TAttributeAccessor &)
119  {
120  // Allocate the output
121  this->AllocateOutputs();
122 
123  ImageType *output = this->GetOutput();
124  ImageType *output2 = this->GetOutput(1);
125 
126  // set the background value for the second output - this is not done in the
127  // superclasses
128  output2->SetBackgroundValue( output->GetBackgroundValue() );
129 
130  typedef typename LabelObjectType::Pointer LabelObjectPointer;
131  typedef std::vector< LabelObjectPointer > VectorType;
132 
133  ProgressReporter progress( this, 0, 2 * output->GetNumberOfLabelObjects() );
134 
135  // get the label objects in a vector, so they can be sorted
136  VectorType labelObjects;
137  labelObjects.reserve( output->GetNumberOfLabelObjects() );
138  typename ImageType::Iterator it( output );
139  while ( ! it.IsAtEnd() )
140  {
141  labelObjects.push_back( it.GetLabelObject() );
142  progress.CompletedPixel();
143  ++it;
144  }
145 
146  // instantiate the comparator and sort the vector
147  if ( m_NumberOfObjects < output->GetNumberOfLabelObjects() )
148  {
149  typename VectorType::iterator end = labelObjects.begin() + m_NumberOfObjects;
150  if ( m_ReverseOrdering )
151  {
153  std::nth_element(labelObjects.begin(), end, labelObjects.end(), comparator);
154  }
155  else
156  {
158  std::nth_element(labelObjects.begin(), end, labelObjects.end(), comparator);
159  }
160  progress.CompletedPixel();
161 
162  // and remove the last objects of the map
163  for ( typename VectorType::const_iterator it2 = end;
164  it2 != labelObjects.end();
165  it2++ )
166  {
167  output2->AddLabelObject(*it2);
168  output->RemoveLabelObject(*it2);
169  progress.CompletedPixel();
170  }
171  }
172  }
173 
174  void PrintSelf(std::ostream & os, Indent indent) const;
175 
177 
180 
181 private:
182  ShapeKeepNObjectsLabelMapFilter(const Self &); //purposely not implemented
183  void operator=(const Self &); //purposely not implemented
184 }; // end of class
185 } // end namespace itk
186 
187 #ifndef ITK_MANUAL_INSTANTIATION
189 #endif
190 
191 #endif

Generated at Sat Mar 8 2014 15:31:13 for Orfeo Toolbox with doxygen 1.8.3.1