X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=appli%2Fexamples%2Fexample_ImageAlgorithmDijkstra_03.cxx;h=0de81262791453d8f802238ce15b6ebbb3e37385;hb=dc4791c63d865f011d4fcac4112deb69e860491c;hp=11fa961e078b81c35c49dca1e94fbd2be91eb12e;hpb=a7ef5dfe43aeeef0039ffae37da8667cc5cc413a;p=FrontAlgorithms.git diff --git a/appli/examples/example_ImageAlgorithmDijkstra_03.cxx b/appli/examples/example_ImageAlgorithmDijkstra_03.cxx index 11fa961..0de8126 100644 --- a/appli/examples/example_ImageAlgorithmDijkstra_03.cxx +++ b/appli/examples/example_ImageAlgorithmDijkstra_03.cxx @@ -14,260 +14,33 @@ #include #include +#include +#include + #include #include +#include #include #include +#include -#include -#include -#include +#include #include #include // ------------------------------------------------------------------------- const unsigned int Dim = 3; -typedef double TPixel; -typedef double TScalar; +typedef float TPixel; +typedef float TScalar; typedef itk::Image< TPixel, Dim > TImage; typedef itk::ImageToVTKImageFilter< TImage > TVTKImage; typedef itk::ImageFileReader< TImage > TImageReader; -typedef itk::ImageFileWriter< TImage > TImageWriter; -typedef fpa::Image::Dijkstra< TImage, TScalar > TBaseDijkstra; +typedef itk::ImageFileWriter< TImage > TImageWriter; +typedef fpa::Image::DijkstraWithSphereBacktracking< TImage, TScalar > TDijkstra; typedef fpa::VTK::ImageMPR TMPR; -typedef fpa::VTK::Image3DObserver< TBaseDijkstra, vtkRenderWindow > TDijkstraObs; - -// ------------------------------------------------------------------------- -class TDijkstra - : public TBaseDijkstra -{ -public: - typedef TDijkstra Self; - typedef TBaseDijkstra Superclass; - typedef itk::SmartPointer< Self > Pointer; - typedef itk::SmartPointer< const Self > ConstPointer; - - typedef Superclass::TCost TCost; - typedef Superclass::TVertex TVertex; - typedef Superclass::InputImageType TImage; - typedef std::deque< TVertex > TVertices; - -protected: - typedef std::pair< TCost, TVertex > _TCandidate; - typedef std::multimap< TCost, TVertex > _TCandidates; - typedef Superclass::_TNode _TNode; - -public: - itkNewMacro( Self ); - itkTypeMacro( TDijkstra, TBaseDijkstra ); - -protected: - TDijkstra( ) - : Superclass( ) - { } - virtual ~TDijkstra( ) - { } - - virtual void _BeforeMainLoop( ) - { - const TImage* img = this->GetInput( ); - - // Correct seeds - TImage::SizeType radius; - radius.Fill( 3 ); - itk::ConstNeighborhoodIterator< TImage > it( - radius, img, img->GetRequestedRegion( ) - ); - for( unsigned int s = 0; s < this->m_Seeds.size( ); ++s ) - { - _TNode seed = this->m_Seeds[ s ]; - it.SetLocation( seed.Vertex ); - - TImage::SizeType size = it.GetSize( ); - unsigned int nN = 1; - for( unsigned int d = 0; d < TImage::ImageDimension; ++d ) - nN *= size[ d ]; - TImage::PixelType max_value = img->GetPixel( seed.Vertex ); - for( unsigned int i = 0; i < nN; ++i ) - { - if( it.GetPixel( i ) > max_value ) - { - seed.Vertex = it.GetIndex( i ); - seed.Parent = seed.Vertex; - max_value = it.GetPixel( i ); - - } // fi - - } // rof - this->m_Seeds[ s ] = seed; - - } // rof - - // End initialization - this->Superclass::_BeforeMainLoop( ); - this->m_Candidates.clear( ); - } - - virtual void _AfterMainLoop( ) - { - typedef itk::Image< unsigned char, TImage::ImageDimension > _TMarkImage; - - this->Superclass::_AfterMainLoop( ); - if( this->m_Candidates.size( ) == 0 ) - return; - - const TImage* input = this->GetInput( ); - TImage::SpacingType spacing = input->GetSpacing( ); - - _TMarkImage::Pointer marks = _TMarkImage::New( ); - marks->SetLargestPossibleRegion( input->GetLargestPossibleRegion( ) ); - marks->SetRequestedRegion( input->GetRequestedRegion( ) ); - marks->SetBufferedRegion( input->GetBufferedRegion( ) ); - marks->SetOrigin( input->GetOrigin( ) ); - marks->SetSpacing( spacing ); - marks->SetDirection( input->GetDirection( ) ); - marks->Allocate( ); - marks->FillBuffer( 0 ); - - this->m_Axes = vtkSmartPointer< vtkPolyData >::New( ); - vtkSmartPointer< vtkPoints > points = - vtkSmartPointer< vtkPoints >::New( ); - vtkSmartPointer< vtkCellArray > lines = - vtkSmartPointer< vtkCellArray >::New( ); - - _TCandidates::const_reverse_iterator cIt = - this->m_Candidates.rbegin( ); - - for( unsigned long leo = 0; leo < 50 && cIt != this->m_Candidates.rend( ); ++cIt ) - { - TVertex vIt = cIt->second; - if( marks->GetPixel( vIt ) != 0 ) - continue; - - leo++; - std::cout << "Leaf: " << leo << " " << cIt->first << " " << vIt << std::endl; - bool start = true; - do - { - double dist = double( input->GetPixel( vIt ) ); - TImage::SizeType radius; - for( unsigned int d = 0; d < TImage::ImageDimension; ++d ) - radius[ d ] = - ( unsigned long )( dist / spacing[ d ] ) + 1; - itk::NeighborhoodIterator< _TMarkImage > mIt( - radius, marks, marks->GetRequestedRegion( ) - ); - mIt.GoToBegin( ); - mIt.SetLocation( vIt ); - - TImage::SizeType size = mIt.GetSize( ); - unsigned int nN = 1; - for( unsigned int d = 0; d < TImage::ImageDimension; ++d ) - nN *= size[ d ]; - for( unsigned int i = 0; i < nN; ++i ) - if( marks->GetRequestedRegion( ).IsInside( mIt.GetIndex( i ) ) ) - mIt.SetPixel( i, ( start )? 255: 100 ); - - start = false; - - // Next vertex - vIt = this->_Parent( vIt ); - - } while( vIt != this->_Parent( vIt ) ); - - // Last vertex - // std::cout << vIt << " " << this->GetSeed( 0 ) << std::endl; - - /* TODO - TVertices pS; - TVertex parent = this->_Parent( vIt ); - while( parent != vIt ) - { - pS.push_front( vIt ); - vIt = parent; - parent = this->_Parent( vIt ); - - TImage::PointType pnt; - this->GetInput( )->TransformIndexToPhysicalPoint( vIt, pnt ); - points->InsertNextPoint( pnt[ 0 ], pnt[ 1 ], pnt[ 2 ] ); - if( points->GetNumberOfPoints( ) > 1 ) - { - lines->InsertNextCell( 2 ); - lines->InsertCellPoint( points->GetNumberOfPoints( ) - 2 ); - lines->InsertCellPoint( points->GetNumberOfPoints( ) - 1 ); - - } // fi - - } // elihw - pS.push_front( vIt ); - TImage::PointType pnt; - this->GetInput( )->TransformIndexToPhysicalPoint( vIt, pnt ); - points->InsertNextPoint( pnt[ 0 ], pnt[ 1 ], pnt[ 2 ] ); - if( points->GetNumberOfPoints( ) > 1 ) - { - lines->InsertNextCell( 2 ); - lines->InsertCellPoint( points->GetNumberOfPoints( ) - 2 ); - lines->InsertCellPoint( points->GetNumberOfPoints( ) - 1 ); - - } // fi - - this->m_Axes->SetPoints( points ); - this->m_Axes->SetLines( lines ); - */ - - } // rof - - itk::ImageFileWriter< _TMarkImage >::Pointer w = - itk::ImageFileWriter< _TMarkImage >::New( ); - w->SetInput( marks ); - w->SetFileName( "marks.mhd" ); - w->Update( ); - - } - - virtual bool _UpdateNeigh( _TNode& nn, const _TNode& n ) - { - TCost nc = this->_Cost( nn.Vertex, n.Vertex ); - if( TCost( 0 ) < nc ) - { - nn.Cost = n.Cost + ( TCost( 1 ) / nc ); - nn.Result = nn.Cost; - return( true ); - } - else - return( false ); - } - - virtual bool _UpdateResult( _TNode& n ) - { - bool ret = this->Superclass::_UpdateResult( n ); - if( ret ) - { - TCost nc = this->_Cost( n.Vertex, n.Parent ); - TCost nc2 = nc * nc * nc * nc; - if( TCost( 0 ) < nc2 ) - { - n.Result = n.Cost / nc2; - this->GetOutput( )->SetPixel( n.Vertex, n.Result ); - this->m_Candidates.insert( _TCandidate( n.Result, n.Vertex ) ); - - } // fi - - } // fi - return( ret ); - } - -private: - TDijkstra( const Self& other ); - Self& operator=( const Self& other ); - -protected: - _TCandidates m_Candidates; -public: - vtkSmartPointer< vtkPolyData > m_Axes; -}; +typedef fpa::VTK::Image3DObserver< TDijkstra, vtkRenderWindow > TDijkstraObs; // ------------------------------------------------------------------------- int main( int argc, char* argv[] ) @@ -283,10 +56,10 @@ int main( int argc, char* argv[] ) } // fi std::string input_image_fn = argv[ 1 ]; - TImage::IndexType seed_idx; - seed_idx[ 0 ] = std::atoi( argv[ 2 ] ); - seed_idx[ 1 ] = std::atoi( argv[ 3 ] ); - seed_idx[ 2 ] = std::atoi( argv[ 4 ] ); + TImage::PointType seed_pnt; + seed_pnt[ 0 ] = std::atof( argv[ 2 ] ); + seed_pnt[ 1 ] = std::atof( argv[ 3 ] ); + seed_pnt[ 2 ] = std::atof( argv[ 4 ] ); std::string output_image_fn = argv[ 5 ]; bool visual_debug = false; if( argc > 6 ) @@ -317,20 +90,35 @@ int main( int argc, char* argv[] ) view.SetSize( 800, 800 ); view.SetImage( vtk_image->GetOutput( ) ); + vtkSmartPointer< vtkImageMarchingCubes > mc = + vtkSmartPointer< vtkImageMarchingCubes >::New( ); + mc->SetInputData( vtk_image->GetOutput( ) ); + mc->SetValue( 0, 1e-2 ); + mc->Update( ); + view.AddPolyData( mc->GetOutput( ), 1, 1, 1, 0.4 ); + // Allow some interaction + view.Render( ); view.Start( ); + TImage::IndexType seed_idx; + input_image->TransformPhysicalPointToIndex( seed_pnt, seed_idx ); + std::cout << seed_idx << " " << seed_pnt << std::endl; + seed_idx[ 0 ] = 256; + seed_idx[ 1 ] = 313; + seed_idx[ 2 ] = 381; + // Extract paths TDijkstra::Pointer paths = TDijkstra::New( ); paths->AddSeed( seed_idx, TScalar( 0 ) ); paths->SetInput( input_image ); - paths->SetNeighborhoodOrder( 1 ); + paths->SetNeighborhoodOrder( 2 ); if( visual_debug ) { // Configure observer TDijkstraObs::Pointer obs = TDijkstraObs::New( ); - obs->SetImage( input_image, view.GetWindow( ) ); + obs->SetRenderWindow( view.GetWindow( ) ); paths->AddObserver( itk::AnyEvent( ), obs ); paths->ThrowEventsOn( ); } @@ -342,49 +130,60 @@ int main( int argc, char* argv[] ) double seconds = double( end - start ) / double( CLOCKS_PER_SEC ); std::cout << "Paths extraction time = " << seconds << std::endl; - view.AddPolyData( paths->m_Axes, 1, 0, 0 ); - view.Start( ); + // Create polydata + vtkSmartPointer< vtkPoints > points = + vtkSmartPointer< vtkPoints >::New( ); + vtkSmartPointer< vtkCellArray > cells = + vtkSmartPointer< vtkCellArray >::New( ); + vtkSmartPointer< vtkFloatArray > scalars = + vtkSmartPointer< vtkFloatArray >::New( ); + + const TDijkstra::TVertices& endpoints = paths->GetEndPoints( ); + const TDijkstra::TTree& tree = paths->GetFinalTree( ); + TDijkstra::TVertices::const_iterator epIt = endpoints.begin( ); + for( unsigned int epId = 0; epIt != endpoints.end( ); ++epIt, ++epId ) + { + double pd = double( epId ) / double( endpoints.size( ) - 1 ); - TImageWriter::Pointer dijkstra_writer = - TImageWriter::New( ); - dijkstra_writer->SetInput( paths->GetOutput( ) ); - dijkstra_writer->SetFileName( "dijkstra.mhd" ); - dijkstra_writer->Update( ); + TDijkstra::TVertex idx = *epIt; + do + { + TImage::PointType pnt; + input_image->TransformIndexToPhysicalPoint( idx, pnt ); - // Show result - /* - TVTKImage::Pointer output_image_vtk = TVTKImage::New( ); - output_image_vtk->SetInput( segmentor->GetOutput( ) ); - output_image_vtk->Update( ); + points->InsertNextPoint( pnt[ 0 ], pnt[ 1 ], pnt[ 2 ] ); + scalars->InsertNextTuple1( pd ); + if( idx != *epIt ) + { + cells->InsertNextCell( 2 ); + cells->InsertCellPoint( points->GetNumberOfPoints( ) - 2 ); + cells->InsertCellPoint( points->GetNumberOfPoints( ) - 1 ); - vtkSmartPointer< vtkImageMarchingCubes > mc = - vtkSmartPointer< vtkImageMarchingCubes >::New( ); - mc->SetInputData( output_image_vtk->GetOutput( ) ); - mc->SetValue( - 0, - double( segmentor->GetInsideValue( ) ) * double( 0.95 ) - ); - mc->Update( ); + } // fi + idx = tree.find( idx )->second; - // Let some interaction and close program - view.AddPolyData( mc->GetOutput( ), 0.1, 0.6, 0.8, 0.5 ); - view.Start( ); + } while( idx != tree.find( idx )->second ); - // Write resulting image - TImageWriter::Pointer output_image_writer = TImageWriter::New( ); - output_image_writer->SetInput( segmentor->GetOutput( ) ); - output_image_writer->SetFileName( output_image_fn ); - try - { - output_image_writer->Update( ); - } - catch( itk::ExceptionObject& err ) - { - std::cerr << "Error caught: " << err << std::endl; - return( 1 ); + } // rof - } // yrt + vtkSmartPointer< vtkPolyData > vtk_tree = + vtkSmartPointer< vtkPolyData >::New( ); + vtk_tree->SetPoints( points ); + vtk_tree->SetLines( cells ); + vtk_tree->GetPointData( )->SetScalars( scalars ); + + view.AddPolyData( vtk_tree ); + view.Render( ); + view.Start( ); + + /* TODO + TImageWriter::Pointer dijkstra_writer = + TImageWriter::New( ); + dijkstra_writer->SetInput( paths->GetOutput( ) ); + dijkstra_writer->SetFileName( "dijkstra.mhd" ); + dijkstra_writer->Update( ); */ + return( 0 ); }