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 ];
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;
52 this->m_Region.SetIndex( min_idx );
53 this->m_Region.SetSize( size );
56 // -------------------------------------------------------------------------
57 template< class _TImage >
58 cpExtensions::Algorithms::ImageToBoundingBoxFromThreshold< _TImage >::
59 ImageToBoundingBoxFromThreshold( )
61 m_LowerThreshold( std::numeric_limits< TPixel >::min( ) ),
62 m_UpperThreshold( std::numeric_limits< TPixel >::max( ) )
66 // -------------------------------------------------------------------------
67 template< class _TImage >
68 cpExtensions::Algorithms::ImageToBoundingBoxFromThreshold< _TImage >::
69 ~ImageToBoundingBoxFromThreshold( )
73 #endif // __cpExtensions__Algorithms__ImageToBoundingBoxFromThreshold__hxx__