Medical Imaging Interaction Toolkit  2024.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 {
21  template <class TInputPixel, class TOutputPixel>
23  {
24 
25  public:
26  GaussianNoiseFunctor(): m_Mu(0), m_Sigma(0) {};
28 
29  void SetMean(double mu)
30  {
31  this->m_Mu = mu;
32  }
33  void SetSigma(double sigma)
34  {
35  this->m_Sigma = sigma;
36  }
37 
38  bool operator!=(const GaussianNoiseFunctor& other) const
39  {
40  return !(*this == other);
41  }
42 
43  bool operator==(const GaussianNoiseFunctor& other) const
44  {
45  return (this->m_Mu == other.m_Mu) && (this->m_Sigma == other.m_Sigma) ;
46  }
47 
48  inline TOutputPixel operator()(const TInputPixel& value) const
49  {
50  double n = noise(this->m_Mu, this->m_Sigma);
51 
52  TOutputPixel result = value + n;
53 
54  return result;
55  }
56 
57  private:
58  double m_Mu, m_Sigma;
59 
60 
65  inline double noise(double mu, double sigma) const
66  {
67  double u1, u2, W, mult;
68  static double x1, x2;
69  static int i = 0;
70 
71  if (i == 1)
72  {
73  i = !i;
74  return (mu + sigma * (double)x2);
75  }
76 
77  do
78  {
79  u1 = -1 + (static_cast <double>(rand()) / static_cast <double>(RAND_MAX)) * 2;
80  u2 = -1 + (static_cast <double>(rand()) / static_cast <double>(RAND_MAX)) * 2;
81  W = u1 * u1 + u2 * u2;
82  }
83  while (W >= 1 || W == 0);
84 
85  mult = sqrt((-2 * log(W)) / W);
86  x1 = u1 * mult;
87  x2 = u2 * mult;
88 
89  i = !i;
90 
91  return (mu + sigma * (double)x1);
92 
93  }
94  };
95 }
96 
97 
98 #endif
mitk::GaussianNoiseFunctor::SetMean
void SetMean(double mu)
Definition: mitkGaussianNoiseFunctor.h:29
mitk::GaussianNoiseFunctor
Definition: mitkGaussianNoiseFunctor.h:22
mitk::GaussianNoiseFunctor::operator==
bool operator==(const GaussianNoiseFunctor &other) const
Definition: mitkGaussianNoiseFunctor.h:43
mitk::GaussianNoiseFunctor::operator!=
bool operator!=(const GaussianNoiseFunctor &other) const
Definition: mitkGaussianNoiseFunctor.h:38
mitk::GaussianNoiseFunctor::operator()
TOutputPixel operator()(const TInputPixel &value) const
Definition: mitkGaussianNoiseFunctor.h:48
mitk
Find image slices visible on a given plane.
Definition: RenderingTests.dox:1
mitk::GaussianNoiseFunctor::GaussianNoiseFunctor
GaussianNoiseFunctor()
Definition: mitkGaussianNoiseFunctor.h:26
mitk::GaussianNoiseFunctor::~GaussianNoiseFunctor
~GaussianNoiseFunctor()
Definition: mitkGaussianNoiseFunctor.h:27
mitk::GaussianNoiseFunctor::SetSigma
void SetSigma(double sigma)
Definition: mitkGaussianNoiseFunctor.h:33