ITK  4.4.0
Insight Segmentation and Registration Toolkit
itkShapeRelabelLabelMapFilter.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 __itkShapeRelabelLabelMapFilter_h
19 #define __itkShapeRelabelLabelMapFilter_h
20 
22 #include "itkLabelObject.h"
24 #include "itkProgressReporter.h"
25 
26 namespace itk
27 {
44 template< class TImage >
45 class ITK_EXPORT ShapeRelabelLabelMapFilter:
46  public InPlaceLabelMapFilter< TImage >
47 {
48 public:
54 
56  typedef TImage ImageType;
57  typedef typename ImageType::Pointer ImagePointer;
58  typedef typename ImageType::ConstPointer ImageConstPointer;
59  typedef typename ImageType::PixelType PixelType;
60  typedef typename ImageType::IndexType IndexType;
61  typedef typename ImageType::LabelObjectType LabelObjectType;
62 
63  typedef typename LabelObjectType::AttributeType AttributeType;
64 
66  itkStaticConstMacro(ImageDimension, unsigned int, TImage::ImageDimension);
67 
69  itkNewMacro(Self);
70 
73 
74 #ifdef ITK_USE_CONCEPT_CHECKING
75 
76 /* itkConceptMacro(InputEqualityComparableCheck,
77  (Concept::EqualityComparable<InputImagePixelType>));
78  itkConceptMacro(IntConvertibleToInputCheck,
79  (Concept::Convertible<int, InputImagePixelType>));
80  itkConceptMacro(InputOStreamWritableCheck,
81  (Concept::OStreamWritable<InputImagePixelType>));*/
82 
84 #endif
85 
91  itkSetMacro(ReverseOrdering, bool);
92  itkGetConstReferenceMacro(ReverseOrdering, bool);
93  itkBooleanMacro(ReverseOrdering);
95 
100  itkGetConstMacro(Attribute, AttributeType);
101  itkSetMacro(Attribute, AttributeType);
102  void SetAttribute(const std::string & s)
103  {
104  this->SetAttribute( LabelObjectType::GetAttributeFromName(s) );
105  }
107 
108 protected:
111 
112  void GenerateData();
113 
114  template< class TAttributeAccessor >
115  void TemplatedGenerateData(const TAttributeAccessor &)
116  {
117  // Allocate the output
118  this->AllocateOutputs();
119 
120  ImageType *output = this->GetOutput();
121 
122  typedef typename LabelObjectType::Pointer LabelObjectPointer;
123  typedef std::vector< LabelObjectPointer > VectorType;
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 );
131  ! it.IsAtEnd();
132  ++it )
133  {
134  labelObjects.push_back(it.GetLabelObject());
135  progress.CompletedPixel();
136  }
137 
138  // Instantiate the comparator and sort the vector
139  if ( m_ReverseOrdering )
140  {
141  std::sort( labelObjects.begin(), labelObjects.end(),
143  }
144  else
145  {
146  std::sort( labelObjects.begin(), labelObjects.end(),
148  }
149  // progress.CompletedPixel();
150 
151  // and put back the objects in the map
152  output->ClearLabels();
154  typename VectorType::const_iterator 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 PrintSelf(std::ostream & os, Indent indent) const;
174 
176 
178 
179 private:
180  ShapeRelabelLabelMapFilter(const Self &); //purposely not implemented
181  void operator=(const Self &); //purposely not implemented
182 }; // end of class
183 } // end namespace itk
184 
185 #ifndef ITK_MANUAL_INSTANTIATION
186 #include "itkShapeRelabelLabelMapFilter.hxx"
187 #endif
188 
189 #endif
190