Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkPANoiseGenerator.cpp
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 #include "mitkPANoiseGenerator.h"
14 #include "mitkPAIOUtil.h"
15 #include "mitkPAExceptions.h"
16 #include <random>
17 #include <chrono>
18 #include <mitkCommon.h>
19 
20 void mitk::pa::NoiseGenerator::ApplyNoiseModel(mitk::pa::Volume::Pointer image, double detectorNoise, double speckleNoise)
21 {
22  if (detectorNoise < 0 || speckleNoise < 0)
23  throw mitk::pa::InvalidInputException("detectorNoise must be >= 0 and speckleNoise must be >= 0");
24 
25  if (detectorNoise < mitk::eps && speckleNoise < mitk::eps)
26  return;
27 
28  std::mt19937 rng;
29  std::random_device randomDevice;
30  if (randomDevice.entropy() > mitk::eps)
31  {
32  rng.seed(randomDevice());
33  }
34  else
35  {
36  rng.seed(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count());
37  }
38  std::normal_distribution<> detector(detectorNoise / 2, detectorNoise / 2);
39  std::normal_distribution<> speckle(1, speckleNoise);
40 
41  unsigned int negativecounter = 0;
42 
43  double* data = image->GetData();
44 
45  for (unsigned int x = 0, xLength = image->GetXDim(); x < xLength; x++)
46  for (unsigned int y = 0, yLength = image->GetYDim(); y < yLength; y++)
47  for (unsigned int z = 0, zLength = image->GetZDim(); z < zLength; z++)
48  {
49  double additiveNoise = detector(rng);
50 
51  double multiplicativeNoise = speckle(rng);
52 
53  double newSignal = (data[image->GetIndex(x, y, z)] + additiveNoise)*multiplicativeNoise;
54 
55  if (newSignal <= mitk::eps)
56  {
57  newSignal = mitk::eps;
58  negativecounter++;
59  }
60 
61  data[image->GetIndex(x, y, z)] = newSignal;
62  }
63 }
this exception is thrown if an invalid input occurs
mitk::Image::Pointer image
static void ApplyNoiseModel(mitk::pa::Volume::Pointer image, double detectorNoise, double speckleNoise)
ApplyNoiseModel Applies noise to an image.
MITKCORE_EXPORT const ScalarType eps