]> Creatis software - cpPlugins.git/blob - lib/cpExtensions/Algorithms/ImageToBoundingBoxFromThreshold.hxx
...
[cpPlugins.git] / lib / cpExtensions / Algorithms / ImageToBoundingBoxFromThreshold.hxx
1 // -------------------------------------------------------------------------
2 // @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
3 // -------------------------------------------------------------------------
4
5 #ifndef __cpExtensions__Algorithms__ImageToBoundingBoxFromThreshold__hxx__
6 #define __cpExtensions__Algorithms__ImageToBoundingBoxFromThreshold__hxx__
7
8 #include <limits>
9 #include <itkImageRegionConstIteratorWithIndex.h>
10
11 // -------------------------------------------------------------------------
12 template< class _TImage >
13 void cpExtensions::Algorithms::ImageToBoundingBoxFromThreshold< _TImage >::
14 Compute( )
15 {
16   typedef itk::ImageRegionConstIteratorWithIndex< _TImage > _TIt;
17
18   TIndex min_idx, max_idx;
19   min_idx.Fill( 0 );
20   max_idx.Fill( -1 );
21
22   if( this->m_Image.IsNotNull( ) )
23   {
24     auto region = this->m_Image->GetRequestedRegion( );
25     min_idx = max_idx = region.GetIndex( );
26     min_idx += region.GetSize( );
27
28     _TIt i( this->m_Image, region );
29     for( i.GoToBegin( ); !i.IsAtEnd( ); ++i )
30     {
31       auto v = i.Get( );
32       if( this->m_LowerThreshold <= v && v <= this->m_UpperThreshold )
33       {
34         auto idx = i.GetIndex( );
35         for( unsigned int d = 0; d < _TImage::ImageDimension; ++d )
36         {
37           min_idx[ d ] = ( idx[ d ] < min_idx[ d ] )? idx[ d ]: min_idx[ d ]; 
38           max_idx[ d ] = ( idx[ d ] > max_idx[ d ] )? idx[ d ]: max_idx[ d ]; 
39  
40         } // rof
41
42       } // fi
43
44     } // rof
45
46   } // fi
47
48   typename _TImage::SizeType size;
49   for( unsigned int d = 0; d < _TImage::ImageDimension; ++d )
50     size[ d ] = max_idx[ d ] - min_idx[ d ] + 1;
51   
52   this->m_Region.SetIndex( min_idx );
53   this->m_Region.SetSize( size );
54 }
55
56 // -------------------------------------------------------------------------
57 template< class _TImage >
58 cpExtensions::Algorithms::ImageToBoundingBoxFromThreshold< _TImage >::
59 ImageToBoundingBoxFromThreshold( )
60   : Superclass( ),
61     m_LowerThreshold( std::numeric_limits< TPixel >::min( ) ),
62     m_UpperThreshold( std::numeric_limits< TPixel >::max( ) )
63 {
64 }
65
66 // -------------------------------------------------------------------------
67 template< class _TImage >
68 cpExtensions::Algorithms::ImageToBoundingBoxFromThreshold< _TImage >::
69 ~ImageToBoundingBoxFromThreshold( )
70 {
71 }
72
73 #endif // __cpExtensions__Algorithms__ImageToBoundingBoxFromThreshold__hxx__
74
75 // eof - $RCSfile$