]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Plugins/OtsuThresholdImageFilter.cxx
...
[cpPlugins.git] / lib / cpPlugins / Plugins / OtsuThresholdImageFilter.cxx
1 #include <cpPlugins/Plugins/OtsuThresholdImageFilter.h>
2 #include <cpPlugins/Interface/Image.h>
3
4 #include <complex>
5
6 #define ITK_MANUAL_INSTANTIATION
7 #include <itkImage.h>
8
9 #include <itkCovariantVector.h>
10 #include <itkDiffusionTensor3D.h>
11 #include <itkPoint.h>
12 #include <itkRGBPixel.h>
13 #include <itkRGBAPixel.h>
14 #include <itkSymmetricSecondRankTensor.h>
15 #include <itkVector.h>
16
17 #undef ITK_MANUAL_INSTANTIATION
18 #include <itkOtsuThresholdImageFilter.h>
19
20 // -------------------------------------------------------------------------
21 cpPlugins::Plugins::OtsuThresholdImageFilter::
22 OtsuThresholdImageFilter( )
23   : Superclass( )
24 {
25   this->m_ClassName = "cpPlugins::OtsuThresholdImageFilter";
26   this->m_ClassCategory = "ImageToImageFilter";
27   this->SetNumberOfInputs( 1 );
28   this->SetNumberOfOutputs( 1 );
29   this->_MakeOutput< cpPlugins::Interface::Image >( 0 );
30
31   using namespace cpPlugins::Interface;
32   this->m_DefaultParameters.Configure( Parameters::Uint, "NumberOfHistogramBins" );
33   this->m_DefaultParameters.Configure( Parameters::Real, "InsideValue" );
34   this->m_DefaultParameters.Configure( Parameters::Real, "OutsideValue" );
35   this->m_DefaultParameters.SetValueAsUint( "NumberOfHistogramBins", 100 );
36   this->m_DefaultParameters.SetValueAsReal( "InsideValue", 255 );
37   this->m_DefaultParameters.SetValueAsReal( "OutsideValue", 0 );
38   this->m_Parameters = this->m_DefaultParameters;
39 }
40
41 // -------------------------------------------------------------------------
42 cpPlugins::Plugins::OtsuThresholdImageFilter::
43 ~OtsuThresholdImageFilter( )
44 {
45 }
46
47 // -------------------------------------------------------------------------
48 std::string cpPlugins::Plugins::OtsuThresholdImageFilter::
49 _GenerateData( )
50 {
51   cpPlugins::Interface::Image* image =
52     this->_Input< cpPlugins::Interface::Image >( 0 );
53   if( image == NULL )
54     return( "OtsuThresholdImageFilter: No input image." );
55
56   itk::DataObject* itk_image = NULL;
57   std::string r = "";
58   cpPlugins_Image_Input_Demangle_Dimension_AllScalarTypes(
59     2, image, itk_image, r, _DemangleOutput
60     );
61   else cpPlugins_Image_Input_Demangle_Dimension_AllScalarTypes(
62     3, image, itk_image, r, _DemangleOutput
63     );
64   else cpPlugins_Image_Input_Demangle_Dimension_AllScalarTypes(
65     4, image, itk_image, r, _DemangleOutput
66     );
67   else r = "OtsuThresholdImageFilter: Input image type not supported.";
68   return( r );
69 }
70
71 // -------------------------------------------------------------------------
72 template< class I >
73 std::string cpPlugins::Plugins::OtsuThresholdImageFilter::
74 _DemangleOutput( itk::DataObject* image )
75 {
76   return(
77     this->_RealGD< I, itk::Image< unsigned char, I::ImageDimension > >(
78       image
79       )
80     );
81 }
82
83 // -------------------------------------------------------------------------
84 template< class I, class O >
85 inline std::string cpPlugins::Plugins::OtsuThresholdImageFilter::
86 _RealGD( itk::DataObject* image )
87 {
88   typedef itk::OtsuThresholdImageFilter< I, O > _F;
89   typedef typename O::PixelType _OP;
90
91   // Get parameters
92   unsigned int bins =
93     this->m_Parameters.GetValueAsUint( "NumberOfHistogramBins" );
94   _OP in_val = _OP( this->m_Parameters.GetValueAsReal( "InsideValue" ) );
95   _OP out_val = _OP( this->m_Parameters.GetValueAsReal( "OutsideValue" ) );
96
97   // Configure filter
98   _F* filter = dynamic_cast< _F* >( this->m_RealProcessObject.GetPointer( ) );
99   if( filter == NULL )
100   {
101     this->m_RealProcessObject = _F::New( );
102     filter = dynamic_cast< _F* >( this->m_RealProcessObject.GetPointer( ) );
103
104   } // fi
105   filter->SetInput( dynamic_cast< I* >( image ) );
106   filter->SetNumberOfHistogramBins( bins );
107   filter->SetInsideValue( in_val );
108   filter->SetOutsideValue( out_val );
109   filter->Update( );
110
111   // Connect output
112   cpPlugins::Interface::Image* out =
113     this->_Output< cpPlugins::Interface::Image >( 0 );
114   if( out != NULL )
115   {
116     out->SetITKImage< O >( filter->GetOutput( ) );
117     return( "" );
118   }
119   else
120     return( "OtsuThresholdImageFilter: output not correctly created." );
121 }
122
123 // -------------------------------------------------------------------------
124 /* TODO
125    namespace cpPlugins
126    {
127    namespace Plugins
128    {
129    cpPlugins_Image_Demangle_Methods_Code_Only_Scalars(
130    OtsuThresholdImageFilter, _DemangleInput
131    );
132    }
133    }
134
135    // -------------------------------------------------------------------------
136    template< class I >
137    std::string cpPlugins::Plugins::OtsuThresholdImageFilter::
138    _DemangleInput( itk::DataObject* image )
139    {
140    }
141
142    // -------------------------------------------------------------------------
143    template< class I, class O >
144    std::string cpPlugins::Plugins::OtsuThresholdImageFilter::
145    _RealGD( itk::DataObject* image )
146    {
147    typedef itk::OtsuThresholdImageFilter< I, O > _F;
148    typedef typename O::PixelType _OP;
149
150    unsigned int bins = this->m_Parameters.GetValueAsUint( "NumberOfHistogramBins" );
151    _OP in = _OP( this->m_Parameters.GetValueAsReal( "InsideValue" ) );
152    _OP out = _OP( this->m_Parameters.GetValueAsReal( "OutsideValue" ) );
153
154    _F* filter = dynamic_cast< _F* >( this->m_RealProcessObject.GetPointer( ) );
155    if( filter == NULL )
156    {
157    this->m_RealProcessObject = _F::New( );
158    filter = dynamic_cast< _F* >( this->m_RealProcessObject.GetPointer( ) );
159
160    } // fi
161    filter->SetInput( dynamic_cast< I* >( image ) );
162    filter->SetNumberOfHistogramBins( bins );
163    filter->SetInsideValue( in );
164    filter->SetOutsideValue( out );
165    filter->Update( );
166    this->m_Outputs[ 0 ]->SetITKDataObject( filter->GetOutput( ) );
167
168    return( "" );
169    }
170 */
171
172 // eof - $RCSfile$