Add Gaussian Noise to an Image#
Synopsis#
Adds Gaussian Noise to a particular image
Results#
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>;
const auto input = itk::ReadImage<ImageType>(inputImage);
// Create the filter and apply the algorithm to the image
using FilterType = itk::AdditiveGaussianNoiseImageFilter<ImageType, ImageType>;
auto filter = FilterType::New();
filter->SetInput(input);
filter->SetMean(mean); // Set the mean
filter->SetStandardDeviation(deviation); // Set the standard deviation
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<class TInputImage, class TOutputImage = TInputImage>
class AdditiveGaussianNoiseImageFilter : 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