1 // -------------------------------------------------------------------------
2 // @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
3 // -------------------------------------------------------------------------
5 #ifndef __CPPLUGINS__EXTENSIONS__ALGORITHMS__LIGHTCOMPENSATIONFILTER__HXX__
6 #define __CPPLUGINS__EXTENSIONS__ALGORITHMS__LIGHTCOMPENSATIONFILTER__HXX__
8 #include <itkImageRegionIterator.h>
9 #include <itkImageRegionConstIterator.h>
10 #include <itkNumericTraits.h>
12 // -------------------------------------------------------------------------
14 cpPlugins::Extensions::Algorithms::LightCompensationFilter< I >::
15 LightCompensationFilter( )
18 this->SetNumberOfRequiredInputs( 1 );
22 // -------------------------------------------------------------------------
24 cpPlugins::Extensions::Algorithms::LightCompensationFilter< I >::
25 ~LightCompensationFilter( )
29 // -------------------------------------------------------------------------
31 void cpPlugins::Extensions::Algorithms::LightCompensationFilter< I >::
32 BeforeThreadedGenerateData( )
34 I* input = const_cast< I* >( this->GetInput( ) );
36 typename TMeanCalculator::Pointer mc = TMeanCalculator::New( );
37 mc->Execute( input, input->GetRequestedRegion( ) );
38 this->m_Mean = mc->GetMean( );
40 double gm = this->m_Mean[ 0 ] + this->m_Mean[ 1 ] + this->m_Mean[ 2 ];
42 this->m_Coefficient = this->m_Mean;
43 this->m_Coefficient.Fill( gm );
44 this->m_Coefficient[ 0 ] /= this->m_Mean[ 0 ];
45 this->m_Coefficient[ 1 ] /= this->m_Mean[ 1 ];
46 this->m_Coefficient[ 2 ] /= this->m_Mean[ 2 ];
49 // -------------------------------------------------------------------------
51 void cpPlugins::Extensions::Algorithms::LightCompensationFilter< I >::
52 ThreadedGenerateData( const TRegion& region, itk::ThreadIdType id )
54 typedef itk::NumericTraits< typename I::PixelType > _TPixelTraits;
55 typedef typename _TPixelTraits::ValueType _TPixelChannel;
56 typedef itk::NumericTraits< _TPixelChannel > _TChannelTraits;
57 static const double max_value = double( _TChannelTraits::max( ) );
59 itk::ImageRegionConstIterator< I > iIt( this->GetInput( ), region );
60 itk::ImageRegionIterator< I > oIt( this->GetOutput( ), region );
64 for( ; !iIt.IsAtEnd( ); ++iIt, ++oIt )
66 double r = double( iIt.Get( )[ 0 ] ) * this->m_Coefficient[ 0 ];
67 double g = double( iIt.Get( )[ 1 ] ) * this->m_Coefficient[ 1 ];
68 double b = double( iIt.Get( )[ 2 ] ) * this->m_Coefficient[ 2 ];
69 if( r > max_value ) r = max_value;
70 if( g > max_value ) g = max_value;
71 if( b > max_value ) b = max_value;
73 _TPixelChannel( r ), _TPixelChannel( g ), _TPixelChannel( b )
79 // -------------------------------------------------------------------------
81 void cpPlugins::Extensions::Algorithms::LightCompensationFilter< I >::
82 AfterThreadedGenerateData( )
86 #endif // __CPPLUGINS__EXTENSIONS__ALGORITHMS__LIGHTCOMPENSATIONFILTER__HXX__