DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
itkAddNoiseToDWIImageFilter.hxx
Go to the documentation of this file.
1 
18 #ifndef __itkAddNoiseToDWIImageFilter_hxx
19 #define __itkAddNoiseToDWIImageFilter_hxx
20 
21 
23 #include "utl.h"
24 
25 
26 namespace itk
27 {
28 
29 template <class TInputImage, class TB0Image, class TMaskImage>
32 {
33  this->SetNumberOfRequiredInputs(1);
34  m_Sigma = -1.0;
35  m_Noisetype = RICIAN;
36 }
37 
38 
39 template <class TInputImage, class TB0Image, class TMaskImage>
40 void
42 ::PrintSelf(std::ostream& os, Indent indent) const
43 {
44  Superclass::PrintSelf(os, indent);
45  PrintVar1(true, m_Sigma, os<<indent);
46  PrintVar1(true, m_Noisetype, os<<indent);
47  PrintVar1(this->GetInput(), this->GetInput(), os<<indent << "dwi");
48  PrintVar1(this->GetB0Image(), this->GetB0Image(), os<<indent << "b0");
49  PrintVar1(this->GetMaskImage(), this->GetMaskImage(), os<<indent << "mask");
50 }
51 
52 
53 template<class TInputImage, class TB0Image, class TMaskImage>
56 {
57  utlException(m_Sigma<=0, "need to set sigma to generate noise");
58 
59  typename InputImageType::ConstPointer inputPtr = this->GetInput();
60  typename MaskImageType::ConstPointer maskPtr = this->GetMaskImage();
61  typename MaskImageType::ConstPointer b0Ptr = this->GetB0Image();
62  utlException(!inputPtr, "no dwi");
63 
64  typename InputImageType::Pointer outputPtr = this->GetOutput();
65  outputPtr->SetRequestedRegion(inputPtr->GetRequestedRegion());
66  outputPtr->SetBufferedRegion(inputPtr->GetBufferedRegion());
67  outputPtr->Allocate();
68 
69  ImageRegionIterator<InputImageType> outputIt(outputPtr, outputPtr->GetLargestPossibleRegion());
70  ImageRegionConstIterator<InputImageType> inputIt(inputPtr, inputPtr->GetLargestPossibleRegion());
71  ImageRegionConstIterator<MaskImageType> maskIt;
72 
73  if (maskPtr)
74  maskIt = ImageRegionConstIterator<MaskImageType>(maskPtr, maskPtr->GetLargestPossibleRegion());
75 
76  itk::ImageRegionConstIterator<B0ImageType> b0It;
77  if (b0Ptr)
78  b0It = ImageRegionConstIterator<B0ImageType>(b0Ptr, b0Ptr->GetLargestPossibleRegion());
79 
80  outputIt.GoToBegin();
81  inputIt.GoToBegin();
82  maskIt.GoToBegin();
83  b0It.GoToBegin();
84 
85  PixelType pixel, zeroPixel;
86  zeroPixel.SetSize(inputPtr->GetNumberOfComponentsPerPixel());
87  zeroPixel.Fill(0);
88 
89  double realSigma = m_Sigma;
90 
91  while(!outputIt.IsAtEnd())
92  {
93  if ((!maskPtr || (maskPtr && maskIt.Get()>0) )
94  && (!b0Ptr || (b0Ptr && b0It.Get()>0)))
95  {
96  pixel = inputIt.Get();
97  if (b0Ptr)
98  realSigma = m_Sigma * b0It.Get();
99  if (m_Noisetype==GAUSSIAN)
100  pixel = utl::AddNoise<PixelType>(pixel, pixel.GetSize(), realSigma, false);
101  if (m_Noisetype==RICIAN)
102  pixel = utl::AddNoise<PixelType>(pixel, pixel.GetSize(), realSigma, true);
103  outputIt.Set(pixel);
104  }
105  else
106  outputIt.Set(zeroPixel);
107 
108  ++inputIt;
109  ++outputIt;
110 
111  if (b0Ptr)
112  ++b0It;
113  if (maskPtr)
114  ++maskIt;
115  }
116 }
117 
118 }
119 
120 #endif
#define PrintVar1(cond, var, os)
Definition: utlCoreMacro.h:447
helper functions specifically used in dmritool
#define utlException(cond, expout)
Definition: utlCoreMacro.h:548
void PrintSelf(std::ostream &os, Indent indent) const ITK_OVERRIDE
virtual void GenerateData() ITK_OVERRIDE