]> Creatis software - cpPlugins.git/blob - plugins/cpPluginsImageFilters/OtsuThresholdImageFilter.cxx
...
[cpPlugins.git] / plugins / cpPluginsImageFilters / OtsuThresholdImageFilter.cxx
1 #include <cpPluginsImageFilters/OtsuThresholdImageFilter.h>
2 #include <cpPlugins/Image.h>
3 #include <cpPlugins_ITKInstances/ImageFilters.h>
4
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>
12
13 // -------------------------------------------------------------------------
14 cpPluginsImageFilters::OtsuThresholdImageFilter::
15 OtsuThresholdImageFilter( )
16   : Superclass( )
17 {
18   this->_AddInput( "Input" );
19   this->_AddOutput< cpPlugins::Image >( "Output" );
20
21   this->m_Parameters.ConfigureAsUint( "NumberOfHistogramBins" );
22   this->m_Parameters.ConfigureAsUint( "InsideValue" );
23   this->m_Parameters.ConfigureAsUint( "OutsideValue" );
24
25   this->m_Parameters.SetUint( "NumberOfHistogramBins", 100 );
26   this->m_Parameters.SetUint( "InsideValue", 255 );
27   this->m_Parameters.SetUint( "OutsideValue", 0 );
28 }
29
30 // -------------------------------------------------------------------------
31 cpPluginsImageFilters::OtsuThresholdImageFilter::
32 ~OtsuThresholdImageFilter( )
33 {
34 }
35
36 // -------------------------------------------------------------------------
37 std::string cpPluginsImageFilters::OtsuThresholdImageFilter::
38 _GenerateData( )
39 {
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 );
45   return( r );
46 }
47
48 // -------------------------------------------------------------------------
49 template< class _TImage >
50 std::string cpPluginsImageFilters::OtsuThresholdImageFilter::
51 _GD0( _TImage* image )
52 {
53   if( image != NULL )
54     return( this->_GD1< _TImage, unsigned char >( image ) );
55   else
56     return( "ImageFilters::OtsuThresholdImageFilter: No valid input image." );
57 }
58
59 // -------------------------------------------------------------------------
60 template< class _TImage, class _TBinaryPixel >
61 std::string cpPluginsImageFilters::OtsuThresholdImageFilter::
62 _GD1( _TImage* image )
63 {
64   typedef itk::Image< _TBinaryPixel, _TImage::ImageDimension > _TBinaryImage;
65   typedef itk::OtsuThresholdImageFilter< _TImage, _TBinaryImage > _TFilter;
66
67   // Get parameters
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" ) );
71
72   // Configure filter
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 );
78   filter->Update( );
79
80   // Connect output
81   this->GetOutputData( "Output" )->SetITK( filter->GetOutput( ) );
82   return( "" );
83 }
84
85 // eof - $RCSfile$