]> Creatis software - FrontAlgorithms.git/blobdiff - appli/CTBronchi/MoriLabelling.hxx
...
[FrontAlgorithms.git] / appli / CTBronchi / MoriLabelling.hxx
index 5ca4fc4c1d1ac4de2523a92f7a2e314929880b07..69e968bc1c377861f3012deb59156b2d1a6dc3b3 100644 (file)
@@ -6,16 +6,15 @@
 #ifndef __CTBronchi__MoriLabelling__hxx__
 #define __CTBronchi__MoriLabelling__hxx__
 
-#include <itkImageRegionConstIterator.h>
-#include <itkImageRegionIterator.h>
+#include <itkImageRegionConstIteratorWithIndex.h>
 
 // -------------------------------------------------------------------------
 template< class _TInputImage, class _TLabelImage >
-const _TLabelImage*
-CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
+const typename CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
+TLabelImage* CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
 GetInputLabelImage( ) const
 {
-  return( this->GetInput( ) );
+  return( this->GetLabels( ) );
 }
 
 // -------------------------------------------------------------------------
@@ -23,20 +22,16 @@ template< class _TInputImage, class _TLabelImage >
 void CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
 SetInputLabelImage( TLabelImage* image )
 {
-  this->SetInput( image );
+  this->SetLabels( image );
 }
 
 // -------------------------------------------------------------------------
 template< class _TInputImage, class _TLabelImage >
-const _TInputImage*
-CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
+const typename CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
+TInputImage* CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
 GetInputRawImage( ) const
 {
-  return(
-    dynamic_cast< const TInputImage* >(
-      this->itk::ProcessObject::GetInput( 1 )
-      )
-    );
+  return( this->GetInput( ) );
 }
 
 // -------------------------------------------------------------------------
@@ -44,7 +39,49 @@ template< class _TInputImage, class _TLabelImage >
 void CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
 SetInputRawImage( TInputImage* image )
 {
-  this->itk::ProcessObject::SetNthInput( 1, image );
+  this->SetInput( image );
+}
+
+// -------------------------------------------------------------------------
+template< class _TInputImage, class _TLabelImage >
+typename CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
+TInputValue CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
+GetUpperThreshold( ) const
+{
+  const TThresholdFunction* func =
+    dynamic_cast< const TThresholdFunction* >( this->GetValuePredicate( ) );
+  if( func != NULL )
+    return( func->GetUpper( ) );
+  else
+    return( TInputValue( 0 ) );
+}
+
+// -------------------------------------------------------------------------
+template< class _TInputImage, class _TLabelImage >
+void CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
+SetUpperThreshold( TInputValue t )
+{
+  TThresholdFunction* func =
+    dynamic_cast< TThresholdFunction* >( this->GetValuePredicate( ) );
+  if( func != NULL )
+    func->SetUpper( t );
+}
+
+// -------------------------------------------------------------------------
+template< class _TInputImage, class _TLabelImage >
+typename CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
+TOutputValue CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
+GetOutsideValue( ) const
+{
+  return( this->GetInitValue( ) );
+}
+
+// -------------------------------------------------------------------------
+template< class _TInputImage, class _TLabelImage >
+void CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
+SetOutsideLabel( TOutputValue o )
+{
+  this->SetInitValue( o );
 }
 
 // -------------------------------------------------------------------------
@@ -52,12 +89,10 @@ template< class _TInputImage, class _TLabelImage >
 CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
 MoriLabelling( )
   : Superclass( ),
-    m_UpperThreshold( TLabel( 0 ) ),
-    m_InsideValue( TLabel( 0 ) ),
-    m_InsideLabel( TLabel( 0 ) ),
-    m_OutsideLabel( TLabel( 0 ) )
+    m_InsideLabel( TOutputValue( 0 ) )
 {
-  this->SetNumberOfRequiredInputs( 2 );
+  typename TThresholdFunction::Pointer func = TThresholdFunction::New( );
+  this->SetPredicate( func );
 }
 
 // -------------------------------------------------------------------------
@@ -69,35 +104,90 @@ CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
 
 // -------------------------------------------------------------------------
 template< class _TInputImage, class _TLabelImage >
-void CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
-ThreadedGenerateData( const TRegion& region, itk::ThreadIdType threadId )
+typename CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
+TNodes CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
+_UnifySeeds( )
 {
-  itk::ImageRegionConstIterator< TLabelImage >
-    lIt( this->GetInputLabelImage( ), region );
-  itk::ImageRegionConstIterator< TInputImage >
-    iIt( this->GetInputRawImage( ), region );
-  itk::ImageRegionIterator< TLabelImage >
-    oIt( this->GetOutput( ), region );
-
-  lIt.GoToBegin( );
-  iIt.GoToBegin( );
-  oIt.GoToBegin( );
-  while( !( lIt.IsAtEnd( ) || iIt.IsAtEnd( ) || oIt.IsAtEnd( ) ) )
+  this->m_Seeds.clear( );
+  const TLabelImage* lbl = this->GetLabels( );
+  if( lbl == NULL )
+  {
+    std::ostringstream msg;
+    msg << "itk::ERROR: CTBronchi::MoriLabelling (" << this
+        << "): Labelled image not defined.";
+    ::itk::ExceptionObject e(
+      __FILE__, __LINE__, msg.str( ).c_str( ), ITK_LOCATION
+      );
+    throw e;
+
+  } // fi
+
+  // Iterate over labels
+  typename TLabelImage::RegionType reg = lbl->GetRequestedRegion( );
+  itk::ImageRegionConstIteratorWithIndex< TLabelImage > lIt( lbl, reg );
+  for( lIt.GoToBegin( ); !lIt.IsAtEnd( ); ++lIt )
   {
-    if( lIt.Get( ) != this->m_InsideValue )
+    if( lIt.Get( ) > 0 )
     {
-      if( this->m_UpperThreshold < iIt.Get( ) )
-        oIt.Set( this->m_OutsideLabel );
+      bool is_seed = false;
+      for( unsigned int d = 0; d < TLabelImage::ImageDimension; ++d )
+      {
+        for( int s = -1; s <= 1; s += 2 )
+        {
+          TVertex neigh = lIt.GetIndex( );
+          neigh[ d ] += s;
+          if( reg.IsInside( neigh ) )
+            is_seed |= ( lbl->GetPixel( neigh ) == 0 );
+
+        } // rof
+
+      } // rof
+
+      if( !is_seed )
+      {
+        typename TSeedsInterface::TNode node;
+        node.Vertex = lIt.GetIndex( );
+        node.Parent = lIt.GetIndex( );
+        node.FrontId = lIt.Get( );
+        node.Value = this->m_InsideLabel;
+        this->_Mark( node.Vertex, node.FrontId );
+        this->_UpdateOutputValue( node );
+      }
       else
-        oIt.Set( TLabel( 0 ) );
-    }
+      {
+        typename TSeedsInterface::TSeed seed;
+        seed.Vertex = lIt.GetIndex( );
+        seed.IsPoint = false;
+        seed.FrontId = lIt.Get( );
+        this->m_Seeds.push_back( seed );
+
+      } // fi
+
+    } // fi
+
+  } // rof
+
+  // Ok, finish initialization
+  return( this->Superclass::_UnifySeeds( ) );
+}
+
+// -------------------------------------------------------------------------
+template< class _TInputImage, class _TLabelImage >
+void CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
+_UpdateOutputValue( TNode& n )
+{
+  const TLabelImage* input_labels = this->GetInputLabelImage( );
+
+  this->Superclass::_UpdateOutputValue( n );
+  if( n.FrontId != 0 )
+  {
+    if( input_labels->GetPixel( n.Vertex ) == this->GetInsideValue( ) )
+      n.Value = this->GetInsideLabel( );
     else
-      oIt.Set( this->m_InsideLabel );
-    ++lIt;
-    ++iIt;
-    ++oIt;
+      n.Value = TOutputValue( 0 );
+    this->TAlgorithm::_UpdateOutputValue( n );
 
-  } // elihw
+  } // fi
 }
 
 #endif // __CTBronchi__MoriLabelling__hxx__