OpenWalnut  1.5.0dev
WSegmentationAlgoWatershed.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WSEGMENTATIONALGOWATERSHED_H
26 #define WSEGMENTATIONALGOWATERSHED_H
27 
28 #ifdef OW_USE_ITK
29 
30 #include <string>
31 
32 #include "itkImage.h"
33 #include "itkWatershedImageFilter.h"
34 #include "itkGradientAnisotropicDiffusionImageFilter.h"
35 #include "itkCastImageFilter.h"
36 #include "itkGradientMagnitudeImageFilter.h"
37 
38 #include "core/dataHandler/WITKImageConversion.h"
39 
40 #include "WSegmentationAlgo.h"
41 
42 /**
43  * Watershed segmentation. Fills a gradient magnitude image with water up to
44  * a certain level. The 'basins' are the segmented regions.
45  *
46  * \class WSegmentationAlgoWatershed
47  */
48 class WSegmentationAlgoWatershed : public WSegmentationAlgo
49 {
50 public:
51  /**
52  * Standard constructor.
53  */
54  WSegmentationAlgoWatershed();
55 
56  /**
57  * Destructor.
58  */
59  virtual ~WSegmentationAlgoWatershed();
60 
61  /**
62  * Return the name of this algorithm.
63  * \return The name.
64  */
65  virtual std::string getName();
66 
67  /**
68  * Return a description of this algorithm.
69  * \return A description.
70  */
71  virtual std::string getDescription();
72 
73  /**
74  * Checks if any properties were changed.
75  * \return True, iff any properties were changed.
76  */
77  virtual bool propChanged();
78 
79  /**
80  * Implements the operation.
81  *
82  * \tparam The type of the values in the dataset's valueset.
83  * \param valueset The dataset's valueset.
84  * \return The resulting dataset.
85  */
86  template< typename T >
87  DataSetPtr operator() ( WValueSet< T > const* valueset ) const;
88 
89 private:
90  /**
91  * Initializes the algorithm's properties.
92  */
93  virtual void properties();
94 
95  /**
96  * A virtual function that calls the correct segmentation operation.
97  * \return The resulting dataset.
98  */
99  virtual DataSetPtr applyOperation();
100 
101  //! The threshold in %.
102  WPropDouble m_threshold;
103 
104  //! The water level in %.
105  WPropDouble m_level;
106 
107  //! The number of iteration in the smoothing filter.
108  WPropInt m_iter;
109 
110  //! The conductance parameter for the anisotropic smoothing.
111  WPropDouble m_conductance;
112 };
113 
114 template< typename T >
115 WSegmentationAlgo::DataSetPtr WSegmentationAlgoWatershed::operator() ( WValueSet< T > const* ) const
116 {
117  typedef itk::Image< T, 3 > ImgType;
118  typedef itk::Image< double, 3 > RealType;
119  typedef itk::Image< uint64_t, 3 > LabelType; // this might be a problem on 32-bit systems
120  typedef itk::Image< float, 3 > FinalType;
121 
122  typedef itk::GradientAnisotropicDiffusionImageFilter< ImgType, RealType > SmoothingType;
123  typedef itk::CastImageFilter< LabelType, FinalType > CastFilter;
124  typedef itk::GradientMagnitudeImageFilter< RealType, RealType > GradFilter;
125  typedef itk::WatershedImageFilter< RealType > WaterFilter;
126 
127  typename ImgType::Pointer image = makeImageFromDataSet< T >( m_dataSet );
128  typename SmoothingType::Pointer smoothing = SmoothingType::New();
129  typename CastFilter::Pointer cast = CastFilter::New();
130  typename GradFilter::Pointer gradientMagnitude = GradFilter::New();
131  typename WaterFilter::Pointer watershed = WaterFilter::New();
132 
133  smoothing->SetNumberOfIterations( m_iter->get( true ) );
134  smoothing->SetTimeStep( 0.0625 );
135  smoothing->SetConductanceParameter( m_conductance->get( true ) );
136  smoothing->SetInput( image );
137  gradientMagnitude->SetInput( smoothing->GetOutput() );
138  watershed->SetInput( gradientMagnitude->GetOutput() );
139  watershed->SetLevel( m_level->get( true ) / 100.0 );
140  watershed->SetThreshold( m_threshold->get( true ) / 100.0 );
141  cast->SetInput( watershed->GetOutput() );
142  cast->Update();
143  return makeDataSetFromImage< float >( cast->GetOutput() );
144 }
145 
146 #endif // OW_USE_ITK
147 
148 #endif // WSEGMENTATIONALGOWATERSHED_H
A base class for segmentation alorithms.
virtual void properties()=0
Initialize your algorithms properties here.
virtual std::string getName()=0
Return the name of this algorithm.
std::shared_ptr< WDataSetScalar > DataSetPtr
A conveniant typedef.
virtual DataSetPtr applyOperation()=0
A virtual function that calls the correct segmentation operation.
virtual bool propChanged()=0
Checks if any properties were changed.
virtual std::string getDescription()=0
Return a description of this algorithm.
Base Class for all value set types.
Definition: WValueSet.h:47