]> Creatis software - cpPlugins.git/blob - plugins/cpPluginsImageFilters/OtsuThresholdImageFilter.cxx
First dump for version 0.1.0
[cpPlugins.git] / plugins / cpPluginsImageFilters / OtsuThresholdImageFilter.cxx
1 #include <cpPluginsImageFilters/OtsuThresholdImageFilter.h>
2 #include <cpPlugins/Image.h>
3
4 #include <itkOtsuThresholdImageFilter.h>
5 #include <itkBinaryFunctorImageFilter.hxx>
6 #include <itkHistogram.hxx>
7 #include <itkHistogramAlgorithmBase.hxx>
8 #include <itkHistogramThresholdImageFilter.hxx>
9 #include <itkImageToHistogramFilter.hxx>
10 #include <itkMaskedImageToHistogramFilter.hxx>
11 #include <itkOtsuMultipleThresholdsCalculator.hxx>
12 #include <itkOtsuThresholdCalculator.hxx>
13
14 // -------------------------------------------------------------------------
15 cpPluginsImageFilters::OtsuThresholdImageFilter::
16 OtsuThresholdImageFilter( )
17   : Superclass( )
18 {
19   this->_AddInput( "Input" );
20   this->_AddOutput< cpPlugins::Image >( "Output" );
21
22   this->m_Parameters.ConfigureAsUint( "NumberOfHistogramBins" );
23   this->m_Parameters.ConfigureAsUint( "InsideValue" );
24   this->m_Parameters.ConfigureAsUint( "OutsideValue" );
25
26   this->m_Parameters.SetUint( "NumberOfHistogramBins", 100 );
27   this->m_Parameters.SetUint( "InsideValue", 255 );
28   this->m_Parameters.SetUint( "OutsideValue", 0 );
29 }
30
31 // -------------------------------------------------------------------------
32 cpPluginsImageFilters::OtsuThresholdImageFilter::
33 ~OtsuThresholdImageFilter( )
34 {
35 }
36
37 // -------------------------------------------------------------------------
38 std::string cpPluginsImageFilters::OtsuThresholdImageFilter::
39 _GenerateData( )
40 {
41   auto image = this->GetInputData( "Input" )->GetITK< itk::DataObject >( );
42   std::string   cpPlugin_Image_Demangle_Pixel_AllScalars( r, _GD0, image, 1 );
43   if( r != "" ) cpPlugin_Image_Demangle_Pixel_AllScalars( r, _GD0, image, 2 );
44   if( r != "" ) cpPlugin_Image_Demangle_Pixel_AllScalars( r, _GD0, image, 3 );
45   if( r != "" ) cpPlugin_Image_Demangle_Pixel_AllScalars( r, _GD0, image, 4 );
46   return( r );
47 }
48
49 // -------------------------------------------------------------------------
50 template< class I >
51 std::string cpPluginsImageFilters::OtsuThresholdImageFilter::
52 _GD0( I* image )
53 {
54   if( image != NULL )
55     return(
56       this->_GD1< I, itk::Image< unsigned char, I::ImageDimension > >( image )
57       );
58   else
59     return( "ImageFilters::OtsuThresholdImageFilter: No valid input image." );
60 }
61
62 // -------------------------------------------------------------------------
63 template< class I, class O >
64 std::string cpPluginsImageFilters::OtsuThresholdImageFilter::
65 _GD1( I* image )
66 {
67   typedef itk::OtsuThresholdImageFilter< I, O > _F;
68   typedef typename O::PixelType _OP;
69
70   // Get parameters
71   unsigned int bins = this->m_Parameters.GetUint( "NumberOfHistogramBins" );
72   _OP in_val = _OP( this->m_Parameters.GetUint( "InsideValue" ) );
73   _OP out_val = _OP( this->m_Parameters.GetUint( "OutsideValue" ) );
74
75   // Configure filter
76   _F* filter = this->_CreateITK< _F >( );
77   filter->SetInput( dynamic_cast< I* >( image ) );
78   filter->SetNumberOfHistogramBins( bins );
79   filter->SetInsideValue( out_val ); // WARNING: these are inverted
80   filter->SetOutsideValue( in_val );
81   filter->Update( );
82
83   // Connect output
84   this->GetOutputData( "Output" )->SetITK( filter->GetOutput( ) );
85   return( "" );
86 }
87
88 // eof - $RCSfile$