template <class TFilter>
{
public:
using Self = CommandIterationUpdate;
protected:
CommandIterationUpdate() = default;
public:
void
{
}
void
{
const auto * filter = static_cast<const TFilter *>(object);
if (typeid(event) != typeid(itk::IterationEvent))
{
return;
}
std::cout << filter->GetElapsedIterations() << ": ";
std::cout << filter->GetRMSChange() << " ";
std::cout << filter->GetCurrentParameters() << std::endl;
}
};
int
main(int argc, char * argv[])
{
if (argc < 18)
{
std::cerr << "Missing Parameters " << std::endl;
std::cerr << "Usage: " << argv[0];
std::cerr << " inputImage outputImage";
std::cerr << " seed1X seed1Y";
std::cerr << " seed2X seed2Y";
std::cerr << " seed3X seed3Y";
std::cerr << " initialDistance";
std::cerr << " sigma";
std::cerr << " propagationScaling shapePriorScaling";
std::cerr << " meanShapeImage numberOfModes shapeModeFilePattern";
std::cerr << " startX startY" << std::endl;
return EXIT_FAILURE;
}
using InternalPixelType = float;
using OutputPixelType = unsigned char;
using ThresholdingFilterType =
auto thresholder = ThresholdingFilterType::New();
thresholder->SetLowerThreshold(-1000.0);
thresholder->SetUpperThreshold(0.0);
thresholder->SetOutsideValue(0);
thresholder->SetInsideValue(255);
using CastFilterType =
using SmoothingFilterType =
InternalImageType>;
auto smoothing = SmoothingFilterType::New();
using GradientFilterType =
InternalImageType>;
auto gradientMagnitude = GradientFilterType::New();
using FastMarchingFilterType =
auto fastMarching = FastMarchingFilterType::New();
using GeodesicActiveContourFilterType =
InternalImageType,
InternalImageType>;
auto geodesicActiveContour = GeodesicActiveContourFilterType::New();
using CenterFilterType =
auto center = CenterFilterType::New();
center->CenterImageOn();
using ReciprocalFilterType =
auto reciprocal = ReciprocalFilterType::New();
const double propagationScaling = std::stod(argv[11]);
const double shapePriorScaling = std::stod(argv[12]);
geodesicActiveContour->SetPropagationScaling(propagationScaling);
geodesicActiveContour->SetShapePriorScaling(shapePriorScaling);
geodesicActiveContour->SetCurvatureScaling(1.0);
geodesicActiveContour->SetAdvectionScaling(1.0);
geodesicActiveContour->SetMaximumRMSError(0.005);
geodesicActiveContour->SetNumberOfIterations(400);
geodesicActiveContour->SetNumberOfLayers(4);
center->SetInput(input);
smoothing->SetInput(center->GetOutput());
gradientMagnitude->SetInput(smoothing->GetOutput());
reciprocal->SetInput(gradientMagnitude->GetOutput());
geodesicActiveContour->SetInput(fastMarching->GetOutput());
geodesicActiveContour->SetFeatureImage(reciprocal->GetOutput());
thresholder->SetInput(geodesicActiveContour->GetOutput());
smoothing->SetTimeStep(0.125);
smoothing->SetNumberOfIterations(5);
smoothing->SetConductanceParameter(9.0);
const double sigma = std::stod(argv[10]);
gradientMagnitude->SetSigma(sigma);
using NodeContainer = FastMarchingFilterType::NodeContainer;
using NodeType = FastMarchingFilterType::NodeType;
auto seeds = NodeContainer::New();
InternalImageType::IndexType seedPosition;
seedPosition[0] = std::stoi(argv[3]);
seedPosition[1] = std::stoi(argv[4]);
const double initialDistance = std::stod(argv[9]);
NodeType node;
const double seedValue = -initialDistance;
node.SetValue(seedValue);
node.SetIndex(seedPosition);
seeds->Initialize();
seeds->InsertElement(0, node);
seedPosition[0] = std::stoi(argv[5]);
seedPosition[1] = std::stoi(argv[6]);
node.SetIndex(seedPosition);
seeds->InsertElement(1, node);
seedPosition[0] = std::stoi(argv[7]);
seedPosition[1] = std::stoi(argv[8]);
node.SetIndex(seedPosition);
seeds->InsertElement(2, node);
fastMarching->SetTrialPoints(seeds);
fastMarching->SetSpeedConstant(1.0);
auto caster1 = CastFilterType::New();
auto caster2 = CastFilterType::New();
auto caster3 = CastFilterType::New();
auto caster4 = CastFilterType::New();
caster1->SetInput(smoothing->GetOutput());
caster1->SetOutputMinimum(0);
caster1->SetOutputMaximum(255);
"GeodesicActiveContourShapePriorImageFilterOutput1.png");
caster2->SetInput(gradientMagnitude->GetOutput());
caster2->SetOutputMinimum(0);
caster2->SetOutputMaximum(255);
"GeodesicActiveContourShapePriorImageFilterOutput2.png");
caster3->SetInput(reciprocal->GetOutput());
caster3->SetOutputMinimum(0);
caster3->SetOutputMaximum(255);
"GeodesicActiveContourShapePriorImageFilterOutput3.png");
caster4->SetInput(fastMarching->GetOutput());
caster4->SetOutputMinimum(0);
caster4->SetOutputMaximum(255);
fastMarching->SetOutputRegion(center->GetOutput()->GetBufferedRegion());
fastMarching->SetOutputSpacing(center->GetOutput()->GetSpacing());
fastMarching->SetOutputOrigin(center->GetOutput()->GetOrigin());
const unsigned int numberOfPCAModes = std::stoi(argv[14]);
using ShapeFunctionType =
auto shape = ShapeFunctionType::New();
shape->SetNumberOfPrincipalComponents(numberOfPCAModes);
std::vector<InternalImageType::Pointer> shapeModeImages(numberOfPCAModes);
fileNamesCreator->SetStartIndex(0);
fileNamesCreator->SetEndIndex(numberOfPCAModes - 1);
fileNamesCreator->SetSeriesFormat(argv[15]);
const std::vector<std::string> & shapeModeFileNames =
fileNamesCreator->GetFileNames();
for (unsigned int k = 0; k < numberOfPCAModes; ++k)
{
shapeModeImages[k] =
}
shape->SetMeanImage(meanShapeImage);
shape->SetPrincipalComponentImages(shapeModeImages);
ShapeFunctionType::ParametersType pcaStandardDeviations(numberOfPCAModes);
pcaStandardDeviations.Fill(1.0);
shape->SetPrincipalComponentStandardDeviations(pcaStandardDeviations);
auto transform = TransformType::New();
shape->SetTransform(transform);
using CostFunctionType =
auto costFunction = CostFunctionType::New();
CostFunctionType::WeightsType weights;
weights[0] = 1.0;
weights[1] = 20.0;
weights[2] = 1.0;
weights[3] = 1.0;
costFunction->SetWeights(weights);
CostFunctionType::ArrayType mean(shape->GetNumberOfShapeParameters());
CostFunctionType::ArrayType stddev(shape->GetNumberOfShapeParameters());
mean.Fill(0.0);
stddev.Fill(1.0);
costFunction->SetShapeParameterMeans(mean);
costFunction->SetShapeParameterStandardDeviations(stddev);
auto optimizer = OptimizerType::New();
auto generator = GeneratorType::New();
generator->Initialize(20020702);
optimizer->SetNormalVariateGenerator(generator);
OptimizerType::ScalesType scales(shape->GetNumberOfParameters());
scales.Fill(1.0);
for (unsigned int k = 0; k < numberOfPCAModes; ++k)
{
scales[k] = 20.0;
}
scales[numberOfPCAModes] = 350.0;
optimizer->SetScales(scales);
constexpr double initRadius = 1.05;
constexpr double grow = 1.1;
const double shrink = pow(grow, -0.25);
optimizer->Initialize(initRadius, grow, shrink);
optimizer->SetEpsilon(1.0e-6);
optimizer->SetMaximumIteration(15);
ShapeFunctionType::ParametersType parameters(
shape->GetNumberOfParameters());
parameters.Fill(0.0);
parameters[numberOfPCAModes + 1] = std::stod(argv[16]);
parameters[numberOfPCAModes + 2] = std::stod(argv[17]);
geodesicActiveContour->SetShapeFunction(shape);
geodesicActiveContour->SetCostFunction(costFunction);
geodesicActiveContour->SetOptimizer(optimizer);
geodesicActiveContour->SetInitialParameters(parameters);
using CommandType = CommandIterationUpdate<GeodesicActiveContourFilterType>;
auto observer = CommandType::New();
geodesicActiveContour->AddObserver(itk::IterationEvent(), observer);
try
{
}
{
std::cerr << "Exception caught !" << std::endl;
std::cerr << excep << std::endl;
return EXIT_FAILURE;
}
std::cout << std::endl;
std::cout << "Max. no. iterations: "
<< geodesicActiveContour->GetNumberOfIterations() << std::endl;
std::cout << "Max. RMS error: "
<< geodesicActiveContour->GetMaximumRMSError() << std::endl;
std::cout << std::endl;
std::cout << "No. elpased iterations: "
<< geodesicActiveContour->GetElapsedIterations() << std::endl;
std::cout << "RMS change: " << geodesicActiveContour->GetRMSChange()
<< std::endl;
std::cout << "Parameters: " << geodesicActiveContour->GetCurrentParameters()
<< std::endl;
"GeodesicActiveContourShapePriorImageFilterOutput4.png");
auto mapWriter = InternalWriterType::New();
mapWriter->SetInput(fastMarching->GetOutput());
mapWriter->SetFileName(
"GeodesicActiveContourShapePriorImageFilterOutput4.mha");
mapWriter->Update();
auto speedWriter = InternalWriterType::New();
speedWriter->SetInput(reciprocal->GetOutput());
speedWriter->SetFileName(
"GeodesicActiveContourShapePriorImageFilterOutput3.mha");
speedWriter->Update();
auto gradientWriter = InternalWriterType::New();
gradientWriter->SetInput(gradientMagnitude->GetOutput());
gradientWriter->SetFileName(
"GeodesicActiveContourShapePriorImageFilterOutput2.mha");
gradientWriter->Update();
using EvaluatorFilterType =
InternalImageType,
InternalImageType>;
auto evaluator = EvaluatorFilterType::New();
evaluator->SetInput(geodesicActiveContour->GetOutput());
evaluator->SetFunction(shape);
shape->SetParameters(geodesicActiveContour->GetInitialParameters());
thresholder->SetInput(evaluator->GetOutput());
"GeodesicActiveContourShapePriorImageFilterOutput5.png");
shape->SetParameters(geodesicActiveContour->GetCurrentParameters());
evaluator->Modified();
"GeodesicActiveContourShapePriorImageFilterOutput6.png");
return EXIT_SUCCESS;
}
Binarize an input image by thresholding.
Computes 1/(1+x) for each pixel in the image.
Superclass for callback/observer methods.
virtual void Execute(Object *caller, const EventObject &event)=0
This filter performs anisotropic diffusion on a scalar itk::Image using the modified curvature diffus...
Abstraction of the Events used to communicating among filters and with GUIs.
Standard exception handling object.
Solve an Eikonal equation using Fast Marching.
Segments structures in an image based on a user supplied edge potential map and user supplied shape m...
Computes the Magnitude of the Gradient of an image by convolution with the first derivative of a Gaus...
Writes image data to a single file.
Templated n-dimensional image class.
Base class for most ITK classes.
1+1 evolutionary strategy optimizer
Compute the signed distance from a N-dimensional PCA Shape.
Applies a linear transformation to the intensity levels of the input Image.
Represents the maximum aprior (MAP) cost function used ShapePriorSegmentationLevelSetImageFilter to e...
Implements transparent reference counting.
Evaluates a SpatialFunction onto a source image.
Normal random variate generator.
BinaryGeneratorImageFilter< TInputImage1, TInputImage2, TOutputImage > Superclass
SmartPointer< Self > Pointer
constexpr unsigned int Dimension
ITK_TEMPLATE_EXPORT void WriteImage(TImagePointer &&image, const std::string &filename, bool compress=false)
TOutputImage::Pointer ReadImage(const std::string &filename)