ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkBinaryImageToLabelMapFilter.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 #ifndef __itkBinaryImageToLabelMapFilter_h
00019 #define __itkBinaryImageToLabelMapFilter_h
00020 
00021 #include "itkImageToImageFilter.h"
00022 #include <vector>
00023 #include <map>
00024 #include "itkProgressReporter.h"
00025 #include "itkBarrier.h"
00026 #include "itkLabelMap.h"
00027 #include "itkLabelObject.h"
00028 
00029 namespace itk
00030 {
00058 template< class TInputImage,
00059           class TOutputImage =
00060             LabelMap< LabelObject< SizeValueType, ::itk::GetImageDimension< TInputImage >::ImageDimension > > >
00061 class ITK_EXPORT BinaryImageToLabelMapFilter:
00062   public ImageToImageFilter< TInputImage, TOutputImage >
00063 {
00064 public:
00068   typedef BinaryImageToLabelMapFilter                     Self;
00069   typedef ImageToImageFilter< TInputImage, TOutputImage > Superclass;
00070   typedef SmartPointer< Self >                            Pointer;
00071   typedef SmartPointer< const Self >                      ConstPointer;
00072 
00076   itkNewMacro(Self);
00077 
00081   itkTypeMacro(BinaryImageToLabelMapFilter, ImageToImageFilter);
00082 
00086   typedef typename Superclass::InputImagePointer InputImagePointer;
00087 
00092   typedef typename TOutputImage::PixelType      OutputPixelType;
00093   typedef typename TInputImage::PixelType       InputPixelType;
00094   typedef typename TInputImage::SizeValueType   SizeValueType;
00095   typedef typename TInputImage::OffsetValueType OffsetValueType;
00096   itkStaticConstMacro(ImageDimension, unsigned int, TOutputImage::ImageDimension);
00097   itkStaticConstMacro(OutputImageDimension, unsigned int, TOutputImage::ImageDimension);
00098   itkStaticConstMacro(InputImageDimension, unsigned int, TInputImage::ImageDimension);
00100 
00101   typedef SizeValueType   LabelType;
00102 
00106   typedef TInputImage                      InputImageType;
00107   typedef typename TInputImage::IndexType  IndexType;
00108   typedef typename TInputImage::SizeType   SizeType;
00109   typedef typename TInputImage::OffsetType OffsetType;
00110 
00111   typedef TOutputImage                      OutputImageType;
00112   typedef typename TOutputImage::RegionType RegionType;
00113   typedef typename TOutputImage::IndexType  OutputIndexType;
00114   typedef typename TOutputImage::SizeType   OutputSizeType;
00115   typedef typename TOutputImage::OffsetType OutputOffsetType;
00116   typedef typename TOutputImage::PixelType  OutputImagePixelType;
00117 
00118   typedef std::list< IndexType > ListType;
00119 
00126   itkSetMacro(FullyConnected, bool);
00127   itkGetConstReferenceMacro(FullyConnected, bool);
00128   itkBooleanMacro(FullyConnected);
00130 
00131   // only set after completion
00132   itkGetConstReferenceMacro(NumberOfObjects, SizeValueType);
00133 
00138   itkSetMacro(OutputBackgroundValue, OutputPixelType);
00139   itkGetConstMacro(OutputBackgroundValue, OutputPixelType);
00141 
00146   itkSetMacro(InputForegroundValue, InputPixelType);
00147   itkGetConstMacro(InputForegroundValue, InputPixelType);
00149 
00150 #ifdef ITK_USE_CONCEPT_CHECKING
00151   // Concept checking -- input and output dimensions must be the same
00152   itkConceptMacro( SameDimension,
00153                    ( Concept::SameDimension< itkGetStaticConstMacro(InputImageDimension),
00154                                              itkGetStaticConstMacro(OutputImageDimension) > ) );
00155 #endif
00156 protected:
00157   BinaryImageToLabelMapFilter();
00158   virtual ~BinaryImageToLabelMapFilter() {}
00159   void PrintSelf(std::ostream & os, Indent indent) const;
00160 
00164   void BeforeThreadedGenerateData();
00165 
00166   void AfterThreadedGenerateData();
00167 
00168   void ThreadedGenerateData(const RegionType & outputRegionForThread, ThreadIdType threadId);
00169 
00173   void GenerateInputRequestedRegion();
00174 
00179   void EnlargeOutputRequestedRegion( DataObject *itkNotUsed(output) );
00180 private:
00181   BinaryImageToLabelMapFilter(const Self &); //purposely not implemented
00182   void operator=(const Self &);              //purposely not implemented
00184 
00185   // some additional types
00186   typedef typename TOutputImage::RegionType::SizeType OutSizeType;
00187 
00188   // types to support the run length encoding of lines
00189   class runLength
00190   {
00191   public:
00192     // run length information - may be a more type safe way of doing this
00193     SizeValueType length;
00194     typename InputImageType::IndexType where; // Index of the start of the run
00195     LabelType label;                  // the initial label of the run
00196   };
00197 
00198   typedef std::vector< runLength > lineEncoding;
00199 
00200   // the map storing lines
00201   typedef std::vector< lineEncoding > LineMapType;
00202 
00203   typedef std::vector< OffsetValueType > OffsetVectorType;
00204 
00205   // the types to support union-find operations
00206   typedef std::vector< LabelType > UnionFindType;
00207   UnionFindType m_UnionFind;
00208   UnionFindType m_Consecutive;
00209   // functions to support union-find operations
00210   void InitUnion(const LabelType size)
00211   {
00212     m_UnionFind = UnionFindType(size + 1);
00213   }
00214 
00215   void InsertSet(const LabelType label);
00216 
00217   LabelType LookupSet(const LabelType label);
00218 
00219   void LinkLabels(const LabelType lab1, const LabelType lab2);
00220 
00221   LabelType CreateConsecutive();
00222 
00224   bool CheckNeighbors(const OutputIndexType & A,
00225                       const OutputIndexType & B);
00226 
00227   void CompareLines(lineEncoding & current, const lineEncoding & Neighbour);
00228 
00229   void FillOutput(const LineMapType & LineMap,
00230                   ProgressReporter & progress);
00231 
00232   void SetupLineOffsets(OffsetVectorType & LineOffsets);
00233 
00234   void Wait()
00235   {
00236     // use m_NumberOfLabels.size() to get the number of thread used
00237     if ( m_NumberOfLabels.size() > 1 )
00238       {
00239       m_Barrier->Wait();
00240       }
00241   }
00242 
00243   OutputPixelType m_OutputBackgroundValue;
00244   InputPixelType  m_InputForegroundValue;
00245 
00246   SizeValueType m_NumberOfObjects;
00247 
00248   bool m_FullyConnected;
00249 
00250   typename std::vector< SizeValueType >   m_NumberOfLabels;
00251   typename std::vector< SizeValueType >   m_FirstLineIdToJoin;
00252 
00253   typename Barrier::Pointer m_Barrier;
00254 
00255 #if !defined( CABLE_CONFIGURATION )
00256   LineMapType m_LineMap;
00257 #endif
00258 };
00259 } // end namespace itk
00260 
00261 #ifndef ITK_MANUAL_INSTANTIATION
00262 #if !defined( CABLE_CONFIGURATION )
00263 #include "itkBinaryImageToLabelMapFilter.hxx"
00264 #endif
00265 #endif
00266 
00267 #endif
00268