File: itkHessianRecursiveGaussianImageFilter.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 (381 lines) | stat: -rw-r--r-- 11,741 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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*=========================================================================
 *
 *  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 itkHessianRecursiveGaussianImageFilter_hxx
#define itkHessianRecursiveGaussianImageFilter_hxx

#include "itkHessianRecursiveGaussianImageFilter.h"
#include "itkImageRegionIteratorWithIndex.h"
#include "itkProgressAccumulator.h"

namespace itk
{
/**
 * Constructor
 */
template< typename TInputImage, typename TOutputImage >
HessianRecursiveGaussianImageFilter< TInputImage, TOutputImage >
::HessianRecursiveGaussianImageFilter()
{
  m_NormalizeAcrossScale = false;

  // note: this is not constant to suppress a warning
  unsigned int numberOfSmoothingFilters = NumberOfSmoothingFilters;

  for ( unsigned int i = 0; i < numberOfSmoothingFilters; i++ )
    {
    GaussianFilterPointer filter = GaussianFilterType::New();
    filter->SetOrder(GaussianFilterType::ZeroOrder);
    filter->SetNormalizeAcrossScale(m_NormalizeAcrossScale);
    filter->InPlaceOn();
    filter->ReleaseDataFlagOn();
    m_SmoothingFilters.push_back(filter);
    }

  m_DerivativeFilterA = DerivativeFilterAType::New();
  m_DerivativeFilterB = DerivativeFilterBType::New();

  m_DerivativeFilterA->SetOrder(DerivativeFilterAType::FirstOrder);
  m_DerivativeFilterA->SetNormalizeAcrossScale(m_NormalizeAcrossScale);

  m_DerivativeFilterB->SetOrder(DerivativeFilterBType::FirstOrder);
  m_DerivativeFilterB->SetNormalizeAcrossScale(m_NormalizeAcrossScale);

  m_DerivativeFilterA->SetInput( this->GetInput() );
  m_DerivativeFilterB->SetInput( m_DerivativeFilterA->GetOutput() );

  m_DerivativeFilterA->InPlaceOff();
  m_DerivativeFilterA->ReleaseDataFlagOff(); // output may be used
                                             // more then once

  m_DerivativeFilterB->InPlaceOn();
  m_DerivativeFilterB->ReleaseDataFlagOn(); // output is only used once

  // Deal with the 2D case.
  if(numberOfSmoothingFilters > 0)
    {
    m_SmoothingFilters[0]->SetInput( m_DerivativeFilterB->GetOutput() );
    }
  // connect up smoothing filter chain if necessary
  for ( unsigned int i = 1; i < numberOfSmoothingFilters; i++ )
    {
    m_SmoothingFilters[i]->SetInput(m_SmoothingFilters[i - 1]->GetOutput() );
    }

  m_ImageAdaptor = OutputImageAdaptorType::New();

  this->SetSigma(1.0);
}

/**
 * Set value of Sigma
 */
template< typename TInputImage, typename TOutputImage >
void
HessianRecursiveGaussianImageFilter< TInputImage, TOutputImage >
::SetSigma(RealType sigma)
{
  unsigned int numberOfSmoothingFilters = NumberOfSmoothingFilters;

  for ( unsigned int i = 0; i < numberOfSmoothingFilters; i++ )
    {
    m_SmoothingFilters[i]->SetSigma(sigma);
    }
  m_DerivativeFilterA->SetSigma(sigma);
  m_DerivativeFilterB->SetSigma(sigma);

  this->Modified();
}

/**
 * Get value of Sigma
 */
template< typename TInputImage, typename TOutputImage >
typename HessianRecursiveGaussianImageFilter< TInputImage, TOutputImage >
::RealType
 HessianRecursiveGaussianImageFilter< TInputImage, TOutputImage >
::GetSigma() const
{
  return m_DerivativeFilterA->GetSigma();
}

/**
 * Set Normalize Across Scale Space
 */
template< typename TInputImage, typename TOutputImage >
void
HessianRecursiveGaussianImageFilter< TInputImage, TOutputImage >
::SetNormalizeAcrossScale(bool normalize)
{
  m_NormalizeAcrossScale = normalize;

  // No normalization across scale is needed for the zero-order
  // gaussian filters. Only the derivatives need normalization.
  m_DerivativeFilterA->SetNormalizeAcrossScale(normalize);
  m_DerivativeFilterB->SetNormalizeAcrossScale(normalize);

  this->Modified();
}

//
//
//
template< typename TInputImage, typename TOutputImage >
void
HessianRecursiveGaussianImageFilter< TInputImage, TOutputImage >
::GenerateInputRequestedRegion()
{
  // call the superclass' implementation of this method. this should
  // copy the output requested region to the input requested region
  Superclass::GenerateInputRequestedRegion();

  // This filter needs all of the input
  typename HessianRecursiveGaussianImageFilter< TInputImage,
                                                TOutputImage >::InputImagePointer image =
    const_cast< InputImageType * >( this->GetInput() );
  if ( image )
    {
    image->SetRequestedRegion( this->GetInput()->GetLargestPossibleRegion() );
    }
}

//
//
//
template< typename TInputImage, typename TOutputImage >
void
HessianRecursiveGaussianImageFilter< TInputImage, TOutputImage >
::EnlargeOutputRequestedRegion(DataObject *output)
{
  TOutputImage *out = dynamic_cast< TOutputImage * >( output );

  if ( out )
    {
    out->SetRequestedRegion( out->GetLargestPossibleRegion() );
    }
}

/**
 * Compute filter for Gaussian kernel
 */
template< typename TInputImage, typename TOutputImage >
void
HessianRecursiveGaussianImageFilter< TInputImage, TOutputImage >
::GenerateData(void)
{
  itkDebugMacro(<< "HessianRecursiveGaussianImageFilter generating data ");

  // Create a process accumulator for tracking the progress of this
  // minipipeline
  ProgressAccumulator::Pointer progress = ProgressAccumulator::New();
  progress->SetMiniPipelineFilter(this);

  // Compute the contribution of each filter to the total progress.
  // This computed weight is an upper bound that assumes that all filters are
  // run for each iteration.
  // However, it is not entirely accurate because it does not take into account
  // that some filters will be reused.  In that case, they will not progress
  // again and will not contribute to the overall progress.
  // This means that the total progress may not reach 1.
  // For example, when run in 3D, m_DerivativeA will not run again when
  // dima = 0 and dimb = 2 because it did not change from when
  // dima = 0 and dimb = 1.
  const double weight =
    1.0 / ( ImageDimension * ( ImageDimension * ( ImageDimension + 1 ) / 2 ) );

  // note: this is not constant to suppress a warning
  unsigned int numberOfSmoothingFilters = NumberOfSmoothingFilters;

  for ( unsigned int i = 0; i < numberOfSmoothingFilters; i++ )
    {
    progress->RegisterInternalFilter(m_SmoothingFilters[i], weight);
    }
  progress->RegisterInternalFilter(m_DerivativeFilterA, weight);
  progress->RegisterInternalFilter(m_DerivativeFilterB, weight);

  const typename TInputImage::ConstPointer inputImage( this->GetInput() );

  m_ImageAdaptor->SetImage( this->GetOutput() );

  m_ImageAdaptor->SetLargestPossibleRegion(
    inputImage->GetLargestPossibleRegion() );

  m_ImageAdaptor->SetBufferedRegion(
    inputImage->GetBufferedRegion() );

  m_ImageAdaptor->SetRequestedRegion(
    inputImage->GetRequestedRegion() );

  m_ImageAdaptor->Allocate();

  m_DerivativeFilterA->SetInput(inputImage);
  m_DerivativeFilterB->SetInput( m_DerivativeFilterA->GetOutput() );

  unsigned int element = 0;

  for ( unsigned int dima = 0; dima < ImageDimension; dima++ )
    {
    for ( unsigned int dimb = dima; dimb < ImageDimension; dimb++ )
      {
      // Manage the diagonal in a different way in order to avoid
      // applying a double smoothing to this direction, and missing
      // to smooth one of the other directions.
      if ( dimb == dima )
        {
        m_DerivativeFilterA->SetOrder(DerivativeFilterAType::SecondOrder);
        m_DerivativeFilterB->SetOrder(DerivativeFilterBType::ZeroOrder);

        // only need the output of m_DerivativeFilterA once
        m_DerivativeFilterB->InPlaceOn();

        unsigned int i = 0;
        unsigned int j = 0;
        // find the direction for the first filter.
        while ( j < ImageDimension )
          {
          if ( j != dima )
            {
            m_DerivativeFilterB->SetDirection(j);
            j++;
            break;
            }
          j++;
          }
        // find the direction for all the other filters
        while ( i < numberOfSmoothingFilters )
          {
          while ( j < ImageDimension )
            {
            if ( j != dima )
              {
              m_SmoothingFilters[i]->SetDirection(j);
              j++;
              break;
              }
            j++;
            }
          i++;
          }

        m_DerivativeFilterA->SetDirection(dima);
        }
      else
        {
        m_DerivativeFilterA->SetOrder(DerivativeFilterAType::FirstOrder);
        m_DerivativeFilterB->SetOrder(DerivativeFilterBType::FirstOrder);

        if ( dimb < ImageDimension - 1 )
          {
          // can reuse the output of m_DerivativeFilterA
          m_DerivativeFilterB->InPlaceOff();
          }
        else
          {
          m_DerivativeFilterB->InPlaceOn();
          }

        unsigned int i = 0;
        unsigned int j = 0;
        while ( i < numberOfSmoothingFilters )
          {
          while ( j < ImageDimension )
            {
            if ( j != dima && j != dimb )
              {
              m_SmoothingFilters[i]->SetDirection(j);
              j++;
              break;
              }
            j++;
            }
          i++;
          }

        m_DerivativeFilterA->SetDirection(dima);
        m_DerivativeFilterB->SetDirection(dimb);
        }

      typename RealImageType::Pointer derivativeImage;

      // Deal with the 2D case.
      if ( numberOfSmoothingFilters > 0 )
        {
        int temp_dim = static_cast< int >( ImageDimension ) - 3;
        GaussianFilterPointer lastFilter = m_SmoothingFilters[temp_dim];
        lastFilter->UpdateLargestPossibleRegion();
        derivativeImage = lastFilter->GetOutput();
        }
      else
        {
        m_DerivativeFilterB->UpdateLargestPossibleRegion();
        derivativeImage = m_DerivativeFilterB->GetOutput();
        }

      // Copy the results to the corresponding component
      // on the output image of vectors
      m_ImageAdaptor->SelectNthElement(element++);

      ImageRegionIteratorWithIndex< RealImageType > it(
        derivativeImage,
        derivativeImage->GetRequestedRegion() );

      ImageRegionIteratorWithIndex< OutputImageAdaptorType > ot(
        m_ImageAdaptor,
        m_ImageAdaptor->GetRequestedRegion() );

      const RealType spacingA = inputImage->GetSpacing()[dima];
      const RealType spacingB = inputImage->GetSpacing()[dimb];

      const RealType factor = spacingA * spacingB;

      it.GoToBegin();
      ot.GoToBegin();
      while ( !it.IsAtEnd() )
        {
        ot.Set(it.Get() / factor);
        ++it;
        ++ot;
        }

      derivativeImage->ReleaseData();
      }
    }

  // manually release memory in last filter in the pipeline
  if (  numberOfSmoothingFilters > 0 )
    {
    m_SmoothingFilters[numberOfSmoothingFilters - 1]->GetOutput()->ReleaseData();
    }
  else
    {
    m_DerivativeFilterB->GetOutput()->ReleaseData();
    }
  m_DerivativeFilterA->GetOutput()->ReleaseData();
}

template< typename TInputImage, typename TOutputImage >
void
HessianRecursiveGaussianImageFilter< TInputImage, TOutputImage >
::PrintSelf(std::ostream & os, Indent indent) const
{
  Superclass::PrintSelf(os, indent);
  os << "NormalizeAcrossScale: " << m_NormalizeAcrossScale << std::endl;
}

} // end namespace itk

#endif