1 // -------------------------------------------------------------------------
2 // @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
3 // -------------------------------------------------------------------------
5 #ifndef __cpExtensions__Algorithms__ImageToBoundingBoxFromThreshold__hxx__
6 #define __cpExtensions__Algorithms__ImageToBoundingBoxFromThreshold__hxx__
9 #include <itkImageRegionConstIteratorWithIndex.h>
11 // -------------------------------------------------------------------------
12 template< class _TImage >
13 void cpExtensions::Algorithms::ImageToBoundingBoxFromThreshold< _TImage >::
16 typedef itk::ImageRegionConstIteratorWithIndex< _TImage > _TIt;
18 TIndex min_idx, max_idx;
22 if( this->m_Image.IsNotNull( ) )
24 auto region = this->m_Image->GetRequestedRegion( );
25 min_idx = max_idx = region.GetIndex( );
26 min_idx += region.GetSize( );
28 _TIt i( this->m_Image, region );
29 for( i.GoToBegin( ); !i.IsAtEnd( ); ++i )
32 if( this->m_LowerThreshold <= v && v <= this->m_UpperThreshold )
34 auto idx = i.GetIndex( );
35 for( unsigned int d = 0; d < _TImage::ImageDimension; ++d )
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 ];
47 for( unsigned int d = 0; d < _TImage::ImageDimension; ++d )
49 min_idx[ d ] -= long( this->m_PAD );
50 max_idx[ d ] += long( this->m_PAD );
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 ];
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;
65 this->m_Region.SetIndex( min_idx );
66 this->m_Region.SetSize( size );
69 // -------------------------------------------------------------------------
70 template< class _TImage >
71 cpExtensions::Algorithms::ImageToBoundingBoxFromThreshold< _TImage >::
72 ImageToBoundingBoxFromThreshold( )
74 m_LowerThreshold( std::numeric_limits< TPixel >::min( ) ),
75 m_UpperThreshold( std::numeric_limits< TPixel >::max( ) ),
80 // -------------------------------------------------------------------------
81 template< class _TImage >
82 cpExtensions::Algorithms::ImageToBoundingBoxFromThreshold< _TImage >::
83 ~ImageToBoundingBoxFromThreshold( )
87 #endif // __cpExtensions__Algorithms__ImageToBoundingBoxFromThreshold__hxx__