1 /*===================================================================
3 The Medical Imaging Interaction Toolkit (MITK)
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 See LICENSE.txt or http://www.mitk.org for details.
15 ===================================================================*/
17 /*===================================================================
19 This file is based heavily on a corresponding ITK filter.
21 ===================================================================*/
23 #ifndef _itkTotalVariationSingleIterationImageFilter_txx
24 #define _itkTotalVariationSingleIterationImageFilter_txx
26 #include "itkTotalVariationSingleIterationImageFilter.h"
29 #include "itkConstShapedNeighborhoodIterator.h"
30 #include "itkImageRegionIterator.h"
31 #include "itkLocalVariationImageFilter.h"
32 #include "itkNeighborhoodAlgorithm.h"
33 #include "itkNeighborhoodInnerProduct.h"
34 #include "itkOffset.h"
35 #include "itkProgressReporter.h"
36 #include "itkZeroFluxNeumannBoundaryCondition.h"
47 template <class TInputImage, class TOutputImage>
48 TotalVariationSingleIterationImageFilter<TInputImage, TOutputImage>::TotalVariationSingleIterationImageFilter()
51 m_LocalVariation = LocalVariationImageType::New();
55 * generate requested region
57 template <class TInputImage, class TOutputImage>
58 void TotalVariationSingleIterationImageFilter<TInputImage, TOutputImage>::GenerateInputRequestedRegion() throw(
59 InvalidRequestedRegionError)
61 // call the superclass' implementation of this method
62 Superclass::GenerateInputRequestedRegion();
64 // get pointers to the input and output
65 typename Superclass::InputImagePointer inputPtr = const_cast<TInputImage *>(this->GetInput());
66 typename Superclass::OutputImagePointer outputPtr = this->GetOutput();
68 if (!inputPtr || !outputPtr)
73 // get a copy of the input requested region (should equal the output
75 typename TInputImage::RegionType inputRequestedRegion;
76 inputRequestedRegion = inputPtr->GetRequestedRegion();
78 // pad the input requested region by 1
79 inputRequestedRegion.PadByRadius(1);
81 // crop the input requested region at the input's largest possible region
82 if (inputRequestedRegion.Crop(inputPtr->GetLargestPossibleRegion()))
84 inputPtr->SetRequestedRegion(inputRequestedRegion);
89 // Couldn't crop the region (requested region is outside the largest
90 // possible region). Throw an exception.
92 // store what we tried to request (prior to trying to crop)
93 inputPtr->SetRequestedRegion(inputRequestedRegion);
96 InvalidRequestedRegionError e(__FILE__, __LINE__);
97 e.SetLocation(ITK_LOCATION);
98 e.SetDescription("Requested region outside possible region.");
99 e.SetDataObject(inputPtr);
107 template <class TInputImage, class TOutputImage>
108 void TotalVariationSingleIterationImageFilter<TInputImage, TOutputImage>::ThreadedGenerateData(
109 const OutputImageRegionType &outputRegionForThread, ThreadIdType threadId)
111 typename OutputImageType::Pointer output = this->GetOutput();
112 typename InputImageType::ConstPointer input = this->GetInput();
114 // Find the data-set boundary "faces"
115 itk::Size<InputImageDimension> size;
116 for (int i = 0; i < InputImageDimension; i++)
119 NeighborhoodAlgorithm::ImageBoundaryFacesCalculator<InputImageType> bC;
120 typename NeighborhoodAlgorithm::ImageBoundaryFacesCalculator<InputImageType>::FaceListType faceList =
121 bC(input, outputRegionForThread, size);
123 NeighborhoodAlgorithm::ImageBoundaryFacesCalculator<LocalVariationImageType> lv_bC;
124 typename NeighborhoodAlgorithm::ImageBoundaryFacesCalculator<LocalVariationImageType>::FaceListType lv_faceList =
125 lv_bC(m_LocalVariation, outputRegionForThread, size);
127 // support progress methods/callbacks
128 ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
130 ZeroFluxNeumannBoundaryCondition<InputImageType> nbc;
131 ZeroFluxNeumannBoundaryCondition<LocalVariationImageType> lv_nbc;
132 std::vector<double> ws;
133 std::vector<double> hs;
135 auto lv_fit = lv_faceList.begin();
137 // Process each of the boundary faces. These are N-d regions which border
138 // the edge of the buffer.
139 for (auto fit = faceList.begin(); fit != faceList.end(); ++fit)
141 // iterators over output, input, original and local variation image
142 ImageRegionIterator<OutputImageType> output_image_it = ImageRegionIterator<OutputImageType>(output, *fit);
143 ImageRegionConstIterator<InputImageType> input_image_it = ImageRegionConstIterator<InputImageType>(input, *fit);
144 ImageRegionConstIterator<InputImageType> orig_image_it =
145 ImageRegionConstIterator<InputImageType>(m_OriginalImage, *fit);
146 ImageRegionConstIterator<LocalVariationImageType> loc_var_image_it =
147 ImageRegionConstIterator<LocalVariationImageType>(m_LocalVariation, *fit);
149 // neighborhood in input image
150 ConstShapedNeighborhoodIterator<InputImageType> input_image_neighbors_it(size, input, *fit);
151 typename ConstShapedNeighborhoodIterator<InputImageType>::OffsetType offset;
152 input_image_neighbors_it.OverrideBoundaryCondition(&nbc);
153 input_image_neighbors_it.ClearActiveList();
154 for (int i = 0; i < InputImageDimension; i++)
158 input_image_neighbors_it.ActivateOffset(offset);
160 input_image_neighbors_it.ActivateOffset(offset);
162 input_image_neighbors_it.GoToBegin();
164 // neighborhood in local variation image
165 ConstShapedNeighborhoodIterator<LocalVariationImageType> loc_var_image_neighbors_it(
166 size, m_LocalVariation, *lv_fit);
167 loc_var_image_neighbors_it.OverrideBoundaryCondition(&lv_nbc);
168 loc_var_image_neighbors_it.ClearActiveList();
169 for (int i = 0; i < InputImageDimension; i++)
173 loc_var_image_neighbors_it.ActivateOffset(offset);
175 loc_var_image_neighbors_it.ActivateOffset(offset);
177 loc_var_image_neighbors_it.GoToBegin();
179 const unsigned int neighborhoodSize = InputImageDimension * 2;
180 ws.resize(neighborhoodSize);
182 while (!output_image_it.IsAtEnd())
184 // 1 / ||nabla_alpha(u)||_a
185 double locvar_alpha_inv = 1.0 / loc_var_image_it.Get();
187 // compute w_alphabetas
190 typename ConstShapedNeighborhoodIterator<LocalVariationImageType>::ConstIterator loc_var_neighbors_it;
191 for (loc_var_neighbors_it = loc_var_image_neighbors_it.Begin(); !loc_var_neighbors_it.IsAtEnd();
192 loc_var_neighbors_it++)
195 // 1 / ||nabla_alpha(u)||_a + 1 / ||nabla_beta(u)||_a
196 ws[count] = locvar_alpha_inv + (1.0 / (double)loc_var_neighbors_it.Get());
200 // h_alphaalpha * u_alpha^zero
201 typename OutputImageType::PixelType res = static_cast<typename OutputImageType::PixelType>(
202 ((typename OutputImageType::PixelType)orig_image_it.Get()) * (m_Lambda / (m_Lambda + wsum)));
204 // add the different h_alphabeta * u_beta
206 typename ConstShapedNeighborhoodIterator<InputImageType>::ConstIterator input_neighbors_it;
207 for (input_neighbors_it = input_image_neighbors_it.Begin(); !input_neighbors_it.IsAtEnd(); input_neighbors_it++)
209 res += input_neighbors_it.Get() * (ws[count++] / (m_Lambda + wsum));
213 output_image_it.Set(res);
215 // increment iterators
220 ++input_image_neighbors_it;
221 ++loc_var_image_neighbors_it;
224 progress.CompletedPixel();
232 * first calculate local variation in the image
234 template <class TInputImage, class TOutputImage>
235 void TotalVariationSingleIterationImageFilter<TInputImage, TOutputImage>::BeforeThreadedGenerateData()
237 typedef typename itk::LocalVariationImageFilter<TInputImage, LocalVariationImageType> FilterType;
238 typename FilterType::Pointer filter = FilterType::New();
239 filter->SetInput(this->GetInput(0));
240 filter->SetNumberOfThreads(this->GetNumberOfThreads());
242 this->m_LocalVariation = filter->GetOutput();
246 * Standard "PrintSelf" method
248 template <class TInputImage, class TOutput>
249 void TotalVariationSingleIterationImageFilter<TInputImage, TOutput>::PrintSelf(std::ostream &os, Indent indent) const
251 Superclass::PrintSelf(os, indent);
254 } // end namespace itk