ITK  4.13.0
Insight Segmentation and Registration Toolkit
itkConnectedComponentImageFilter.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 itkConnectedComponentImageFilter_h
19 #define itkConnectedComponentImageFilter_h
20 
21 #include "itkImageToImageFilter.h"
22 #include "itkImage.h"
23 #include <vector>
24 #include <map>
25 #include "itkProgressReporter.h"
26 #include "itkBarrier.h"
27 
28 namespace itk
29 {
58 template< typename TInputImage, typename TOutputImage, typename TMaskImage = TInputImage >
59 class ITK_TEMPLATE_EXPORT ConnectedComponentImageFilter:
60  public ImageToImageFilter< TInputImage, TOutputImage >
61 {
62 public:
68 
72  typedef typename Superclass::InputImagePointer InputImagePointer;
73 
78  typedef typename TOutputImage::PixelType OutputPixelType;
79  typedef typename TOutputImage::InternalPixelType OutputInternalPixelType;
80  typedef typename TInputImage::PixelType InputPixelType;
81  typedef typename TInputImage::InternalPixelType InputInternalPixelType;
82  typedef typename TMaskImage::PixelType MaskPixelType;
83  itkStaticConstMacro(ImageDimension, unsigned int,
84  TOutputImage::ImageDimension);
85  itkStaticConstMacro(OutputImageDimension, unsigned int,
86  TOutputImage::ImageDimension);
87  itkStaticConstMacro(InputImageDimension, unsigned int,
88  TInputImage::ImageDimension);
90 
94  typedef TInputImage InputImageType;
95  typedef TMaskImage MaskImageType;
97  typedef typename TInputImage::SizeType SizeType;
98  typedef typename TInputImage::OffsetType OffsetType;
99 
100  typedef TOutputImage OutputImageType;
101  typedef typename TOutputImage::RegionType RegionType;
104  typedef typename TOutputImage::OffsetType OutputOffsetType;
105  typedef typename TOutputImage::PixelType OutputImagePixelType;
106 
107  typedef std::list< IndexType > ListType;
108  typedef typename MaskImageType::Pointer MaskImagePointer;
109 
115 
120 
124  itkNewMacro(Self);
125 
132  itkSetMacro(FullyConnected, bool);
133  itkGetConstReferenceMacro(FullyConnected, bool);
134  itkBooleanMacro(FullyConnected);
136 
138  typedef IdentifierType LabelType;
139 
140  // only set after completion
141  itkGetConstReferenceMacro(ObjectCount, LabelType);
142 
143  // Concept checking -- input and output dimensions must be the same
144  itkConceptMacro( SameDimension,
145  ( Concept::SameDimension< itkGetStaticConstMacro(InputImageDimension),
146  itkGetStaticConstMacro(OutputImageDimension) > ) );
147  itkConceptMacro( OutputImagePixelTypeIsInteger, ( Concept::IsInteger< OutputImagePixelType > ) );
148 
149  itkSetInputMacro(MaskImage, MaskImageType);
150  itkGetInputMacro(MaskImage, MaskImageType);
151 
157  itkSetMacro(BackgroundValue, OutputImagePixelType);
158  itkGetConstMacro(BackgroundValue, OutputImagePixelType);
160 
161 protected:
163  {
164  m_FullyConnected = false;
165  m_ObjectCount = 0;
167 
168  // implicit
169  // #0 "Primary" required
170 
171  // #1 "MaskImage" optional
172  Self::AddOptionalInputName("MaskImage",1);
173  }
174 
175  virtual ~ConnectedComponentImageFilter() ITK_OVERRIDE {}
176  void PrintSelf(std::ostream & os, Indent indent) const ITK_OVERRIDE;
177 
181  void BeforeThreadedGenerateData() ITK_OVERRIDE;
182 
183  void AfterThreadedGenerateData() ITK_OVERRIDE;
184 
185  void ThreadedGenerateData(const RegionType & outputRegionForThread, ThreadIdType threadId) ITK_OVERRIDE;
186 
190  void GenerateInputRequestedRegion() ITK_OVERRIDE;
191 
196  void EnlargeOutputRequestedRegion( DataObject * itkNotUsed(output) ) ITK_OVERRIDE;
197 
198  bool m_FullyConnected;
199 
200 private:
201  ITK_DISALLOW_COPY_AND_ASSIGN(ConnectedComponentImageFilter);
202 
203  LabelType m_ObjectCount;
204  OutputImagePixelType m_BackgroundValue;
205 
206  // some additional types
207  typedef typename TOutputImage::RegionType::SizeType OutSizeType;
208 
209  // types to support the run length encoding of lines
210  class runLength
211  {
212 public:
213  // run length information - may be a more type safe way of doing this
215  typename TInputImage::IndexType where; // Index of the start of the run
216  LabelType label; // the initial label of the run
217  };
218 
219  typedef std::vector< runLength > lineEncoding;
220 
221  // the map storing lines
222  typedef std::vector< lineEncoding > LineMapType;
223 
224  typedef std::vector< typename TInputImage::OffsetValueType > OffsetVec;
225 
226  // the types to support union-find operations
227  typedef std::vector< LabelType > UnionFindType;
230 
231  // functions to support union-find operations
233  {
234  m_UnionFind = UnionFindType(size + 1);
235  }
236 
237  void InsertSet(const LabelType label);
238 
239  SizeValueType LookupSet(const LabelType label);
240 
241  void LinkLabels(const LabelType lab1, const LabelType lab2);
242 
243  SizeValueType CreateConsecutive();
244 
246  bool CheckNeighbors(const OutputIndexType & A,
247  const OutputIndexType & B);
248 
249  void CompareLines(lineEncoding & current, const lineEncoding & Neighbour);
250 
251  void FillOutput(const LineMapType & LineMap,
252  ProgressReporter & progress);
253 
254  void SetupLineOffsets(OffsetVec & LineOffsets);
255 
256  void Wait()
257  {
258  // use m_NumberOfLabels.size() to get the number of thread used
259  if ( m_NumberOfLabels.size() > 1 )
260  {
261  m_Barrier->Wait();
262  }
263  }
264 
265  typename std::vector< IdentifierType > m_NumberOfLabels;
266  typename std::vector< IdentifierType > m_FirstLineIdToJoin;
267 
269 
270  typename TInputImage::ConstPointer m_Input;
271 #if !defined( ITK_WRAPPING_PARSER )
273 #endif
274 };
275 } // end namespace itk
276 
277 #ifndef ITK_MANUAL_INSTANTIATION
278 #if !defined( ITK_WRAPPING_PARSER )
279 #include "itkConnectedComponentImageFilter.hxx"
280 #endif
281 #endif
282 
283 #endif
signed long OffsetValueType
Definition: itkIntTypes.h:154
std::vector< typename TInputImage::OffsetValueType > OffsetVec
ImageToImageFilter< TInputImage, TOutputImage > Superclass
TOutputImage::RegionType::SizeType OutSizeType
TInputImage::InternalPixelType InputInternalPixelType
Base class for all process objects that output image data.
unsigned long SizeValueType
Definition: itkIntTypes.h:143
SizeValueType IdentifierType
Definition: itkIntTypes.h:147
TOutputImage::InternalPixelType OutputInternalPixelType
OutputImageType::PixelType OutputImagePixelType
unsigned int ThreadIdType
Definition: itkIntTypes.h:159
Implements progress tracking for a filter.
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
#define itkConceptMacro(name, concept)
Base class for all data objects in ITK.
Label the objects in a binary image.