]> 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" );
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   cpPlugins::Interface::Image* image =
34     this->GetInput< cpPlugins::Interface::Image >( "Input" );
35   if( image == NULL )
36     return( "OtsuThresholdImageFilter: No input image." );
37
38   itk::DataObject* itk_image = NULL;
39   std::string r = "";
40   cpPlugins_Image_Demangle_AllScalarTypes( 2, image, itk_image, r, _GD0 );
41   else cpPlugins_Image_Demangle_AllScalarTypes( 3, image, itk_image, r, _GD0 );
42   else cpPlugins_Image_Demangle_AllScalarTypes( 4, image, itk_image, r, _GD0 );
43   else r = "OtsuThresholdImageFilter: Input image type not supported.";
44   return( r );
45 }
46
47 // -------------------------------------------------------------------------
48 template< class I >
49 std::string cpPlugins::BasicFilters::OtsuThresholdImageFilter::
50 _GD0( itk::DataObject* image )
51 {
52   return(
53     this->_RealGD< I, itk::Image< unsigned char, I::ImageDimension > >(
54       image
55       )
56     );
57 }
58
59 // -------------------------------------------------------------------------
60 template< class I, class O >
61 inline std::string cpPlugins::BasicFilters::OtsuThresholdImageFilter::
62 _RealGD( itk::DataObject* image )
63 {
64   typedef itk::OtsuThresholdImageFilter< I, O > _F;
65   typedef typename O::PixelType _OP;
66
67   // Get parameters
68   unsigned int bins = this->m_Parameters->GetUint( "NumberOfHistogramBins" );
69   _OP in_val = _OP( this->m_Parameters->GetUint( "InsideValue" ) );
70   _OP out_val = _OP( this->m_Parameters->GetUint( "OutsideValue" ) );
71
72   // Configure filter
73   _F* filter = this->_CreateITK< _F >( );
74   filter->SetInput( dynamic_cast< I* >( 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   cpPlugins::Interface::Image* out =
82     this->GetOutput< cpPlugins::Interface::Image >( "Output" );
83   if( out != NULL )
84   {
85     out->SetITK< O >( filter->GetOutput( ) );
86     return( "" );
87   }
88   else
89     return( "OtsuThresholdImageFilter: output not correctly created." );
90 }
91
92 // eof - $RCSfile$