]> Creatis software - cpPlugins.git/blob - lib/cpExtensions/Algorithms/LightCompensationFilter.hxx
...
[cpPlugins.git] / lib / cpExtensions / Algorithms / LightCompensationFilter.hxx
1 // -------------------------------------------------------------------------
2 // @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
3 // -------------------------------------------------------------------------
4
5 #ifndef __CPEXTENSIONS__ALGORITHMS__LIGHTCOMPENSATIONFILTER__HXX__
6 #define __CPEXTENSIONS__ALGORITHMS__LIGHTCOMPENSATIONFILTER__HXX__
7
8 #include <itkImageRegionIterator.h>
9 #include <itkImageRegionConstIterator.h>
10 #include <itkNumericTraits.h>
11
12 // -------------------------------------------------------------------------
13 template< class I >
14 cpExtensions::Algorithms::LightCompensationFilter< I >::
15 LightCompensationFilter( )
16   : Superclass( )
17 {
18   this->SetNumberOfRequiredInputs( 1 );
19   this->InPlaceOff( );
20 }
21
22 // -------------------------------------------------------------------------
23 template< class I >
24 cpExtensions::Algorithms::LightCompensationFilter< I >::
25 ~LightCompensationFilter( )
26 {
27 }
28
29 // -------------------------------------------------------------------------
30 template< class I >
31 void cpExtensions::Algorithms::LightCompensationFilter< I >::
32 BeforeThreadedGenerateData( )
33 {
34   this->Superclass::BeforeThreadedGenerateData( );
35
36   I* input = const_cast< I* >( this->GetInput( ) );
37
38   typename TMeanCalculator::Pointer mc = TMeanCalculator::New( );
39   mc->Execute( input, input->GetRequestedRegion( ) );
40   this->m_Mean = mc->GetMean( );
41
42   double gm = this->m_Mean[ 0 ] + this->m_Mean[ 1 ] + this->m_Mean[ 2 ];
43   gm /= double( 3 );
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 ];
49 }
50
51 // -------------------------------------------------------------------------
52 template< class I >
53 void cpExtensions::Algorithms::LightCompensationFilter< I >::
54 ThreadedGenerateData( const TRegion& region, itk::ThreadIdType id )
55 {
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( ) );
60
61   itk::ImageRegionConstIterator< I > iIt( this->GetInput( ), region );
62   itk::ImageRegionIterator< I > oIt( this->GetOutput( ), region );
63
64   iIt.GoToBegin( );
65   oIt.GoToBegin( );
66   for( ; !iIt.IsAtEnd( ); ++iIt, ++oIt )
67   {
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 ];
71
72     if( r > max_value ) r = max_value;
73     if( g > max_value ) g = max_value;
74     if( b > max_value ) b = max_value;
75
76     typename I::PixelType pix;
77     pix.SetRed( _TPixelChannel( r ) );
78     pix.SetGreen( _TPixelChannel( g ) );
79     pix.SetBlue( _TPixelChannel( b ) );
80
81     oIt.Set( pix );
82
83   } // rof
84 }
85
86 // -------------------------------------------------------------------------
87 template< class I >
88 void cpExtensions::Algorithms::LightCompensationFilter< I >::
89 AfterThreadedGenerateData( )
90 {
91   this->Superclass::AfterThreadedGenerateData( );
92 }
93
94 #endif // __CPEXTENSIONS__ALGORITHMS__LIGHTCOMPENSATIONFILTER__HXX__
95
96 // eof - $RCSfile$