Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
itkTotalVariationDenoisingImageFilter.txx
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 /*===================================================================
18 
19 This file is based heavily on a corresponding ITK filter.
20 
21 ===================================================================*/
22 #ifndef _itkTotalVariationDenoisingImageFilter_txx
23 #define _itkTotalVariationDenoisingImageFilter_txx
24 #include "itkTotalVariationDenoisingImageFilter.h"
25 
26 #include "itkConstShapedNeighborhoodIterator.h"
27 #include "itkImageRegionConstIterator.h"
28 #include "itkImageRegionIterator.h"
29 #include "itkLocalVariationImageFilter.h"
30 #include "itkNeighborhoodAlgorithm.h"
31 #include "itkNeighborhoodInnerProduct.h"
32 #include "itkOffset.h"
33 #include "itkProgressReporter.h"
34 #include "itkZeroFluxNeumannBoundaryCondition.h"
35 
36 #include <algorithm>
37 #include <vector>
38 
39 namespace itk
40 {
41  template <class TInputImage, class TOutputImage>
42  TotalVariationDenoisingImageFilter<TInputImage, TOutputImage>::TotalVariationDenoisingImageFilter()
43  {
44  m_Lambda = 1.0;
45  }
46 
47  template <class TInputImage, class TOutputImage>
48  void TotalVariationDenoisingImageFilter<TInputImage, TOutputImage>::GenerateData()
49  {
50  // first we cast the input image to match output type
51  typename CastType::Pointer infilter = CastType::New();
52  infilter->SetInput(this->GetInput());
53  infilter->Update();
54  typename TOutputImage::Pointer image = infilter->GetOutput();
55 
56  // a second copy of the input image is saved as reference
57  infilter = CastType::New();
58  infilter->SetInput(this->GetInput());
59  infilter->Update();
60  typename TOutputImage::Pointer origImage = infilter->GetOutput();
61 
62  typename SingleIterationFilterType::Pointer filter;
63  for (int i = 0; i < m_NumberIterations; i++)
64  {
65  filter = SingleIterationFilterType::New();
66  filter->SetInput(image);
67  filter->SetOriginalImage(origImage);
68  filter->SetLambda(m_Lambda);
69  filter->SetNumberOfThreads(this->GetNumberOfThreads());
70  filter->UpdateLargestPossibleRegion();
71  image = filter->GetOutput();
72  std::cout << "Iteration " << i + 1 << "/" << m_NumberIterations << std::endl;
73  }
74 
75  typename OutputImageType::Pointer output = this->GetOutput();
76  output->SetSpacing(image->GetSpacing());
77  typename OutputImageType::RegionType largestPossibleRegion;
78  largestPossibleRegion.SetSize(image->GetLargestPossibleRegion().GetSize());
79  largestPossibleRegion.SetIndex(image->GetLargestPossibleRegion().GetIndex());
80  output->SetLargestPossibleRegion(image->GetLargestPossibleRegion());
81  output->SetBufferedRegion(image->GetLargestPossibleRegion());
82  output->Allocate();
83 
84  itk::ImageRegionIterator<OutputImageType> oit(output, output->GetLargestPossibleRegion());
85  oit.GoToBegin();
86 
87  itk::ImageRegionConstIterator<OutputImageType> iit(image, image->GetLargestPossibleRegion());
88  iit.GoToBegin();
89 
90  while (!oit.IsAtEnd())
91  {
92  oit.Set(iit.Get());
93  ++iit;
94  ++oit;
95  }
96  }
97 
98  /**
99  * Standard "PrintSelf" method
100  */
101  template <class TInputImage, class TOutput>
102  void TotalVariationDenoisingImageFilter<TInputImage, TOutput>::PrintSelf(std::ostream &os, Indent indent) const
103  {
104  Superclass::PrintSelf(os, indent);
105  }
106 
107 } // end namespace itk
108 
109 #endif