Medical Imaging Interaction Toolkit  2018.4.99-12ad79a3
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 (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 /*===================================================================
14 
15 This file is based heavily on a corresponding ITK filter.
16 
17 ===================================================================*/
18 #ifndef _itkTotalVariationDenoisingImageFilter_txx
19 #define _itkTotalVariationDenoisingImageFilter_txx
20 #include "itkTotalVariationDenoisingImageFilter.h"
21 
22 #include "itkConstShapedNeighborhoodIterator.h"
23 #include "itkImageRegionConstIterator.h"
24 #include "itkImageRegionIterator.h"
25 #include "itkLocalVariationImageFilter.h"
26 #include "itkNeighborhoodAlgorithm.h"
27 #include "itkNeighborhoodInnerProduct.h"
28 #include "itkOffset.h"
29 #include "itkProgressReporter.h"
30 #include "itkZeroFluxNeumannBoundaryCondition.h"
31 
32 #include <algorithm>
33 #include <vector>
34 
35 namespace itk
36 {
37  template <class TInputImage, class TOutputImage>
38  TotalVariationDenoisingImageFilter<TInputImage, TOutputImage>::TotalVariationDenoisingImageFilter()
39  : m_Lambda(1.0), m_NumberIterations(0)
40  {
41  }
42 
43  template <class TInputImage, class TOutputImage>
44  void TotalVariationDenoisingImageFilter<TInputImage, TOutputImage>::GenerateData()
45  {
46  // first we cast the input image to match output type
47  typename CastType::Pointer infilter = CastType::New();
48  infilter->SetInput(this->GetInput());
49  infilter->Update();
50  typename TOutputImage::Pointer image = infilter->GetOutput();
51 
52  // a second copy of the input image is saved as reference
53  infilter = CastType::New();
54  infilter->SetInput(this->GetInput());
55  infilter->Update();
56  typename TOutputImage::Pointer origImage = infilter->GetOutput();
57 
58  typename SingleIterationFilterType::Pointer filter;
59  for (int i = 0; i < m_NumberIterations; i++)
60  {
61  filter = SingleIterationFilterType::New();
62  filter->SetInput(image);
63  filter->SetOriginalImage(origImage);
64  filter->SetLambda(m_Lambda);
65  filter->SetNumberOfThreads(this->GetNumberOfThreads());
66  filter->UpdateLargestPossibleRegion();
67  image = filter->GetOutput();
68  std::cout << "Iteration " << i + 1 << "/" << m_NumberIterations << std::endl;
69  }
70 
71  typename OutputImageType::Pointer output = this->GetOutput();
72  output->SetSpacing(image->GetSpacing());
73  typename OutputImageType::RegionType largestPossibleRegion;
74  largestPossibleRegion.SetSize(image->GetLargestPossibleRegion().GetSize());
75  largestPossibleRegion.SetIndex(image->GetLargestPossibleRegion().GetIndex());
76  output->SetLargestPossibleRegion(image->GetLargestPossibleRegion());
77  output->SetBufferedRegion(image->GetLargestPossibleRegion());
78  output->Allocate();
79 
80  itk::ImageRegionIterator<OutputImageType> oit(output, output->GetLargestPossibleRegion());
81  oit.GoToBegin();
82 
83  itk::ImageRegionConstIterator<OutputImageType> iit(image, image->GetLargestPossibleRegion());
84  iit.GoToBegin();
85 
86  while (!oit.IsAtEnd())
87  {
88  oit.Set(iit.Get());
89  ++iit;
90  ++oit;
91  }
92  }
93 
94  /**
95  * Standard "PrintSelf" method
96  */
97  template <class TInputImage, class TOutput>
98  void TotalVariationDenoisingImageFilter<TInputImage, TOutput>::PrintSelf(std::ostream &os, Indent indent) const
99  {
100  Superclass::PrintSelf(os, indent);
101  }
102 
103 } // end namespace itk
104 
105 #endif