OpenWalnut  1.5.0dev
WSegmentationAlgoRegionGrowingConfidenceConnected.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 WSEGMENTATIONALGOREGIONGROWINGCONFIDENCECONNECTED_H
26 #define WSEGMENTATIONALGOREGIONGROWINGCONFIDENCECONNECTED_H
27 
28 #ifdef OW_USE_ITK
29 
30 #include <memory>
31 #include <string>
32 #include <vector>
33 
34 #include "WSegmentationAlgo.h"
35 #include "core/dataHandler/WITKImageConversion.h"
36 #include "core/graphicsEngine/WROIBox.h"
37 #include "core/kernel/WKernel.h"
38 #include "core/kernel/WROIManager.h"
39 #include "itkCastImageFilter.h"
40 #include "itkConfidenceConnectedImageFilter.h"
41 #include "itkGradientAnisotropicDiffusionImageFilter.h"
42 #include "itkImage.h"
43 
44 /**
45  * Confidence connected region growing segmentation.
46  * \class WSegmentationAlgoRegionGrowingConfidenceConnected
47  */
48 class WSegmentationAlgoRegionGrowingConfidenceConnected : public WSegmentationAlgo
49 {
50 public:
51  /**
52  * Standard constructor.
53  */
54  WSegmentationAlgoRegionGrowingConfidenceConnected();
55 
56  /**
57  * Destructor.
58  */
59  virtual ~WSegmentationAlgoRegionGrowingConfidenceConnected();
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 number of iteration in the smoothing filter.
102  WPropInt m_smoothingIter;
103 
104  //! The conductance parameter for the anisotropic smoothing.
105  WPropDouble m_conductance;
106 
107  //! The number of iteration in region growing.
108  WPropInt m_regionGrowingIterations;
109 
110  //! The initial neighborhood radius.
111  WPropInt m_neighborhoodRadius;
112 
113  //! The multiplier for the variance.
114  WPropDouble m_multiplier;
115 };
116 
117 template< typename T >
118 WSegmentationAlgo::DataSetPtr WSegmentationAlgoRegionGrowingConfidenceConnected::operator() ( WValueSet< T > const* ) const
119 {
120  typedef itk::Image< T, 3 > ImgType;
121  typedef itk::Image< double, 3 > RealType;
122  typedef itk::Image< uint64_t, 3 > LabelType; // this might be a problem on 32-bit systems
123  typedef itk::Image< float, 3 > FinalType;
124 
125  typedef itk::GradientAnisotropicDiffusionImageFilter< ImgType, RealType > SmoothingType;
126  typedef itk::CastImageFilter< LabelType, FinalType > CastFilter;
127  typedef itk::ConfidenceConnectedImageFilter< RealType, LabelType > RegionGrowingFilter;
128 
129  typename ImgType::Pointer image = makeImageFromDataSet< T >( m_dataSet );
130  typename SmoothingType::Pointer smoothing = SmoothingType::New();
131  typename CastFilter::Pointer cast = CastFilter::New();
132  typename RegionGrowingFilter::Pointer regionGrowing = RegionGrowingFilter::New();
133 
134  std::vector< osg::ref_ptr< WROI > > rois = WKernel::getRunningKernel()->getRoiManager()->getRois();
135  itk::Index< 3 > i;
136 
137  WROIBox* box = NULL;
138 
139  unsigned int k = 0;
140  while( !box && k < rois.size() )
141  {
142  box = dynamic_cast< WROIBox* >( rois[ k ].get() );
143  ++k;
144  }
145 
146  if( box )
147  {
148  std::shared_ptr< WGridRegular3D > grid = std::dynamic_pointer_cast< WGridRegular3D >( m_dataSet->getGrid() );
149  WVector3d v = 0.5 * ( box->getMinPos() + box->getMaxPos() );
150  WValue< int > voxel = grid->getVoxelCoord( v );
151 
152  i[ 0 ] = static_cast< int32_t >( voxel[ 0 ] );
153  i[ 1 ] = static_cast< int32_t >( voxel[ 1 ] );
154  i[ 2 ] = static_cast< int32_t >( voxel[ 2 ] );
155 
156  smoothing->SetNumberOfIterations( m_smoothingIter->get( true ) );
157  smoothing->SetTimeStep( 0.0625 );
158  smoothing->SetConductanceParameter( m_conductance->get( true ) );
159  smoothing->SetInput( image );
160  regionGrowing->SetInput( smoothing->GetOutput() );
161  regionGrowing->SetMultiplier( m_multiplier->get( true ) );
162  regionGrowing->SetNumberOfIterations( m_regionGrowingIterations->get( true ) );
163  regionGrowing->SetReplaceValue( 255.0f );
164  regionGrowing->SetInitialNeighborhoodRadius( m_neighborhoodRadius->get( true ) );
165  regionGrowing->SetSeed( i );
166  cast->SetInput( regionGrowing->GetOutput() );
167  try
168  {
169  cast->Update();
170  }
171  catch( ... )
172  {
173  throw WException( "Problem in Region Growing Segmentation" );
174  }
175  return makeDataSetFromImage< float >( cast->GetOutput() );
176  }
177  else
178  {
179  return DataSetPtr();
180  }
181 }
182 
183 #endif // OW_USE_ITK
184 
185 #endif // WSEGMENTATIONALGOREGIONGROWINGCONFIDENCECONNECTED_H
Basic exception handler.
Definition: WException.h:39
static WKernel * getRunningKernel()
Returns pointer to the currently running kernel.
Definition: WKernel.cpp:117
std::shared_ptr< WROIManager > getRoiManager()
get for roi manager
Definition: WKernel.cpp:209
A box representing a region of interest.
Definition: WROIBox.h:49
WPosition getMinPos() const
Get the corner of the box that has minimal x, y and z values.
Definition: WROIBox.cpp:103
WPosition getMaxPos() const
Get the corner of the box that has maximal x, y and z values.
Definition: WROIBox.cpp:108
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
Base class for all higher level values like tensors, vectors, matrices and so on.
Definition: WValue.h:41