Compute Gradient Anisotropic Diffusion#
Synopsis#
Perform anisotropic diffusion on an image.
Results#
Code#
Python#
#!/usr/bin/env python
import itk
import argparse
parser = argparse.ArgumentParser(description="Compute Gradient Anisotropic Diffusion.")
parser.add_argument("input_image")
parser.add_argument("output_image")
parser.add_argument("number_of_iterations", type=int)
parser.add_argument("time_step", type=float)
parser.add_argument("conductance", type=float)
args = parser.parse_args()
InputPixelType = itk.F
OutputPixelType = itk.UC
Dimension = 2
InputImageType = itk.Image[InputPixelType, Dimension]
OutputImageType = itk.Image[OutputPixelType, Dimension]
ReaderType = itk.ImageFileReader[InputImageType]
reader = ReaderType.New()
reader.SetFileName(args.input_image)
FilterType = itk.GradientAnisotropicDiffusionImageFilter[InputImageType, InputImageType]
gradientAnisotropicDiffusionFilter = FilterType.New()
gradientAnisotropicDiffusionFilter.SetInput(reader.GetOutput())
gradientAnisotropicDiffusionFilter.SetNumberOfIterations(args.number_of_iterations)
gradientAnisotropicDiffusionFilter.SetTimeStep(args.time_step)
gradientAnisotropicDiffusionFilter.SetConductanceParameter(args.conductance)
RescaleFilterType = itk.RescaleIntensityImageFilter[InputImageType, OutputImageType]
rescaler = RescaleFilterType.New()
rescaler.SetInput(gradientAnisotropicDiffusionFilter.GetOutput())
outputPixelTypeMinimum = itk.NumericTraits[OutputPixelType].min()
outputPixelTypeMaximum = itk.NumericTraits[OutputPixelType].max()
rescaler.SetOutputMinimum(outputPixelTypeMinimum)
rescaler.SetOutputMaximum(outputPixelTypeMaximum)
WriterType = itk.ImageFileWriter[OutputImageType]
writer = WriterType.New()
writer.SetFileName(args.output_image)
writer.SetInput(rescaler.GetOutput())
writer.Update()
C++#
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkGradientAnisotropicDiffusionImageFilter.h"
#include "itkRescaleIntensityImageFilter.h"
int
main(int argc, char * argv[])
{
if (argc != 6)
{
std::cerr << "Usage: " << std::endl;
std::cerr << argv[0];
std::cerr << " <InputFileName> <OutputFileName>";
std::cerr << " <numberOfIterations> <timeStep> <conductance>";
std::cerr << std::endl;
return EXIT_FAILURE;
}
const char * inputFileName = argv[1];
const char * outputFileName = argv[2];
constexpr unsigned int Dimension = 2;
using InputPixelType = float;
using InputImageType = itk::Image<InputPixelType, Dimension>;
using OutputPixelType = unsigned char;
using OutputImageType = itk::Image<OutputPixelType, Dimension>;
const int numberOfIterations = std::stoi(argv[3]);
const InputPixelType timeStep = std::stod(argv[4]);
const InputPixelType conductance = std::stod(argv[5]);
const auto input = itk::ReadImage<InputImageType>(inputFileName);
using FilterType = itk::GradientAnisotropicDiffusionImageFilter<InputImageType, InputImageType>;
auto filter = FilterType::New();
filter->SetInput(input);
filter->SetNumberOfIterations(numberOfIterations);
filter->SetTimeStep(timeStep);
filter->SetConductanceParameter(conductance);
using RescaleType = itk::RescaleIntensityImageFilter<InputImageType, OutputImageType>;
auto rescaler = RescaleType::New();
rescaler->SetInput(filter->GetOutput());
rescaler->SetOutputMinimum(itk::NumericTraits<OutputPixelType>::min());
rescaler->SetOutputMaximum(itk::NumericTraits<OutputPixelType>::max());
try
{
itk::WriteImage(rescaler->GetOutput(), outputFileName);
}
catch (const itk::ExceptionObject & error)
{
std::cerr << "Error: " << error << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
Classes demonstrated#
-
template<typename TInputImage, typename TOutputImage>
class GradientAnisotropicDiffusionImageFilter : public itk::AnisotropicDiffusionImageFilter<TInputImage, TOutputImage> This filter performs anisotropic diffusion on a scalar itk::Image using the classic Perona-Malik, gradient magnitude based equation.
For detailed information on anisotropic diffusion, see itkAnisotropicDiffusionFunction and itkGradientNDAnisotropicDiffusionFunction.
- Inputs and Outputs
The input to this filter should be a scalar itk::Image of any dimensionality. The output image will be a diffused copy of the input.
- Parameters
Please see the description of parameters given in itkAnisotropicDiffusionImageFilter.
- See
AnisotropicDiffusionImageFilter
- See
AnisotropicDiffusionFunction
- See
GradientAnisotropicDiffusionFunction