forked from InsightSoftwareConsortium/ITK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
itkPCAShapeSignedDistanceFunction.txx
230 lines (181 loc) · 7.25 KB
/
itkPCAShapeSignedDistanceFunction.txx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: itkPCAShapeSignedDistanceFunction.txx
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Insight Software Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#ifndef __itkPCAShapeSignedDistanceFunction_txx
#define __itkPCAShapeSignedDistanceFunction_txx
#include "itkPCAShapeSignedDistanceFunction.h"
#include "itkTranslationTransform.h"
#include "itkLinearInterpolateImageFunction.h"
#include "itkNearestNeighborExtrapolateImageFunction.h"
namespace itk
{
// Constructor with default arguments
template<typename TCoordRep, unsigned int VSpaceDimension, typename TImage>
PCAShapeSignedDistanceFunction<TCoordRep, VSpaceDimension,TImage>
::PCAShapeSignedDistanceFunction()
{
m_NumberOfPrincipalComponents = 0;
m_NumberOfTransformParameters = 0;
m_MeanImage = NULL;
m_PrincipalComponentImages.resize(0);
m_PrincipalComponentStandardDeviations.SetSize(0);
m_Transform = TranslationTransform<TCoordRep, SpaceDimension>::New();
m_Interpolators.resize(0);
m_Extrapolators.resize(0);
m_Selectors.resize(0);
m_WeightOfPrincipalComponents.SetSize(0);
m_TransformParameters.SetSize(0);
this->GetParameters().SetSize(0);
}
// Set the number of principal components
template<typename TCoordRep, unsigned int VSpaceDimension, typename TImage>
void
PCAShapeSignedDistanceFunction<TCoordRep, VSpaceDimension,TImage>
::SetNumberOfPrincipalComponents(unsigned int n)
{
m_NumberOfPrincipalComponents = n;
m_PrincipalComponentImages.resize(n,NULL);
m_PrincipalComponentStandardDeviations.SetSize(n);
m_PrincipalComponentStandardDeviations.Fill( 1.0 );
m_WeightOfPrincipalComponents.SetSize(n);
}
// Set the parameters
template<typename TCoordRep, unsigned int VSpaceDimension, typename TImage>
void
PCAShapeSignedDistanceFunction<TCoordRep, VSpaceDimension,TImage>
::SetParameters( const ParametersType & parameters )
{
this->m_Parameters = parameters;
// set the shape parameters
unsigned int i;
for(i=0; i<m_NumberOfPrincipalComponents; i++)
{ m_WeightOfPrincipalComponents[i] = parameters[i]; }
// set the transform parameters
m_NumberOfTransformParameters =
parameters.size() - m_NumberOfPrincipalComponents;
m_TransformParameters.SetSize(m_NumberOfTransformParameters);
for(i=0; i<m_NumberOfTransformParameters; i++)
{m_TransformParameters[i] = parameters[m_NumberOfPrincipalComponents+i];}
// set up the transform
m_Transform->SetParameters(m_TransformParameters);
}
// Print self
template<typename TCoordRep, unsigned int VSpaceDimension, typename TImage>
void
PCAShapeSignedDistanceFunction<TCoordRep, VSpaceDimension,TImage>
::PrintSelf(std::ostream &os, Indent indent) const
{
Superclass::PrintSelf(os,indent);
os << indent << "Transform: "
<< m_Transform.GetPointer() << std::endl;
os << indent << "NumberOfPrincipalComponents: "
<< m_NumberOfPrincipalComponents << std::endl;
os << indent << "PrincipalComponentStandardDeviations: "
<< m_PrincipalComponentStandardDeviations << std::endl;
os << indent << "MeanImage: "
<< m_MeanImage.GetPointer() << std::endl;
os << indent << "WeightOfPrincipalComponents: "
<< m_WeightOfPrincipalComponents << std::endl;
os << indent << "TransformParameters: "
<< m_TransformParameters << std::endl;
}
// Initialize the function
template<typename TCoordRep, unsigned int VSpaceDimension, typename TImage>
void
PCAShapeSignedDistanceFunction<TCoordRep, VSpaceDimension,TImage>
::Initialize() throw ( ExceptionObject )
{
// verify mean image
if ( !m_MeanImage )
{
itkExceptionMacro( << "MeanImage is not present." );
}
// verify principal component images
if ( m_PrincipalComponentImages.size() < m_NumberOfPrincipalComponents )
{
itkExceptionMacro( << "PrincipalComponentsImages does not have at least "
<< m_NumberOfPrincipalComponents
<< " number of elements." );
}
// verify image buffered region
typename ImageType::RegionType meanImageRegion =
m_MeanImage->GetBufferedRegion();
for (unsigned int i=0; i< m_NumberOfPrincipalComponents; i++)
{
if ( !m_PrincipalComponentImages[i] )
{
itkExceptionMacro( << "PrincipalComponentImages["
<< i << "] is not present." );
}
if ( m_PrincipalComponentImages[i]->GetBufferedRegion() !=
meanImageRegion )
{
itkExceptionMacro( << "The buffered region of the PrincipalComponentImages["
<< i << "] is different from the MeanImage." );
}
}
// set up the interpolators/extrapolators for each of the mean and pc images
m_Interpolators.resize(m_NumberOfPrincipalComponents + 1);
m_Extrapolators.resize(m_NumberOfPrincipalComponents + 1);
m_Selectors.resize(m_NumberOfPrincipalComponents+1);
// interpolator/extrapolator for mean image
m_Interpolators[0] =
LinearInterpolateImageFunction<ImageType, CoordRepType>::New();
m_Interpolators[0]->SetInputImage(m_MeanImage);
m_Extrapolators[0] =
NearestNeighborExtrapolateImageFunction<ImageType, CoordRepType>::New();
m_Extrapolators[0]->SetInputImage(m_MeanImage);
// interpolators/extrapolators for pc images
for (unsigned int k=1; k<=m_NumberOfPrincipalComponents; k++)
{
m_Interpolators[k] =
LinearInterpolateImageFunction<ImageType, CoordRepType>::New();
m_Interpolators[k]->SetInputImage(m_PrincipalComponentImages[k-1]);
m_Extrapolators[k] =
NearestNeighborExtrapolateImageFunction<ImageType, CoordRepType>::New();
m_Extrapolators[k]->SetInputImage(m_PrincipalComponentImages[k-1]);
}
}
// Evaluate the signed distance
template<typename TCoordRep, unsigned int VSpaceDimension, typename TImage>
typename PCAShapeSignedDistanceFunction<TCoordRep, VSpaceDimension,TImage>
::OutputType
PCAShapeSignedDistanceFunction<TCoordRep, VSpaceDimension,TImage>
::Evaluate( const PointType& point ) const
{
// transform the point into the shape model space
PointType mappedPoint = m_Transform->TransformPoint(point);
itkDebugMacro(<< "mappedPoint:" << mappedPoint);
if(!m_Interpolators[0]->IsInsideBuffer(mappedPoint))
{
for(unsigned int i=0; i<=m_NumberOfPrincipalComponents; i++)
{ m_Selectors[i] = m_Extrapolators[i]; }
itkDebugMacro(<< "use extrapolator");
}
else
{
for(unsigned int i=0; i<=m_NumberOfPrincipalComponents; i++)
{ m_Selectors[i] = m_Interpolators[i]; }
itkDebugMacro(<< "use interpolator");
}
typedef typename NumericTraits<OutputType>::RealType RealType;
RealType output = m_Selectors[0]->Evaluate(mappedPoint);
for(unsigned int i=0; i<m_NumberOfPrincipalComponents; i++)
{
output += m_Selectors[i+1]->Evaluate(mappedPoint) *
m_PrincipalComponentStandardDeviations[i] *
m_WeightOfPrincipalComponents[i];
}
return output;
}
} // end namespace itk
#endif