@@ -112,16 +112,21 @@ NonLocalPatchBasedImageFilter<TInputImage, TOutputImage>::VectorizeImagePatch(co
112
112
113
113
if (normalize)
114
114
{
115
+ // Convert the current image patch to a z-score.
115
116
RealType mean = 0.0 ;
116
117
RealType standardDeviation = 0.0 ;
117
118
this ->GetMeanAndStandardDeviationOfVectorizedImagePatch (patchVector, mean, standardDeviation);
118
-
119
- standardDeviation = std::max (standardDeviation, NumericTraits<RealType>::OneValue ());
120
-
121
- typename InputImagePixelVectorType::iterator it;
122
- for (it = patchVector.begin (); it != patchVector.end (); ++it)
119
+ if (standardDeviation < NumericTraits<RealType>::epsilon ())
120
+ {
121
+ for (auto & it : patchVector)
122
+ {
123
+ it = 0 .;
124
+ }
125
+ }
126
+ const auto inv_standardDeviation = 1 . / standardDeviation;
127
+ for (auto & it : patchVector)
123
128
{
124
- * it = (* it - mean) / standardDeviation ;
129
+ it = (it - mean) * inv_standardDeviation ;
125
130
}
126
131
}
127
132
return patchVector;
@@ -138,13 +143,13 @@ NonLocalPatchBasedImageFilter<TInputImage, TOutputImage>::GetMeanAndStandardDevi
138
143
RealType sumOfSquares = 0.0 ;
139
144
RealType count = 0.0 ;
140
145
141
- typename InputImagePixelVectorType::const_iterator it;
142
- for (it = patchVector.begin (); it != patchVector.end (); ++it)
146
+ for (const auto it : patchVector)
143
147
{
144
- if (std::isfinite (*it))
148
+ if (std::isfinite (it)) // Silently skip non-finite values used to indicate
149
+ // out-of-bounds.
145
150
{
146
- sum += * it;
147
- sumOfSquares += itk::Math::sqr (* it);
151
+ sum += it;
152
+ sumOfSquares += itk::Math::sqr (it);
148
153
count += itk::NumericTraits<RealType>::OneValue ();
149
154
}
150
155
}
@@ -204,13 +209,11 @@ NonLocalPatchBasedImageFilter<TInputImage, TOutputImage>::ComputeNeighborhoodPat
204
209
{
205
210
return NumericTraits<RealType>::max ();
206
211
}
207
-
208
212
if (this ->m_SimilarityMetric == SimilarityMetricEnum::PEARSON_CORRELATION)
209
213
{
210
- RealType varianceX = sumOfSquaresX - itk::Math::sqr (sumX) / N;
211
- varianceX = std::max (varianceX, static_cast <RealType>(1.0e-6 ));
212
-
213
- RealType measure = itk::Math::sqr (sumXY) / varianceX;
214
+ const RealType varianceX = sumOfSquaresX - itk::Math::sqr (sumX) / N;
215
+ const RealType measure =
216
+ (varianceX > std::numeric_limits<RealType>::epsilon ()) ? itk::Math::sqr (sumXY) / varianceX : 0 .;
214
217
if (sumXY > 0 )
215
218
{
216
219
return -measure;
0 commit comments