ITK  4.2.0
Insight Segmentation and Registration Toolkit
itkBinaryImageToLabelMapFilter.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 __itkBinaryImageToLabelMapFilter_h
19 #define __itkBinaryImageToLabelMapFilter_h
20 
21 #include "itkImageToImageFilter.h"
22 #include <vector>
23 #include <map>
24 #include "itkProgressReporter.h"
25 #include "itkBarrier.h"
26 #include "itkLabelMap.h"
27 #include "itkLabelObject.h"
28 
29 namespace itk
30 {
58 template< class TInputImage,
59  class TOutputImage =
60  LabelMap< LabelObject< SizeValueType, ::itk::GetImageDimension< TInputImage >::ImageDimension > > >
61 class ITK_EXPORT BinaryImageToLabelMapFilter:
62  public ImageToImageFilter< TInputImage, TOutputImage >
63 {
64 public:
72 
76  itkNewMacro(Self);
77 
82 
86  typedef typename Superclass::InputImagePointer InputImagePointer;
87 
92  typedef typename TOutputImage::PixelType OutputPixelType;
93  typedef typename TInputImage::PixelType InputPixelType;
96  itkStaticConstMacro(ImageDimension, unsigned int, TOutputImage::ImageDimension);
97  itkStaticConstMacro(OutputImageDimension, unsigned int, TOutputImage::ImageDimension);
98  itkStaticConstMacro(InputImageDimension, unsigned int, TInputImage::ImageDimension);
100 
102 
106  typedef TInputImage InputImageType;
107  typedef typename TInputImage::IndexType IndexType;
108  typedef typename TInputImage::SizeType SizeType;
109  typedef typename TInputImage::OffsetType OffsetType;
110 
111  typedef TOutputImage OutputImageType;
112  typedef typename TOutputImage::RegionType RegionType;
113  typedef typename TOutputImage::IndexType OutputIndexType;
114  typedef typename TOutputImage::SizeType OutputSizeType;
115  typedef typename TOutputImage::OffsetType OutputOffsetType;
116  typedef typename TOutputImage::PixelType OutputImagePixelType;
117 
118  typedef std::list< IndexType > ListType;
119 
126  itkSetMacro(FullyConnected, bool);
127  itkGetConstReferenceMacro(FullyConnected, bool);
128  itkBooleanMacro(FullyConnected);
130 
131  // only set after completion
132  itkGetConstReferenceMacro(NumberOfObjects, SizeValueType);
133 
138  itkSetMacro(OutputBackgroundValue, OutputPixelType);
139  itkGetConstMacro(OutputBackgroundValue, OutputPixelType);
141 
146  itkSetMacro(InputForegroundValue, InputPixelType);
147  itkGetConstMacro(InputForegroundValue, InputPixelType);
149 
150 #ifdef ITK_USE_CONCEPT_CHECKING
151  // Concept checking -- input and output dimensions must be the same
152  itkConceptMacro( SameDimension,
153  ( Concept::SameDimension< itkGetStaticConstMacro(InputImageDimension),
154  itkGetStaticConstMacro(OutputImageDimension) > ) );
155 #endif
156 protected:
159  void PrintSelf(std::ostream & os, Indent indent) const;
160 
164  void BeforeThreadedGenerateData();
165 
166  void AfterThreadedGenerateData();
167 
168  void ThreadedGenerateData(const RegionType & outputRegionForThread, ThreadIdType threadId);
169 
173  void GenerateInputRequestedRegion();
174 
179  void EnlargeOutputRequestedRegion( DataObject *itkNotUsed(output) );
180 private:
181  BinaryImageToLabelMapFilter(const Self &); //purposely not implemented
182  void operator=(const Self &); //purposely not implemented
184 
185  // some additional types
186  typedef typename TOutputImage::RegionType::SizeType OutSizeType;
187 
188  // types to support the run length encoding of lines
189  class runLength
190  {
191  public:
192  // run length information - may be a more type safe way of doing this
194  typename InputImageType::IndexType where; // Index of the start of the run
195  LabelType label; // the initial label of the run
196  };
197 
198  typedef std::vector< runLength > lineEncoding;
199 
200  // the map storing lines
201  typedef std::vector< lineEncoding > LineMapType;
202 
203  typedef std::vector< OffsetValueType > OffsetVectorType;
204 
205  // the types to support union-find operations
206  typedef std::vector< LabelType > UnionFindType;
209  // functions to support union-find operations
210  void InitUnion(const LabelType size)
211  {
212  m_UnionFind = UnionFindType(size + 1);
213  }
214 
215  void InsertSet(const LabelType label);
216 
217  LabelType LookupSet(const LabelType label);
218 
219  void LinkLabels(const LabelType lab1, const LabelType lab2);
220 
221  LabelType CreateConsecutive();
222 
224  bool CheckNeighbors(const OutputIndexType & A,
225  const OutputIndexType & B);
226 
227  void CompareLines(lineEncoding & current, const lineEncoding & Neighbour);
228 
229  void FillOutput(const LineMapType & LineMap,
230  ProgressReporter & progress);
231 
232  void SetupLineOffsets(OffsetVectorType & LineOffsets);
233 
234  void Wait()
235  {
236  // use m_NumberOfLabels.size() to get the number of thread used
237  if ( m_NumberOfLabels.size() > 1 )
238  {
239  m_Barrier->Wait();
240  }
241  }
242 
245 
247 
249 
250  typename std::vector< SizeValueType > m_NumberOfLabels;
251  typename std::vector< SizeValueType > m_FirstLineIdToJoin;
252 
254 
255 #if !defined( CABLE_CONFIGURATION )
257 #endif
258 };
259 } // end namespace itk
260 
261 #ifndef ITK_MANUAL_INSTANTIATION
262 #if !defined( CABLE_CONFIGURATION )
263 #include "itkBinaryImageToLabelMapFilter.hxx"
264 #endif
265 #endif
266 
267 #endif
268