]> Creatis software - cpPlugins.git/blob - plugins/cpPluginsImageFilters/OtsuThresholdImageFilter.cxx
b5dff63a9e44c9e76a26ad2abd36934c4a464cf5
[cpPlugins.git] / plugins / cpPluginsImageFilters / OtsuThresholdImageFilter.cxx
1 #include <cpPluginsImageFilters/OtsuThresholdImageFilter.h>
2 #include <cpPlugins/Image.h>
3 #include <cpPlugins_ITKInstances/ImageFilters.h>
4
5 #include <itkOtsuThresholdImageFilter.h>
6 #include <itkOtsuMultipleThresholdsCalculator.hxx>
7 #include <itkOtsuThresholdCalculator.hxx>
8 /*
9   #include <itkHistogram.hxx>
10   #include <itkHistogramAlgorithmBase.hxx>
11   #include <itkHistogramThresholdImageFilter.hxx>
12   #include <itkImageToHistogramFilter.hxx>
13   #include <itkMaskedImageToHistogramFilter.hxx>
14 */
15
16 // -------------------------------------------------------------------------
17 cpPluginsImageFilters::OtsuThresholdImageFilter::
18 OtsuThresholdImageFilter( )
19   : Superclass( )
20 {
21   this->_AddInput( "Input" );
22   this->_AddOutput< cpPlugins::Image >( "Output" );
23
24   this->m_Parameters.ConfigureAsUint( "NumberOfHistogramBins" );
25   this->m_Parameters.ConfigureAsUint( "InsideValue" );
26   this->m_Parameters.ConfigureAsUint( "OutsideValue" );
27
28   this->m_Parameters.SetUint( "NumberOfHistogramBins", 100 );
29   this->m_Parameters.SetUint( "InsideValue", 255 );
30   this->m_Parameters.SetUint( "OutsideValue", 0 );
31 }
32
33 // -------------------------------------------------------------------------
34 cpPluginsImageFilters::OtsuThresholdImageFilter::
35 ~OtsuThresholdImageFilter( )
36 {
37 }
38
39 // -------------------------------------------------------------------------
40 std::string cpPluginsImageFilters::OtsuThresholdImageFilter::
41 _GenerateData( )
42 {
43   auto image = this->GetInputData( "Input" )->GetITK< itk::DataObject >( );
44   std::string   cpPlugin_Image_Demangle_Pixel_AllScalars( r, _GD0, image, 1 );
45   if( r != "" ) cpPlugin_Image_Demangle_Pixel_AllScalars( r, _GD0, image, 2 );
46   if( r != "" ) cpPlugin_Image_Demangle_Pixel_AllScalars( r, _GD0, image, 3 );
47   if( r != "" ) cpPlugin_Image_Demangle_Pixel_AllScalars( r, _GD0, image, 4 );
48   return( r );
49 }
50
51 // -------------------------------------------------------------------------
52 template< class _TImage >
53 std::string cpPluginsImageFilters::OtsuThresholdImageFilter::
54 _GD0( _TImage* image )
55 {
56   if( image != NULL )
57     return( this->_GD1< _TImage, unsigned char >( image ) );
58   else
59     return( "ImageFilters::OtsuThresholdImageFilter: No valid input image." );
60 }
61
62 // -------------------------------------------------------------------------
63 template< class _TImage, class _TBinaryPixel >
64 std::string cpPluginsImageFilters::OtsuThresholdImageFilter::
65 _GD1( _TImage* image )
66 {
67   typedef itk::Image< _TBinaryPixel, _TImage::ImageDimension > _TBinaryImage;
68   typedef itk::OtsuThresholdImageFilter< _TImage, _TBinaryImage > _TFilter;
69
70   // Get parameters
71   unsigned int bins = this->m_Parameters.GetUint( "NumberOfHistogramBins" );
72   _TBinaryPixel in_val = _TBinaryPixel( this->m_Parameters.GetUint( "InsideValue" ) );
73   _TBinaryPixel out_val = _TBinaryPixel( this->m_Parameters.GetUint( "OutsideValue" ) );
74
75   // Configure filter
76   _TFilter* filter = this->_CreateITK< _TFilter >( );
77   filter->SetInput( 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$