1 #include <RegionGrowFunctors/BinaryThreshold.h>
2 #include <cpInstances/Image.h>
4 #include <fpa/Image/Functors/RegionGrowBinaryThreshold.h>
5 #include <itkConstNeighborhoodIterator.h>
6 #include <vtkPolyData.h>
8 // -------------------------------------------------------------------------
9 fpaPluginsRegionGrowFunctors::BinaryThreshold::
13 typedef cpPlugins::BaseObjects::DataObject _TData;
14 typedef cpInstances::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( "LowerThreshold", 0 );
22 this->m_Parameters.ConfigureAsReal( "UpperThreshold", 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 )
45 typedef itk::ConstNeighborhoodIterator< _TImage > _TInIt;
47 fpa::Image::Functors::RegionGrowBinaryThreshold< _TImage >
49 auto out = this->GetOutput( "Output" );
50 auto f = out->GetITK< _TFunctor >( );
53 typename _TFunctor::Pointer ptr_f = _TFunctor::New( );
54 f = ptr_f.GetPointer( );
59 // Compute thresholds from seeds
60 auto seeds = this->GetInputData< vtkPolyData >( "Seeds" );
63 std::vector< typename _TImage::IndexType > indices;
64 typename _TImage::PointType pnt;
65 typename _TImage::IndexType idx;
67 ( _TImage::ImageDimension < 3 )? _TImage::ImageDimension: 3;
69 for( int i = 0; i < seeds->GetNumberOfPoints( ); ++i )
72 seeds->GetPoint( i, buf );
74 for( unsigned int d = 0; d < dim; ++d )
76 if( image->TransformPhysicalPointToIndex( pnt, idx ) )
77 indices.push_back( idx );
81 typename _TImage::SizeType r;
82 r.Fill( this->m_Parameters.GetUint( "Radius" ) );
83 _TInIt iIt( r, image, image->GetRequestedRegion( ) );
84 double v_s1 = double( 0 );
85 double v_s2 = double( 0 );
86 double v_n = double( 0 );
87 double v_min = std::numeric_limits< double >::max( );
88 double v_max = -v_min;
89 for( auto idxIt = indices.begin( ); idxIt != indices.end( ); ++idxIt )
91 iIt.SetLocation( *idxIt );
92 for( unsigned int i = 0; i < iIt.Size( ); ++i )
94 double v = double( iIt.GetPixel( i ) );
98 v_min = ( v < v_min )? v: v_min;
99 v_max = ( v_max < v )? v: v_max;
104 double s = ( v_s2 - ( ( v_s1 * v_s1 ) / v_n ) ) / ( v_n - double( 1 ) );
105 s = std::sqrt( s ) / double( 2 );
106 f->SetLowerThreshold( v_min );
107 f->SetUpperThreshold( v_max );
111 f->SetLowerThreshold( this->m_Parameters.GetReal( "LowerThreshold" ) );
112 f->SetUpperThreshold( this->m_Parameters.GetReal( "UpperThreshold" ) );