4 #include <itkImageRegionIterator.h> 5 #include <itkImageRegionIteratorWithIndex.h> 6 #include <itkImageRegionConstIterator.h> 7 #include <itkImageRegionConstIteratorWithIndex.h> 8 #include <itkTimeProbe.h> 11 #include <boost/math/distributions/fisher_f.hpp> 16 template <
class PixelScalarType>
21 Superclass::BeforeThreadedGenerateData();
25 unsigned int nbInputs = this->GetNumberOfIndexedInputs();
27 itkExceptionMacro(
"Error: No inputs available... Exiting...");
30 template <
class PixelScalarType>
35 typedef itk::ImageRegionConstIteratorWithIndex< InputImageType > InIteratorType;
36 typedef itk::ImageRegionConstIterator < InputImageType > DatabaseIteratorType;
37 typedef itk::ImageRegionIterator < OutputImageType > OutRegionIteratorType;
38 typedef itk::ImageRegionConstIterator < MaskImageType > MaskRegionIteratorType;
40 OutRegionIteratorType outIterator(this->GetOutput(0), outputRegionForThread);
41 OutRegionIteratorType outScoreIterator(this->GetOutput(1), outputRegionForThread);
42 OutRegionIteratorType outNumPatchesIterator(this->GetOutput(2), outputRegionForThread);
44 MaskRegionIteratorType maskIterator (this->GetComputationMask(), outputRegionForThread);
46 unsigned int numSamplesDatabase = m_DatabaseImages.size();
47 std::vector <DatabaseIteratorType> databaseIterators(numSamplesDatabase);
48 for (
unsigned int k = 0;k < numSamplesDatabase;++k)
49 databaseIterators[k] = DatabaseIteratorType(m_DatabaseImages[k],outputRegionForThread);
52 InIteratorType patientIterator(input, outputRegionForThread);
55 std::vector <VectorType> databaseSamples;
56 std::vector <double> databaseWeights;
58 int maxAbsDisp = (int)floor((
double)(m_SearchNeighborhood / m_SearchStepSize)) * m_SearchStepSize;
62 PatchSearcherType patchSearcher;
64 patchSearcher.SetSearchStepSize(m_SearchStepSize);
65 patchSearcher.SetMaxAbsDisp(maxAbsDisp);
66 patchSearcher.SetInputImage(input);
67 patchSearcher.SetBetaParameter(m_BetaParameter);
68 patchSearcher.SetWeightThreshold(m_WeightThreshold);
69 patchSearcher.SetMeanThreshold(m_MeanThreshold);
70 patchSearcher.SetVarianceThreshold(m_VarianceThreshold);
71 patchSearcher.SetDatabaseCovarianceDistanceAverage(m_DatabaseCovarianceDistanceAverage);
72 patchSearcher.SetDatabaseCovarianceDistanceStd(m_DatabaseCovarianceDistanceStd);
73 patchSearcher.SetDatabaseMeanDistanceAverage(m_DatabaseMeanDistanceAverage);
74 patchSearcher.SetDatabaseMeanDistanceStd(m_DatabaseMeanDistanceStd);
75 patchSearcher.SetDataMask(this->GetComputationMask());
77 for (
unsigned int k = 0;k < numSamplesDatabase;++k)
78 patchSearcher.AddComparisonImage(m_DatabaseImages[k]);
80 while (!outIterator.IsAtEnd())
82 if (maskIterator.Get() == 0)
85 outScoreIterator.Set(0.0);
86 outNumPatchesIterator.Set(0.0);
91 ++outNumPatchesIterator;
93 for (
unsigned int k = 0;k < numSamplesDatabase;++k)
94 ++databaseIterators[k];
99 patchSearcher.UpdateAtPosition(patientIterator.GetIndex());
101 databaseSamples = patchSearcher.GetDatabaseSamples();
102 databaseWeights = patchSearcher.GetDatabaseWeights();
105 for (
unsigned int k = 0;k < numSamplesDatabase;++k)
107 databaseSamples.push_back(databaseIterators[k].Get());
108 databaseWeights.push_back(1.0);
111 double diffScore = 0;
112 patientSample = patientIterator.Get();
117 if (databaseSamples.size() > 1)
118 pValue = this->ComputeWeightedDistanceScore(patientSample,databaseWeights,databaseSamples,diffScore);
122 itkExceptionMacro(
"dying in final calculations");
125 outIterator.Set(pValue);
126 outScoreIterator.Set(diffScore);
127 outNumPatchesIterator.Set(databaseSamples.size());
129 this->IncrementNumberOfProcessedPoints();
132 ++outNumPatchesIterator;
136 for (
unsigned int k = 0;k < numSamplesDatabase;++k)
137 ++databaseIterators[k];
141 template <
class PixelScalarType>
145 std::vector < itk::VariableLengthVector <double> > &databaseSamples,
double &diffScore)
149 unsigned int nsamples = databaseSamples.size();
151 unsigned int ndim = patientSample.GetSize();
153 if (nsamples <= ndim)
157 double sumWeights = 0;
158 itk::VariableLengthVector <double> databaseMean(ndim);
159 databaseMean.Fill(0);
161 for (
unsigned int i = 0;i < nsamples;++i)
162 sumWeights += databaseWeights[i];
164 double sqrSumWeights = 0;
165 for (
unsigned int i = 0;i < nsamples;++i)
167 databaseWeights[i] /= sumWeights;
168 sqrSumWeights += databaseWeights[i] * databaseWeights[i];
171 for (
unsigned int i = 0;i < nsamples;++i)
173 databaseMean += databaseWeights[i] * databaseSamples[i];
176 vnl_matrix <double> covMatrixDatabase(ndim,ndim);
177 covMatrixDatabase.fill(0);
179 for (
unsigned int i = 0;i < nsamples;++i)
181 for (
unsigned int j = 0;j < ndim;++j)
182 for (
unsigned int k = j;k < ndim;++k)
183 covMatrixDatabase(j,k) += databaseWeights[i]*(databaseMean[j] - databaseSamples[i][j])*(databaseMean[k] - databaseSamples[i][k]);
186 for (
unsigned int j = 0;j < ndim;++j)
187 for (
unsigned int k = j;k < ndim;++k)
189 covMatrixDatabase(j,k) *= 1.0 / (1.0 - sqrSumWeights);
191 covMatrixDatabase(k,j) = covMatrixDatabase(j,k);
194 vnl_matrix_inverse <double> invCovMatrixDatabase(covMatrixDatabase);
195 vnl_matrix <double> covInvDatabase = invCovMatrixDatabase.inverse();
198 for (
unsigned int j = 0;j < ndim;++j)
199 for (
unsigned int k = j;k < ndim;++k)
205 diffScore += factor*(patientSample[j] - databaseMean[j])*(patientSample[k] - databaseMean[k])*covInvDatabase(j,k);
208 double testScore = nsamples * (nsamples - ndim) * diffScore / ((nsamples *nsamples - 1) * ndim);
209 boost::math::fisher_f_distribution <double> fisherDist(ndim,nsamples - ndim);
210 double resVal = 1.0 - boost::math::cdf(fisherDist, testScore);
212 diffScore = std::sqrt(diffScore);
void BeforeThreadedGenerateData() ITK_OVERRIDE
itk::VectorImage< PixelScalarType, 3 > InputImageType
void DynamicThreadedGenerateData(const OutputImageRegionType &outputRegionForThread) ITK_OVERRIDE
InputImageType::PixelType VectorType
void SetPatchHalfSize(unsigned int arg)
Superclass::OutputImageRegionType OutputImageRegionType