#include "itkLevelSetIterationUpdateCommand.h"
#include "itkVTKVisualize2DLevelSetAsElevationMap.h"
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 = std::stoi(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();
size.Fill(120);
region.SetIndex(index);
region.SetSize(size);
InputIteratorType iIt(binary, region);
iIt.GoToBegin();
while (!iIt.IsAtEnd())
{
++iIt;
}
BinaryImageToLevelSetType::Pointer adaptor = BinaryImageToLevelSetType::New();
adaptor->SetInputImage(binary);
adaptor->Initialize();
LevelSetType::Pointer levelSet = adaptor->GetLevelSet();
HeavisideFunctionType::Pointer heaviside = HeavisideFunctionType::New();
heaviside->SetEpsilon(1.5);
LevelSetContainerType::Pointer levelSetContainer = LevelSetContainerType::New();
levelSetContainer->SetHeaviside(heaviside);
levelSetContainer->AddLevelSet(0, levelSet);
using ChanAndVeseInternalTermType =
ChanAndVeseInternalTermType::Pointer cvInternalTerm = ChanAndVeseInternalTermType::New();
cvInternalTerm->SetInput(input);
cvInternalTerm->SetCoefficient(0.5);
using ChanAndVeseExternalTermType =
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);
using VisualizationType = itk::VTKVisualize2DLevelSetAsElevationMap<InputImageType, LevelSetType>;
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);
using IterationUpdateCommandType = itk::LevelSetIterationUpdateCommand<LevelSetEvolutionType, VisualizationType>;
IterationUpdateCommandType::Pointer iterationUpdateCommand = IterationUpdateCommandType::New();
iterationUpdateCommand->SetFilterToUpdate(visualizer);
iterationUpdateCommand->SetUpdatePeriod(5);
evolution->AddObserver(itk::IterationEvent(), iterationUpdateCommand);
evolution->Update();
return EXIT_SUCCESS;
}