]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Extensions/Algorithms/RGBImageToHSVChannelsFilter.hxx
Generic gaussian model estimator added
[cpPlugins.git] / lib / cpPlugins / Extensions / Algorithms / RGBImageToHSVChannelsFilter.hxx
1 // -------------------------------------------------------------------------
2 // @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
3 // -------------------------------------------------------------------------
4
5 #ifndef __CPPLUGINS__EXTENSIONS__ALGORITHMS__RGBIMAGETOHSVCHANNELSFILTER__HXX__
6 #define __CPPLUGINS__EXTENSIONS__ALGORITHMS__RGBIMAGETOHSVCHANNELSFILTER__HXX__
7
8 #include <cmath>
9 #include <limits>
10 #include <vnl/vnl_math.h>
11
12 #include <itkImageRegionIterator.h>
13 #include <itkImageRegionConstIterator.h>
14
15 // -------------------------------------------------------------------------
16 template< class I, class O >
17 O* cpPlugins::Extensions::Algorithms::RGBImageToHSVChannelsFilter< I, O >::
18 GetHueOutput( )
19 {
20   return( this->GetOutput( 0 ) );
21 }
22
23 // -------------------------------------------------------------------------
24 template< class I, class O >
25 O* cpPlugins::Extensions::Algorithms::RGBImageToHSVChannelsFilter< I, O >::
26 GetSaturationOutput( )
27 {
28   return( this->GetOutput( 1 ) );
29 }
30
31 // -------------------------------------------------------------------------
32 template< class I, class O >
33 O* cpPlugins::Extensions::Algorithms::RGBImageToHSVChannelsFilter< I, O >::
34 GetValueOutput( )
35 {
36   return( this->GetOutput( 2 ) );
37 }
38
39 // -------------------------------------------------------------------------
40 template< class I, class O >
41 const O*
42 cpPlugins::Extensions::Algorithms::RGBImageToHSVChannelsFilter< I, O >::
43 GetHueOutput( ) const
44 {
45   if( this->GetNumberOfOutputs( ) > 0 )
46     return(
47       dynamic_cast< const O* >(
48         this->itk::ProcessObject::GetOutput( 0 )
49         )
50       );
51   else
52     return( NULL );
53 }
54
55 // -------------------------------------------------------------------------
56 template< class I, class O >
57 const O*
58 cpPlugins::Extensions::Algorithms::RGBImageToHSVChannelsFilter< I, O >::
59 GetSaturationOutput( ) const
60 {
61   if( this->GetNumberOfOutputs( ) > 1 )
62     return(
63       dynamic_cast< const O* >(
64         this->itk::ProcessObject::GetOutput( 1 )
65         )
66       );
67   else
68     return( NULL );
69 }
70
71 // -------------------------------------------------------------------------
72 template< class I, class O >
73 const O*
74 cpPlugins::Extensions::Algorithms::RGBImageToHSVChannelsFilter< I, O >::
75 GetValueOutput( ) const
76 {
77   if( this->GetNumberOfOutputs( ) > 2 )
78     return(
79       dynamic_cast< const O* >(
80         this->itk::ProcessObject::GetOutput( 2 )
81         )
82       );
83   else
84     return( NULL );
85 }
86
87 // -------------------------------------------------------------------------
88 template< class I, class O >
89 void
90 cpPlugins::Extensions::Algorithms::RGBImageToHSVChannelsFilter< I, O >::
91 GraftHueOutput( O* hue )
92 {
93   this->GraftNthOutput( 0, hue );
94 }
95
96 // -------------------------------------------------------------------------
97 template< class I, class O >
98 void
99 cpPlugins::Extensions::Algorithms::RGBImageToHSVChannelsFilter< I, O >::
100 GraftSaturationOutput( O* saturation )
101 {
102   this->GraftNthOutput( 1, saturation );
103 }
104
105 // -------------------------------------------------------------------------
106 template< class I, class O >
107 void
108 cpPlugins::Extensions::Algorithms::RGBImageToHSVChannelsFilter< I, O >::
109 GraftValueOutput( O* value )
110 {
111   this->GraftNthOutput( 2, value );
112 }
113
114 // -------------------------------------------------------------------------
115 template< class I, class O >
116 cpPlugins::Extensions::Algorithms::RGBImageToHSVChannelsFilter< I, O >::
117 RGBImageToHSVChannelsFilter( )
118   : Superclass( )
119 {
120   this->SetNumberOfRequiredInputs( 1 );
121   this->SetNumberOfRequiredOutputs( 3 );
122   for( unsigned int i = 0; i < 3; i++ )
123   {
124     typename O::Pointer o = O::New( );
125     this->SetNthOutput( i, o );
126
127   } // rof
128 }
129
130 // -------------------------------------------------------------------------
131 template< class I, class O >
132 cpPlugins::Extensions::Algorithms::RGBImageToHSVChannelsFilter< I, O >::
133 ~RGBImageToHSVChannelsFilter( )
134 {
135 }
136
137 // -------------------------------------------------------------------------
138 template< class I, class O >
139 void
140 cpPlugins::Extensions::Algorithms::RGBImageToHSVChannelsFilter< I, O >::
141 BeforeThreadedGenerateData( )
142 {
143 }
144
145 // -------------------------------------------------------------------------
146 template< class I, class O >
147 void cpPlugins::Extensions::Algorithms::RGBImageToHSVChannelsFilter< I, O >::
148 AfterThreadedGenerateData( )
149 {
150 }
151
152 // -------------------------------------------------------------------------
153 template< class I, class O >
154 void
155 cpPlugins::Extensions::Algorithms::RGBImageToHSVChannelsFilter< I, O >::
156 ThreadedGenerateData(
157   const typename Superclass::OutputImageRegionType& region,
158   itk::ThreadIdType threadId
159   )
160 {
161   typedef itk::ImageRegionConstIterator< I > TInIt;
162   typedef itk::ImageRegionIterator< O > TOutIt;
163
164   TInIt inIt( this->GetInput( ), region );
165   TOutIt hIt( this->GetHueOutput( ), region );
166   TOutIt sIt( this->GetSaturationOutput( ), region );
167   TOutIt vIt( this->GetValueOutput( ), region );
168   inIt.GoToBegin( );
169   hIt.GoToBegin( );
170   sIt.GoToBegin( );
171   vIt.GoToBegin( );
172   for( ; !inIt.IsAtEnd( ); ++inIt, ++hIt, ++sIt, ++vIt )
173   {
174     TOutputPixel H, S, V;
175     Self::_RGB2HSV( inIt.Get( ), H, S, V );
176     hIt.Set( H );
177     sIt.Set( S );
178     vIt.Set( V );
179
180   } // rof
181 }
182
183 // -------------------------------------------------------------------------
184 template< class I, class O >
185 void cpPlugins::Extensions::Algorithms::RGBImageToHSVChannelsFilter< I, O >::
186 _RGB2HSV(
187   const TInputPixel& RGB,
188   TOutputPixel& H, TOutputPixel& S, TOutputPixel& V
189   )
190 {
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 );
198
199   double R = double( RGB.GetRed( ) );
200   double G = double( RGB.GetGreen( ) );
201   double B = double( RGB.GetBlue( ) );
202   double sRGB = R + G + B;
203   double RG = R - G;
204   double RB = R - B;
205   double GB = G - B;
206
207   // Hue
208   double A = std::sqrt( ( RG * RG ) + ( RB * GB ) );
209   if( A != _0 )
210     A = std::acos( ( RG + RB ) / ( _2 * A ) );
211   A /= _2pi;
212   H = TOutputPixel( mVal * ( ( G >= B )? A: _1 - A ) );
213
214   // Saturation
215   if( sRGB != _0 )
216   {
217     double C = ( G < R )? G: R;
218     C        = ( B < C )? B: C;
219     S = TOutputPixel( mVal * ( _1 - ( ( _3 * C ) / sRGB ) ) );
220   }
221   else
222     S = TOutputPixel( 0 );
223   
224   // Value
225   V = TOutputPixel( sRGB / _3 );
226 }
227
228 #endif // __CPPLUGINS__EXTENSIONS__ALGORITHMS__RGBIMAGETOHSVCHANNELSFILTER__HXX__
229
230 // eof - $RCSfile$