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