1 // -------------------------------------------------------------------------
2 // @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
3 // -------------------------------------------------------------------------
5 #ifndef __CPEXTENSIONS__ALGORITHMS__LIGHTCOMPENSATIONFILTER__HXX__
6 #define __CPEXTENSIONS__ALGORITHMS__LIGHTCOMPENSATIONFILTER__HXX__
8 #include <itkImageRegionIterator.h>
9 #include <itkImageRegionConstIterator.h>
10 #include <itkNumericTraits.h>
12 // -------------------------------------------------------------------------
14 cpExtensions::Algorithms::LightCompensationFilter< I >::
15 LightCompensationFilter( )
18 this->SetNumberOfRequiredInputs( 1 );
22 // -------------------------------------------------------------------------
24 cpExtensions::Algorithms::LightCompensationFilter< I >::
25 ~LightCompensationFilter( )
29 // -------------------------------------------------------------------------
31 void cpExtensions::Algorithms::LightCompensationFilter< I >::
32 BeforeThreadedGenerateData( )
34 this->Superclass::BeforeThreadedGenerateData( );
36 I* input = const_cast< I* >( this->GetInput( ) );
38 typename TMeanCalculator::Pointer mc = TMeanCalculator::New( );
39 mc->Execute( input, input->GetRequestedRegion( ) );
40 this->m_Mean = mc->GetMean( );
42 double gm = this->m_Mean[ 0 ] + this->m_Mean[ 1 ] + this->m_Mean[ 2 ];
44 this->m_Coefficient = this->m_Mean;
45 this->m_Coefficient.Fill( gm );
46 this->m_Coefficient[ 0 ] /= this->m_Mean[ 0 ];
47 this->m_Coefficient[ 1 ] /= this->m_Mean[ 1 ];
48 this->m_Coefficient[ 2 ] /= this->m_Mean[ 2 ];
51 // -------------------------------------------------------------------------
53 void cpExtensions::Algorithms::LightCompensationFilter< I >::
54 ThreadedGenerateData( const TRegion& region, itk::ThreadIdType id )
56 typedef itk::NumericTraits< typename I::PixelType > _TPixelTraits;
57 typedef typename _TPixelTraits::ValueType _TPixelChannel;
58 typedef itk::NumericTraits< _TPixelChannel > _TChannelTraits;
59 static const double max_value = double( _TChannelTraits::max( ) );
61 itk::ImageRegionConstIterator< I > iIt( this->GetInput( ), region );
62 itk::ImageRegionIterator< I > oIt( this->GetOutput( ), region );
66 for( ; !iIt.IsAtEnd( ); ++iIt, ++oIt )
68 double r = double( iIt.Get( )[ 0 ] ) * this->m_Coefficient[ 0 ];
69 double g = double( iIt.Get( )[ 1 ] ) * this->m_Coefficient[ 1 ];
70 double b = double( iIt.Get( )[ 2 ] ) * this->m_Coefficient[ 2 ];
72 if( r > max_value ) r = max_value;
73 if( g > max_value ) g = max_value;
74 if( b > max_value ) b = max_value;
76 typename I::PixelType pix;
77 pix.SetRed( _TPixelChannel( r ) );
78 pix.SetGreen( _TPixelChannel( g ) );
79 pix.SetBlue( _TPixelChannel( b ) );
86 // -------------------------------------------------------------------------
88 void cpExtensions::Algorithms::LightCompensationFilter< I >::
89 AfterThreadedGenerateData( )
91 this->Superclass::AfterThreadedGenerateData( );
94 #endif // __CPEXTENSIONS__ALGORITHMS__LIGHTCOMPENSATIONFILTER__HXX__