int main( int argc, char* argv[] )
{
if( argc != 3)
{
std::cerr << "Missing Arguments" << std::endl;
std::cerr << argv[0] << std::endl;
std::cerr << "1- Input Image" << std::endl;
std::cerr << "2- Number of Iterations" << std::endl;
return EXIT_FAILURE;
}
using InputPixelType = unsigned char;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName( argv[1] );
reader->Update();
InputImageType::Pointer input = reader->GetOutput();
int numberOfIterations = atoi( argv[2] );
using LevelSetPixelType = float;
using LevelSetOutputType = LevelSetType::OutputType;
using LevelSetRealType = LevelSetType::OutputRealType;
BinaryImageType::Pointer binary = BinaryImageType::New();
binary->SetRegions( input->GetLargestPossibleRegion() );
binary->CopyInformation( input );
binary->Allocate();
binary->FillBuffer( itk::NumericTraits< LevelSetOutputType >::Zero );
size.Fill( 120 );
region.SetIndex( index );
region.SetSize( size );
InputIteratorType iIt( binary, region );
iIt.GoToBegin();
while( !iIt.IsAtEnd() )
{
iIt.Set( itk::NumericTraits< LevelSetOutputType >::One );
++iIt;
}
LevelSetType >;
BinaryImageToLevelSetType::Pointer adaptor = BinaryImageToLevelSetType::New();
adaptor->SetInputImage( binary );
adaptor->Initialize();
LevelSetType::Pointer levelSet = adaptor->GetModifiableLevelSet();
HeavisideFunctionType::Pointer heaviside = HeavisideFunctionType::New();
heaviside->SetEpsilon( 1.5 );
LevelSetContainerType::Pointer levelSetContainer = LevelSetContainerType::New();
levelSetContainer->SetHeaviside( heaviside );
levelSetContainer->AddLevelSet( 0, levelSet );
ChanAndVeseInternalTermType::Pointer cvInternalTerm = ChanAndVeseInternalTermType::New();
cvInternalTerm->SetInput( input );
cvInternalTerm->SetCoefficient( 0.5 );
ChanAndVeseExternalTermType::Pointer cvExternalTerm = ChanAndVeseExternalTermType::New();
cvExternalTerm->SetInput( input );
TermContainerType::Pointer termContainer = TermContainerType::New();
termContainer->SetLevelSetContainer( levelSetContainer );
termContainer->SetInput( input );
termContainer->AddTerm( 0, cvInternalTerm );
termContainer->AddTerm( 1, cvExternalTerm );
EquationContainerType::Pointer equationContainer = EquationContainerType::New();
equationContainer->SetLevelSetContainer( levelSetContainer );
equationContainer->AddEquation( 0, termContainer );
using StoppingCriterionType = itk::LevelSetEvolutionNumberOfIterationsStoppingCriterion< LevelSetContainerType >;
StoppingCriterionType::Pointer criterion = StoppingCriterionType::New();
criterion->SetNumberOfIterations( numberOfIterations );
VisualizationType::Pointer visualizer = VisualizationType::New();
visualizer->SetInputImage( input );
visualizer->SetLevelSet( levelSet );
visualizer->SetScreenCapture( true );
LevelSetEvolutionType::Pointer evolution = LevelSetEvolutionType::New();
evolution->SetEquationContainer( equationContainer );
evolution->SetStoppingCriterion( criterion );
evolution->SetLevelSetContainer( levelSetContainer );
IterationUpdateCommandType::Pointer iterationUpdateCommand = IterationUpdateCommandType::New();
iterationUpdateCommand->SetFilterToUpdate( visualizer );
iterationUpdateCommand->SetUpdatePeriod( 5 );
evolution->AddObserver( itk::IterationEvent(), iterationUpdateCommand );
evolution->Update();
return EXIT_SUCCESS;
}