]> Creatis software - cpPlugins.git/blob - plugins/cpPluginsImageFilters/BinaryThresholdImageFilter.cxx
6545d56750072111aa70e519d1f482d2d6f039bc
[cpPlugins.git] / plugins / cpPluginsImageFilters / BinaryThresholdImageFilter.cxx
1 #include <cpPluginsImageFilters/BinaryThresholdImageFilter.h>
2 #include <cpPlugins/Image.h>
3 #include <cpPlugins_ITKInstances/ImageFilters.h>
4
5 #include <itkBinaryThresholdImageFilter.h>
6 #include <itkBinaryThresholdImageFilter.hxx>
7 #include <itkUnaryFunctorImageFilter.hxx>
8
9 // -------------------------------------------------------------------------
10 cpPluginsImageFilters::BinaryThresholdImageFilter::
11 BinaryThresholdImageFilter( )
12   : Superclass( )
13 {
14   this->_AddInput( "Input" );
15   this->_AddOutput< cpPlugins::Image >( "Output" );
16
17   this->m_Parameters.ConfigureAsReal( "LowerThresholdValue" );
18   this->m_Parameters.ConfigureAsReal( "UpperThresholdValue" );
19   this->m_Parameters.ConfigureAsUint( "InsideValue" );
20   this->m_Parameters.ConfigureAsUint( "OutsideValue" );
21
22   std::vector< std::string > choices;
23   choices.push_back( "char" );
24   choices.push_back( "short" );
25   choices.push_back( "int" );
26   choices.push_back( "long" );
27   choices.push_back( "float" );
28   choices.push_back( "double" );
29   choices.push_back( "unsigned char" );
30   choices.push_back( "unsigned short" );
31   choices.push_back( "unsigned int" );
32   choices.push_back( "unsigned long" );
33   this->m_Parameters.ConfigureAsChoices( "OutputResolution", choices );
34
35   this->m_Parameters.SetReal( "LowerThresholdValue", 0 );
36   this->m_Parameters.SetReal( "UpperThresholdValue", 10000 );
37   this->m_Parameters.SetReal( "InsideValue", 1 );
38   this->m_Parameters.SetReal( "OutsideValue", 0 );
39   this->m_Parameters.SetSelectedChoice( "OutputResolution", "unsigned char" );
40 }
41
42 // -------------------------------------------------------------------------
43 cpPluginsImageFilters::BinaryThresholdImageFilter::
44 ~BinaryThresholdImageFilter( )
45 {
46 }
47
48 // -------------------------------------------------------------------------
49 std::string cpPluginsImageFilters::BinaryThresholdImageFilter::
50 _GenerateData( )
51 {
52   auto image = this->GetInputData( "Input" )->GetITK< itk::DataObject >( );
53   std::string   cpPlugin_Image_Demangle_Pixel_AllScalars( r, _GD0, image, 1 );
54   if( r != "" ) cpPlugin_Image_Demangle_Pixel_AllScalars( r, _GD0, image, 2 );
55   if( r != "" ) cpPlugin_Image_Demangle_Pixel_AllScalars( r, _GD0, image, 3 );
56   if( r != "" ) cpPlugin_Image_Demangle_Pixel_AllScalars( r, _GD0, image, 4 );
57   return( r );
58 }
59
60 // -------------------------------------------------------------------------
61 template< class _TImage >
62 std::string cpPluginsImageFilters::BinaryThresholdImageFilter::
63 _GD0( _TImage* image )
64 {
65   if( image != NULL )
66   {
67     auto choice = this->m_Parameters.GetSelectedChoice( "OutputResolution" );
68     if( choice == "char" )
69       return( this->_GD1< _TImage, char >( image ) );
70     else if( choice == "short" )
71       return( this->_GD1< _TImage, short >( image ) );
72     else if( choice == "int" )
73       return( this->_GD1< _TImage, int >( image ) );
74     else if( choice == "long" )
75       return( this->_GD1< _TImage, long >( image ) );
76     else if( choice == "float" )
77       return( this->_GD1< _TImage, float >( image ) );
78     else if( choice == "double" )
79       return( this->_GD1< _TImage, double >( image ) );
80     else if( choice == "unsigned char" )
81       return( this->_GD1< _TImage, unsigned char >( image ) );
82     else if( choice == "unsigned short" )
83       return( this->_GD1< _TImage, unsigned short >( image ) );
84     else if( choice == "unsigned int" )
85       return( this->_GD1< _TImage, unsigned int >( image ) );
86     else if( choice == "unsigned long" )
87       return( this->_GD1< _TImage, unsigned long >( image ) );
88     else return( "BinaryThresholdImageFilter: no valid output type." );
89   }
90   else
91     return(
92       "ImageFilters::BinaryThresholdImageFilter: No valid input image."
93       );
94 }
95
96 // -------------------------------------------------------------------------
97 template< class _TImage, class _TBinaryPixel >
98 std::string cpPluginsImageFilters::BinaryThresholdImageFilter::
99 _GD1( _TImage* image )
100 {
101   typedef itk::Image< _TBinaryPixel, _TImage::ImageDimension > _TBinaryImage;
102   typedef itk::BinaryThresholdImageFilter< _TImage, _TBinaryImage > _F;
103   typedef typename _TImage::PixelType _TP;
104   typedef typename _TBinaryImage::PixelType _UP;
105
106   // Get parameters
107   _TP lower_val = _TP( this->m_Parameters.GetReal( "LowerThresholdValue" ) );
108   _TP upper_val = _TP( this->m_Parameters.GetReal( "UpperThresholdValue" ) );
109   _UP in_val    = _UP( this->m_Parameters.GetReal( "InsideValue" ) );
110   _UP out_val   = _UP( this->m_Parameters.GetReal( "OutsideValue" ) );
111
112   // Configure filter
113   _F* filter = this->_CreateITK< _F >( );
114   filter->SetInput( image );
115   filter->SetLowerThreshold( lower_val );
116   filter->SetUpperThreshold( upper_val );
117   filter->SetInsideValue( in_val );
118   filter->SetOutsideValue( out_val );
119   filter->Update( );
120
121   // Connect output
122   this->GetOutputData( "Output" )->SetITK( filter->GetOutput( ) );
123   return( "" );
124 }
125
126 // eof - $RCSfile$