17 #ifndef __itkVectorConfidenceConnectedImageFilter_txx
18 #define __itkVectorConfidenceConnectedImageFilter_txx
28 #include "itkNumericTraits.h"
38 template <
class TInputImage,
class TOutputImage>
43 m_NumberOfIterations = 4;
45 m_InitialNeighborhoodRadius = 1;
46 m_ReplaceValue = NumericTraits<OutputImagePixelType>::One;
47 m_ThresholdFunction = DistanceThresholdFunctionType::New();
53 template <
class TInputImage,
class TOutputImage>
58 this->Superclass::PrintSelf(os, indent);
59 os << indent <<
"Number of iterations: " << m_NumberOfIterations
61 os << indent <<
"Multiplier for confidence interval: " << m_Multiplier
63 os << indent <<
"ReplaceValue: "
64 <<
static_cast<typename NumericTraits<OutputImagePixelType>::PrintType
>(m_ReplaceValue)
66 os << indent <<
"InitialNeighborhoodRadius: " << m_InitialNeighborhoodRadius
71 template <
class TInputImage,
class TOutputImage>
76 Superclass::GenerateInputRequestedRegion();
77 if ( this->GetInput() )
80 const_cast< TInputImage *
>( this->GetInput() );
81 input->SetRequestedRegionToLargestPossibleRegion();
85 template <
class TInputImage,
class TOutputImage>
90 Superclass::EnlargeOutputRequestedRegion(output);
94 template <
class TInputImage,
class TOutputImage>
99 typedef typename InputImageType::PixelType InputPixelType;
112 outputImage->SetBufferedRegion( region );
113 outputImage->Allocate();
114 outputImage->FillBuffer ( NumericTraits<OutputImagePixelType>::Zero );
118 typename VectorMeanImageFunctionType::Pointer meanFunction
119 = VectorMeanImageFunctionType::New();
121 meanFunction->SetInputImage( inputImage );
122 meanFunction->SetNeighborhoodRadius( m_InitialNeighborhoodRadius );
124 typename CovarianceImageFunctionType::Pointer varianceFunction
125 = CovarianceImageFunctionType::New();
126 varianceFunction->SetInputImage( inputImage );
127 varianceFunction->SetNeighborhoodRadius( m_InitialNeighborhoodRadius );
130 m_ThresholdFunction->SetInputImage ( inputImage );
135 typedef typename InputPixelType::ValueType ComponentPixelType;
136 typedef typename NumericTraits< ComponentPixelType >::RealType ComponentRealType;
138 const unsigned int dimension = InputPixelType::Dimension;
143 covariance.fill( NumericTraits<ComponentRealType>::Zero );
144 mean.fill( NumericTraits<ComponentRealType>::Zero );
146 typedef typename VectorMeanImageFunctionType::OutputType MeanFunctionVectorType;
147 typedef typename CovarianceImageFunctionType::OutputType CovarianceFunctionMatrixType;
149 typename SeedsContainerType::const_iterator si = m_Seeds.begin();
150 typename SeedsContainerType::const_iterator li = m_Seeds.end();
153 const MeanFunctionVectorType meanContribution = meanFunction->EvaluateAtIndex( *si );
154 const CovarianceFunctionMatrixType covarianceContribution = varianceFunction->EvaluateAtIndex( *si );
155 for(
unsigned int ii=0; ii < dimension; ii++)
157 mean[ ii ] += meanContribution[ ii ];
158 for(
unsigned int jj=0; jj < dimension; jj++)
160 covariance[ ii ][ jj ] += covarianceContribution[ ii ][ jj ];
165 for(
unsigned int ik=0; ik < dimension; ik++)
167 mean[ ik ] /= m_Seeds.size();
168 for(
unsigned int jk=0; jk < dimension; jk++)
170 covariance[ ik ][ jk ] /= m_Seeds.size();
174 m_ThresholdFunction->SetMean( mean );
175 m_ThresholdFunction->SetCovariance( covariance );
177 m_ThresholdFunction->SetThreshold( m_Multiplier );
179 itkDebugMacro(<<
"\nMultiplier originally = " << m_Multiplier );
185 si = m_Seeds.begin();
189 const double distance =
190 m_ThresholdFunction->EvaluateDistanceAtIndex( *si );
191 if( distance > m_Multiplier )
193 m_Multiplier = distance;
199 m_ThresholdFunction->SetThreshold( m_Multiplier );
201 itkDebugMacro(<<
"\nMultiplier after verifying seeds inclusion = " << m_Multiplier );
210 IteratorType it = IteratorType ( outputImage, m_ThresholdFunction, m_Seeds );
212 while( !it.IsAtEnd())
214 it.Set(m_ReplaceValue);
218 ProgressReporter progress(
this, 0, region.GetNumberOfPixels() * m_NumberOfIterations );
220 for (loop = 0; loop < m_NumberOfIterations; ++loop)
228 typename SecondFunctionType::Pointer secondFunction = SecondFunctionType::New();
229 secondFunction->SetInputImage ( outputImage );
230 secondFunction->ThresholdBetween( m_ReplaceValue, m_ReplaceValue );
235 covariance.fill( NumericTraits<ComponentRealType>::Zero );
236 mean.fill( NumericTraits<ComponentRealType>::Zero );
238 unsigned long num = 0;
240 SecondIteratorType sit
241 = SecondIteratorType ( inputImage, secondFunction, m_Seeds );
243 while( !sit.IsAtEnd())
245 const InputPixelType pixelValue = sit.Get();
246 for(
unsigned int i=0; i<dimension; i++)
248 const ComponentRealType pixelValueI =
static_cast<ComponentRealType
>( pixelValue[i] );
249 covariance[i][i] += pixelValueI * pixelValueI;
250 mean[i] += pixelValueI;
251 for(
unsigned int j=i+1; j<dimension; j++)
253 const ComponentRealType pixelValueJ =
static_cast<ComponentRealType
>( pixelValue[j] );
254 const ComponentRealType product = pixelValueI * pixelValueJ;
255 covariance[i][j] += product;
256 covariance[j][i] += product;
262 for(
unsigned int ii=0; ii<dimension; ii++)
264 mean[ii] /=
static_cast<double>(num);
265 for(
unsigned int jj=0; jj<dimension; jj++)
267 covariance[ii][jj] /=
static_cast<double>(num);
271 for(
unsigned int ik=0; ik<dimension; ik++)
273 for(
unsigned int jk=0; jk<dimension; jk++)
275 covariance[ik][jk] -= mean[ik] * mean[jk];
279 m_ThresholdFunction->SetMean( mean );
280 m_ThresholdFunction->SetCovariance( covariance );
290 outputImage->FillBuffer ( NumericTraits<OutputImagePixelType>::Zero );
291 IteratorType thirdIt = IteratorType ( outputImage, m_ThresholdFunction, m_Seeds );
295 while( !thirdIt.IsAtEnd())
297 thirdIt.Set(m_ReplaceValue);
299 progress.CompletedPixel();
310 if( this->GetAbortGenerateData() )
320 template <
class TInputImage,
class TOutputImage>
326 return m_ThresholdFunction->GetCovariance();
329 template <
class TInputImage,
class TOutputImage>
335 return m_ThresholdFunction->GetMean();