ANIMA  4.0
animaNoiseGeneratorImageFilter.hxx
Go to the documentation of this file.
1 #pragma once
2 
5 
6 #include <itkImageRegionIterator.h>
7 #include <itkImageRegionConstIterator.h>
8 
9 namespace anima
10 {
11 
12 template <class ImageType>
13 void
16 {
17  //-------------------------------
18  // Creates the additional outputs
19  //-------------------------------
20  unsigned int prevNum = this->GetNumberOfOutputs();
21 
22  this->SetNumberOfIndexedOutputs(m_NumberOfReplicates);
23 
24  for (unsigned int i = prevNum;i < m_NumberOfReplicates;++i)
25  {
26  this->SetNthOutput(i,this->MakeOutput(i).GetPointer());
27  }
28 
29  //---------------------------------------------------
30  // Call the superclass' implementation of this method
31  //---------------------------------------------------
32  Superclass::GenerateOutputInformation();
33 }
34 
35 template <class ImageType>
36 void
39 {
40  Superclass::BeforeThreadedGenerateData();
41 
42  m_Generators.clear();
43  std::mt19937 motherGenerator(time(ITK_NULLPTR));
44  for (int i = 0;i < this->GetNumberOfWorkUnits();++i)
45  m_Generators.push_back(std::mt19937(motherGenerator()));
46 }
47 
48 template <class ImageType>
49 void
52 {
53  typedef itk::ImageRegionConstIterator<InputImageType> InputImageIteratorType;
54  typedef itk::ImageRegionIterator<OutputImageType> OutputImageIteratorType;
55 
56  InputImageIteratorType inputIterator(this->GetInput(), outputRegionForThread);
57 
58  std::vector<OutputImageIteratorType> outIterators(m_NumberOfReplicates);
59  for (unsigned int i = 0;i < m_NumberOfReplicates;++i)
60  outIterators[i] = OutputImageIteratorType(this->GetOutput(i), outputRegionForThread);
61 
62  unsigned int threadId = this->GetSafeThreadId();
63 
64  while (!inputIterator.IsAtEnd())
65  {
66  double refData = inputIterator.Get();
67 
68  for (unsigned int i = 0;i < m_NumberOfReplicates;++i)
69  {
70  double realNoise = SampleFromGaussianDistribution(0.0, m_NoiseSigma, m_Generators[threadId]);
71  double data = refData + realNoise;
72 
73  if (!m_UseGaussianDistribution)
74  {
75  double imagNoise = SampleFromGaussianDistribution(0.0, m_NoiseSigma, m_Generators[threadId]);
76  data = std::sqrt(data * data + imagNoise * imagNoise);
77  }
78 
79  if ((std::isnan(data)) || (!std::isfinite(data)))
80  data = refData;
81 
82  outIterators[i].Set(data);
83  ++outIterators[i];
84  }
85 
86  ++inputIterator;
87  }
88 
89  this->SafeReleaseThreadId(threadId);
90 }
91 
92 } //end of namespace anima
Superclass::OutputImageRegionType OutputImageRegionType
void DynamicThreadedGenerateData(const OutputImageRegionType &outputRegionForThread) ITK_OVERRIDE
double SampleFromGaussianDistribution(const T &mean, const T &std, std::mt19937 &generator)