]> Creatis software - cpPlugins.git/blobdiff - lib/cpExtensions/Algorithms/ImageToBoundingBoxFromThreshold.hxx
yet another refactoring
[cpPlugins.git] / lib / cpExtensions / Algorithms / ImageToBoundingBoxFromThreshold.hxx
diff --git a/lib/cpExtensions/Algorithms/ImageToBoundingBoxFromThreshold.hxx b/lib/cpExtensions/Algorithms/ImageToBoundingBoxFromThreshold.hxx
new file mode 100644 (file)
index 0000000..55659eb
--- /dev/null
@@ -0,0 +1,89 @@
+// -------------------------------------------------------------------------
+// @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
+// -------------------------------------------------------------------------
+
+#ifndef __cpExtensions__Algorithms__ImageToBoundingBoxFromThreshold__hxx__
+#define __cpExtensions__Algorithms__ImageToBoundingBoxFromThreshold__hxx__
+
+#include <limits>
+#include <itkImageRegionConstIteratorWithIndex.h>
+
+// -------------------------------------------------------------------------
+template< class _TImage >
+void cpExtensions::Algorithms::ImageToBoundingBoxFromThreshold< _TImage >::
+Compute( )
+{
+  typedef itk::ImageRegionConstIteratorWithIndex< _TImage > _TIt;
+
+  TIndex min_idx, max_idx;
+  min_idx.Fill( 0 );
+  max_idx.Fill( -1 );
+
+  if( this->m_Image.IsNotNull( ) )
+  {
+    auto region = this->m_Image->GetRequestedRegion( );
+    min_idx = max_idx = region.GetIndex( );
+    min_idx += region.GetSize( );
+
+    _TIt i( this->m_Image, region );
+    for( i.GoToBegin( ); !i.IsAtEnd( ); ++i )
+    {
+      auto v = i.Get( );
+      if( this->m_LowerThreshold <= v && v <= this->m_UpperThreshold )
+      {
+        auto idx = i.GetIndex( );
+        for( unsigned int d = 0; d < _TImage::ImageDimension; ++d )
+        {
+          min_idx[ d ] = ( idx[ d ] < min_idx[ d ] )? idx[ d ]: min_idx[ d ]; 
+          max_idx[ d ] = ( idx[ d ] > max_idx[ d ] )? idx[ d ]: max_idx[ d ]; 
+        } // rof
+
+      } // fi
+
+    } // rof
+
+    // Fix indices
+    for( unsigned int d = 0; d < _TImage::ImageDimension; ++d )
+    {
+      min_idx[ d ] -= long( this->m_PAD );
+      max_idx[ d ] += long( this->m_PAD );
+
+      if( min_idx[ d ] < region.GetIndex( )[ d ] )
+        min_idx[ d ] = region.GetIndex( )[ d ];
+      if( max_idx[ d ] > region.GetIndex( )[ d ] + region.GetSize( )[ d ] )
+        max_idx[ d ] = region.GetIndex( )[ d ] + region.GetSize( )[ d ];
+
+    } // rof
+
+  } // fi
+
+  typename _TImage::SizeType size;
+  for( unsigned int d = 0; d < _TImage::ImageDimension; ++d )
+    size[ d ] = max_idx[ d ] - min_idx[ d ] + 1;
+  
+  this->m_Region.SetIndex( min_idx );
+  this->m_Region.SetSize( size );
+}
+
+// -------------------------------------------------------------------------
+template< class _TImage >
+cpExtensions::Algorithms::ImageToBoundingBoxFromThreshold< _TImage >::
+ImageToBoundingBoxFromThreshold( )
+  : Superclass( ),
+    m_LowerThreshold( std::numeric_limits< TPixel >::min( ) ),
+    m_UpperThreshold( std::numeric_limits< TPixel >::max( ) ),
+    m_PAD( 0 )
+{
+}
+
+// -------------------------------------------------------------------------
+template< class _TImage >
+cpExtensions::Algorithms::ImageToBoundingBoxFromThreshold< _TImage >::
+~ImageToBoundingBoxFromThreshold( )
+{
+}
+
+#endif // __cpExtensions__Algorithms__ImageToBoundingBoxFromThreshold__hxx__
+
+// eof - $RCSfile$