Compute Gradient Magnitude Recursive Gaussian of Grayscale Image#
Synopsis#
Compute the gradient magnitude of the image after first smoothing with a Gaussian kernel.
Results#
Code#
Python#
#!/usr/bin/env python
import itk
import argparse
parser = argparse.ArgumentParser(
description="Compute Gradient Magnitude Recursive Gaussian."
)
parser.add_argument("input_image")
parser.add_argument("output_image")
parser.add_argument("sigma")
args = parser.parse_args()
PixelType = itk.F
Dimension = 2
ImageType = itk.Image[PixelType, Dimension]
reader = itk.ImageFileReader[ImageType].New()
reader.SetFileName(args.input_image)
gradientMagnitudeImageFilter = itk.GradientMagnitudeRecursiveGaussianImageFilter.New(
reader
)
gradientMagnitudeImageFilter.SetInput(reader.GetOutput())
gradientMagnitudeImageFilter.SetSigma(args.sigma)
writer = itk.ImageFileWriter[ImageType].New()
writer.SetFileName(args.output_image)
writer.SetInput(gradientMagnitudeImageFilter.GetOutput())
writer.Update()
C++#
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkGradientMagnitudeRecursiveGaussianImageFilter.h"
int
main(int argc, char * argv[])
{
if (argc != 4)
{
std::cerr << "Usage: " << std::endl;
std::cerr << argv[0];
std::cerr << "<InputFileName> <OutputFileName> <Sigma>";
std::cerr << std::endl;
return EXIT_FAILURE;
}
const char * inputImage = argv[1];
const char * outputImage = argv[2];
const double sigma = std::stod(argv[3]);
constexpr unsigned int Dimension = 2;
using PixelType = float;
using ImageType = itk::Image<PixelType, Dimension>;
const auto input = itk::ReadImage<ImageType>(inputImage);
using FilterType = itk::GradientMagnitudeRecursiveGaussianImageFilter<ImageType, ImageType>;
auto filter = FilterType::New();
filter->SetInput(input);
filter->SetSigma(sigma);
try
{
itk::WriteImage(filter->GetOutput(), outputImage);
}
catch (const itk::ExceptionObject & error)
{
std::cerr << "Error: " << error << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
Classes demonstrated#
-
template<typename TInputImage, typename TOutputImage = TInputImage>
class GradientMagnitudeRecursiveGaussianImageFilter : public itk::InPlaceImageFilter<TInputImage, TOutputImage> Computes the Magnitude of the Gradient of an image by convolution with the first derivative of a Gaussian.
This filter is implemented using the recursive gaussian filters
- ITK Sphinx Examples: