File: itkRecursiveSeparableImageFilter.hxx

package info (click to toggle)
insighttoolkit5 5.4.3-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 704,384 kB
  • sloc: cpp: 783,592; ansic: 628,724; xml: 44,704; fortran: 34,250; python: 22,874; sh: 4,078; pascal: 2,636; lisp: 2,158; makefile: 464; yacc: 328; asm: 205; perl: 203; lex: 146; tcl: 132; javascript: 98; csh: 81
file content (325 lines) | stat: -rw-r--r-- 10,767 bytes parent folder | download
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
/*=========================================================================
 *
 *  Copyright NumFOCUS
 *
 *  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
 *
 *         https://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 itkRecursiveSeparableImageFilter_hxx
#define itkRecursiveSeparableImageFilter_hxx

#include "itkObjectFactory.h"
#include "itkImageLinearIteratorWithIndex.h"
#include "itkMakeUniqueForOverwrite.h"

namespace itk
{
template <typename TInputImage, typename TOutputImage>
RecursiveSeparableImageFilter<TInputImage, TOutputImage>::RecursiveSeparableImageFilter()
  : m_N0(1.0)
  , m_N1(1.0)
  , m_N2(1.0)
  , m_N3(1.0)
  , m_D1(0.0)
  , m_D2(0.0)
  , m_D3(0.0)
  , m_D4(0.0)
  , m_M1(0.0)
  , m_M2(0.0)
  , m_M3(0.0)
  , m_M4(0.0)
  , m_BN1(0.0)
  , m_BN2(0.0)
  , m_BN3(0.0)
  , m_BN4(0.0)
  , m_BM1(0.0)
  , m_BM2(0.0)
  , m_BM3(0.0)
  , m_BM4(0.0)

{
  this->SetNumberOfRequiredOutputs(1);
  this->SetNumberOfRequiredInputs(1);

  this->InPlaceOff();
}

/**
 * Set Input Image
 */
template <typename TInputImage, typename TOutputImage>
void
RecursiveSeparableImageFilter<TInputImage, TOutputImage>::SetInputImage(const TInputImage * input)
{
  // ProcessObject is not const_correct so this const_cast is required
  ProcessObject::SetNthInput(0, const_cast<TInputImage *>(input));
}

/**
 * Get Input Image
 */
template <typename TInputImage, typename TOutputImage>
const TInputImage *
RecursiveSeparableImageFilter<TInputImage, TOutputImage>::GetInputImage()
{
  return dynamic_cast<const TInputImage *>((ProcessObject::GetInput(0)));
}

/**
 * Apply Recursive Filter
 */
template <typename TInputImage, typename TOutputImage>
void
RecursiveSeparableImageFilter<TInputImage, TOutputImage>::FilterDataArray(RealType * const       outs,
                                                                          const RealType * const data,
                                                                          RealType * const       scratch,
                                                                          const SizeValueType    ln) const
{

  RealType * const scratch1 = outs;
  RealType * const scratch2 = scratch;
  /**
   * Causal direction pass
   */

  // this value is assumed to exist from the border to infinity.
  const RealType & outV1 = data[0];

  /**
   * Initialize borders
   */

  MathEMAMAMAM(scratch1[0], outV1, m_N0, outV1, m_N1, outV1, m_N2, outV1, m_N3);
  MathEMAMAMAM(scratch1[1], data[1], m_N0, outV1, m_N1, outV1, m_N2, outV1, m_N3);
  MathEMAMAMAM(scratch1[2], data[2], m_N0, data[1], m_N1, outV1, m_N2, outV1, m_N3);
  MathEMAMAMAM(scratch1[3], data[3], m_N0, data[2], m_N1, data[1], m_N2, outV1, m_N3);

  // note that the outV1 value is multiplied by the Boundary coefficients m_BNi
  MathSMAMAMAM(scratch1[0], outV1, m_BN1, outV1, m_BN2, outV1, m_BN3, outV1, m_BN4);
  MathSMAMAMAM(scratch1[1], scratch1[0], m_D1, outV1, m_BN2, outV1, m_BN3, outV1, m_BN4);
  MathSMAMAMAM(scratch1[2], scratch1[1], m_D1, scratch1[0], m_D2, outV1, m_BN3, outV1, m_BN4);
  MathSMAMAMAM(scratch1[3], scratch1[2], m_D1, scratch1[1], m_D2, scratch1[0], m_D3, outV1, m_BN4);

  /**
   * Recursively filter the rest
   */
  for (unsigned int i = 4; i < ln; ++i)
  {
    MathEMAMAMAM(scratch1[i], data[i], m_N0, data[i - 1], m_N1, data[i - 2], m_N2, data[i - 3], m_N3);
    MathSMAMAMAM(
      scratch1[i], scratch1[i - 1], m_D1, scratch1[i - 2], m_D2, scratch1[i - 3], m_D3, scratch1[i - 4], m_D4);
  }

  /**
   * Store the causal result: outs = scratch already done via alias
   *
   */

  /**
   * AntiCausal direction pass
   */

  // this value is assumed to exist from the border to infinity.
  const RealType & outV2 = data[ln - 1];

  /**
   * Initialize borders
   */
  MathEMAMAMAM(scratch2[ln - 1], outV2, m_M1, outV2, m_M2, outV2, m_M3, outV2, m_M4);
  MathEMAMAMAM(scratch2[ln - 2], data[ln - 1], m_M1, outV2, m_M2, outV2, m_M3, outV2, m_M4);
  MathEMAMAMAM(scratch2[ln - 3], data[ln - 2], m_M1, data[ln - 1], m_M2, outV2, m_M3, outV2, m_M4);
  MathEMAMAMAM(scratch2[ln - 4], data[ln - 3], m_M1, data[ln - 2], m_M2, data[ln - 1], m_M3, outV2, m_M4);

  // note that the outV2value is multiplied by the Boundary coefficients m_BMi
  MathSMAMAMAM(scratch2[ln - 1], outV2, m_BM1, outV2, m_BM2, outV2, m_BM3, outV2, m_BM4);
  MathSMAMAMAM(scratch2[ln - 2], scratch2[ln - 1], m_D1, outV2, m_BM2, outV2, m_BM3, outV2, m_BM4);
  MathSMAMAMAM(scratch2[ln - 3], scratch2[ln - 2], m_D1, scratch2[ln - 1], m_D2, outV2, m_BM3, outV2, m_BM4);
  MathSMAMAMAM(scratch2[ln - 4], scratch2[ln - 3], m_D1, scratch2[ln - 2], m_D2, scratch2[ln - 1], m_D3, outV2, m_BM4);

  /**
   * Recursively filter the rest
   */
  for (unsigned int i = ln - 4; i > 0; i--)
  {
    MathEMAMAMAM(scratch2[i - 1], data[i], m_M1, data[i + 1], m_M2, data[i + 2], m_M3, data[i + 3], m_M4);
    MathSMAMAMAM(
      scratch2[i - 1], scratch2[i], m_D1, scratch2[i + 1], m_D2, scratch2[i + 2], m_D3, scratch2[i + 3], m_D4);
  }

  /**
   * Roll the antiCausal part into the output
   */
  for (unsigned int i = 0; i < ln; ++i)
  {
    outs[i] += scratch2[i];
  }
}

//
// we need all of the image in just the "Direction" we are separated into
//
template <typename TInputImage, typename TOutputImage>
void
RecursiveSeparableImageFilter<TInputImage, TOutputImage>::EnlargeOutputRequestedRegion(DataObject * output)
{
  auto * out = dynamic_cast<TOutputImage *>(output);

  if (out)
  {
    OutputImageRegionType         outputRegion = out->GetRequestedRegion();
    const OutputImageRegionType & largestOutputRegion = out->GetLargestPossibleRegion();

    // verify sane parameter
    if (this->m_Direction >= outputRegion.GetImageDimension())
    {
      itkExceptionMacro("Direction selected for filtering is greater than ImageDimension");
    }

    // expand output region to match largest in the "Direction" dimension
    outputRegion.SetIndex(m_Direction, largestOutputRegion.GetIndex(m_Direction));
    outputRegion.SetSize(m_Direction, largestOutputRegion.GetSize(m_Direction));

    out->SetRequestedRegion(outputRegion);
  }
}


template <typename TInputImage, typename TOutputImage>
void
RecursiveSeparableImageFilter<TInputImage, TOutputImage>::BeforeThreadedGenerateData()
{
  using RegionType = ImageRegion<TInputImage::ImageDimension>;

  typename TInputImage::ConstPointer inputImage(this->GetInputImage());
  typename TOutputImage::Pointer     outputImage(this->GetOutput());

  const unsigned int imageDimension = inputImage->GetImageDimension();

  if (this->m_Direction >= imageDimension)
  {
    itkExceptionMacro("Direction selected for filtering is greater than ImageDimension");
  }

  const typename InputImageType::SpacingType & pixelSize = inputImage->GetSpacing();

  this->SetUp(pixelSize[m_Direction]);

  const RegionType region = outputImage->GetRequestedRegion();

  const unsigned int ln = region.GetSize()[this->m_Direction];

  if (ln < 4)
  {
    itkExceptionMacro(
      "The number of pixels along direction "
      << this->m_Direction
      << " is less than 4. This filter requires a minimum of four pixels along the dimension to be processed.");
  }
}

template <typename TInputImage, typename TOutputImage>
void
RecursiveSeparableImageFilter<TInputImage, TOutputImage>::GenerateData()
{
  // Call a method that can be overridden by a subclass to allocate
  // memory for the filter's outputs
  this->AllocateOutputs();

  // Call a method that can be overridden by a subclass to perform
  // some calculations prior to splitting the main computations into
  // separate threads
  this->BeforeThreadedGenerateData();

  using RegionType = ImageRegion<TInputImage::ImageDimension>;
  typename TOutputImage::Pointer outputImage(this->GetOutput());
  const RegionType               region = outputImage->GetRequestedRegion();

  this->GetMultiThreader()->SetNumberOfWorkUnits(this->GetNumberOfWorkUnits());
  this->GetMultiThreader()->template ParallelizeImageRegionRestrictDirection<TOutputImage::ImageDimension>(
    this->m_Direction,
    region,
    [this](const RegionType & lambdaRegion) { this->DynamicThreadedGenerateData(lambdaRegion); },
    this);
}

/**
 * Compute Recursive filter
 * line by line in one of the dimensions
 */
template <typename TInputImage, typename TOutputImage>
void
RecursiveSeparableImageFilter<TInputImage, TOutputImage>::DynamicThreadedGenerateData(
  const OutputImageRegionType & outputRegionForThread)
{
  using OutputPixelType = typename TOutputImage::PixelType;

  using InputConstIteratorType = ImageLinearConstIteratorWithIndex<TInputImage>;
  using OutputIteratorType = ImageLinearIteratorWithIndex<TOutputImage>;

  using RegionType = ImageRegion<TInputImage::ImageDimension>;

  typename TInputImage::ConstPointer inputImage(this->GetInputImage());
  typename TOutputImage::Pointer     outputImage(this->GetOutput());

  RegionType region = outputRegionForThread;

  InputConstIteratorType inputIterator(inputImage, region);
  OutputIteratorType     outputIterator(outputImage, region);

  inputIterator.SetDirection(this->m_Direction);
  outputIterator.SetDirection(this->m_Direction);

  const SizeValueType ln = region.GetSize(this->m_Direction);

  const auto inps = make_unique_for_overwrite<RealType[]>(ln);
  const auto outs = make_unique_for_overwrite<RealType[]>(ln);
  const auto scratch = make_unique_for_overwrite<RealType[]>(ln);

  inputIterator.GoToBegin();
  outputIterator.GoToBegin();

  while (!inputIterator.IsAtEnd() && !outputIterator.IsAtEnd())
  {
    unsigned int i = 0;
    while (!inputIterator.IsAtEndOfLine())
    {
      inps[i++] = inputIterator.Get();
      ++inputIterator;
    }

    this->FilterDataArray(outs.get(), inps.get(), scratch.get(), ln);

    unsigned int j = 0;
    while (!outputIterator.IsAtEndOfLine())
    {
      outputIterator.Set(static_cast<OutputPixelType>(outs[j++]));
      ++outputIterator;
    }

    inputIterator.NextLine();
    outputIterator.NextLine();
  }
}

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

  os << indent << "Direction: " << m_Direction << std::endl;
}

} // end namespace itk

#endif