Computes Smoothing With Gaussian Kernel¶
Synopsis¶
Computes the smoothing of an image by convolution with Gaussian kernels.
Results¶
![Input image](../../../../_images/BrainProtonDensitySlice7.png)
Input image¶
![Output image](../../../../_images/OutputBaseline39.png)
Output image¶
Code¶
Python¶
#!/usr/bin/env python
import itk
import argparse
parser = argparse.ArgumentParser(description="Computes Smoothing With Gaussian Kernel.")
parser.add_argument("input_image")
parser.add_argument("output_image")
parser.add_argument("sigma", type=float)
args = parser.parse_args()
PixelType = itk.UC
Dimension = 2
ImageType = itk.Image[PixelType, Dimension]
reader = itk.ImageFileReader[ImageType].New()
reader.SetFileName(args.input_image)
smoothFilter = itk.SmoothingRecursiveGaussianImageFilter[ImageType, ImageType].New()
smoothFilter.SetInput(reader.GetOutput())
smoothFilter.SetSigma(args.sigma)
writer = itk.ImageFileWriter[ImageType].New()
writer.SetFileName(args.output_image)
writer.SetInput(smoothFilter.GetOutput())
writer.Update()
C++¶
#include "itkSmoothingRecursiveGaussianImageFilter.h"
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
int
main(int argc, char * argv[])
{
if (argc != 4)
{
std::cerr << "Usage: " << std::endl;
std::cerr << argv[0] << " <InputImageFile> <OutputImageFile> <sigma>" << std::endl;
return EXIT_FAILURE;
}
constexpr unsigned int Dimension = 2;
const char * inputFileName = argv[1];
const char * outputFileName = argv[2];
const float sigmaValue = std::stod(argv[3]);
using PixelType = unsigned char;
using ImageType = itk::Image<PixelType, Dimension>;
using ReaderType = itk::ImageFileReader<ImageType>;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(inputFileName);
using FilterType = itk::SmoothingRecursiveGaussianImageFilter<ImageType, ImageType>;
FilterType::Pointer smoothFilter = FilterType::New();
smoothFilter->SetSigma(sigmaValue);
smoothFilter->SetInput(reader->GetOutput());
using WriterType = itk::ImageFileWriter<ImageType>;
WriterType::Pointer writer = WriterType::New();
writer->SetInput(smoothFilter->GetOutput());
writer->SetFileName(outputFileName);
try
{
writer->Update();
}
catch (itk::ExceptionObject & error)
{
std::cerr << "Error: " << error << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
Classes demonstrated¶
-
template<typename
TInputImage
, typenameTOutputImage
= TInputImage>
classSmoothingRecursiveGaussianImageFilter
: public itk::InPlaceImageFilter<TInputImage, TOutputImage> Computes the smoothing of an image by convolution with the Gaussian kernels implemented as IIR filters.
This filter is implemented using the recursive gaussian filters. For multi-component images, the filter works on each component independently.
For this filter to be able to run in-place the input and output image types need to be the same and/or the same type as the RealImageType.