| 1 |
|
/*========================================================================= |
| 2 |
|
|
| 3 |
|
Program: Insight Segmentation & Registration Toolkit |
| 4 |
|
Module: $RCSfile: itkDifferenceImageFilter.txx.html,v $ |
| 5 |
|
Language: C++ |
| 6 |
|
Date: $Date: 2006/01/17 19:15:34 $ |
| 7 |
|
Version: $Revision: 1.4 $ |
| 8 |
|
|
| 9 |
|
Copyright (c) Insight Software Consortium. All rights reserved. |
| 10 |
|
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details. |
| 11 |
|
|
| 12 |
|
This software is distributed WITHOUT ANY WARRANTY; without even |
| 13 |
|
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 |
|
PURPOSE. See the above copyright notices for more information. |
| 15 |
|
|
| 16 |
|
=========================================================================*/ |
| 17 |
DEF |
#ifndef _itkDifferenceImageFilter_txx |
| 18 |
DEF |
#define _itkDifferenceImageFilter_txx |
| 19 |
|
|
| 20 |
|
#include "itkDifferenceImageFilter.h" |
| 21 |
|
|
| 22 |
|
#include "itkConstNeighborhoodIterator.h" |
| 23 |
|
#include "itkImageRegionConstIterator.h" |
| 24 |
|
#include "itkImageRegionIterator.h" |
| 25 |
|
#include "itkNeighborhoodAlgorithm.h" |
| 26 |
|
#include "itkProgressReporter.h" |
| 27 |
|
#include "itkZeroFluxNeumannBoundaryCondition.h" |
| 28 |
|
|
| 29 |
|
namespace itk |
| 30 |
|
{ |
| 31 |
|
|
| 32 |
|
//---------------------------------------------------------------------------- |
| 33 |
|
template <class TInputImage, class TOutputImage> |
| 34 |
|
DifferenceImageFilter<TInputImage, TOutputImage> |
| 35 |
|
::DifferenceImageFilter() |
| 36 |
|
{ |
| 37 |
|
// We require two inputs to execute. |
| 38 |
|
this->SetNumberOfRequiredInputs(2); |
| 39 |
|
|
| 40 |
|
// Set the default DifferenceThreshold. |
| 41 |
|
m_DifferenceThreshold = NumericTraits<OutputPixelType>::Zero; |
| 42 |
|
|
| 43 |
|
// Set the default ToleranceRadius. |
| 44 |
|
m_ToleranceRadius = 0; |
| 45 |
|
|
| 46 |
|
// Initialize statistics about difference image. |
| 47 |
|
m_MeanDifference = NumericTraits<RealType>::Zero; |
| 48 |
|
m_TotalDifference = NumericTraits<AccumulateType>::Zero; |
| 49 |
|
} |
| 50 |
|
|
| 51 |
|
//---------------------------------------------------------------------------- |
| 52 |
|
template<class TInputImage, class TOutputImage> |
| 53 |
|
void |
| 54 |
|
DifferenceImageFilter<TInputImage, TOutputImage> |
| 55 |
|
::PrintSelf(std::ostream& os, Indent indent) const |
| 56 |
|
{ |
| 57 |
|
this->Superclass::PrintSelf(os, indent); |
| 58 |
|
os << indent << "ToleranceRadius: " << m_ToleranceRadius << "\n"; |
| 59 |
|
os << indent << "DifferenceThreshold: " << m_DifferenceThreshold << "\n"; |
| 60 |
|
os << indent << "MeanDifference: " << m_MeanDifference << "\n"; |
| 61 |
|
os << indent << "TotalDifference: " << m_TotalDifference << "\n"; |
| 62 |
|
} |
| 63 |
|
|
| 64 |
|
//---------------------------------------------------------------------------- |
| 65 |
|
template <class TInputImage, class TOutputImage> |
| 66 |
|
void |
| 67 |
|
DifferenceImageFilter<TInputImage, TOutputImage> |
| 68 |
|
::SetValidInput(const InputImageType* validImage) |
| 69 |
|
{ |
| 70 |
|
// The valid image should be input 0. |
| 71 |
|
this->SetInput(0, validImage); |
| 72 |
|
} |
| 73 |
|
|
| 74 |
|
//---------------------------------------------------------------------------- |
| 75 |
|
template <class TInputImage, class TOutputImage> |
| 76 |
|
void |
| 77 |
|
DifferenceImageFilter<TInputImage, TOutputImage> |
| 78 |
|
::SetTestInput(const InputImageType* testImage) |
| 79 |
|
{ |
| 80 |
|
// The test image should be input 1. |
| 81 |
|
this->SetInput(1, testImage); |
| 82 |
|
} |
| 83 |
|
|
| 84 |
|
//---------------------------------------------------------------------------- |
| 85 |
|
template<class TInputImage, class TOutputImage> |
| 86 |
|
void |
| 87 |
|
DifferenceImageFilter<TInputImage, TOutputImage> |
| 88 |
|
::BeforeThreadedGenerateData() |
| 89 |
|
{ |
| 90 |
|
int numberOfThreads = this->GetNumberOfThreads(); |
| 91 |
|
|
| 92 |
|
// Initialize statistics about difference image. |
| 93 |
|
m_MeanDifference = NumericTraits<RealType>::Zero; |
| 94 |
|
m_TotalDifference = NumericTraits<AccumulateType>::Zero; |
| 95 |
|
|
| 96 |
|
// Resize the thread temporaries |
| 97 |
|
m_ThreadDifferenceSum.SetSize(numberOfThreads); |
| 98 |
|
|
| 99 |
|
// Initialize the temporaries |
| 100 |
|
m_ThreadDifferenceSum.Fill(NumericTraits<AccumulateType>::Zero); |
| 101 |
|
} |
| 102 |
|
|
| 103 |
|
//---------------------------------------------------------------------------- |
| 104 |
|
template <class TInputImage, class TOutputImage> |
| 105 |
|
void |
| 106 |
|
DifferenceImageFilter<TInputImage, TOutputImage> |
| 107 |
|
::ThreadedGenerateData(const OutputImageRegionType &threadRegion, int threadId) |
| 108 |
|
{ |
| 109 |
|
typedef ConstNeighborhoodIterator<InputImageType> SmartIterator; |
| 110 |
|
typedef ImageRegionConstIterator<OutputImageType> InputIterator; |
| 111 |
TDA |
typedef ImageRegionIterator<OutputImageType> OutputIterator; |
| 112 |
LEN,TDA |
typedef NeighborhoodAlgorithm::ImageBoundaryFacesCalculator<InputImageType> FacesCalculator; |
| 113 |
TDA |
typedef typename FacesCalculator::RadiusType RadiusType; |
| 114 |
TDA |
typedef typename FacesCalculator::FaceListType FaceListType; |
| 115 |
TDA |
typedef typename FaceListType::iterator FaceListIterator; |
| 116 |
TDA |
typedef typename InputImageType::PixelType InputPixelType; |
| 117 |
|
|
| 118 |
|
// Prepare standard boundary condition. |
| 119 |
|
ZeroFluxNeumannBoundaryCondition<InputImageType> nbc; |
| 120 |
|
|
| 121 |
|
// Get a pointer to each image. |
| 122 |
|
const InputImageType* validImage = this->GetInput(0); |
| 123 |
|
const InputImageType* testImage = this->GetInput(1); |
| 124 |
|
OutputImageType* outputPtr = this->GetOutput(); |
| 125 |
|
|
| 126 |
|
// Create a radius of pixels. |
| 127 |
|
RadiusType radius; |
| 128 |
|
if(m_ToleranceRadius > 0) |
| 129 |
|
{ |
| 130 |
|
radius.Fill(m_ToleranceRadius); |
| 131 |
|
} |
| 132 |
|
else |
| 133 |
|
{ |
| 134 |
|
radius.Fill(0); |
| 135 |
|
} |
| 136 |
|
|
| 137 |
|
// Find the data-set boundary faces. |
| 138 |
|
FacesCalculator boundaryCalculator; |
| 139 |
|
FaceListType faceList = boundaryCalculator(testImage, threadRegion, radius); |
| 140 |
|
|
| 141 |
|
// Support progress methods/callbacks. |
| 142 |
|
ProgressReporter progress(this, threadId, threadRegion.GetNumberOfPixels()); |
| 143 |
|
|
| 144 |
|
// Process the internal face and each of the boundary faces. |
| 145 |
|
for (FaceListIterator face = faceList.begin(); face != faceList.end(); ++face) |
| 146 |
|
{ |
| 147 |
|
SmartIterator test(radius, testImage, *face); // Iterate over test image. |
| 148 |
|
InputIterator valid(validImage, *face); // Iterate over valid image. |
| 149 |
|
OutputIterator out(outputPtr, *face); // Iterate over output image. |
| 150 |
|
test.OverrideBoundaryCondition(&nbc); |
| 151 |
|
|
| 152 |
|
for(valid.GoToBegin(), test.GoToBegin(), out.GoToBegin(); |
| 153 |
IND |
********!valid.IsAtEnd(); |
| 154 |
IND |
********++valid, ++test, ++out) |
| 155 |
|
{ |
| 156 |
|
// Get the current valid pixel. |
| 157 |
|
InputPixelType t = valid.Get(); |
| 158 |
|
|
| 159 |
|
// Find the closest-valued pixel in the neighborhood of the test |
| 160 |
|
// image. |
| 161 |
|
OutputPixelType minimumDifference = NumericTraits<OutputPixelType>::max(); |
| 162 |
|
unsigned int neighborhoodSize = test.Size(); |
| 163 |
|
for (unsigned int i=0; i < neighborhoodSize; ++i) |
| 164 |
|
{ |
| 165 |
|
// Use the RealType for the difference to make sure we get the |
| 166 |
|
// sign. |
| 167 |
|
RealType difference = static_cast<RealType>(t) - test.GetPixel(i); |
| 168 |
|
if(NumericTraits<RealType>::IsNegative(difference)) |
| 169 |
|
{ |
| 170 |
|
difference = -difference; |
| 171 |
|
} |
| 172 |
|
OutputPixelType d = static_cast<OutputPixelType>(difference); |
| 173 |
|
if(d < minimumDifference) |
| 174 |
|
{ |
| 175 |
|
minimumDifference = d; |
| 176 |
|
} |
| 177 |
|
} |
| 178 |
|
|
| 179 |
|
// Check if difference is above threshold. |
| 180 |
|
if(minimumDifference >= m_DifferenceThreshold) |
| 181 |
|
{ |
| 182 |
|
// Store the minimum difference value in the output image. |
| 183 |
|
out.Set(minimumDifference); |
| 184 |
|
|
| 185 |
|
// Update difference image statistics. |
| 186 |
|
m_ThreadDifferenceSum[threadId] += minimumDifference; |
| 187 |
|
} |
| 188 |
|
else |
| 189 |
|
{ |
| 190 |
|
// Difference is below threshold. |
| 191 |
|
out.Set(NumericTraits<OutputPixelType>::Zero); |
| 192 |
|
} |
| 193 |
|
|
| 194 |
|
// Update progress. |
| 195 |
|
progress.CompletedPixel(); |
| 196 |
|
} |
| 197 |
|
} |
| 198 |
|
} |
| 199 |
|
|
| 200 |
|
//---------------------------------------------------------------------------- |
| 201 |
|
template <class TInputImage, class TOutputImage> |
| 202 |
|
void |
| 203 |
|
DifferenceImageFilter<TInputImage, TOutputImage> |
| 204 |
|
::AfterThreadedGenerateData() |
| 205 |
|
{ |
| 206 |
|
// Set statistics about difference image. |
| 207 |
|
int numberOfThreads = this->GetNumberOfThreads(); |
| 208 |
|
for(int i=0; i < numberOfThreads; ++i) |
| 209 |
|
{ |
| 210 |
|
m_TotalDifference += m_ThreadDifferenceSum[i]; |
| 211 |
|
} |
| 212 |
|
|
| 213 |
|
// Get the total number of pixels processed. |
| 214 |
|
OutputImageRegionType region = this->GetOutput()->GetRequestedRegion(); |
| 215 |
|
AccumulateType numberOfPixels = region.GetNumberOfPixels(); |
| 216 |
|
|
| 217 |
|
// Calculate the mean difference. |
| 218 |
|
m_MeanDifference = m_TotalDifference / numberOfPixels; |
| 219 |
|
} |
| 220 |
|
|
| 221 |
|
} // end namespace itk |
| 222 |
|
|
| 223 |
|
#endif |
| 224 |
|
|