ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkWatershedSegmenter.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 __itkWatershedSegmenter_h
00019 #define __itkWatershedSegmenter_h
00020 
00021 
00022 #include "itkWatershedBoundary.h"
00023 #include "itkWatershedSegmentTable.h"
00024 #include "itkEquivalencyTable.h"
00025 
00026 namespace itk
00027 {
00028 namespace watershed
00029 {
00087 template< class TInputImage >
00088 class ITK_EXPORT Segmenter:
00089   public ProcessObject
00090 {
00091 public:
00093   typedef Segmenter Self;
00094 
00096   typedef TInputImage InputImageType;
00097   itkStaticConstMacro(ImageDimension, unsigned int,
00098                       TInputImage::ImageDimension);
00099 
00100   typedef Image< IdentifierType, itkGetStaticConstMacro(ImageDimension) >
00101   OutputImageType;
00102   typedef typename InputImageType::RegionType ImageRegionType;
00103   typedef typename InputImageType::PixelType  InputPixelType;
00104   typedef Boundary< InputPixelType, itkGetStaticConstMacro(ImageDimension) >
00105   BoundaryType;
00106   typedef typename BoundaryType::IndexType         BoundaryIndexType;
00107   typedef typename BoundaryType::FlatHashValueType BoundaryFlatHashValueType;
00108   typedef SegmentTable< InputPixelType >           SegmentTableType;
00109   typedef DataObject::Pointer                      DataObjectPointer;
00110 
00113   typedef ProcessObject              Superclass;
00114   typedef SmartPointer< Self >       Pointer;
00115   typedef SmartPointer< const Self > ConstPointer;
00116   itkNewMacro(Self);
00117   itkTypeMacro(WatershedSegmenter, ProcessObject);
00119 
00121   typedef typename InputImageType::Pointer   InputImageTypePointer;
00122   typedef typename OutputImageType::Pointer  OutputImageTypePointer;
00123   typedef typename SegmentTableType::Pointer SegmentTableTypePointer;
00124   typedef typename BoundaryType::Pointer     BoundaryTypePointer;
00125 
00127   static IdentifierType NULL_LABEL;
00128 
00130   static short NULL_FLOW;
00131 
00133   InputImageType * GetInputImage(void)
00134   {
00135     return static_cast< InputImageType * >
00136            ( this->ProcessObject::GetInput(0) );
00137   }
00138 
00139   void SetInputImage(InputImageType *img)
00140   {  this->ProcessObject::SetNthInput(0, img); }
00141 
00144   OutputImageType * GetOutputImage(void)
00145   {
00146     return static_cast< OutputImageType * >
00147            ( this->ProcessObject::GetOutput(0) );
00148   }
00149 
00150   void SetOutputImage(OutputImageType *img)
00151   { this->ProcessObject::SetNthOutput(0, img);    }
00152 
00155   SegmentTableType * GetSegmentTable(void)
00156   {
00157     return static_cast< SegmentTableType * >
00158            ( this->ProcessObject::GetOutput(1) );
00159   }
00160 
00161   void SetSegmentTable(SegmentTableType *s)
00162   { this->ProcessObject::SetNthOutput(1, s); }
00163 
00166   BoundaryType * GetBoundary(void)
00167   {
00168     return static_cast< BoundaryType * >
00169            ( this->ProcessObject::GetOutput(2) );
00170   }
00171 
00172   void SetBoundary(BoundaryType *b)
00173   { this->ProcessObject::SetNthOutput(2, b); }
00174 
00176   void GenerateData();
00177 
00184   void SetLargestPossibleRegion(ImageRegionType reg)
00185   {
00186     if ( reg == m_LargestPossibleRegion ) { return; }
00187     m_LargestPossibleRegion = reg;
00188     this->Modified();
00189   }
00191 
00192   ImageRegionType GetLargestPossibleRegion() const
00193   { return m_LargestPossibleRegion; }
00194 
00197   static void RelabelImage(OutputImageTypePointer,
00198                            ImageRegionType,
00199                            EquivalencyTable::Pointer);
00200 
00202   typedef ProcessObject::DataObjectPointerArraySizeType DataObjectPointerArraySizeType;
00203   using Superclass::MakeOutput;
00204   virtual DataObjectPointer MakeOutput(DataObjectPointerArraySizeType idx);
00205 
00208   itkSetMacro(CurrentLabel, IdentifierType);
00209   itkGetConstMacro(CurrentLabel, IdentifierType);
00211 
00222   itkSetClampMacro(Threshold, double, 0.0, 1.0);
00223   itkGetConstMacro(Threshold, double);
00225 
00229   itkSetMacro(DoBoundaryAnalysis, bool);
00230   itkGetConstMacro(DoBoundaryAnalysis, bool);
00232 
00237   itkGetConstMacro(SortEdgeLists, bool);
00238   itkSetMacro(SortEdgeLists, bool);
00239 protected:
00240 
00243   struct flat_region_t {
00244     IdentifierType *min_label_ptr;
00245     InputPixelType bounds_min;
00246     //    InputPixelType  bounds_max; // <-- may not be necc.
00247     InputPixelType value;
00248     bool is_on_boundary;
00249     flat_region_t():is_on_boundary(false) {}
00250   };
00251 
00253   typedef itksys::hash_map< IdentifierType, flat_region_t, itksys::hash< IdentifierType > >
00254   flat_region_table_t;
00255 
00256   struct connectivity_t {
00257     unsigned int size;
00258     unsigned int *index;
00259     typename InputImageType::OffsetType * direction;
00260   };
00261 
00266   typedef itksys::hash_map< IdentifierType, InputPixelType, itksys::hash< IdentifierType >
00267                             > edge_table_t;
00268 
00269   typedef itksys::hash_map< IdentifierType, edge_table_t, itksys::hash< IdentifierType >
00270                          > edge_table_hash_t;
00271 
00272   Segmenter();
00273   Segmenter(const Self &) {}
00274   virtual ~Segmenter();
00275   void PrintSelf(std::ostream & os, Indent indent) const;
00276 
00277   void operator=(const Self &) {}
00278 
00281   virtual void GenerateConnectivity();
00282 
00286   void GenerateInputRequestedRegion();
00287 
00288   void GenerateOutputRequestedRegion(DataObject *output);
00289 
00290   void UpdateOutputInformation();
00291 
00294   void InitializeBoundary();
00295 
00299   void AnalyzeBoundaryFlow(InputImageTypePointer,
00300                            flat_region_table_t &,
00301                            InputPixelType);
00302 
00306   void BuildRetainingWall(InputImageTypePointer,
00307                           ImageRegionType, InputPixelType);
00308 
00311   void LabelMinima(InputImageTypePointer,
00312                    ImageRegionType, flat_region_table_t &,
00313                    InputPixelType);
00314 
00318   void GradientDescent(InputImageTypePointer, ImageRegionType);
00319 
00322   void DescendFlatRegions(flat_region_table_t &, ImageRegionType);
00323 
00326   void UpdateSegmentTable(InputImageTypePointer, ImageRegionType);
00327 
00331   void CollectBoundaryInformation(flat_region_table_t &);
00332 
00338   static void Threshold(InputImageTypePointer destination,
00339                         InputImageTypePointer source,
00340                         const ImageRegionType source_region,
00341                         const ImageRegionType destination_region,
00342                         InputPixelType threshold);
00343 
00345   static void MinMax(InputImageTypePointer img,
00346                      ImageRegionType region,
00347                      InputPixelType & min,
00348                      InputPixelType & max);
00349 
00351   static void MergeFlatRegions(flat_region_table_t &, EquivalencyTable::Pointer);
00352 
00354   static void SetInputImageValues(InputImageTypePointer img,
00355                                   const ImageRegionType region,
00356                                   InputPixelType value);
00357 
00358   static void SetOutputImageValues(OutputImageTypePointer img,
00359                                    const ImageRegionType region,
00360                                    IdentifierType value);
00361 
00363   //  bool CheckLabeledBoundaries();
00364 
00367   connectivity_t m_Connectivity;
00368 private:
00369 
00371   //  void PrintFlatRegions(flat_region_table_t &t);
00372 
00376   ImageRegionType m_LargestPossibleRegion;
00377 
00378   bool            m_SortEdgeLists;
00379   bool            m_DoBoundaryAnalysis;
00380   double          m_Threshold;
00381   double          m_MaximumFloodLevel;
00382   IdentifierType  m_CurrentLabel;
00383 };
00384 } // end namespace watershed
00385 } // end namespace itk
00386 
00387 #ifndef ITK_MANUAL_INSTANTIATION
00388 #include "itkWatershedSegmenter.hxx"
00389 #endif
00390 
00391 #endif
00392