ITK  4.13.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 <list>
23 #include <map>
24 #include <vector>
25 #include "itkProgressReporter.h"
26 #include "itkBarrier.h"
27 #include "itkLabelMap.h"
28 #include "itkLabelObject.h"
30 
31 namespace itk
32 {
60 template< typename TInputImage,
61  typename TOutputImage =
62  LabelMap< LabelObject< SizeValueType, TInputImage::ImageDimension > > >
63 class ITK_TEMPLATE_EXPORT BinaryImageToLabelMapFilter:
64  public ImageToImageFilter< TInputImage, TOutputImage >
65 {
66 public:
74 
78  itkNewMacro(Self);
79 
84 
88  typedef typename Superclass::InputImagePointer InputImagePointer;
89 
94  typedef typename TOutputImage::PixelType OutputPixelType;
95  typedef typename TInputImage::PixelType InputPixelType;
98  itkStaticConstMacro(ImageDimension, unsigned int, TOutputImage::ImageDimension);
99  itkStaticConstMacro(OutputImageDimension, unsigned int, TOutputImage::ImageDimension);
100  itkStaticConstMacro(InputImageDimension, unsigned int, TInputImage::ImageDimension);
102 
106  typedef TInputImage InputImageType;
108  typedef typename TInputImage::SizeType SizeType;
109  typedef typename TInputImage::OffsetType OffsetType;
110 
111  typedef TOutputImage OutputImageType;
112  typedef typename TOutputImage::RegionType RegionType;
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 
157 protected:
159  virtual ~BinaryImageToLabelMapFilter() ITK_OVERRIDE {}
160  void PrintSelf(std::ostream & os, Indent indent) const ITK_OVERRIDE;
161 
163 
167  void BeforeThreadedGenerateData() ITK_OVERRIDE;
168 
169  void AfterThreadedGenerateData() ITK_OVERRIDE;
170 
171  void ThreadedGenerateData(const RegionType & outputRegionForThread, ThreadIdType threadId) ITK_OVERRIDE;
172 
176  void GenerateInputRequestedRegion() ITK_OVERRIDE;
177 
182  void EnlargeOutputRequestedRegion( DataObject *itkNotUsed(output) ) ITK_OVERRIDE;
183 
187  virtual const ImageRegionSplitterBase* GetImageRegionSplitter() const ITK_OVERRIDE;
188 
189 private:
190  ITK_DISALLOW_COPY_AND_ASSIGN(BinaryImageToLabelMapFilter);
191 
192  // some additional types
193  typedef typename TOutputImage::RegionType::SizeType OutSizeType;
194 
195  // types to support the run length encoding of lines
196  class runLength
197  {
198  public:
199  // run length information - may be a more type safe way of doing this
201  typename InputImageType::IndexType where; // Index of the start of the run
202  InternalLabelType label; // the initial label of the run
203  };
204 
205  typedef std::vector< runLength > lineEncoding;
206 
207  // the map storing lines
208  typedef std::vector< lineEncoding > LineMapType;
209 
210  typedef std::vector< OffsetValueType > OffsetVectorType;
211 
212  // the types to support union-find operations
213  typedef std::vector< InternalLabelType > UnionFindType;
215 
216  typedef std::vector< OutputPixelType > ConsecutiveVectorType;
218 
219  // functions to support union-find operations
220  void InitUnion(const InternalLabelType size);
221 
222  void InsertSet(const InternalLabelType label);
223 
224  InternalLabelType LookupSet(const InternalLabelType label);
225 
226  void LinkLabels(const InternalLabelType lab1, const InternalLabelType lab2);
227 
228  SizeValueType CreateConsecutive();
229 
231  bool CheckNeighbors(const OutputIndexType & A,
232  const OutputIndexType & B);
233 
234  void CompareLines(lineEncoding & current, const lineEncoding & Neighbour);
235 
236  void FillOutput(const LineMapType & LineMap,
237  ProgressReporter & progress);
238 
239  void SetupLineOffsets(OffsetVectorType & LineOffsets);
240 
241  void Wait();
242 
245 
247 
249 
250  std::vector< SizeValueType > m_NumberOfLabels;
251  std::vector< SizeValueType > m_FirstLineIdToJoin;
252 
254 
256 
257 #if !defined( ITK_WRAPPING_PARSER )
259 #endif
260 };
261 } // end namespace itk
262 
263 #ifndef ITK_MANUAL_INSTANTIATION
264 #if !defined( ITK_WRAPPING_PARSER )
265 #include "itkBinaryImageToLabelMapFilter.hxx"
266 #endif
267 #endif
268 
269 #endif
signed long OffsetValueType
Definition: itkIntTypes.h:154
std::vector< OutputPixelType > ConsecutiveVectorType
std::vector< SizeValueType > m_FirstLineIdToJoin
TInputImage::OffsetValueType OffsetValueType
ImageToImageFilter< TInputImage, TOutputImage > Superclass
Base class for all process objects that output image data.
unsigned long SizeValueType
Definition: itkIntTypes.h:143
std::vector< InternalLabelType > UnionFindType
Superclass::InputImagePointer InputImagePointer
TOutputImage::RegionType::SizeType OutSizeType
Divide an image region into several pieces.
ImageRegionSplitterDirection::Pointer m_ImageRegionSplitter
unsigned int ThreadIdType
Definition: itkIntTypes.h:159
Implements progress tracking for a filter.
Label the connected components in a binary image and produce a collection of label objects...
Base class for filters that take an image as input and produce an image as output.
Control indentation during Print() invocation.
Definition: itkIndent.h:49
std::vector< OffsetValueType > OffsetVectorType
#define itkConceptMacro(name, concept)
Base class for all data objects in ITK.