1 #include <RegionGrowFunctors/BinaryThreshold.h>
2 #include <cpInstances/DataObjects/Image.h>
4 #include <fpa/Image/Functors/RegionGrow/BinaryThreshold.h>
5 #include <itkConstNeighborhoodIterator.h>
6 #include <vtkPolyData.h>
8 // -------------------------------------------------------------------------
9 fpaPluginsRegionGrowFunctors::BinaryThreshold::
13 typedef cpPlugins::Pipeline::DataObject _TData;
14 typedef cpInstances::DataObjects::Image _TImage;
16 this->_ConfigureInput< _TImage >( "Input", true, false );
17 this->_ConfigureInput< _TData >( "Seeds", false, false );
18 this->_ConfigureOutput< _TData >( "Output" );
20 this->m_Parameters.ConfigureAsUint( "Radius", 1 );
21 this->m_Parameters.ConfigureAsReal( "Lower", 0 );
22 this->m_Parameters.ConfigureAsReal( "Upper", 0 );
25 // -------------------------------------------------------------------------
26 fpaPluginsRegionGrowFunctors::BinaryThreshold::
31 // -------------------------------------------------------------------------
32 void fpaPluginsRegionGrowFunctors::BinaryThreshold::
35 auto o = this->GetInputData( "Input" );
36 cpPlugins_Demangle_Image_ScalarPixels_AllDims_1( o, _GD0 )
37 this->_Error( "Invalid input image." );
40 // -------------------------------------------------------------------------
41 template< class _TImage >
42 void fpaPluginsRegionGrowFunctors::BinaryThreshold::
43 _GD0( _TImage* image )
46 typedef itk::ConstNeighborhoodIterator< _TImage > _TInIt;
48 fpa::Image::Functors::RegionGrow::BinaryThreshold< _TImage >
50 auto out = this->GetOutput( "Output" );
51 auto f = out->GetITK< _TFunctor >( );
54 typename _TFunctor::Pointer ptr_f = _TFunctor::New( );
55 f = ptr_f.GetPointer( );
60 // Compute thresholds from seeds
61 auto seeds = this->GetInputData< vtkPolyData >( "Seeds" );
64 std::vector< typename _TImage::IndexType > indices;
65 typename _TImage::PointType pnt;
66 typename _TImage::IndexType idx;
68 ( _TImage::ImageDimension < 3 )? _TImage::ImageDimension: 3;
70 for( int i = 0; i < seeds->GetNumberOfPoints( ); ++i )
73 seeds->GetPoint( i, buf );
75 for( unsigned int d = 0; d < dim; ++d )
77 if( image->TransformPhysicalPointToIndex( pnt, idx ) )
78 indices.push_back( idx );
82 typename _TImage::SizeType r;
83 r.Fill( this->m_Parameters.GetUint( "Radius" ) );
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 )
93 iIt.SetLocation( *idxIt );
94 for( unsigned int i = 0; i < iIt.Size( ); ++i )
96 double v = double( iIt.GetPixel( i ) );
100 v_min = ( v < v_min )? v: v_min;
101 v_max = ( v_max < v )? v: v_max;
106 double m = v_s1 / v_n;
107 double s = ( v_s2 - ( ( v_s1 * v_s1 ) / v_n ) ) / ( v_n - double( 1 ) );
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( ) );
118 f->SetLower( this->m_Parameters.GetReal( "Lower" ) );
119 f->SetUpper( this->m_Parameters.GetReal( "Upper" ) );