Medical Imaging Interaction Toolkit  2026.06.00
Medical Imaging Interaction Toolkit
mitkGaussianNoiseFunctor.h
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #ifndef mitkGaussianNoiseFunctor_h
14 #define mitkGaussianNoiseFunctor_h
15 
16 #include <iostream>
17 #include <cstdlib>
18 
19 namespace mitk
20 {
31  template <class TInputPixel, class TOutputPixel>
33  {
34 
35  public:
36  GaussianNoiseFunctor(): m_Mu(0), m_Sigma(0) {};
38 
40  void SetMean(double mu)
41  {
42  this->m_Mu = mu;
43  }
45  void SetSigma(double sigma)
46  {
47  this->m_Sigma = sigma;
48  }
49 
50  bool operator!=(const GaussianNoiseFunctor& other) const
51  {
52  return !(*this == other);
53  }
54 
55  bool operator==(const GaussianNoiseFunctor& other) const
56  {
57  return (this->m_Mu == other.m_Mu) && (this->m_Sigma == other.m_Sigma) ;
58  }
59 
60  inline TOutputPixel operator()(const TInputPixel& value) const
61  {
62  double n = noise(this->m_Mu, this->m_Sigma);
63 
64  TOutputPixel result = value + n;
65 
66  return result;
67  }
68 
69  private:
70  double m_Mu, m_Sigma;
71 
72 
76  inline double noise(double mu, double sigma) const
77  {
78  double u1, u2, W, mult;
79  static double x1, x2;
80  static int i = 0;
81 
82  if (i == 1)
83  {
84  i = !i;
85  return (mu + sigma * (double)x2);
86  }
87 
88  do
89  {
90  u1 = -1 + (static_cast <double>(rand()) / static_cast <double>(RAND_MAX)) * 2;
91  u2 = -1 + (static_cast <double>(rand()) / static_cast <double>(RAND_MAX)) * 2;
92  W = u1 * u1 + u2 * u2;
93  }
94  while (W >= 1 || W == 0);
95 
96  mult = sqrt((-2 * log(W)) / W);
97  x1 = u1 * mult;
98  x2 = u2 * mult;
99 
100  i = !i;
101 
102  return (mu + sigma * (double)x1);
103 
104  }
105  };
106 }
107 
108 
109 #endif
Pixel functor that adds Gaussian noise to a pixel value.
void SetMean(double mu)
Sets the mean of the Gaussian noise distribution.
TOutputPixel operator()(const TInputPixel &value) const
void SetSigma(double sigma)
Sets the standard deviation of the Gaussian noise distribution.
bool operator!=(const GaussianNoiseFunctor &other) const
bool operator==(const GaussianNoiseFunctor &other) const
Find image slices visible on a given plane.