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