| 1 | /*========================================================================= |
| 2 | * |
| 3 | * Copyright NumFOCUS |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * https://www.apache.org/licenses/LICENSE-2.0.txt |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | * |
| 17 | *=========================================================================*/ |
| 18 | #ifndef itkVarianceImageFilter_hxx |
| 19 | #define itkVarianceImageFilter_hxx |
| 20 | |
| 21 | #include "itkConstNeighborhoodIterator.h" |
| 22 | #include "itkNeighborhoodInnerProduct.h" |
| 23 | #include "itkImageRegionIterator.h" |
| 24 | #include "itkNeighborhoodAlgorithm.h" |
| 25 | #include "itkOffset.h" |
| 26 | #include "itkProgressReporter.h" |
| 27 | |
| 28 | namespace itk |
| 29 | { |
| 30 | template <typename TInputImage, typename TOutputImage> |
| 31 | VarianceImageFilter<TInputImage, TOutputImage>::VarianceImageFilter() |
| 32 | { |
| 33 | this->DynamicMultiThreadingOff(); |
| 34 | } |
| 35 | |
| 36 | template <typename TInputImage, typename TOutputImage> |
| 37 | void |
| 38 | VarianceImageFilter<TInputImage, TOutputImage>::ThreadedGenerateData( |
| 39 | const OutputImageRegionType & outputRegionForThread, |
| 40 | ThreadIdType threadId) |
| 41 | { |
| 42 | unsigned int i; |
| 43 | |
| 44 | ZeroFluxNeumannBoundaryCondition<InputImageType> nbc; |
| 45 | |
| 46 | ConstNeighborhoodIterator<InputImageType> bit; |
| 47 | ImageRegionIterator<OutputImageType> it; |
| 48 | |
| 49 | // Allocate output |
| 50 | typename OutputImageType::Pointer output = this->GetOutput(); |
| 51 | typename InputImageType::ConstPointer input = this->GetInput(); |
| 52 | |
| 53 | // Find the data-set boundary "faces" |
| 54 | typename NeighborhoodAlgorithm::ImageBoundaryFacesCalculator<InputImageType>::FaceListType faceList; |
| 55 | NeighborhoodAlgorithm::ImageBoundaryFacesCalculator<InputImageType> bC; |
| 56 | faceList = bC(input, outputRegionForThread, this->GetRadius()); |
| 57 | |
| 58 | typename NeighborhoodAlgorithm::ImageBoundaryFacesCalculator<InputImageType>::FaceListType::iterator fit; |
| 59 | |
| 60 | // support progress methods/callbacks |
| 61 | ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels()); |
| 62 | |
| 63 | InputRealType sum; |
| 64 | InputRealType sumOfSquares; |
| 65 | |
| 66 | // Process each of the boundary faces. These are N-d regions which border |
| 67 | // the edge of the buffer. |
| 68 | for (fit = faceList.begin(); fit != faceList.end(); ++fit) |
| 69 | { |
| 70 | bit = ConstNeighborhoodIterator<InputImageType>(this->GetRadius(), input, *fit); |
| 71 | unsigned int neighborhoodSize = bit.Size(); |
| 72 | it = ImageRegionIterator<OutputImageType>(output, *fit); |
| 73 | bit.OverrideBoundaryCondition(&nbc); |
| 74 | bit.GoToBegin(); |
| 75 | |
| 76 | while (!bit.IsAtEnd()) |
| 77 | { |
| 78 | sum = NumericTraits<InputRealType>::ZeroValue(); |
| 79 | sumOfSquares = NumericTraits<InputRealType>::ZeroValue(); |
| 80 | |
| 81 | for (i = 0; i < neighborhoodSize; ++i) |
| 82 | { |
| 83 | sum += static_cast<InputRealType>(bit.GetPixel(i)); |
| 84 | sumOfSquares += itk::Math::sqr(static_cast<InputRealType>(bit.GetPixel(i))); |
| 85 | } |
| 86 | |
| 87 | // get the variance value |
| 88 | const double num = static_cast<double>(neighborhoodSize); |
| 89 | OutputPixelType var = (sumOfSquares - (itk::Math::sqr(sum) / num)) / (num - 1.0); |
| 90 | |
| 91 | it.Set(var); |
| 92 | |
| 93 | ++bit; |
| 94 | ++it; |
| 95 | progress.CompletedPixel(); |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | } // end namespace itk |
| 100 | |
| 101 | #endif |