1 // -------------------------------------------------------------------------
2 // @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
3 // -------------------------------------------------------------------------
5 #ifndef __CPPLUGINS__EXTENSIONS__ALGORITHMS__RGBIMAGETOHSVCHANNELSFILTER__HXX__
6 #define __CPPLUGINS__EXTENSIONS__ALGORITHMS__RGBIMAGETOHSVCHANNELSFILTER__HXX__
10 #include <vnl/vnl_math.h>
12 #include <itkImageRegionIterator.h>
13 #include <itkImageRegionConstIterator.h>
15 // -------------------------------------------------------------------------
16 template< class I, class O >
17 O* cpPlugins::Extensions::Algorithms::RGBImageToHSVChannelsFilter< I, O >::
20 return( this->GetOutput( 0 ) );
23 // -------------------------------------------------------------------------
24 template< class I, class O >
25 O* cpPlugins::Extensions::Algorithms::RGBImageToHSVChannelsFilter< I, O >::
26 GetSaturationOutput( )
28 return( this->GetOutput( 1 ) );
31 // -------------------------------------------------------------------------
32 template< class I, class O >
33 O* cpPlugins::Extensions::Algorithms::RGBImageToHSVChannelsFilter< I, O >::
36 return( this->GetOutput( 2 ) );
39 // -------------------------------------------------------------------------
40 template< class I, class O >
42 cpPlugins::Extensions::Algorithms::RGBImageToHSVChannelsFilter< I, O >::
45 if( this->GetNumberOfOutputs( ) > 0 )
47 dynamic_cast< const O* >(
48 this->itk::ProcessObject::GetOutput( 0 )
55 // -------------------------------------------------------------------------
56 template< class I, class O >
58 cpPlugins::Extensions::Algorithms::RGBImageToHSVChannelsFilter< I, O >::
59 GetSaturationOutput( ) const
61 if( this->GetNumberOfOutputs( ) > 1 )
63 dynamic_cast< const O* >(
64 this->itk::ProcessObject::GetOutput( 1 )
71 // -------------------------------------------------------------------------
72 template< class I, class O >
74 cpPlugins::Extensions::Algorithms::RGBImageToHSVChannelsFilter< I, O >::
75 GetValueOutput( ) const
77 if( this->GetNumberOfOutputs( ) > 2 )
79 dynamic_cast< const O* >(
80 this->itk::ProcessObject::GetOutput( 2 )
87 // -------------------------------------------------------------------------
88 template< class I, class O >
90 cpPlugins::Extensions::Algorithms::RGBImageToHSVChannelsFilter< I, O >::
91 GraftHueOutput( O* hue )
93 this->GraftNthOutput( 0, hue );
96 // -------------------------------------------------------------------------
97 template< class I, class O >
99 cpPlugins::Extensions::Algorithms::RGBImageToHSVChannelsFilter< I, O >::
100 GraftSaturationOutput( O* saturation )
102 this->GraftNthOutput( 1, saturation );
105 // -------------------------------------------------------------------------
106 template< class I, class O >
108 cpPlugins::Extensions::Algorithms::RGBImageToHSVChannelsFilter< I, O >::
109 GraftValueOutput( O* value )
111 this->GraftNthOutput( 2, value );
114 // -------------------------------------------------------------------------
115 template< class I, class O >
116 cpPlugins::Extensions::Algorithms::RGBImageToHSVChannelsFilter< I, O >::
117 RGBImageToHSVChannelsFilter( )
120 this->SetNumberOfRequiredInputs( 1 );
121 this->SetNumberOfRequiredOutputs( 3 );
122 for( unsigned int i = 0; i < 3; i++ )
124 typename O::Pointer o = O::New( );
125 this->SetNthOutput( i, o );
130 // -------------------------------------------------------------------------
131 template< class I, class O >
132 cpPlugins::Extensions::Algorithms::RGBImageToHSVChannelsFilter< I, O >::
133 ~RGBImageToHSVChannelsFilter( )
137 // -------------------------------------------------------------------------
138 template< class I, class O >
140 cpPlugins::Extensions::Algorithms::RGBImageToHSVChannelsFilter< I, O >::
141 BeforeThreadedGenerateData( )
145 // -------------------------------------------------------------------------
146 template< class I, class O >
147 void cpPlugins::Extensions::Algorithms::RGBImageToHSVChannelsFilter< I, O >::
148 AfterThreadedGenerateData( )
152 // -------------------------------------------------------------------------
153 template< class I, class O >
155 cpPlugins::Extensions::Algorithms::RGBImageToHSVChannelsFilter< I, O >::
156 ThreadedGenerateData(
157 const typename Superclass::OutputImageRegionType& region,
158 itk::ThreadIdType threadId
161 typedef itk::ImageRegionConstIterator< I > TInIt;
162 typedef itk::ImageRegionIterator< O > TOutIt;
164 TInIt inIt( this->GetInput( ), region );
165 TOutIt hIt( this->GetHueOutput( ), region );
166 TOutIt sIt( this->GetSaturationOutput( ), region );
167 TOutIt vIt( this->GetValueOutput( ), region );
172 for( ; !inIt.IsAtEnd( ); ++inIt, ++hIt, ++sIt, ++vIt )
174 TOutputPixel H, S, V;
175 Self::_RGB2HSV( inIt.Get( ), H, S, V );
183 // -------------------------------------------------------------------------
184 template< class I, class O >
185 void cpPlugins::Extensions::Algorithms::RGBImageToHSVChannelsFilter< I, O >::
187 const TInputPixel& RGB,
188 TOutputPixel& H, TOutputPixel& S, TOutputPixel& V
191 typedef typename TInputPixel::ComponentType TComponent;
192 const double mVal = double( std::numeric_limits< TComponent >::max( ) );
193 const double _0 = double( 0 );
194 const double _1 = double( 1 );
195 const double _2 = double( 2 );
196 const double _3 = double( 3 );
197 const double _2pi = _2 * double( vnl_math::pi );
199 double R = double( RGB.GetRed( ) );
200 double G = double( RGB.GetGreen( ) );
201 double B = double( RGB.GetBlue( ) );
202 double sRGB = R + G + B;
208 double A = std::sqrt( ( RG * RG ) + ( RB * GB ) );
210 A = std::acos( ( RG + RB ) / ( _2 * A ) );
212 H = TOutputPixel( mVal * ( ( G >= B )? A: _1 - A ) );
217 double C = ( G < R )? G: R;
219 S = TOutputPixel( mVal * ( _1 - ( ( _3 * C ) / sRGB ) ) );
222 S = TOutputPixel( 0 );
225 V = TOutputPixel( sRGB / _3 );
228 #endif // __CPPLUGINS__EXTENSIONS__ALGORITHMS__RGBIMAGETOHSVCHANNELSFILTER__HXX__