]> Creatis software - cpPlugins.git/blob - plugins/cpPluginsImageFilters/OtsuThresholdImageFilter.cxx
177d4645f219aa557cd7935680c519512073f3a4
[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(
58       this->_GD1< _TImage, itk::Image< unsigned char, _TImage::ImageDimension > >( image )
59       );
60   else
61     return( "ImageFilters::OtsuThresholdImageFilter: No valid input image." );
62 }
63
64 // -------------------------------------------------------------------------
65 template< class _TImage, class _TBinaryImage >
66 std::string cpPluginsImageFilters::OtsuThresholdImageFilter::
67 _GD1( _TImage* image )
68 {
69   typedef itk::OtsuThresholdImageFilter< _TImage, _TBinaryImage > _F;
70   typedef typename _TBinaryImage::PixelType _UP;
71
72   // Get parameters
73   unsigned int bins = this->m_Parameters.GetUint( "NumberOfHistogramBins" );
74   _UP in_val = _UP( this->m_Parameters.GetUint( "InsideValue" ) );
75   _UP out_val = _UP( this->m_Parameters.GetUint( "OutsideValue" ) );
76
77   // Configure filter
78   _F* filter = this->_CreateITK< _F >( );
79   filter->SetInput( image );
80   filter->SetNumberOfHistogramBins( bins );
81   filter->SetInsideValue( out_val ); // WARNING: these are inverted
82   filter->SetOutsideValue( in_val );
83   filter->Update( );
84
85   // Connect output
86   this->GetOutputData( "Output" )->SetITK( filter->GetOutput( ) );
87   return( "" );
88 }
89
90 // eof - $RCSfile$