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>
9 #include <itkHistogram.hxx>
10 #include <itkHistogramAlgorithmBase.hxx>
11 #include <itkHistogramThresholdImageFilter.hxx>
12 #include <itkImageToHistogramFilter.hxx>
13 #include <itkMaskedImageToHistogramFilter.hxx>
16 // -------------------------------------------------------------------------
17 cpPluginsImageFilters::OtsuThresholdImageFilter::
18 OtsuThresholdImageFilter( )
21 this->_AddInput( "Input" );
22 this->_AddOutput< cpPlugins::Image >( "Output" );
24 this->m_Parameters.ConfigureAsUint( "NumberOfHistogramBins" );
25 this->m_Parameters.ConfigureAsUint( "InsideValue" );
26 this->m_Parameters.ConfigureAsUint( "OutsideValue" );
28 this->m_Parameters.SetUint( "NumberOfHistogramBins", 100 );
29 this->m_Parameters.SetUint( "InsideValue", 255 );
30 this->m_Parameters.SetUint( "OutsideValue", 0 );
33 // -------------------------------------------------------------------------
34 cpPluginsImageFilters::OtsuThresholdImageFilter::
35 ~OtsuThresholdImageFilter( )
39 // -------------------------------------------------------------------------
40 std::string cpPluginsImageFilters::OtsuThresholdImageFilter::
43 auto image = this->GetInputData( "Input" )->GetITK< itk::DataObject >( );
44 std::string cpPlugin_Image_Demangle_Pixel_AllScalars( r, _GD0, image, 1 );
45 if( r != "" ) cpPlugin_Image_Demangle_Pixel_AllScalars( r, _GD0, image, 2 );
46 if( r != "" ) cpPlugin_Image_Demangle_Pixel_AllScalars( r, _GD0, image, 3 );
47 if( r != "" ) cpPlugin_Image_Demangle_Pixel_AllScalars( r, _GD0, image, 4 );
51 // -------------------------------------------------------------------------
52 template< class _TImage >
53 std::string cpPluginsImageFilters::OtsuThresholdImageFilter::
54 _GD0( _TImage* image )
58 this->_GD1< _TImage, itk::Image< unsigned char, _TImage::ImageDimension > >( image )
61 return( "ImageFilters::OtsuThresholdImageFilter: No valid input image." );
64 // -------------------------------------------------------------------------
65 template< class _TImage, class _TBinaryImage >
66 std::string cpPluginsImageFilters::OtsuThresholdImageFilter::
67 _GD1( _TImage* image )
69 typedef itk::OtsuThresholdImageFilter< _TImage, _TBinaryImage > _F;
70 typedef typename _TBinaryImage::PixelType _UP;
73 unsigned int bins = this->m_Parameters.GetUint( "NumberOfHistogramBins" );
74 _UP in_val = _UP( this->m_Parameters.GetUint( "InsideValue" ) );
75 _UP out_val = _UP( this->m_Parameters.GetUint( "OutsideValue" ) );
78 _F* filter = this->_CreateITK< _F >( );
79 filter->SetInput( image );
80 filter->SetNumberOfHistogramBins( bins );
81 filter->SetInsideValue( out_val ); // WARNING: these are inverted
82 filter->SetOutsideValue( in_val );
86 this->GetOutputData( "Output" )->SetITK( filter->GetOutput( ) );