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 ===================================================================*/
22 #ifndef _itkLocalVariationImageFilter_txx
23 #define _itkLocalVariationImageFilter_txx
24 #include "itkLocalVariationImageFilter.h"
26 #include "itkConstShapedNeighborhoodIterator.h"
27 #include "itkImageRegionIterator.h"
28 #include "itkNeighborhoodAlgorithm.h"
29 #include "itkNeighborhoodInnerProduct.h"
30 #include "itkOffset.h"
31 #include "itkProgressReporter.h"
32 #include "itkVectorImage.h"
33 #include "itkZeroFluxNeumannBoundaryCondition.h"
40 template <class TInputImage, class TOutputImage>
41 LocalVariationImageFilter<TInputImage, TOutputImage>::LocalVariationImageFilter()
45 template <class TInputImage, class TOutputImage>
46 void LocalVariationImageFilter<TInputImage, TOutputImage>::GenerateInputRequestedRegion() throw(
47 InvalidRequestedRegionError)
49 // call the superclass' implementation of this method
50 Superclass::GenerateInputRequestedRegion();
52 // get pointers to the input and output
53 typename Superclass::InputImagePointer inputPtr = const_cast<TInputImage *>(this->GetInput());
54 typename Superclass::OutputImagePointer outputPtr = this->GetOutput();
56 if (!inputPtr || !outputPtr)
61 // get a copy of the input requested region (should equal the output
63 typename TInputImage::RegionType inputRequestedRegion;
64 inputRequestedRegion = inputPtr->GetRequestedRegion();
66 // pad the input requested region by 1
67 inputRequestedRegion.PadByRadius(1);
69 // crop the input requested region at the input's largest possible region
70 if (inputRequestedRegion.Crop(inputPtr->GetLargestPossibleRegion()))
72 inputPtr->SetRequestedRegion(inputRequestedRegion);
77 // Couldn't crop the region (requested region is outside the largest
78 // possible region). Throw an exception.
80 // store what we tried to request (prior to trying to crop)
81 inputPtr->SetRequestedRegion(inputRequestedRegion);
84 InvalidRequestedRegionError e(__FILE__, __LINE__);
85 e.SetLocation(ITK_LOCATION);
86 e.SetDescription("Requested region outside possible region.");
87 e.SetDataObject(inputPtr);
93 double SquaredEuclideanMetric<itk::VariableLengthVector<float>>::Calc(itk::VariableLengthVector<float> p)
95 return p.GetSquaredNorm();
99 double SquaredEuclideanMetric<itk::VariableLengthVector<double>>::Calc(itk::VariableLengthVector<double> p)
101 return p.GetSquaredNorm();
104 template <class TPixelType>
105 double SquaredEuclideanMetric<TPixelType>::Calc(TPixelType p)
110 template <class TInputImage, class TOutputImage>
111 void LocalVariationImageFilter<TInputImage, TOutputImage>::ThreadedGenerateData(
112 const OutputImageRegionType &outputRegionForThread, ThreadIdType threadId)
115 typename OutputImageType::Pointer output = this->GetOutput();
116 typename InputImageType::ConstPointer input = this->GetInput();
118 itk::Size<InputImageDimension> size;
119 for (int i = 0; i < InputImageDimension; i++)
122 // Find the data-set boundary "faces"
123 NeighborhoodAlgorithm::ImageBoundaryFacesCalculator<InputImageType> bC;
124 typename NeighborhoodAlgorithm::ImageBoundaryFacesCalculator<InputImageType>::FaceListType faceList =
125 bC(input, outputRegionForThread, size);
127 // support progress methods/callbacks
128 ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
130 ZeroFluxNeumannBoundaryCondition<InputImageType> nbc;
131 std::vector<InputPixelType> pixels;
133 // Process each of the boundary faces. These are N-d regions which border
134 // the edge of the buffer.
135 for (auto fit = faceList.begin(); fit != faceList.end(); ++fit)
137 // iterators over output and input
138 ImageRegionIterator<OutputImageType> output_image_it(output, *fit);
139 ImageRegionConstIterator<InputImageType> input_image_it(input.GetPointer(), *fit);
141 // neighborhood iterator for input image
142 ConstShapedNeighborhoodIterator<InputImageType> input_image_neighbors_it(size, input, *fit);
143 typename ConstShapedNeighborhoodIterator<InputImageType>::OffsetType offset;
144 input_image_neighbors_it.OverrideBoundaryCondition(&nbc);
145 input_image_neighbors_it.ClearActiveList();
146 for (int i = 0; i < InputImageDimension; i++)
150 input_image_neighbors_it.ActivateOffset(offset);
152 input_image_neighbors_it.ActivateOffset(offset);
154 input_image_neighbors_it.GoToBegin();
155 // const unsigned int neighborhoodSize = InputImageDimension*2;
157 while (!input_image_neighbors_it.IsAtEnd())
159 // collect all the pixels in the neighborhood, note that we use
160 // GetPixel on the NeighborhoodIterator to honor the boundary conditions
161 typename OutputImageType::PixelType locVariation = 0;
162 typename ConstShapedNeighborhoodIterator<InputImageType>::ConstIterator input_neighbors_it;
163 for (input_neighbors_it = input_image_neighbors_it.Begin(); !input_neighbors_it.IsAtEnd(); input_neighbors_it++)
165 typename TInputImage::PixelType diffVec = input_neighbors_it.Get() - input_image_it.Get();
166 locVariation += SquaredEuclideanMetric<typename TInputImage::PixelType>::Calc(diffVec);
168 locVariation = sqrt(locVariation + 0.0001);
169 output_image_it.Set(locVariation);
172 ++input_image_neighbors_it;
177 progress.CompletedPixel();
183 * Standard "PrintSelf" method
185 template <class TInputImage, class TOutput>
186 void LocalVariationImageFilter<TInputImage, TOutput>::PrintSelf(std::ostream &os, Indent indent) const
188 Superclass::PrintSelf(os, indent);
191 } // end namespace itk
193 #endif //_itkLocalVariationImageFilter_txx