File: itkRecursiveGaussianImageFilter.hxx

package info (click to toggle)
insighttoolkit4 4.13.3withdata-dfsg2-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 491,256 kB
  • sloc: cpp: 557,600; ansic: 180,546; fortran: 34,788; python: 16,572; sh: 2,187; lisp: 2,070; tcl: 993; java: 362; perl: 200; makefile: 133; csh: 81; pascal: 69; xml: 19; ruby: 10
file content (361 lines) | stat: -rw-r--r-- 11,980 bytes parent folder | download | duplicates (5)
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*=========================================================================
 *
 *  Copyright Insight Software Consortium
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0.txt
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *=========================================================================*/
#ifndef itkRecursiveGaussianImageFilter_hxx
#define itkRecursiveGaussianImageFilter_hxx

#include "itkRecursiveGaussianImageFilter.h"
#include "itkObjectFactory.h"
#include "itkImageLinearIteratorWithIndex.h"
#include <new>

namespace itk
{
template< typename TInputImage, typename TOutputImage >
RecursiveGaussianImageFilter< TInputImage, TOutputImage >
::RecursiveGaussianImageFilter()
{
  m_Sigma = 1.0;
  m_NormalizeAcrossScale = false;
  m_Order = ZeroOrder;
}

/**
 *   Explicitly set a zeroth order derivative.
 */
template< typename TInputImage, typename TOutputImage >
void
RecursiveGaussianImageFilter< TInputImage, TOutputImage >
::SetZeroOrder()
{
  this->SetOrder(ZeroOrder);
}

/**
 *   Explicitly set a first order derivative.
 */
template< typename TInputImage, typename TOutputImage >
void
RecursiveGaussianImageFilter< TInputImage, TOutputImage >
::SetFirstOrder()
{
  this->SetOrder(FirstOrder);
}

/**
 *   Explicitly set a second order derivative.
 */
template< typename TInputImage, typename TOutputImage >
void
RecursiveGaussianImageFilter< TInputImage, TOutputImage >
::SetSecondOrder()
{
  this->SetOrder(SecondOrder);
}

/**
 *   Compute filter for Gaussian kernel.
 */
template< typename TInputImage, typename TOutputImage >
void
RecursiveGaussianImageFilter< TInputImage, TOutputImage >
::SetUp(ScalarRealType spacing)
{
  const ScalarRealType spacingTolerance = 1e-8;

  /**  Parameters of exponential series. */
  ScalarRealType A1[3];
  ScalarRealType B1[3];
  ScalarRealType W1;
  ScalarRealType L1;
  ScalarRealType A2[3];
  ScalarRealType B2[3];
  ScalarRealType W2;
  ScalarRealType L2;

  ScalarRealType direction = 1.0;

  if ( spacing < 0.0 )
    {
    direction = -1.0;
    spacing = -spacing;
    }

  if ( spacing < spacingTolerance )
    {
    itkExceptionMacro(<< "The spacing " << spacing << "is suspiciosly small in this image");
    }

  const ScalarRealType sigmad = m_Sigma / spacing;
  ScalarRealType       across_scale_normalization = 1.0;


  A1[0] = static_cast< ScalarRealType >(  1.3530 );
  B1[0] = static_cast< ScalarRealType >(  1.8151 );
  W1    = static_cast< ScalarRealType >(  0.6681 );
  L1    = static_cast< ScalarRealType >( -1.3932 );
  A2[0] = static_cast< ScalarRealType >( -0.3531 );
  B2[0] = static_cast< ScalarRealType >(  0.0902 );
  W2    = static_cast< ScalarRealType >(  2.0787 );
  L2    = static_cast< ScalarRealType >( -1.3732 );

  A1[1] = static_cast< ScalarRealType >( -0.6724 );
  B1[1] = static_cast< ScalarRealType >( -3.4327 );
  A2[1] = static_cast< ScalarRealType >(  0.6724 );
  B2[1] = static_cast< ScalarRealType >(  0.6100 );

  A1[2] = static_cast< ScalarRealType >( -1.3563 );
  B1[2] = static_cast< ScalarRealType >(  5.2318 );
  A2[2] = static_cast< ScalarRealType >(  0.3446 );
  B2[2] = static_cast< ScalarRealType >( -2.2355 );

  ScalarRealType SD, DD, ED;
  this->ComputeDCoefficients(sigmad, W1, L1, W2, L2, SD, DD, ED);
  ScalarRealType SN, DN, EN;

  switch ( m_Order )
    {
    case ZeroOrder:
      {
      // Approximation of convolution with a gaussian.
      ComputeNCoefficients(sigmad,
                           A1[0], B1[0], W1, L1,
                           A2[0], B2[0], W2, L2,
                           this->m_N0,
                           this->m_N1,
                           this->m_N2,
                           this->m_N3,
                           SN, DN, EN);

      ScalarRealType alpha0 = 2 * SN / SD - this->m_N0;
      this->m_N0 *= across_scale_normalization / alpha0;
      this->m_N1 *= across_scale_normalization / alpha0;
      this->m_N2 *= across_scale_normalization / alpha0;
      this->m_N3 *= across_scale_normalization / alpha0;
      const bool symmetric = true;
      this->ComputeRemainingCoefficients(symmetric);
      break;
      }
    case FirstOrder:
      {
      if ( this->GetNormalizeAcrossScale() )
        {
        across_scale_normalization =  m_Sigma;
        }
      // Approximation of convolution with the first derivative of a  Gaussian
      ComputeNCoefficients(sigmad,
                           A1[1], B1[1], W1, L1,
                           A2[1], B2[1], W2, L2,
                           this->m_N0, this->m_N1, this->m_N2, this->m_N3,
                           SN, DN, EN);

      ScalarRealType alpha1 = 2 * ( SN * DD - DN * SD ) / ( SD * SD );
      // If negative spacing, negate the first derivative response.
      alpha1 *= direction;

      this->m_N0 *= across_scale_normalization / alpha1;
      this->m_N1 *= across_scale_normalization / alpha1;
      this->m_N2 *= across_scale_normalization / alpha1;
      this->m_N3 *= across_scale_normalization / alpha1;

      const bool symmetric = false;
      this->ComputeRemainingCoefficients(symmetric);
      break;
      }
    case SecondOrder:
      {
       if ( this->GetNormalizeAcrossScale() )
         {
         across_scale_normalization = itk::Math::sqr(  m_Sigma );
         }
      // Approximation of convolution with the second derivative of a
      // Gaussian.
      ScalarRealType N0_0, N1_0, N2_0, N3_0;
      ScalarRealType N0_2, N1_2, N2_2, N3_2;
      ScalarRealType SN0, DN0, EN0;
      ScalarRealType SN2, DN2, EN2;
      ComputeNCoefficients(sigmad,
                           A1[0], B1[0], W1, L1,
                           A2[0], B2[0], W2, L2,
                           N0_0, N1_0, N2_0, N3_0,
                           SN0, DN0, EN0);
      ComputeNCoefficients(sigmad,
                           A1[2], B1[2], W1, L1,
                           A2[2], B2[2], W2, L2,
                           N0_2, N1_2, N2_2, N3_2,
                           SN2, DN2, EN2);

      ScalarRealType beta = -( 2 * SN2 - SD * N0_2 ) / ( 2 * SN0 - SD * N0_0 );
      this->m_N0 = N0_2 + beta * N0_0;
      this->m_N1 = N1_2 + beta * N1_0;
      this->m_N2 = N2_2 + beta * N2_0;
      this->m_N3 = N3_2 + beta * N3_0;
      SN = SN2 + beta * SN0;
      DN = DN2 + beta * DN0;
      EN = EN2 + beta * EN0;

      ScalarRealType alpha2;
      alpha2  = EN * SD * SD - ED * SN * SD - 2 * DN * DD * SD + 2 * DD * DD * SN;
      alpha2 /= SD * SD * SD;

      this->m_N0 *= across_scale_normalization / alpha2;
      this->m_N1 *= across_scale_normalization / alpha2;
      this->m_N2 *= across_scale_normalization / alpha2;
      this->m_N3 *= across_scale_normalization / alpha2;

      const bool symmetric = true;
      this->ComputeRemainingCoefficients(symmetric);
      break;
      }
    default:
      {
      itkExceptionMacro(<< "Unknown Order");
      return;
      }
    }
}

/**
 * Compute the N coefficients.
 */
template< typename TInputImage, typename TOutputImage >
void
RecursiveGaussianImageFilter< TInputImage, TOutputImage >
::ComputeNCoefficients(ScalarRealType sigmad,
                       ScalarRealType A1, ScalarRealType B1, ScalarRealType W1, ScalarRealType L1,
                       ScalarRealType A2, ScalarRealType B2, ScalarRealType W2, ScalarRealType L2,
                       ScalarRealType & N0, ScalarRealType & N1, ScalarRealType & N2, ScalarRealType & N3,
                       ScalarRealType & SN, ScalarRealType & DN, ScalarRealType & EN)
{
  ScalarRealType Sin1 = std::sin(W1 / sigmad);
  ScalarRealType Sin2 = std::sin(W2 / sigmad);
  ScalarRealType Cos1 = std::cos(W1 / sigmad);
  ScalarRealType Cos2 = std::cos(W2 / sigmad);
  ScalarRealType Exp1 = std::exp(L1 / sigmad);
  ScalarRealType Exp2 = std::exp(L2 / sigmad);

  N0  = A1 + A2;
  N1  = Exp2 * ( B2 * Sin2 - ( A2 + 2 * A1 ) * Cos2 );
  N1 += Exp1 * ( B1 * Sin1 - ( A1 + 2 * A2 ) * Cos1 );
  N2  = ( A1 + A2 ) * Cos2 * Cos1;
  N2 -= B1 * Cos2 * Sin1 + B2 * Cos1 * Sin2;
  N2 *= 2 * Exp1 * Exp2;
  N2 += A2 * Exp1 * Exp1 + A1 * Exp2 * Exp2;
  N3  = Exp2 * Exp1 * Exp1 * ( B2 * Sin2 - A2 * Cos2 );
  N3 += Exp1 * Exp2 * Exp2 * ( B1 * Sin1 - A1 * Cos1 );

  SN = N0 + N1 + N2 + N3;
  DN = N1 + 2 * N2 + 3 * N3;
  EN = N1 + 4 * N2 + 9 * N3;
}

/**
 * Compute the D coefficients.
 */
template< typename TInputImage, typename TOutputImage >
void
RecursiveGaussianImageFilter< TInputImage, TOutputImage >
::ComputeDCoefficients(ScalarRealType sigmad,
                       ScalarRealType W1, ScalarRealType L1, ScalarRealType W2, ScalarRealType L2,
                       ScalarRealType & SD, ScalarRealType & DD, ScalarRealType & ED)
{
  //  const ScalarRealType Sin1 = std::sin(W1 / sigmad);
  //  const ScalarRealType Sin2 = std::sin(W2 / sigmad);
  const ScalarRealType Cos1 = std::cos(W1 / sigmad);
  const ScalarRealType Cos2 = std::cos(W2 / sigmad);
  const ScalarRealType Exp1 = std::exp(L1 / sigmad);
  const ScalarRealType Exp2 = std::exp(L2 / sigmad);

  this->m_D4 = Exp1 * Exp1 * Exp2 * Exp2;
  this->m_D3 = -2 * Cos1 * Exp1 * Exp2 * Exp2;
  this->m_D3 += -2 * Cos2 * Exp2 * Exp1 * Exp1;
  this->m_D2 = 4 * Cos2 * Cos1 * Exp1 * Exp2;
  this->m_D2 += Exp1 * Exp1 + Exp2 * Exp2;
  this->m_D1 = -2 * ( Exp2 * Cos2 + Exp1 * Cos1 );

  SD = 1.0 + this->m_D1 + this->m_D2 + this->m_D3 + this->m_D4;
  DD = this->m_D1 + 2 * this->m_D2 + 3 * this->m_D3 + 4 * this->m_D4;
  ED = this->m_D1 + 4 * this->m_D2 + 9 * this->m_D3 + 16 * this->m_D4;
}

/**
 * Compute the M coefficients and the boundary coefficients.
 */
template< typename TInputImage, typename TOutputImage >
void
RecursiveGaussianImageFilter< TInputImage, TOutputImage >
::ComputeRemainingCoefficients(bool symmetric)
{
  if ( symmetric )
    {
    this->m_M1 = this->m_N1 - this->m_D1 * this->m_N0;
    this->m_M2 = this->m_N2 - this->m_D2 * this->m_N0;
    this->m_M3 = this->m_N3 - this->m_D3 * this->m_N0;
    this->m_M4 =            -this->m_D4 * this->m_N0;
    }
  else
    {
    this->m_M1 = -( this->m_N1 - this->m_D1 * this->m_N0 );
    this->m_M2 = -( this->m_N2 - this->m_D2 * this->m_N0 );
    this->m_M3 = -( this->m_N3 - this->m_D3 * this->m_N0 );
    this->m_M4 =                 this->m_D4 * this->m_N0;
    }

  // Compute coefficients to be used at the boundaries
  // in order to simulate edge extension boundary conditions.
  const ScalarRealType SN = this->m_N0 + this->m_N1 + this->m_N2 + this->m_N3;
  const ScalarRealType SM = this->m_M1 + this->m_M2 + this->m_M3 + this->m_M4;
  const ScalarRealType SD = 1.0 + this->m_D1 + this->m_D2 + this->m_D3 + this->m_D4;

  this->m_BN1 = this->m_D1 * SN / SD;
  this->m_BN2 = this->m_D2 * SN / SD;
  this->m_BN3 = this->m_D3 * SN / SD;
  this->m_BN4 = this->m_D4 * SN / SD;

  this->m_BM1 = this->m_D1 * SM / SD;
  this->m_BM2 = this->m_D2 * SM / SD;
  this->m_BM3 = this->m_D3 * SM / SD;
  this->m_BM4 = this->m_D4 * SM / SD;
}

template< typename TInputImage, typename TOutputImage >
void
RecursiveGaussianImageFilter< TInputImage, TOutputImage >
::VerifyPreconditions()
{
  this->Superclass::VerifyPreconditions();

  if( this->m_Sigma <= 0.0 )
    {
    itkExceptionMacro( "Sigma must be greater than zero." );
    }
}

template< typename TInputImage, typename TOutputImage >
void
RecursiveGaussianImageFilter< TInputImage, TOutputImage >
::PrintSelf(std::ostream & os, Indent indent) const
{
  Superclass::PrintSelf(os, indent);

  os << "Sigma: " << m_Sigma << std::endl;
  os << "Order: " << m_Order << std::endl;
  os << "NormalizeAcrossScale: " << m_NormalizeAcrossScale << std::endl;
}
} // end namespace itk

#endif