ITK  5.1.0
Insight Toolkit
SphinxExamples/src/Segmentation/LevelSetsv4Visualization/VisualizeEvolvingDense2DLevelSetAsElevationMap/Code.cxx
/*=========================================================================
*
* Copyright Insight Software Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#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;
}
// Image Dimension
constexpr unsigned int Dimension = 2;
using InputPixelType = unsigned char;
using InputImageType = itk::Image<InputPixelType, Dimension>;
// Read input image (to be processed).
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 LevelSetImageType = itk::Image<LevelSetPixelType, Dimension>;
using LevelSetOutputType = LevelSetType::OutputType;
using LevelSetRealType = LevelSetType::OutputRealType;
// Generate a binary mask that will be used as initialization for the level
// set evolution.
BinaryImageType::Pointer binary = BinaryImageType::New();
binary->SetRegions(input->GetLargestPossibleRegion());
binary->CopyInformation(input);
binary->Allocate();
index.Fill(5);
size.Fill(120);
region.SetIndex(index);
region.SetSize(size);
InputIteratorType iIt(binary, region);
iIt.GoToBegin();
while (!iIt.IsAtEnd())
{
++iIt;
}
// convert a binary mask to a level-set function
BinaryImageToLevelSetType::Pointer adaptor = BinaryImageToLevelSetType::New();
adaptor->SetInputImage(binary);
adaptor->Initialize();
LevelSetType::Pointer levelSet = adaptor->GetLevelSet();
// The Heaviside function
HeavisideFunctionType::Pointer heaviside = HeavisideFunctionType::New();
heaviside->SetEpsilon(1.5);
// Create the level set container
LevelSetContainerType::Pointer levelSetContainer = LevelSetContainerType::New();
levelSetContainer->SetHeaviside(heaviside);
levelSetContainer->AddLevelSet(0, levelSet);
// Create the terms.
//
// // Chan and Vese internal term
using ChanAndVeseInternalTermType =
ChanAndVeseInternalTermType::Pointer cvInternalTerm = ChanAndVeseInternalTermType::New();
cvInternalTerm->SetInput(input);
cvInternalTerm->SetCoefficient(0.5);
// // Chan and Vese external term
using ChanAndVeseExternalTermType =
ChanAndVeseExternalTermType::Pointer cvExternalTerm = ChanAndVeseExternalTermType::New();
cvExternalTerm->SetInput(input);
// Create term container (equation rhs)
TermContainerType::Pointer termContainer = TermContainerType::New();
termContainer->SetLevelSetContainer(levelSetContainer);
termContainer->SetInput(input);
termContainer->AddTerm(0, cvInternalTerm);
termContainer->AddTerm(1, cvExternalTerm);
// Create equation container
EquationContainerType::Pointer equationContainer = EquationContainerType::New();
equationContainer->SetLevelSetContainer(levelSetContainer);
equationContainer->AddEquation(0, termContainer);
// Create stopping criteria
using StoppingCriterionType = itk::LevelSetEvolutionNumberOfIterationsStoppingCriterion<LevelSetContainerType>;
StoppingCriterionType::Pointer criterion = StoppingCriterionType::New();
criterion->SetNumberOfIterations(numberOfIterations);
// Create the visualizer
using VisualizationType = itk::VTKVisualize2DLevelSetAsElevationMap<InputImageType, LevelSetType>;
VisualizationType::Pointer visualizer = VisualizationType::New();
visualizer->SetInputImage(input);
visualizer->SetLevelSet(levelSet);
visualizer->SetScreenCapture(true);
// Create evolution class
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;
}
itk::BinaryImageToLevelSetImageAdaptor
Definition: itkBinaryImageToLevelSetImageAdaptor.h:51
itkBinaryImageToLevelSetImageAdaptor.h
itkLevelSetEquationChanAndVeseExternalTerm.h
itk::LevelSetDenseImage
Base class for the "dense" representation of a level-set function on one image.
Definition: itkLevelSetDenseImage.h:41
itkImageFileReader.h
itk::GTest::TypedefsAndConstructors::Dimension2::SizeType
ImageBaseType::SizeType SizeType
Definition: itkGTestTypedefsAndConstructors.h:49
itkLevelSetEquationContainer.h
itkLevelSetEquationTermContainer.h
itk::Size::Fill
void Fill(SizeValueType value)
Definition: itkSize.h:211
itk::ImageFileReader
Data source that reads image data from a single file.
Definition: itkImageFileReader.h:75
itkSinRegularizedHeavisideStepFunction.h
itk::GTest::TypedefsAndConstructors::Dimension2::IndexType
ImageBaseType::IndexType IndexType
Definition: itkGTestTypedefsAndConstructors.h:50
itk::SinRegularizedHeavisideStepFunction
Sin-based implementation of the Regularized (smoothed) Heaviside functions.
Definition: itkSinRegularizedHeavisideStepFunction.h:53
itk::LevelSetEquationChanAndVeseExternalTerm
Class to represent the external energy Chan And Vese term.
Definition: itkLevelSetEquationChanAndVeseExternalTerm.h:50
itk::GTest::TypedefsAndConstructors::Dimension2::RegionType
ImageBaseType::RegionType RegionType
Definition: itkGTestTypedefsAndConstructors.h:54
itkLevelSetEquationChanAndVeseInternalTerm.h
itk::ImageRegionIteratorWithIndex
A multi-dimensional iterator templated over image type that walks pixels within a region and is speci...
Definition: itkImageRegionIteratorWithIndex.h:71
itk::LevelSetContainer
Container of Level-Sets.
Definition: itkLevelSetContainer.h:39
itkLevelSetEvolutionNumberOfIterationsStoppingCriterion.h
itkLevelSetDenseImage.h
itk::NumericTraits
Define additional traits for native types such as int or float.
Definition: itkNumericTraits.h:58
itkLevelSetEvolution.h
itk::LevelSetEquationChanAndVeseInternalTerm
Class to represent the internal energy Chan And Vese term.
Definition: itkLevelSetEquationChanAndVeseInternalTerm.h:48
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:86
itk::LevelSetEvolution
Class for iterating and evolving the level-set function.
Definition: itkLevelSetEvolution.h:48
itk::LevelSetEquationContainer
Class for holding a set of level set equations (PDEs).
Definition: itkLevelSetEquationContainer.h:58
itk::GTest::TypedefsAndConstructors::Dimension2::Dimension
constexpr unsigned int Dimension
Definition: itkGTestTypedefsAndConstructors.h:44
itk::LevelSetEquationTermContainer
Class for container holding the terms of a given level set update equation.
Definition: itkLevelSetEquationTermContainer.h:42
itkLevelSetContainer.h