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 )
57 return( this->_GD1< _TImage, unsigned char >( image ) );
59 return( "ImageFilters::OtsuThresholdImageFilter: No valid input image." );
62 // -------------------------------------------------------------------------
63 template< class _TImage, class _TBinaryPixel >
64 std::string cpPluginsImageFilters::OtsuThresholdImageFilter::
65 _GD1( _TImage* image )
67 typedef itk::Image< _TBinaryPixel, _TImage::ImageDimension > _TBinaryImage;
68 typedef itk::OtsuThresholdImageFilter< _TImage, _TBinaryImage > _TFilter;
71 unsigned int bins = this->m_Parameters.GetUint( "NumberOfHistogramBins" );
72 _TBinaryPixel in_val = _TBinaryPixel( this->m_Parameters.GetUint( "InsideValue" ) );
73 _TBinaryPixel out_val = _TBinaryPixel( this->m_Parameters.GetUint( "OutsideValue" ) );
76 _TFilter* filter = this->_CreateITK< _TFilter >( );
77 filter->SetInput( image );
78 filter->SetNumberOfHistogramBins( bins );
79 filter->SetInsideValue( out_val ); // WARNING: these are inverted
80 filter->SetOutsideValue( in_val );
84 this->GetOutputData( "Output" )->SetITK( filter->GetOutput( ) );