-
Notifications
You must be signed in to change notification settings - Fork 1
/
itkLevelSetPropagationTerm.h
165 lines (127 loc) · 4.87 KB
/
itkLevelSetPropagationTerm.h
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
#ifndef __itkLevelSetPropagationTerm_h_
#define __itkLevelSetPropagationTerm_h_
#include "itkLevelSetTerm.h"
#include "itkLevelSetDerivativesTerm.h"
#include "itkLinearInterpolateImageFunction.h"
namespace itk {
template <class TImage, class TSpeedImage>
class ITK_EXPORT LevelSetPropagationTerm
: public LevelSetTerm<TImage>
{
public:
typedef LevelSetPropagationTerm Self;
typedef LevelSetTerm<TImage> Superclass;
typedef SmartPointer<Self> Pointer;
typedef SmartPointer<const Self> ConstPointer;
itkNewMacro(Self);
itkTypeMacro(LevelSetPropagationTerm, LevelSetTerm);
itkStaticConstMacro(ImageDimension, unsigned int, Superclass::ImageDimension);
typedef double TimeStepType;
typedef typename Superclass::ImageType ImageType;
typedef typename Superclass::PixelType PixelType;
typedef PixelType ScalarValueType;
typedef typename Superclass::PixelRealType PixelRealType;
typedef typename Superclass::RadiusType RadiusType;
typedef typename Superclass::NeighborhoodType NeighborhoodType;
typedef typename Superclass::NeighborhoodScalesType NeighborhoodScalesType;
typedef typename Superclass::FloatOffsetType FloatOffsetType;
typedef typename ImageType::IndexType IndexType;
typedef TSpeedImage SpeedImageType;
typedef typename SpeedImageType::PixelType SpeedScalarType;
typedef LinearInterpolateImageFunction<SpeedImageType> InterpolatorType;
typedef typename InterpolatorType::ContinuousIndexType ContinuousIndexType;
typedef typename LevelSetDerivativesTerm<ImageType>::GlobalDataType DerivativesGlobalDataType;
struct GlobalDataStruct : public Superclass::GlobalDataStruct
{
GlobalDataStruct()
{
m_Speed = NumericTraits<ScalarValueType>::Zero;
m_GradientMagnitude = NumericTraits<ScalarValueType>::Zero;
}
ScalarValueType m_Speed;
ScalarValueType m_GradientMagnitude;
};
itkRegisterGlobalDataMacro;
virtual const SpeedImageType *GetSpeedImage() const
{ return m_SpeedImage.GetPointer(); }
virtual void SetSpeedImage(const SpeedImageType *f)
{ m_SpeedImage = f; }
virtual void Initialize()
{
Superclass::Initialize();
m_Interpolator->SetInputImage(m_SpeedImage);
}
virtual PixelType ComputeUpdate(const NeighborhoodType &neighborhood,
void *globalData,
const FloatOffsetType& offset)
{
const ScalarValueType ZERO = NumericTraits<ScalarValueType>::Zero;
if (this->m_Weight == ZERO)
{
return (PixelType)ZERO;
}
GlobalDataType *gd = (GlobalDataType *)globalData;
DerivativesGlobalDataType *dgd = (DerivativesGlobalDataType *)gd->m_DependencyGlobalDataMap["LevelSetDerivativesTerm"];
if (dgd == NULL)
{
itkExceptionMacro("LevelSetDerivativesTerm is a dependency but its global data is not available.");
}
ScalarValueType speed = ZERO;
IndexType idx = neighborhood.GetIndex();
ContinuousIndexType cdx;
for (unsigned i = 0; i < ImageDimension; ++i)
{
cdx[i] = static_cast<double>(idx[i]) - offset[i];
}
if (m_Interpolator->IsInsideBuffer(cdx))
{
speed = static_cast<ScalarValueType>(m_Interpolator->EvaluateAtContinuousIndex(cdx));
}
else
{
speed = static_cast<ScalarValueType>(m_SpeedImage->GetPixel(idx));
}
gd->m_Speed = speed;
gd->m_MaxChange = vnl_math_max(gd->m_MaxChange,vnl_math_abs(speed));
ScalarValueType gradient_magnitude = ZERO;
if (speed > ZERO)
{
for (unsigned int i = 0; i< ImageDimension; i++)
{
gradient_magnitude += vnl_math_sqr( vnl_math_max(dgd->m_dx_backward[i], ZERO) )
+ vnl_math_sqr( vnl_math_min(dgd->m_dx_forward[i], ZERO) );
}
}
else
{
for (unsigned int i = 0; i< ImageDimension; i++)
{
gradient_magnitude += vnl_math_sqr( vnl_math_min(dgd->m_dx_backward[i], ZERO) )
+ vnl_math_sqr( vnl_math_max(dgd->m_dx_forward[i], ZERO) );
}
}
gradient_magnitude = vcl_sqrt(gradient_magnitude);
gd->m_GradientMagnitude = gradient_magnitude;
ScalarValueType propagation_term = - this->m_Weight * speed * gradient_magnitude;
return (PixelType)propagation_term;
}
virtual TimeStepType ComputeGlobalTimeStep(void *GlobalData) const
{
return NumericTraits<TimeStepType>::max();
}
protected:
LevelSetPropagationTerm()
{
this->AddDependency(LevelSetDerivativesTerm<ImageType>::New());
m_Interpolator = InterpolatorType::New();
}
virtual ~LevelSetPropagationTerm() {}
void PrintSelf(std::ostream &s, Indent indent) const {};
typename SpeedImageType::ConstPointer m_SpeedImage;
typename InterpolatorType::Pointer m_Interpolator;
private:
LevelSetPropagationTerm(const Self&); //purposely not implemented
void operator=(const Self&); //purposely not implemented
};
}
#endif