]> Creatis software - FrontAlgorithms.git/blob - plugins/RegionGrowFunctors/BinaryThreshold.cxx
3a4da319d5356fec385fcabb4857ed9fbbcdd200
[FrontAlgorithms.git] / plugins / RegionGrowFunctors / BinaryThreshold.cxx
1 #include <RegionGrowFunctors/BinaryThreshold.h>
2 #include <cpInstances/DataObjects/Image.h>
3
4 #include <fpa/Image/Functors/RegionGrow/BinaryThreshold.h>
5 #include <itkConstNeighborhoodIterator.h>
6 #include <vtkPolyData.h>
7
8 // -------------------------------------------------------------------------
9 fpaPluginsRegionGrowFunctors::BinaryThreshold::
10 BinaryThreshold( )
11   : Superclass( )
12 {
13   typedef cpPlugins::Pipeline::DataObject _TData;
14   typedef cpInstances::DataObjects::Image _TImage;
15
16   this->_ConfigureInput< _TImage >( "Input", true, false );
17   this->_ConfigureInput< _TData >( "Seeds", false, false );
18   this->_ConfigureOutput< _TData >( "Output" );
19
20   this->m_Parameters.ConfigureAsUint( "Radius", 1 );
21   this->m_Parameters.ConfigureAsReal( "Lower", 0 );
22   this->m_Parameters.ConfigureAsReal( "Upper", 0 );
23 }
24
25 // -------------------------------------------------------------------------
26 fpaPluginsRegionGrowFunctors::BinaryThreshold::
27 ~BinaryThreshold( )
28 {
29 }
30
31 // -------------------------------------------------------------------------
32 void fpaPluginsRegionGrowFunctors::BinaryThreshold::
33 _GenerateData( )
34 {
35   auto o = this->GetInputData( "Input" );
36   cpPlugins_Demangle_Image_ScalarPixels_AllDims_1( o, _GD0 )
37     this->_Error( "Invalid input image." );
38 }
39
40 // -------------------------------------------------------------------------
41 template< class _TImage >
42 void fpaPluginsRegionGrowFunctors::BinaryThreshold::
43 _GD0( _TImage* image )
44 {
45   /* TODO
46      typedef itk::ConstNeighborhoodIterator< _TImage > _TInIt;
47      typedef
48      fpa::Image::Functors::RegionGrow::BinaryThreshold< _TImage >
49      _TFunctor;
50      auto out = this->GetOutput( "Output" );
51      auto f = out->GetITK< _TFunctor >( );
52      if( f == NULL )
53      {
54      typename _TFunctor::Pointer ptr_f = _TFunctor::New( );
55      f = ptr_f.GetPointer( );
56      out->SetITK( f );
57
58      } // fi
59
60      // Compute thresholds from seeds
61      auto seeds = this->GetInputData< vtkPolyData >( "Seeds" );
62      if( seeds != NULL )
63      {
64      std::vector< typename _TImage::IndexType > indices;
65      typename _TImage::PointType pnt;
66      typename _TImage::IndexType idx;
67      unsigned int dim =
68      ( _TImage::ImageDimension < 3 )? _TImage::ImageDimension: 3;
69
70      for( int i = 0; i < seeds->GetNumberOfPoints( ); ++i )
71      {
72      double buf[ 3 ];
73      seeds->GetPoint( i, buf );
74      pnt.Fill( 0 );
75      for( unsigned int d = 0; d < dim; ++d )
76      pnt[ d ] = buf[ d ];
77      if( image->TransformPhysicalPointToIndex( pnt, idx ) )
78      indices.push_back( idx );
79
80      } // rof
81
82      typename _TImage::SizeType r;
83      r.Fill( this->m_Parameters.GetUint( "Radius" ) );
84
85      _TInIt iIt( r, image, image->GetRequestedRegion( ) );
86      double v_s1 = double( 0 );
87      double v_s2 = double( 0 );
88      double v_n = double( 0 );
89      double v_min = std::numeric_limits< double >::max( );
90      double v_max = -v_min;
91      for( auto idxIt = indices.begin( ); idxIt != indices.end( ); ++idxIt )
92      {
93      iIt.SetLocation( *idxIt );
94      for( unsigned int i = 0; i < iIt.Size( ); ++i )
95      {
96      double v = double( iIt.GetPixel( i ) );
97      v_s1 += v;
98      v_s2 += v * v;
99      v_n += double( 1 );
100      v_min = ( v < v_min )? v: v_min;
101      v_max = ( v_max < v )? v: v_max;
102
103      } // rof
104
105      } // rof
106      double m = v_s1 / v_n;
107      double s = ( v_s2 - ( ( v_s1 * v_s1 ) / v_n ) ) / ( v_n - double( 1 ) );
108      s = std::sqrt( s );
109      v_min = m - s;
110      v_max = m + s;
111      f->SetLower( v_min );
112      f->SetUpper( v_max );
113      this->m_Parameters.SetReal( "Lower", f->GetLower( ) );
114      this->m_Parameters.SetReal( "Upper", f->GetUpper( ) );
115      }
116      else
117      {
118      f->SetLower( this->m_Parameters.GetReal( "Lower" ) );
119      f->SetUpper( this->m_Parameters.GetReal( "Upper" ) );
120
121      } // fi
122   */
123 }
124
125 // eof - $RCSfile$