KWStyle - itkMedianImageFunction.txx
 
Matrix View
Description

1 /*=========================================================================
2
3   Program:   Insight Segmentation & Registration Toolkit
4   Module:    $RCSfile: itkMedianImageFunction.txx.html,v $
5   Language:  C++
6   Date:      $Date: 2006/01/17 19:15:41 $
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 _itkMedianImageFunction_txx
18 DEF #define _itkMedianImageFunction_txx
19 #include "itkMedianImageFunction.h"
20
21 #include "itkNumericTraits.h"
22 #include "itkConstNeighborhoodIterator.h"
23 #include <vector>
24 #include <algorithm>
25
26
27 namespace itk
28 {
29
30 /**
31  * Constructor
32  */
33 template <class TInputImage, class TCoordRep>
34 MedianImageFunction<TInputImage,TCoordRep>
35 ::MedianImageFunction()
36 {
37 }
38
39
40 /**
41  *
42  */
43 template <class TInputImage, class TCoordRep>
44 void
45 MedianImageFunction<TInputImage,TCoordRep>
46 ::PrintSelf(std::ostream& os, Indent indent) const
47 {
48   this->Superclass::PrintSelf(os,indent);
49 }
50
51
52 /**
53  *
54  */
55 template <class TInputImage, class TCoordRep>
56 typename MedianImageFunction<TInputImage,TCoordRep>
57 ::OutputType
58 MedianImageFunction<TInputImage,TCoordRep>
59 ::EvaluateAtIndex(const IndexType& index) const
60 {
61   unsigned int i;
62   
63   if( !this->GetInputImage() )
64     {
65     return ( NumericTraits<OutputType>::max() );
66     }
67   
68   if ( !this->IsInsideBuffer( index ) )
69     {
70     return ( NumericTraits<OutputType>::max() );
71     }
72
73   // Create an N-d neighborhood kernel, using a zeroflux boundary condition
74   typename InputImageType::SizeType kernelSize;
75   kernelSize.Fill( 1 );
76   
77   ConstNeighborhoodIterator<InputImageType>
78 LEN,IND ****it(kernelSize, this->GetInputImage(), this->GetInputImage()->GetBufferedRegion());
79
80   // Set the iterator at the desired location
81   it.SetLocation(index);
82
83   // We have to copy the pixels so we can run std::nth_element.
84   std::vector<InputPixelType> pixels;
85   typename std::vector<InputPixelType>::iterator medianIterator;
86   
87   // Walk the neighborhood
88   for (i = 0; i < it.Size(); ++i)
89     {
90     pixels.push_back( it.GetPixel(i) );
91     }
92
93   // Get the median value
94   unsigned int medianPosition = it.Size() / 2;
95   medianIterator = pixels.begin() + medianPosition;
96   std::nth_element(pixels.begin(), medianIterator, pixels.end());
97              
98   return ( *medianIterator );
99 }
100
101
102 // namespace itk
103
104 #endif
105

Generated by KWStyle 1.0b on Tuesday January,17 at 02:14:21PM
© Kitware Inc.