+// =========================================================================
+// @author Leonardo Florez Valencia
+// @email florez-l@javeriana.edu.co
+// =========================================================================
+
#ifndef __fpa__Image__SkeletonFilter__hxx__
#define __fpa__Image__SkeletonFilter__hxx__
-#include <queue>
-#include <itkImageRegionConstIteratorWithIndex.h>
+#include <itkImageRegionIteratorWithIndex.h>
+#include <itkMinimumMaximumImageCalculator.h>
+
+#include <fpa/Image/Functors/VertexIdentity.h>
+#include <fpa/Base/Functors/InvertValue.h>
// -------------------------------------------------------------------------
-#define fpa_Image_SkeletonFilter_InputMacro( i_n, i_t, i_i ) \
- template< class _TDistanceMap, class _TCostMap > \
- typename fpa::Image::SkeletonFilter< _TDistanceMap, _TCostMap >:: \
- i_t* fpa::Image::SkeletonFilter< _TDistanceMap, _TCostMap >:: \
- Get##i_n( ) \
- { \
- return( \
- dynamic_cast< i_t* >( this->Superclass::GetInput( i_i ) ) \
- ); \
- } \
- template< class _TDistanceMap, class _TCostMap > \
- const \
- typename fpa::Image::SkeletonFilter< _TDistanceMap, _TCostMap >:: \
- i_t* fpa::Image::SkeletonFilter< _TDistanceMap, _TCostMap >:: \
- Get##i_n( ) const \
- { \
- return( \
- dynamic_cast< const i_t* >( this->Superclass::GetInput( i_i ) ) \
- ); \
- } \
- template< class _TDistanceMap, class _TCostMap > \
- void fpa::Image::SkeletonFilter< _TDistanceMap, _TCostMap >:: \
- Set##i_n( i_t* input ) \
- { \
- this->Superclass::SetNthInput( i_i, input ); \
- }
+template< class _TImage, class _TScalar >
+void fpa::Image::SkeletonFilter< _TImage, _TScalar >::
+SetInput( const TScalarImage* image )
+{
+ // Do nothing
+}
-fpa_Image_SkeletonFilter_InputMacro( DistanceMap, TDistanceMap, 0 );
-fpa_Image_SkeletonFilter_InputMacro( CostMap, TCostMap, 1 );
-fpa_Image_SkeletonFilter_InputMacro( MinimumSpanningTree, TMST, 2 );
+// -------------------------------------------------------------------------
+template< class _TImage, class _TScalar >
+void fpa::Image::SkeletonFilter< _TImage, _TScalar >::
+SetInput( const TImage* image )
+{
+ this->m_DistanceMap->SetInput( image );
+ this->Superclass::SetInput( this->m_DistanceMap->GetOutput( ) );
+}
// -------------------------------------------------------------------------
-#define fpa_Image_SkeletonFilter_OutputMacro( o_n, o_t, o_i ) \
- template< class _TDistanceMap, class _TCostMap > \
- typename fpa::Image::SkeletonFilter< _TDistanceMap, _TCostMap >:: \
- o_t* fpa::Image::SkeletonFilter< _TDistanceMap, _TCostMap >:: \
- Get##o_n( ) \
- { \
- return( \
- dynamic_cast< o_t* >( this->Superclass::GetOutput( o_i ) ) \
- ); \
- } \
- template< class _TDistanceMap, class _TCostMap > \
- const typename \
- fpa::Image::SkeletonFilter< _TDistanceMap, _TCostMap >:: \
- o_t* fpa::Image::SkeletonFilter< _TDistanceMap, _TCostMap >:: \
- Get##o_n( ) const \
- { \
- return( \
- dynamic_cast< const o_t* >( this->Superclass::GetOutput( o_i ) ) \
- ); \
- }
+template< class _TImage, class _TScalar >
+_TImage* fpa::Image::SkeletonFilter< _TImage, _TScalar >::
+GetInput( )
+{
+ return( this->m_DistanceMap->GetInput( ) );
+}
+
+// -------------------------------------------------------------------------
+template< class _TImage, class _TScalar >
+const _TImage* fpa::Image::SkeletonFilter< _TImage, _TScalar >::
+GetInput( ) const
+{
+ return( this->m_DistanceMap->GetInput( ) );
+}
+
+// -------------------------------------------------------------------------
+template< class _TImage, class _TScalar >
+typename fpa::Image::SkeletonFilter< _TImage, _TScalar >::
+TSkeleton* fpa::Image::SkeletonFilter< _TImage, _TScalar >::
+GetSkeleton( )
+{
+ return(
+ dynamic_cast< TSkeleton* >(
+ this->itk::ProcessObject::GetOutput( this->m_SkeletonIdx )
+ )
+ );
+}
-fpa_Image_SkeletonFilter_OutputMacro( Skeleton, TSkeleton, 0 );
-fpa_Image_SkeletonFilter_OutputMacro( EndPoints, TIndices, 1 );
-fpa_Image_SkeletonFilter_OutputMacro( Bifurcations, TIndices, 2 );
-fpa_Image_SkeletonFilter_OutputMacro( Marks, TMarks, 3 );
+// -------------------------------------------------------------------------
+template< class _TImage, class _TScalar >
+typename fpa::Image::SkeletonFilter< _TImage, _TScalar >::
+TMarks* fpa::Image::SkeletonFilter< _TImage, _TScalar >::
+GetMarks( )
+{
+ return(
+ dynamic_cast< TMarks* >(
+ this->itk::ProcessObject::GetOutput( this->m_MarksIdx )
+ )
+ );
+}
// -------------------------------------------------------------------------
-template< class _TDistanceMap, class _TCostMap >
-fpa::Image::SkeletonFilter< _TDistanceMap, _TCostMap >::
+template< class _TImage, class _TScalar >
+fpa::Image::SkeletonFilter< _TImage, _TScalar >::
SkeletonFilter( )
- : Superclass( )
+ : Superclass( ),
+ m_SeedFromMaximumDistance( false )
{
- this->SetNumberOfRequiredInputs( 3 );
- this->SetNumberOfRequiredOutputs( 3 );
+ typedef fpa::Image::Functors::VertexIdentity< TScalarImage, _TScalar > _TVertexFunc;
+ typedef fpa::Base::Functors::InvertValue< _TScalar, _TScalar > _TValueFunc;
+
+ typename _TVertexFunc::Pointer vertex_func = _TVertexFunc::New( );
+ typename _TValueFunc::Pointer value_func = _TValueFunc::New( );
+ value_func->SetAlpha( 1 );
+ value_func->SetBeta( 1 );
+
+ this->SetFunctor( vertex_func );
+ this->SetFunctor( value_func );
+
+ this->m_DistanceMap = TDistanceMap::New( );
+ this->m_DistanceMap->InsideIsPositiveOn( );
+ this->m_DistanceMap->SquaredDistanceOff( );
+ this->m_DistanceMap->UseImageSpacingOn( );
+
+ unsigned int nOutputs = this->GetNumberOfRequiredOutputs( );
+ this->SetNumberOfRequiredOutputs( nOutputs + 2 );
+ this->m_SkeletonIdx = nOutputs;
+ this->m_MarksIdx = nOutputs + 1;
- typename TIndices::Pointer end_points = TIndices::New( );
- typename TIndices::Pointer bifurcations = TIndices::New( );
typename TSkeleton::Pointer skeleton = TSkeleton::New( );
typename TMarks::Pointer marks = TMarks::New( );
- this->SetNthOutput( 0, skeleton.GetPointer( ) );
- this->SetNthOutput( 1, end_points.GetPointer( ) );
- this->SetNthOutput( 2, bifurcations.GetPointer( ) );
- this->SetNthOutput( 3, marks.GetPointer( ) );
+ this->SetNthOutput( this->m_SkeletonIdx, skeleton.GetPointer( ) );
+ this->SetNthOutput( this->m_MarksIdx, marks.GetPointer( ) );
}
// -------------------------------------------------------------------------
-template< class _TDistanceMap, class _TCostMap >
-fpa::Image::SkeletonFilter< _TDistanceMap, _TCostMap >::
+template< class _TImage, class _TScalar >
+fpa::Image::SkeletonFilter< _TImage, _TScalar >::
~SkeletonFilter( )
{
}
// -------------------------------------------------------------------------
-template< class _TDistanceMap, class _TCostMap >
-void fpa::Image::SkeletonFilter< _TDistanceMap, _TCostMap >::
+template< class _TImage, class _TScalar >
+void fpa::Image::SkeletonFilter< _TImage, _TScalar >::
GenerateData( )
{
- // 0. I/O objects
- const TDistanceMap* dmap = this->GetDistanceMap( );
- const TCostMap* cmap = this->GetCostMap( );
- const TMST* mst = this->GetMinimumSpanningTree( );
- TIndices* ep = this->GetEndPoints( );
- TIndices* bi = this->GetBifurcations( );
- TSkeleton* sk = this->GetSkeleton( );
-
- // 1. Check input correspondance
- // TODO
-
- // 2. Detect end-points
- this->_EndPoints( dmap, cmap, mst, ep->Get( ) );
-
- // 3. Build skeleton and keep track of bifurcations
- this->_Skeleton( dmap, cmap, mst, ep->Get( ), bi->Get( ), sk );
+ // Prepare distance map
+ this->m_DistanceMap->Update( );
+
+ // Update start seed
+ TVertex seed;
+ if( this->m_SeedFromMaximumDistance )
+ {
+ typedef itk::MinimumMaximumImageCalculator< TScalarImage > _TMinMax;
+ typename _TMinMax::Pointer minmax = _TMinMax::New( );
+ minmax->SetImage( this->m_DistanceMap->GetOutput( ) );
+ minmax->Compute( );
+ seed = minmax->GetIndexOfMaximum( );
+ }
+ else
+ seed = *( this->BeginSeeds( ) );
+ this->m_Seeds.clear( );
+ this->m_Seeds.insert( seed );
+
+ // Prepare skeleton candidates queue
+ this->m_SkeletonQueue.clear( );
+
+ // Go!
+ this->Superclass::GenerateData( );
+
+ // Backtracking
+ _TAdjacencies A;
+ std::vector< TVertex > end_points;
+ this->_EndPoints( end_points, A );
+ this->_Skeleton( end_points, A );
}
// -------------------------------------------------------------------------
-template< class _TDistanceMap, class _TCostMap >
-void fpa::Image::SkeletonFilter< _TDistanceMap, _TCostMap >::
-_EndPoints(
- const TDistanceMap* dmap,
- const TCostMap* cmap,
- const TMST* mst,
- TIndicesData& end_points
- )
+template< class _TImage, class _TScalar >
+void fpa::Image::SkeletonFilter< _TImage, _TScalar >::
+_SetOutputValue( const TVertex& vertex, const TOutputValue& value )
{
- typedef itk::ImageRegionConstIteratorWithIndex< _TDistanceMap > _TDistMapIt;
- typedef itk::ImageRegionConstIteratorWithIndex< _TCostMap > _TCostMapIt;
- typedef std::multimap< double, TIndex, std::greater< double > > _TQueue;
- typedef typename _TQueue::value_type _TQueueValue;
- typedef itk::Image< unsigned char, _TCostMap::ImageDimension > _TMarks;
+ typedef typename _TSkeletonQueue::value_type _TSkeletonQueueValue;
- // Some values
- // typename _TMarks::Pointer marks = _TMarks::New( );
- auto marks = this->GetMarks( );
- marks->SetLargestPossibleRegion( dmap->GetLargestPossibleRegion( ) );
- marks->SetRequestedRegion( dmap->GetRequestedRegion( ) );
- marks->SetBufferedRegion( dmap->GetBufferedRegion( ) );
- marks->SetSpacing( dmap->GetSpacing( ) );
- marks->SetOrigin( dmap->GetOrigin( ) );
- marks->SetDirection( dmap->GetDirection( ) );
- marks->Allocate( );
- marks->FillBuffer( 0 );
-
- // Create queue
- _TQueue queue;
- _TDistMapIt dIt( dmap, dmap->GetRequestedRegion( ) );
- _TCostMapIt cIt( cmap, cmap->GetRequestedRegion( ) );
- dIt.GoToBegin( );
- cIt.GoToBegin( );
- for( ; !dIt.IsAtEnd( ) && !cIt.IsAtEnd( ); ++dIt, ++cIt )
+ this->Superclass::_SetOutputValue( vertex, value );
+ double d = double( this->m_DistanceMap->GetOutput( )->GetPixel( vertex ) );
+ if( d >= double( 0 ) )
{
- double d = double( dIt.Get( ) );
- if( d > 0 )
- {
- double v = double( cIt.Get( ) ) / ( d * d );
- queue.insert( _TQueueValue( v, dIt.GetIndex( ) ) );
+ // Update skeleton candidates
+ d += double( 1e-5 );
+ double v = double( value ) / ( d * d );
+ this->m_SkeletonQueue.insert( _TSkeletonQueueValue( v, vertex ) );
- } // fi
+ } // fi
+}
- } // rof
+// -------------------------------------------------------------------------
+template< class _TImage, class _TScalar >
+void fpa::Image::SkeletonFilter< _TImage, _TScalar >::
+_EndPoints( std::vector< TVertex >& end_points, _TAdjacencies& A )
+{
+ typedef typename _TSkeletonQueue::value_type _TSkeletonQueueValue;
+
+ TMarks* marks = this->GetMarks( );
+ TMST* mst = this->GetMinimumSpanningTree( );
+ typename TImage::SpacingType spac = marks->GetSpacing( );
+
+ // Some values
+ marks->SetLargestPossibleRegion( mst->GetLargestPossibleRegion( ) );
+ marks->SetRequestedRegion( mst->GetRequestedRegion( ) );
+ marks->SetBufferedRegion( mst->GetBufferedRegion( ) );
+ marks->SetSpacing( mst->GetSpacing( ) );
+ marks->SetOrigin( mst->GetOrigin( ) );
+ marks->SetDirection( mst->GetDirection( ) );
+ marks->Allocate( );
+ marks->FillBuffer( 0 );
// BFS from maximum queue
- auto region = dmap->GetRequestedRegion( );
- double init_v = queue.begin( )->first;
- while( queue.size( ) > 0 )
+ while( this->m_SkeletonQueue.size( ) > 0 )
{
// Get node
- auto nIt = queue.begin( );
- auto n = *nIt;
- queue.erase( nIt );
+ typename _TSkeletonQueue::iterator nIt = this->m_SkeletonQueue.begin( );
+ _TSkeletonQueueValue n = *nIt;
+ this->m_SkeletonQueue.erase( nIt );
+ // Mark it and update end-points
unsigned char m = marks->GetPixel( n.second );
- // if( ( m & 0x01 ) == 0x01 )
- if( m != 0 || ( n.first / init_v ) < double( 1e-1 ) )
+ if( m != 0 )
continue;
+ marks->SetPixel( n.second, 1 );
+ end_points.push_back( n.second );
- std::cout << n.second << " " << n.first << " " << dmap->GetPixel( n.second ) << std::endl;
-
- // Mark it and update end-points
- m |= 0x01;
- marks->SetPixel( n.second, m );
- end_points.insert( n.second );
-
- // Get path
- typename TMST::TPath::Pointer path;
- mst->GetPath( path, n.second );
- for( unsigned long i = 0; i < path->GetSize( ); ++i )
+ // Mark path
+ TVertex it = n.second;
+ TVertex p = mst->GetParent( it );
+ while( it != p )
{
- TIndex idx = path->GetVertex( i );
- typename _TCostMap::PointType cnt;
- cmap->TransformIndexToPhysicalPoint( idx, cnt );
- double d = double( 2 ) * double( dmap->GetPixel( idx ) );
-
- // Mark sphere
- std::queue< TIndex > q;
- q.push( idx );
- while( q.size( ) > 0 )
- {
- TIndex v = q.front( );
- q.pop( );
- unsigned char m = marks->GetPixel( v );
- if( ( m & 0x02 ) == 0x02 )
- continue;
- m |= 0x02;
- marks->SetPixel( v, m );
+ this->_MarkSphere( it );
+ it = p;
+ p = mst->GetParent( it );
- for( unsigned int x = 0; x < _TCostMap::ImageDimension; ++x )
- {
- for( int y = -1; y <= 1; y += 2 )
- {
- TIndex w = v;
- w[ x ] += y;
- if( region.IsInside( w ) )
- {
- typename _TCostMap::PointType p;
- cmap->TransformIndexToPhysicalPoint( w, p );
- if( cnt.EuclideanDistanceTo( p ) <= d )
- q.push( w );
+ } // elihw
+ this->_MarkSphere( it );
+ A[ n.second ] = it;
- } // fi
+ } // elihw
+}
- } // rof
+// -------------------------------------------------------------------------
+template< class _TImage, class _TScalar >
+void fpa::Image::SkeletonFilter< _TImage, _TScalar >::
+_Skeleton( const std::vector< TVertex >& end_points, _TAdjacencies& A )
+{
+ typedef typename TSkeleton::TPath _TPath;
+ typedef itk::Image< unsigned long, TImage::ImageDimension > _TTagsImage;
+
+ TMST* mst = this->GetMinimumSpanningTree( );
+ TSkeleton* skeleton = this->GetSkeleton( );
+
+ // Tag branches
+ typename _TTagsImage::Pointer tags = _TTagsImage::New( );
+ tags->SetLargestPossibleRegion( mst->GetLargestPossibleRegion( ) );
+ tags->SetRequestedRegion( mst->GetRequestedRegion( ) );
+ tags->SetBufferedRegion( mst->GetBufferedRegion( ) );
+ tags->Allocate( );
+ tags->FillBuffer( 0 );
+ for( TVertex it: end_points )
+ {
+ TVertex p = mst->GetParent( it );
+ while( it != p )
+ {
+ tags->SetPixel( it, tags->GetPixel( it ) + 1 );
+ it = p;
+ p = mst->GetParent( it );
- } // rof
+ } // elihw
+ tags->SetPixel( it, tags->GetPixel( it ) + 1 );
- } // elihw
+ } // rof
- } // rof
+ // Build paths (branches)
+ for( TVertex it: end_points )
+ {
+ TVertex p = mst->GetParent( it );
+ TVertex sIdx = it;
+ typename _TPath::Pointer path = _TPath::New( );
+ path->SetReferenceImage( mst );
+ while( it != p )
+ {
+ if( tags->GetPixel( sIdx ) != tags->GetPixel( it ) )
+ {
+ // Ok, a new branch has been added
+ path->AddVertex( it );
+ skeleton->AddBranch( path );
+
+ // Update a new starting index
+ path = _TPath::New( );
+ path->SetReferenceImage( mst );
+ sIdx = it;
+ }
+ else
+ path->AddVertex( it );
+ it = p;
+ p = mst->GetParent( it );
+
+ } // elihw
+
+ // Finally, add last branch
+ path->AddVertex( it );
+ skeleton->AddBranch( path );
- } // elihw
+ } // rof
}
// -------------------------------------------------------------------------
-template< class _TDistanceMap, class _TCostMap >
-void fpa::Image::SkeletonFilter< _TDistanceMap, _TCostMap >::
-_Skeleton(
- const TDistanceMap* dmap,
- const TCostMap* cmap,
- const TMST* mst,
- const TIndicesData& end_points,
- TIndicesData& bifurcations,
- TSkeleton* skeleton
- )
+template< class _TImage, class _TScalar >
+void fpa::Image::SkeletonFilter< _TImage, _TScalar >::
+_MarkSphere( const TVertex& idx )
{
- typedef typename TMST::TPath _TPath;
+ typedef itk::ImageRegionIteratorWithIndex< TMarks > _TMarksIt;
+
+ static const double _eps = std::sqrt( double( TImage::ImageDimension + 1 ) );
+ const TScalarImage* dmap = this->m_DistanceMap->GetOutput( );
+ TMarks* marks = this->GetMarks( );
+ typename TImage::SpacingType spac = dmap->GetSpacing( );
+ typename TImage::RegionType region = dmap->GetRequestedRegion( );
- for( auto eIt = end_points.begin( ); eIt != end_points.end( ); ++eIt )
+ typename TImage::PointType cnt;
+ dmap->TransformIndexToPhysicalPoint( idx, cnt );
+ double r = double( dmap->GetPixel( idx ) ) * _eps;
+
+ TVertex i0, i1;
+ for( unsigned int d = 0; d < TImage::ImageDimension; ++d )
{
- typename _TPath::Pointer path;
- mst->GetPath( path, *eIt );
- skeleton->AddBranch( path );
+ long off = long( std::ceil( r / double( spac[ d ] ) ) );
+ if( off < 3 )
+ off = 3;
+ i0[ d ] = idx[ d ] - off;
+ i1[ d ] = idx[ d ] + off;
+
+ if( i0[ d ] < region.GetIndex( )[ d ] )
+ i0[ d ] = region.GetIndex( )[ d ];
+
+ if( i1[ d ] >= region.GetIndex( )[ d ] + region.GetSize( )[ d ] )
+ i1[ d ] = region.GetIndex( )[ d ] + region.GetSize( )[ d ] - 1;
+
+ } // rof
+
+ typename TImage::SizeType size;
+ for( unsigned int d = 0; d < TImage::ImageDimension; ++d )
+ size[ d ] = i1[ d ] - i0[ d ] + 1;
+
+ typename TImage::RegionType neighRegion;
+ neighRegion.SetIndex( i0 );
+ neighRegion.SetSize( size );
+
+ _TMarksIt mIt( marks, neighRegion );
+ for( mIt.GoToBegin( ); !mIt.IsAtEnd( ); ++mIt )
+ {
+ TVertex w = mIt.GetIndex( );
+ typename TImage::PointType p;
+ marks->TransformIndexToPhysicalPoint( w, p );
+ mIt.Set( 1 );
} // rof
}