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