]> Creatis software - FrontAlgorithms.git/blob - plugins/RegionGrowFunctors/BinaryThreshold.cxx
9ed2db673500df1c8f59a63c3451f30ae5734aa6
[FrontAlgorithms.git] / plugins / RegionGrowFunctors / BinaryThreshold.cxx
1 #include <RegionGrowFunctors/BinaryThreshold.h>
2 #include <cpInstances/DataObjects/Image.h>
3
4 #include <fpa/Image/Functors/RegionGrowBinaryThreshold.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( "LowerThreshold", 0 );
22   this->m_Parameters.ConfigureAsReal( "UpperThreshold", 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   typedef itk::ConstNeighborhoodIterator< _TImage > _TInIt;
46   typedef
47     fpa::Image::Functors::RegionGrowBinaryThreshold< _TImage >
48     _TFunctor;
49   auto out = this->GetOutput( "Output" );
50   auto f = out->GetITK< _TFunctor >( );
51   if( f == NULL )
52   {
53     typename _TFunctor::Pointer ptr_f = _TFunctor::New( );
54     f = ptr_f.GetPointer( );
55     out->SetITK( f );
56
57   } // fi
58
59   // Compute thresholds from seeds
60   auto seeds = this->GetInputData< vtkPolyData >( "Seeds" );
61   if( seeds != NULL )
62   {
63     std::vector< typename _TImage::IndexType > indices;
64     typename _TImage::PointType pnt;
65     typename _TImage::IndexType idx;
66     unsigned int dim =
67       ( _TImage::ImageDimension < 3 )? _TImage::ImageDimension: 3;
68
69     for( int i = 0; i < seeds->GetNumberOfPoints( ); ++i )
70     {
71       double buf[ 3 ];
72       seeds->GetPoint( i, buf );
73       pnt.Fill( 0 );
74       for( unsigned int d = 0; d < dim; ++d )
75         pnt[ d ] = buf[ d ];
76       if( image->TransformPhysicalPointToIndex( pnt, idx ) )
77         indices.push_back( idx );
78
79     } // rof
80
81     typename _TImage::SizeType r;
82     r.Fill( this->m_Parameters.GetUint( "Radius" ) );
83
84     _TInIt iIt( r, image, image->GetRequestedRegion( ) );
85     double v_s1 = double( 0 );
86     double v_s2 = double( 0 );
87     double v_n = double( 0 );
88     double v_min = std::numeric_limits< double >::max( );
89     double v_max = -v_min;
90     for( auto idxIt = indices.begin( ); idxIt != indices.end( ); ++idxIt )
91     {
92       iIt.SetLocation( *idxIt );
93       for( unsigned int i = 0; i < iIt.Size( ); ++i )
94       {
95         double v = double( iIt.GetPixel( i ) );
96         v_s1 += v;
97         v_s2 += v * v;
98         v_n += double( 1 );
99         v_min = ( v < v_min )? v: v_min;
100         v_max = ( v_max < v )? v: v_max;
101
102       } // rof
103
104     } // rof
105     double m = v_s1 / v_n;
106     double s = ( v_s2 - ( ( v_s1 * v_s1 ) / v_n ) ) / ( v_n - double( 1 ) );
107     s = std::sqrt( s );
108     v_min = m - s;
109     v_max = m + s;
110     f->SetLowerThreshold( v_min );
111     f->SetUpperThreshold( v_max );
112     this->m_Parameters.SetReal( "LowerThreshold", f->GetLowerThreshold( ) );
113     this->m_Parameters.SetReal( "UpperThreshold", f->GetUpperThreshold( ) );
114   }
115   else
116   {
117     f->SetLowerThreshold( this->m_Parameters.GetReal( "LowerThreshold" ) );
118     f->SetUpperThreshold( this->m_Parameters.GetReal( "UpperThreshold" ) );
119
120   } // fi
121 }
122
123 // eof - $RCSfile$