| 1 |
|
/*========================================================================= |
| 2 |
|
|
| 3 |
|
Program: Insight Segmentation & Registration Toolkit |
| 4 |
|
Module: $RCSfile: itkVectorNearestNeighborInterpolateImageFunction.txx.html,v $ |
| 5 |
|
Language: C++ |
| 6 |
|
Date: $Date: 2006/01/17 19:15:49 $ |
| 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 _itkVectorNearestNeighborInterpolateImageFunction_txx |
| 18 |
DEF |
#define _itkVectorNearestNeighborInterpolateImageFunction_txx |
| 19 |
|
#include "itkVectorNearestNeighborInterpolateImageFunction.h" |
| 20 |
|
|
| 21 |
|
#include "vnl/vnl_math.h" |
| 22 |
|
|
| 23 |
|
namespace itk |
| 24 |
|
{ |
| 25 |
|
|
| 26 |
|
/** |
| 27 |
|
* Define the number of neighbors |
| 28 |
|
*/ |
| 29 |
|
template<class TInputImage, class TCoordRep> |
| 30 |
|
const unsigned long |
| 31 |
|
VectorNearestNeighborInterpolateImageFunction< TInputImage, TCoordRep > |
| 32 |
|
::m_Neighbors = 1 << TInputImage::ImageDimension; |
| 33 |
|
|
| 34 |
|
|
| 35 |
|
/** |
| 36 |
|
* Constructor |
| 37 |
|
*/ |
| 38 |
|
template<class TInputImage, class TCoordRep> |
| 39 |
|
VectorNearestNeighborInterpolateImageFunction< TInputImage, TCoordRep > |
| 40 |
|
::VectorNearestNeighborInterpolateImageFunction() |
| 41 |
|
{ |
| 42 |
|
|
| 43 |
|
} |
| 44 |
|
|
| 45 |
|
|
| 46 |
|
/** |
| 47 |
|
* PrintSelf |
| 48 |
|
*/ |
| 49 |
|
template<class TInputImage, class TCoordRep> |
| 50 |
|
void |
| 51 |
|
VectorNearestNeighborInterpolateImageFunction< TInputImage, TCoordRep > |
| 52 |
|
::PrintSelf(std::ostream& os, Indent indent) const |
| 53 |
|
{ |
| 54 |
|
this->Superclass::PrintSelf(os,indent); |
| 55 |
|
} |
| 56 |
|
|
| 57 |
|
|
| 58 |
|
/** |
| 59 |
|
* Evaluate at image index position, no bounds checking.... |
| 60 |
|
* use IsInsideBuffer() if needed |
| 61 |
|
*/ |
| 62 |
|
|
| 63 |
|
/** |
| 64 |
|
* The nearest neighbour is assumed to be the pixel that has the largest |
| 65 |
IND |
* overlap with the pixel that is centered on the indicated point (defined |
| 66 |
|
* by ContinuousIndexType& index. |
| 67 |
|
* |
| 68 |
|
* We also achieve speedup by noting that if overlap is > 1/2 it |
| 69 |
|
* is the nearest neighbour and if < 1/m_Neighbors, it isn't. |
| 70 |
|
* If all overlaps are the same, we choose the last neighbour. |
| 71 |
|
*/ |
| 72 |
|
template<class TInputImage, class TCoordRep> |
| 73 |
|
typename VectorNearestNeighborInterpolateImageFunction< TInputImage, TCoordRep > |
| 74 |
|
::OutputType |
| 75 |
|
VectorNearestNeighborInterpolateImageFunction< TInputImage, TCoordRep > |
| 76 |
|
::EvaluateAtContinuousIndex( |
| 77 |
|
const ContinuousIndexType& index) const |
| 78 |
|
{ |
| 79 |
|
unsigned int dim; // index over dimension |
| 80 |
|
|
| 81 |
|
/** |
| 82 |
|
* Compute base index = closest index below point |
| 83 |
|
* Compute distance from point to base index |
| 84 |
|
*/ |
| 85 |
|
signed long baseIndex[ImageDimension]; |
| 86 |
|
double distance[ImageDimension]; |
| 87 |
|
|
| 88 |
|
for( dim = 0; dim < ImageDimension; dim++ ) |
| 89 |
|
{ |
| 90 |
|
baseIndex[dim] = (long) floor( index[dim] ); |
| 91 |
|
distance[dim] = index[dim] - double( baseIndex[dim] ); |
| 92 |
|
} |
| 93 |
|
|
| 94 |
|
OutputType output; |
| 95 |
|
output.Fill( 0.0 ); |
| 96 |
|
|
| 97 |
|
double uniformOverlap = (1/(static_cast <double> (m_Neighbors))); |
| 98 |
|
|
| 99 |
|
for( unsigned int counter = 0; counter < m_Neighbors; counter++ ) |
| 100 |
|
{ |
| 101 |
|
|
| 102 |
|
double overlap = 1.0; // fraction overlap |
| 103 |
|
double currentMaxOverlap = 0.0; // max overlap until now |
| 104 |
|
unsigned int upper = counter; // each bit indicates upper/lower neighbour |
| 105 |
|
IndexType neighIndex; |
| 106 |
|
|
| 107 |
|
// get neighbor index and overlap fraction |
| 108 |
|
for( dim = 0; dim < ImageDimension; dim++ ) |
| 109 |
|
{ |
| 110 |
|
|
| 111 |
|
if ( upper & 1 ) |
| 112 |
|
{ |
| 113 |
|
neighIndex[dim] = baseIndex[dim] + 1; |
| 114 |
|
overlap *= distance[dim]; |
| 115 |
|
} |
| 116 |
|
else |
| 117 |
|
{ |
| 118 |
|
neighIndex[dim] = baseIndex[dim]; |
| 119 |
|
overlap *= 1.0 - distance[dim]; |
| 120 |
|
} |
| 121 |
|
|
| 122 |
|
upper >>= 1; |
| 123 |
|
|
| 124 |
|
} |
| 125 |
|
|
| 126 |
|
// get neighbor value only if overlap is greater than previous ones |
| 127 |
|
if((overlap > uniformOverlap) && (overlap > currentMaxOverlap) ) |
| 128 |
|
{ |
| 129 |
|
const PixelType input = this->GetInputImage()->GetPixel( neighIndex ); |
| 130 |
|
for(unsigned int k = 0; k < Dimension; k++ ) |
| 131 |
|
{ |
| 132 |
|
output[k] = static_cast<RealType>( input[k] ); |
| 133 |
|
} |
| 134 |
|
currentMaxOverlap = overlap; |
| 135 |
|
} |
| 136 |
|
|
| 137 |
|
if( currentMaxOverlap >= 0.5 ) |
| 138 |
|
{ |
| 139 |
|
// finished |
| 140 |
|
break; |
| 141 |
|
} |
| 142 |
|
|
| 143 |
|
/** |
| 144 |
|
* If all overlaps are a the same, we adopt a uniform policy of choosing |
| 145 |
|
* the last neighbor. Just a convention.. |
| 146 |
|
*/ |
| 147 |
|
if( (counter == (m_Neighbors-1)) && (currentMaxOverlap == uniformOverlap) ) |
| 148 |
|
{ |
| 149 |
|
const PixelType input = this->GetInputImage()->GetPixel( neighIndex ); |
| 150 |
|
for(unsigned int k = 0; k < Dimension; k++ ) |
| 151 |
|
{ |
| 152 |
|
output[k] = static_cast<RealType>( input[k] ); |
| 153 |
|
} |
| 154 |
|
} |
| 155 |
|
} |
| 156 |
|
|
| 157 |
|
return ( output ); |
| 158 |
|
} |
| 159 |
|
|
| 160 |
|
} // namespace itk |
| 161 |
|
|
| 162 |
|
#endif |
| 163 |
|
|