[5d12a0]: / src / addNoiseToImage.cxx

Download this file

107 lines (89 with data), 3.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
#include <nanobind/nanobind.h>
#include <nanobind/stl/vector.h>
#include <nanobind/stl/string.h>
#include <nanobind/stl/tuple.h>
#include <nanobind/stl/list.h>
#include <nanobind/ndarray.h>
#include <nanobind/stl/shared_ptr.h>
#include <exception>
#include <vector>
#include <string>
#include "itkImage.h"
#include "itkAdditiveGaussianNoiseImageFilter.h"
#include "itkSaltAndPepperNoiseImageFilter.h"
#include "itkShotNoiseImageFilter.h"
#include "itkSpeckleNoiseImageFilter.h"
#include "antsImage.h"
namespace nb = nanobind;
using namespace nb::literals;
template <typename ImageType>
AntsImage<ImageType> additiveGaussianNoise( AntsImage<ImageType> & antsImage,
float mean,
float standardDeviation )
{
typename ImageType::Pointer itkImage = antsImage.ptr;
using NoiseFilterType = itk::AdditiveGaussianNoiseImageFilter<ImageType, ImageType>;
typename NoiseFilterType::Pointer noiser = NoiseFilterType::New();
noiser->SetInput( itkImage );
noiser->SetMean( mean );
noiser->SetStandardDeviation( standardDeviation );
noiser->Update();
AntsImage<ImageType> outImage = { noiser->GetOutput() };
return outImage;
}
template <typename ImageType>
AntsImage<ImageType> saltAndPepperNoise( AntsImage<ImageType> & antsImage,
float probability,
float saltValue,
float pepperValue )
{
typename ImageType::Pointer itkImage = antsImage.ptr;
using NoiseFilterType = itk::SaltAndPepperNoiseImageFilter<ImageType, ImageType>;
typename NoiseFilterType::Pointer noiser = NoiseFilterType::New();
noiser->SetInput( itkImage );
noiser->SetProbability( probability );
noiser->SetSaltValue( saltValue );
noiser->SetPepperValue( pepperValue );
noiser->Update();
AntsImage<ImageType> outImage = { noiser->GetOutput() };
return outImage;
}
template <typename ImageType>
AntsImage<ImageType> shotNoise( AntsImage<ImageType> & antsImage,
float scale
)
{
typename ImageType::Pointer itkImage = antsImage.ptr;
using NoiseFilterType = itk::ShotNoiseImageFilter<ImageType, ImageType>;
typename NoiseFilterType::Pointer noiser = NoiseFilterType::New();
noiser->SetInput( itkImage );
noiser->SetScale( scale );
noiser->Update();
AntsImage<ImageType> outImage = { noiser->GetOutput() };
return outImage;
}
template <typename ImageType>
AntsImage<ImageType> speckleNoise( AntsImage<ImageType> & antsImage,
float scale
)
{
typename ImageType::Pointer itkImage = antsImage.ptr;
using NoiseFilterType = itk::SpeckleNoiseImageFilter<ImageType, ImageType>;
typename NoiseFilterType::Pointer noiser = NoiseFilterType::New();
noiser->SetInput( itkImage );
noiser->SetStandardDeviation( scale );
noiser->Update();
AntsImage<ImageType> outImage = { noiser->GetOutput() };
return outImage;
}
void local_addNoiseToImage(nb::module_ &m)
{
m.def("additiveGaussianNoiseF2", &additiveGaussianNoise<itk::Image<float, 2>>);
m.def("additiveGaussianNoiseF3", &additiveGaussianNoise<itk::Image<float, 3>>);
m.def("saltAndPepperNoiseF2", &saltAndPepperNoise<itk::Image<float, 2>>);
m.def("saltAndPepperNoiseF3", &saltAndPepperNoise<itk::Image<float, 3>>);
m.def("shotNoiseF2", &shotNoise<itk::Image<float, 2>>);
m.def("shotNoiseF3", &shotNoise<itk::Image<float, 3>>);
m.def("speckleNoiseF2", &speckleNoise<itk::Image<float, 2>>);
m.def("speckleNoiseF3", &speckleNoise<itk::Image<float, 3>>);
}