]> Creatis software - cpPlugins.git/blob - lib/cpExtensions/Algorithms/RegionOfInterestImageCalculator.hxx
yet another refactoring
[cpPlugins.git] / lib / cpExtensions / Algorithms / RegionOfInterestImageCalculator.hxx
1 // -------------------------------------------------------------------------
2 // @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
3 // -------------------------------------------------------------------------
4
5 #ifndef __CPEXTENSIONS__ALGORITHMS__REGIONOFINTERESTIMAGECALCULATOR__HXX__
6 #define __CPEXTENSIONS__ALGORITHMS__REGIONOFINTERESTIMAGECALCULATOR__HXX__
7
8 #include <itkImageRegionConstIteratorWithIndex.h>
9
10 // -------------------------------------------------------------------------
11 template< class _TImage >
12 void cpExtensions::Algorithms::RegionOfInterestImageCalculator< _TImage >::
13 Compute( )
14 {
15   typedef itk::ImageRegionConstIteratorWithIndex< _TImage > _TIterator;
16   typedef typename _TImage::RegionType _TRegion;
17
18   if( this->m_Image.IsNotNull( ) )
19   {
20     _TRegion region = this->m_Image->GetRequestedRegion( );
21     TIndex minidx = region.GetIndex( );
22     TIndex maxidx = minidx + region.GetSize( );
23     for( unsigned int d = 0; d < _TImage::ImageDimension; ++d )
24       maxidx[ d ] -= 1;
25
26     bool first = true;
27     _TIterator i( this->m_Image, region );
28     for( i.GoToBegin( ); !i.IsAtEnd( ); ++i )
29     {
30       if( i.Get( ) != this->m_BackgroundValue )
31       {
32         TIndex idx = i.GetIndex( );
33         if( !first )
34         {
35           for( unsigned int d = 0; d < _TImage::ImageDimension; ++d )
36           {
37             minidx[ d ] = ( idx[ d ] < minidx[ d ] )? idx[ d ]: minidx[ d ];
38             maxidx[ d ] = ( idx[ d ] > maxidx[ d ] )? idx[ d ]: maxidx[ d ];
39
40           } // rof
41         }
42         else
43         {
44           minidx = maxidx = idx;
45           first = false;
46
47         } // fi
48
49       } // fi
50
51     } // rof
52     this->m_Minimum = minidx;
53     this->m_Maximum = maxidx;
54   }
55   else
56   {
57     this->m_Minimum.Fill( 0 );
58     this->m_Maximum.Fill( 0 );
59
60   } // fi
61 }
62       
63 // -------------------------------------------------------------------------
64 template< class _TImage >
65 cpExtensions::Algorithms::RegionOfInterestImageCalculator< _TImage >::
66 RegionOfInterestImageCalculator( )
67   : Superclass( ),
68     m_BackgroundValue( TPixel( 0 ) )
69 {
70   this->m_Minimum.Fill( 0 );
71   this->m_Maximum.Fill( 0 );
72 }
73
74 // -------------------------------------------------------------------------
75 template< class _TImage >
76 cpExtensions::Algorithms::RegionOfInterestImageCalculator< _TImage >::
77 ~RegionOfInterestImageCalculator( )
78 {
79 }
80
81
82 /* TODO
83    typename _TImage::ConstPointer m_Image;
84    TPixel m_BackgroundValue;
85    TIndex m_Minimum;
86    TIndex m_Maximum;
87    };
88 */
89
90 #endif // __CPEXTENSIONS__ALGORITHMS__REGIONOFINTERESTIMAGECALCULATOR__HXX__
91
92 // eof - $RCSfile$