ITK  4.3.0
Insight Segmentation and Registration Toolkit
itkWatershedSegmenter.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 __itkWatershedSegmenter_h
19 #define __itkWatershedSegmenter_h
20 
21 
22 #include "itkWatershedBoundary.h"
24 #include "itkEquivalencyTable.h"
25 
26 namespace itk
27 {
28 namespace watershed
29 {
87 template< class TInputImage >
88 class ITK_EXPORT Segmenter:
89  public ProcessObject
90 {
91 public:
93  typedef Segmenter Self;
94 
96  typedef TInputImage InputImageType;
97  itkStaticConstMacro(ImageDimension, unsigned int,
98  TInputImage::ImageDimension);
99 
102  typedef typename InputImageType::RegionType ImageRegionType;
103  typedef typename InputImageType::PixelType InputPixelType;
110 
116  itkNewMacro(Self);
117  itkTypeMacro(WatershedSegmenter, ProcessObject);
119 
121  typedef typename InputImageType::Pointer InputImageTypePointer;
125 
128 
130  static short NULL_FLOW;
131 
133  InputImageType * GetInputImage(void)
134  {
135  return itkDynamicCastInDebugMode< InputImageType * >
136  ( this->ProcessObject::GetInput(0) );
137  }
138 
139  void SetInputImage(InputImageType *img)
140  { this->ProcessObject::SetNthInput(0, img); }
141 
144  OutputImageType * GetOutputImage(void)
145  {
146  return itkDynamicCastInDebugMode< OutputImageType * >
147  ( this->ProcessObject::GetOutput(0) );
148  }
149 
150  void SetOutputImage(OutputImageType *img)
151  { this->ProcessObject::SetNthOutput(0, img); }
152 
155  SegmentTableType * GetSegmentTable(void)
156  {
157  return itkDynamicCastInDebugMode< SegmentTableType * >
158  ( this->ProcessObject::GetOutput(1) );
159  }
160 
161  void SetSegmentTable(SegmentTableType *s)
162  { this->ProcessObject::SetNthOutput(1, s); }
163 
166  BoundaryType * GetBoundary(void)
167  {
168  return itkDynamicCastInDebugMode< BoundaryType * >
169  ( this->ProcessObject::GetOutput(2) );
170  }
171 
172  void SetBoundary(BoundaryType *b)
173  { this->ProcessObject::SetNthOutput(2, b); }
174 
176  void GenerateData();
177 
184  void SetLargestPossibleRegion(ImageRegionType reg)
185  {
186  if ( reg == m_LargestPossibleRegion ) { return; }
187  m_LargestPossibleRegion = reg;
188  this->Modified();
189  }
191 
192  ImageRegionType GetLargestPossibleRegion() const
193  { return m_LargestPossibleRegion; }
194 
197  static void RelabelImage(OutputImageTypePointer,
198  ImageRegionType,
200 
203  using Superclass::MakeOutput;
204  virtual DataObjectPointer MakeOutput(DataObjectPointerArraySizeType idx);
205 
208  itkSetMacro(CurrentLabel, IdentifierType);
209  itkGetConstMacro(CurrentLabel, IdentifierType);
211 
222  itkSetClampMacro(Threshold, double, 0.0, 1.0);
223  itkGetConstMacro(Threshold, double);
225 
229  itkSetMacro(DoBoundaryAnalysis, bool);
230  itkGetConstMacro(DoBoundaryAnalysis, bool);
232 
237  itkGetConstMacro(SortEdgeLists, bool);
238  itkSetMacro(SortEdgeLists, bool);
240 
241 protected:
244  struct flat_region_t {
247  // InputPixelType bounds_max; // <-- may not be necc.
250  flat_region_t():is_on_boundary(false) {}
251  };
252 
254  typedef itksys::hash_map< IdentifierType, flat_region_t, itksys::hash< IdentifierType > >
256 
257  struct connectivity_t {
258  unsigned int size;
259  unsigned int *index;
260  typename InputImageType::OffsetType * direction;
261  };
262 
267  typedef itksys::hash_map< IdentifierType, InputPixelType, itksys::hash< IdentifierType >
269 
270  typedef itksys::hash_map< IdentifierType, edge_table_t, itksys::hash< IdentifierType >
272 
273  Segmenter();
274  Segmenter(const Self &) {}
275  virtual ~Segmenter();
276  void PrintSelf(std::ostream & os, Indent indent) const;
277 
278  void operator=(const Self &) {}
279 
282  virtual void GenerateConnectivity();
283 
287  void GenerateInputRequestedRegion();
288 
289  void GenerateOutputRequestedRegion(DataObject *output);
290 
291  void UpdateOutputInformation();
292 
295  void InitializeBoundary();
296 
300  void AnalyzeBoundaryFlow(InputImageTypePointer,
301  flat_region_table_t &,
302  InputPixelType);
303 
307  void BuildRetainingWall(InputImageTypePointer,
308  ImageRegionType, InputPixelType);
309 
312  void LabelMinima(InputImageTypePointer,
313  ImageRegionType, flat_region_table_t &,
314  InputPixelType);
315 
319  void GradientDescent(InputImageTypePointer, ImageRegionType);
320 
323  void DescendFlatRegions(flat_region_table_t &, ImageRegionType);
324 
327  void UpdateSegmentTable(InputImageTypePointer, ImageRegionType);
328 
332  void CollectBoundaryInformation(flat_region_table_t &);
333 
339  static void Threshold(InputImageTypePointer destination,
340  InputImageTypePointer source,
341  const ImageRegionType source_region,
342  const ImageRegionType destination_region,
343  InputPixelType threshold);
344 
346  static void MinMax(InputImageTypePointer img,
347  ImageRegionType region,
348  InputPixelType & min,
349  InputPixelType & max);
350 
352  static void MergeFlatRegions(flat_region_table_t &, EquivalencyTable::Pointer);
353 
355  static void SetInputImageValues(InputImageTypePointer img,
356  const ImageRegionType region,
357  InputPixelType value);
358 
359  static void SetOutputImageValues(OutputImageTypePointer img,
360  const ImageRegionType region,
361  IdentifierType value);
362 
364  // bool CheckLabeledBoundaries();
365 
368  connectivity_t m_Connectivity;
369 
370 private:
372  // void PrintFlatRegions(flat_region_table_t &t);
373 
378 
381  double m_Threshold;
384 };
385 } // end namespace watershed
386 } // end namespace itk
387 
388 #ifndef ITK_MANUAL_INSTANTIATION
389 #include "itkWatershedSegmenter.hxx"
390 #endif
391 
392 #endif
393