1 #include <Plugins/RegionGrowBinaryThresholdWithSeeds.h>
2 #include <cpPlugins/DataObjects/Image.h>
3 #include <cpPlugins/DataObjects/Image_Demanglers.h>
5 #include <fpa/Image/Functors/RegionGrowBinaryThreshold.h>
6 #include <itkConstNeighborhoodIterator.h>
7 #include <vtkPolyData.h>
9 // -------------------------------------------------------------------------
10 fpaPlugins::RegionGrowBinaryThresholdWithSeeds::
11 RegionGrowBinaryThresholdWithSeeds( )
14 typedef cpPlugins::BaseObjects::DataObject _TData;
15 typedef cpPlugins::DataObjects::Image _TImage;
17 this->_ConfigureInput< _TImage >( "Input", true, false );
18 this->_ConfigureInput< _TData >( "Seeds", true, false );
19 this->_ConfigureOutput< _TData >( "Output" );
21 this->m_Parameters.ConfigureAsUint( "Radius", 1 );
24 // -------------------------------------------------------------------------
25 fpaPlugins::RegionGrowBinaryThresholdWithSeeds::
26 ~RegionGrowBinaryThresholdWithSeeds( )
30 // -------------------------------------------------------------------------
31 void fpaPlugins::RegionGrowBinaryThresholdWithSeeds::
34 auto o = this->GetInputData( "Input" );
35 cpPlugins_Demangle_Image_ScalarPixels_AllDims_1( o, _GD0 )
36 this->_Error( "Invalid input image." );
39 // -------------------------------------------------------------------------
40 template< class _TImage >
41 void fpaPlugins::RegionGrowBinaryThresholdWithSeeds::
42 _GD0( _TImage* image )
44 typedef itk::ConstNeighborhoodIterator< _TImage > _TInIt;
46 fpa::Image::Functors::RegionGrowBinaryThreshold< _TImage >
48 auto out = this->GetOutput( "Output" );
49 auto f = out->GetITK< _TFunctor >( );
52 typename _TFunctor::Pointer ptr_f = _TFunctor::New( );
53 f = ptr_f.GetPointer( );
58 // Compute thresholds from seeds
59 auto seeds = this->GetInputData< vtkPolyData >( "Seeds" );
60 std::vector< typename _TImage::IndexType > indices;
63 typename _TImage::PointType pnt;
64 typename _TImage::IndexType idx;
66 ( _TImage::ImageDimension < 3 )? _TImage::ImageDimension: 3;
68 for( int i = 0; i < seeds->GetNumberOfPoints( ); ++i )
71 seeds->GetPoint( i, buf );
73 for( unsigned int d = 0; d < dim; ++d )
75 if( image->TransformPhysicalPointToIndex( pnt, idx ) )
76 indices.push_back( idx );
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 )
92 iIt.SetLocation( *idxIt );
93 for( unsigned int i = 0; i < iIt.Size( ); ++i )
95 double v = double( iIt.GetPixel( i ) );
99 v_min = ( v < v_min )? v: v_min;
100 v_max = ( v_max < v )? v: v_max;
105 double s = ( v_s2 - ( ( v_s1 * v_s1 ) / v_n ) ) / ( v_n - double( 1 ) );
106 s = std::sqrt( s ) / double( 2 );
108 f->SetLowerThreshold( v_min );
109 f->SetUpperThreshold( v_max );