1 #include <cpPluginsImageFilters/OtsuThresholdImageFilter.h>
2 #include <cpPlugins/Image.h>
3 #include <cpPlugins_ITKInstances/ImageFilters.h>
5 #include <itkOtsuThresholdImageFilter.h>
6 #include <itkOtsuMultipleThresholdsCalculator.hxx>
7 #include <itkOtsuThresholdCalculator.hxx>
8 #include <itkHistogramThresholdImageFilter.hxx>
9 #include <itkImageToHistogramFilter.hxx>
10 #include <itkMaskedImageToHistogramFilter.hxx>
11 #include <itkBinaryThresholdImageFilter.hxx>
13 // -------------------------------------------------------------------------
14 cpPluginsImageFilters::OtsuThresholdImageFilter::
15 OtsuThresholdImageFilter( )
18 this->_AddInput( "Input" );
19 this->_AddOutput< cpPlugins::Image >( "Output" );
21 this->m_Parameters.ConfigureAsUint( "NumberOfHistogramBins" );
22 this->m_Parameters.ConfigureAsUint( "InsideValue" );
23 this->m_Parameters.ConfigureAsUint( "OutsideValue" );
25 this->m_Parameters.SetUint( "NumberOfHistogramBins", 100 );
26 this->m_Parameters.SetUint( "InsideValue", 255 );
27 this->m_Parameters.SetUint( "OutsideValue", 0 );
30 // -------------------------------------------------------------------------
31 cpPluginsImageFilters::OtsuThresholdImageFilter::
32 ~OtsuThresholdImageFilter( )
36 // -------------------------------------------------------------------------
37 std::string cpPluginsImageFilters::OtsuThresholdImageFilter::
40 auto image = this->GetInputData( "Input" )->GetITK< itk::DataObject >( );
41 std::string cpPlugin_Image_Demangle_Pixel_AllScalars( r, _GD0, image, 1 );
42 if( r != "" ) cpPlugin_Image_Demangle_Pixel_AllScalars( r, _GD0, image, 2 );
43 if( r != "" ) cpPlugin_Image_Demangle_Pixel_AllScalars( r, _GD0, image, 3 );
44 if( r != "" ) cpPlugin_Image_Demangle_Pixel_AllScalars( r, _GD0, image, 4 );
48 // -------------------------------------------------------------------------
49 template< class _TImage >
50 std::string cpPluginsImageFilters::OtsuThresholdImageFilter::
51 _GD0( _TImage* image )
54 return( this->_GD1< _TImage, unsigned char >( image ) );
56 return( "ImageFilters::OtsuThresholdImageFilter: No valid input image." );
59 // -------------------------------------------------------------------------
60 template< class _TImage, class _TBinaryPixel >
61 std::string cpPluginsImageFilters::OtsuThresholdImageFilter::
62 _GD1( _TImage* image )
64 typedef itk::Image< _TBinaryPixel, _TImage::ImageDimension > _TBinaryImage;
65 typedef itk::OtsuThresholdImageFilter< _TImage, _TBinaryImage > _TFilter;
68 unsigned int bins = this->m_Parameters.GetUint( "NumberOfHistogramBins" );
69 _TBinaryPixel in_val = _TBinaryPixel( this->m_Parameters.GetUint( "InsideValue" ) );
70 _TBinaryPixel out_val = _TBinaryPixel( this->m_Parameters.GetUint( "OutsideValue" ) );
73 _TFilter* filter = this->_CreateITK< _TFilter >( );
74 filter->SetInput( image );
75 filter->SetNumberOfHistogramBins( bins );
76 filter->SetInsideValue( out_val ); // WARNING: these are inverted
77 filter->SetOutsideValue( in_val );
81 this->GetOutputData( "Output" )->SetITK( filter->GetOutput( ) );