Add Gaussian Noise to an Image¶
Synopsis¶
Adds Gaussian Noise to a particular image
Results¶
Input Image¶
Output Image¶
Code¶
Python¶
#!/usr/bin/env python
import itk
import argparse
parser = argparse.ArgumentParser(description="Additive Gaussian Noise Image Filter.")
parser.add_argument("input_image")
parser.add_argument("output_image")
parser.add_argument("mean", type=float)
parser.add_argument("standard_deviation", type=float)
args = parser.parse_args()
# Use unsigned char to save to PNG format
InputPixelType = itk.UC
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.AdditiveGaussianNoiseImageFilter[InputImageType, InputImageType]
AdditiveFilter = FilterType.New()
AdditiveFilter.SetInput(reader.GetOutput())
AdditiveFilter.SetMean(args.mean)
AdditiveFilter.SetStandardDeviation(args.standard_deviation)
WriterType = itk.ImageFileWriter[OutputImageType]
writer = WriterType.New()
writer.SetFileName(args.output_image)
writer.SetInput(AdditiveFilter.GetOutput())
writer.Update()
C++¶
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkAdditiveGaussianNoiseImageFilter.h"
int
main(int argc, char * argv[])
{
  // Check for proper arguments; if not, explain usage.
  if (argc != 5)
  {
    std::cerr << "Usage: " << std::endl;
    std::cerr << argv[0];
    std::cerr << " <InputFileName> <OutputFileName> [Mean] [Standard Deviation]";
    std::cerr << std::endl;
    return EXIT_FAILURE;
  }
  // Initialize and assign user provided variables
  const char * inputImage = argv[1];
  const char * outputImage = argv[2];
  // Get floating point numbers for the mean and std dev to perform the algorithm
  const double mean = std::stod(argv[3]);
  const double deviation = std::stod(argv[4]);
  constexpr unsigned int Dimension = 2;
  // Use unsigned char to save to PNG format
  using PixelType = unsigned char;
  using ImageType = itk::Image<PixelType, Dimension>;
  // Read the file to be converted
  using ReaderType = itk::ImageFileReader<ImageType>;
  ReaderType::Pointer reader = ReaderType::New();
  reader->SetFileName(inputImage);
  using ImageType = itk::Image<PixelType, Dimension>;
  // Create the filter and apply the algorithm to the image
  using FilterType = itk::AdditiveGaussianNoiseImageFilter<ImageType, ImageType>;
  FilterType::Pointer filter = FilterType::New();
  filter->SetInput(reader->GetOutput());
  filter->SetMean(mean);                   // Set the mean
  filter->SetStandardDeviation(deviation); // Set the standard deviation
  // Set the writer to save file
  using WriterType = itk::ImageFileWriter<ImageType>;
  WriterType::Pointer writer = WriterType::New();
  writer->SetFileName(outputImage);
  writer->SetInput(filter->GetOutput());
  // Write the output image
  try
  {
    writer->Update();
  }
  catch (itk::ExceptionObject & error)
  {
    std::cerr << "Error: " << error << std::endl;
    return EXIT_FAILURE;
  }
  return EXIT_SUCCESS;
}
Classes demonstrated¶
- 
template<class 
TInputImage, classTOutputImage= TInputImage>
classAdditiveGaussianNoiseImageFilter: public itk::NoiseBaseImageFilter<TInputImage, TOutputImage> Alter an image with additive Gaussian white noise.
Additive Gaussian white noise can be modeled as:
The noise is independent of the pixel intensities.

where
 is the observed image, 
 is the noise-free image and 
 is a normally distributed random variable of mean 
 and variance 
:
This code was contributed in the Insight Journal paper “Noise
Simulation”.
https://www.insight-journal.org/browse/publication/721- Author
 Gaetan Lehmann

