]> Creatis software - cpPlugins.git/blob - lib/cpExtensions/Algorithms/ImageToBoundingBoxFromThreshold.hxx
yet another refactoring
[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     // Fix indices
47     for( unsigned int d = 0; d < _TImage::ImageDimension; ++d )
48     {
49       min_idx[ d ] -= long( this->m_PAD );
50       max_idx[ d ] += long( this->m_PAD );
51
52       if( min_idx[ d ] < region.GetIndex( )[ d ] )
53         min_idx[ d ] = region.GetIndex( )[ d ];
54       if( max_idx[ d ] > region.GetIndex( )[ d ] + region.GetSize( )[ d ] )
55         max_idx[ d ] = region.GetIndex( )[ d ] + region.GetSize( )[ d ];
56
57     } // rof
58
59   } // fi
60
61   typename _TImage::SizeType size;
62   for( unsigned int d = 0; d < _TImage::ImageDimension; ++d )
63     size[ d ] = max_idx[ d ] - min_idx[ d ] + 1;
64   
65   this->m_Region.SetIndex( min_idx );
66   this->m_Region.SetSize( size );
67 }
68
69 // -------------------------------------------------------------------------
70 template< class _TImage >
71 cpExtensions::Algorithms::ImageToBoundingBoxFromThreshold< _TImage >::
72 ImageToBoundingBoxFromThreshold( )
73   : Superclass( ),
74     m_LowerThreshold( std::numeric_limits< TPixel >::min( ) ),
75     m_UpperThreshold( std::numeric_limits< TPixel >::max( ) ),
76     m_PAD( 0 )
77 {
78 }
79
80 // -------------------------------------------------------------------------
81 template< class _TImage >
82 cpExtensions::Algorithms::ImageToBoundingBoxFromThreshold< _TImage >::
83 ~ImageToBoundingBoxFromThreshold( )
84 {
85 }
86
87 #endif // __cpExtensions__Algorithms__ImageToBoundingBoxFromThreshold__hxx__
88
89 // eof - $RCSfile$