]> Creatis software - FrontAlgorithms.git/blob - plugins/Plugins/RegionGrowBinaryThresholdWithSeeds.cxx
...
[FrontAlgorithms.git] / plugins / Plugins / RegionGrowBinaryThresholdWithSeeds.cxx
1 #include <Plugins/RegionGrowBinaryThresholdWithSeeds.h>
2 #include <cpPlugins/DataObjects/Image.h>
3 #include <cpPlugins/DataObjects/Image_Demanglers.h>
4
5 #include <fpa/Image/Functors/RegionGrowBinaryThreshold.h>
6 #include <itkConstNeighborhoodIterator.h>
7 #include <vtkPolyData.h>
8
9 // -------------------------------------------------------------------------
10 fpaPlugins::RegionGrowBinaryThresholdWithSeeds::
11 RegionGrowBinaryThresholdWithSeeds( )
12   : Superclass( )
13 {
14   typedef cpPlugins::BaseObjects::DataObject _TData;
15   typedef cpPlugins::DataObjects::Image _TImage;
16
17   this->_ConfigureInput< _TImage >( "Input", true, false );
18   this->_ConfigureInput< _TData >( "Seeds", true, false );
19   this->_ConfigureOutput< _TData >( "Output" );
20
21   this->m_Parameters.ConfigureAsUint( "Radius", 1 );
22 }
23
24 // -------------------------------------------------------------------------
25 fpaPlugins::RegionGrowBinaryThresholdWithSeeds::
26 ~RegionGrowBinaryThresholdWithSeeds( )
27 {
28 }
29
30 // -------------------------------------------------------------------------
31 void fpaPlugins::RegionGrowBinaryThresholdWithSeeds::
32 _GenerateData( )
33 {
34   auto o = this->GetInputData( "Input" );
35   cpPlugins_Demangle_Image_ScalarPixels_AllDims_1( o, _GD0 )
36     this->_Error( "Invalid input image." );
37 }
38
39 // -------------------------------------------------------------------------
40 template< class _TImage >
41 void fpaPlugins::RegionGrowBinaryThresholdWithSeeds::
42 _GD0( _TImage* image )
43 {
44   typedef itk::ConstNeighborhoodIterator< _TImage > _TInIt;
45   typedef
46     fpa::Image::Functors::RegionGrowBinaryThreshold< _TImage >
47     _TFunctor;
48   auto out = this->GetOutput( "Output" );
49   auto f = out->GetITK< _TFunctor >( );
50   if( f == NULL )
51   {
52     typename _TFunctor::Pointer ptr_f = _TFunctor::New( );
53     f = ptr_f.GetPointer( );
54     out->SetITK( f );
55
56   } // fi
57
58   // Compute thresholds from seeds
59   auto seeds = this->GetInputData< vtkPolyData >( "Seeds" );
60   std::vector< typename _TImage::IndexType > indices;
61   if( seeds != NULL )
62   {
63     typename _TImage::PointType pnt;
64     typename _TImage::IndexType idx;
65     unsigned int dim =
66       ( _TImage::ImageDimension < 3 )? _TImage::ImageDimension: 3;
67
68     for( int i = 0; i < seeds->GetNumberOfPoints( ); ++i )
69     {
70       double buf[ 3 ];
71       seeds->GetPoint( i, buf );
72       pnt.Fill( 0 );
73       for( unsigned int d = 0; d < dim; ++d )
74         pnt[ d ] = buf[ d ];
75       if( image->TransformPhysicalPointToIndex( pnt, idx ) )
76         indices.push_back( idx );
77
78     } // rof
79
80   } // fi
81
82   typename _TImage::SizeType r;
83   r.Fill( this->m_Parameters.GetUint( "Radius" ) );
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 s = ( v_s2 - ( ( v_s1 * v_s1 ) / v_n ) ) / ( v_n - double( 1 ) );
106   s = std::sqrt( s ) / double( 2 );
107
108   f->SetLowerThreshold( v_min );
109   f->SetUpperThreshold( v_max );
110 }
111
112 // eof - $RCSfile$