ITK  4.2.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 {
54 template< class TInputImage, class TOutputImage, class TMaskImage = TInputImage >
56  public ImageToImageFilter< TInputImage, TOutputImage >
57 {
58 public:
64 
68  typedef typename Superclass::InputImagePointer InputImagePointer;
69 
74  typedef typename TOutputImage::PixelType OutputPixelType;
75  typedef typename TOutputImage::InternalPixelType OutputInternalPixelType;
76  typedef typename TInputImage::PixelType InputPixelType;
77  typedef typename TInputImage::InternalPixelType InputInternalPixelType;
78  typedef typename TMaskImage::PixelType MaskPixelType;
79  itkStaticConstMacro(ImageDimension, unsigned int,
80  TOutputImage::ImageDimension);
81  itkStaticConstMacro(OutputImageDimension, unsigned int,
82  TOutputImage::ImageDimension);
83  itkStaticConstMacro(InputImageDimension, unsigned int,
84  TInputImage::ImageDimension);
86 
90  typedef TInputImage InputImageType;
91  typedef TMaskImage MaskImageType;
92  typedef typename TInputImage::IndexType IndexType;
93  typedef typename TInputImage::SizeType SizeType;
94  typedef typename TInputImage::OffsetType OffsetType;
95 
96  typedef TOutputImage OutputImageType;
97  typedef typename TOutputImage::RegionType RegionType;
98  typedef typename TOutputImage::IndexType OutputIndexType;
99  typedef typename TOutputImage::SizeType OutputSizeType;
100  typedef typename TOutputImage::OffsetType OutputOffsetType;
101  typedef typename TOutputImage::PixelType OutputImagePixelType;
102 
103  typedef std::list< IndexType > ListType;
104  typedef typename MaskImageType::Pointer MaskImagePointer;
105 
111 
116 
120  itkNewMacro(Self);
121 
128  itkSetMacro(FullyConnected, bool);
129  itkGetConstReferenceMacro(FullyConnected, bool);
130  itkBooleanMacro(FullyConnected);
132 
134  typedef IdentifierType LabelType;
135 
136  // only set after completion
137  itkGetConstReferenceMacro(ObjectCount, LabelType);
138 
139  // Concept checking -- input and output dimensions must be the same
140  itkConceptMacro( SameDimension,
141  ( Concept::SameDimension< itkGetStaticConstMacro(InputImageDimension),
142  itkGetStaticConstMacro(OutputImageDimension) > ) );
143  itkConceptMacro( OutputImagePixelTypeIsInteger, ( Concept::IsInteger< OutputImagePixelType > ) );
144 
145  void SetMaskImage(TMaskImage *mask)
146  {
147  this->SetNthInput( 1, const_cast< TMaskImage * >( mask ) );
148  }
149 
150  const TMaskImage * GetMaskImage() const
151  {
152  return ( static_cast< const TMaskImage * >( this->ProcessObject::GetInput(1) ) );
153  }
154 
157  itkSetMacro(BackgroundValue, OutputImagePixelType);
158  itkGetConstMacro(BackgroundValue, OutputImagePixelType);
159 protected:
161  {
162  m_FullyConnected = false;
163  m_ObjectCount = 0;
165  }
167 
169  void PrintSelf(std::ostream & os, Indent indent) const;
170 
174  void BeforeThreadedGenerateData();
175 
176  void AfterThreadedGenerateData();
177 
178  void ThreadedGenerateData(const RegionType & outputRegionForThread, ThreadIdType threadId);
179 
183  void GenerateInputRequestedRegion();
184 
189  void EnlargeOutputRequestedRegion( DataObject * itkNotUsed(output) );
190 
192 private:
193  ConnectedComponentImageFilter(const Self &); //purposely not implemented
194  void operator=(const Self &); //purposely not implemented
195 
198 
199  // some additional types
200  typedef typename TOutputImage::RegionType::SizeType OutSizeType;
201 
202  // types to support the run length encoding of lines
203  class runLength
204  {
205 public:
206  // run length information - may be a more type safe way of doing this
208  typename TInputImage::IndexType where; // Index of the start of the run
209  LabelType label; // the initial label of the run
210  };
211 
212  typedef std::vector< runLength > lineEncoding;
213 
214  // the map storing lines
215  typedef std::vector< lineEncoding > LineMapType;
216 
217  typedef std::vector< typename TInputImage::OffsetValueType > OffsetVec;
218 
219  // the types to support union-find operations
220  typedef std::vector< LabelType > UnionFindType;
223 
224  // functions to support union-find operations
225  void InitUnion( SizeValueType size )
226  {
227  m_UnionFind = UnionFindType(size + 1);
228  }
229 
230  void InsertSet(const LabelType label);
231 
232  SizeValueType LookupSet(const LabelType label);
233 
234  void LinkLabels(const LabelType lab1, const LabelType lab2);
235 
236  SizeValueType CreateConsecutive();
237 
239  bool CheckNeighbors(const OutputIndexType & A,
240  const OutputIndexType & B);
241 
242  void CompareLines(lineEncoding & current, const lineEncoding & Neighbour);
243 
244  void FillOutput(const LineMapType & LineMap,
245  ProgressReporter & progress);
246 
247  void SetupLineOffsets(OffsetVec & LineOffsets);
248 
249  void Wait()
250  {
251  // use m_NumberOfLabels.size() to get the number of thread used
252  if ( m_NumberOfLabels.size() > 1 )
253  {
254  m_Barrier->Wait();
255  }
256  }
257 
258  typename std::vector< IdentifierType > m_NumberOfLabels;
259  typename std::vector< IdentifierType > m_FirstLineIdToJoin;
260 
262 
263  typename TInputImage::ConstPointer m_Input;
264 #if !defined( CABLE_CONFIGURATION )
266 #endif
267 };
268 } // end namespace itk
269 
270 #ifndef ITK_MANUAL_INSTANTIATION
271 #if !defined( CABLE_CONFIGURATION )
272 #include "itkConnectedComponentImageFilter.hxx"
273 #endif
274 #endif
275 
276 #endif
277