]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Plugins/BasicFilters/OtsuThresholdImageFilter.cxx
contour widget example done
[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->m_ClassName = "cpPlugins::BasicFilters::OtsuThresholdImageFilter";
12   this->m_ClassCategory = "ImageToImageFilter";
13   this->SetNumberOfInputs( 1 );
14   this->SetNumberOfOutputs( 1 );
15   this->_MakeOutput< cpPlugins::Interface::Image >( 0 );
16
17   using namespace cpPlugins::Interface;
18   this->m_DefaultParameters.Configure(
19     Parameters::Uint, "NumberOfHistogramBins"
20     );
21   this->m_DefaultParameters.Configure( Parameters::Real, "InsideValue" );
22   this->m_DefaultParameters.Configure( Parameters::Real, "OutsideValue" );
23   this->m_DefaultParameters.SetValueAsUint( "NumberOfHistogramBins", 100 );
24   this->m_DefaultParameters.SetValueAsReal( "InsideValue", 255 );
25   this->m_DefaultParameters.SetValueAsReal( "OutsideValue", 0 );
26   this->m_Parameters = this->m_DefaultParameters;
27 }
28
29 // -------------------------------------------------------------------------
30 cpPlugins::BasicFilters::OtsuThresholdImageFilter::
31 ~OtsuThresholdImageFilter( )
32 {
33 }
34
35 // -------------------------------------------------------------------------
36 std::string cpPlugins::BasicFilters::OtsuThresholdImageFilter::
37 _GenerateData( )
38 {
39   cpPlugins::Interface::Image* image =
40     this->GetInput< cpPlugins::Interface::Image >( 0 );
41   if( image == NULL )
42     return( "OtsuThresholdImageFilter: No input image." );
43
44   itk::DataObject* itk_image = NULL;
45   std::string r = "";
46   cpPlugins_Image_Input_Demangle_Dimension_AllScalarTypes(
47     2, image, itk_image, r, _DemangleOutput
48     );
49   else cpPlugins_Image_Input_Demangle_Dimension_AllScalarTypes(
50     3, image, itk_image, r, _DemangleOutput
51     );
52   else cpPlugins_Image_Input_Demangle_Dimension_AllScalarTypes(
53     4, image, itk_image, r, _DemangleOutput
54     );
55   else r = "OtsuThresholdImageFilter: Input image type not supported.";
56   return( r );
57 }
58
59 // -------------------------------------------------------------------------
60 template< class I >
61 std::string cpPlugins::BasicFilters::OtsuThresholdImageFilter::
62 _DemangleOutput( itk::DataObject* image )
63 {
64   return(
65     this->_RealGD< I, itk::Image< unsigned char, I::ImageDimension > >(
66       image
67       )
68     );
69 }
70
71 // -------------------------------------------------------------------------
72 template< class I, class O >
73 inline std::string cpPlugins::BasicFilters::OtsuThresholdImageFilter::
74 _RealGD( itk::DataObject* image )
75 {
76   typedef itk::OtsuThresholdImageFilter< I, O > _F;
77   typedef typename O::PixelType _OP;
78
79   // Get parameters
80   unsigned int bins =
81     this->m_Parameters.GetValueAsUint( "NumberOfHistogramBins" );
82   _OP in_val = _OP( this->m_Parameters.GetValueAsReal( "InsideValue" ) );
83   _OP out_val = _OP( this->m_Parameters.GetValueAsReal( "OutsideValue" ) );
84
85   // Configure filter
86   _F* filter = dynamic_cast< _F* >( this->m_RealProcessObject.GetPointer( ) );
87   if( filter == NULL )
88   {
89     this->m_RealProcessObject = _F::New( );
90     filter = dynamic_cast< _F* >( this->m_RealProcessObject.GetPointer( ) );
91
92   } // fi
93   filter->SetInput( dynamic_cast< I* >( image ) );
94   filter->SetNumberOfHistogramBins( bins );
95   filter->SetInsideValue( in_val );
96   filter->SetOutsideValue( out_val );
97   filter->Update( );
98
99   // Connect output
100   cpPlugins::Interface::Image* out =
101     this->GetOutput< cpPlugins::Interface::Image >( 0 );
102   if( out != NULL )
103   {
104     out->SetITKImage< O >( filter->GetOutput( ) );
105     return( "" );
106   }
107   else
108     return( "OtsuThresholdImageFilter: output not correctly created." );
109 }
110
111 // eof - $RCSfile$