File: itkMaximumGradientMagnitudeImageFilter.hxx

package info (click to toggle)
itksnap 3.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 10,196 kB
  • ctags: 9,196
  • sloc: cpp: 62,895; sh: 175; makefile: 13
file content (266 lines) | stat: -rw-r--r-- 7,126 bytes parent folder | download | duplicates (2)
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
#ifndef ITKMAXIMUMGRADIENTMAGNITUDEIMAGEFILTER_HXX
#define ITKMAXIMUMGRADIENTMAGNITUDEIMAGEFILTER_HXX

#include "itkMaximumGradientMagnitudeImageFilter.h"

#include "itkConstNeighborhoodIterator.h"
#include "itkNeighborhoodInnerProduct.h"
#include "itkImageRegionIterator.h"
#include "itkDerivativeOperator.h"
#include "itkNeighborhoodAlgorithm.h"
#include "itkProgressReporter.h"


#include <vector>

namespace itk
{
template< typename TInputImage >
MaximumGradientMagnitudeImageFilter< TInputImage >
::MaximumGradientMagnitudeImageFilter()
{
  this->SetNumberOfRequiredOutputs(2);
  // first output is a copy of the image, DataObject created by
  // superclass
  //
  // allocate the data objects for the remaining outputs which are
  // just decorators around floating point types

  typename DoubleObjectType::Pointer output =
      static_cast<DoubleObjectType *>(this->MakeOutput(1).GetPointer());
  this->ProcessObject::SetNthOutput(1, output.GetPointer());

  this->GetMaximumOutput()->Set(0.0);

  m_UseImageSpacing = true;
}

template< typename TInputImage >
DataObject::Pointer
MaximumGradientMagnitudeImageFilter< TInputImage >
::MakeOutput(DataObjectPointerArraySizeType output)
{
  switch ( output )
    {
    case 0:
      return TInputImage::New().GetPointer();
      break;
    case 1:
      return DoubleObjectType::New().GetPointer();
      break;
    default:
      // might as well make an image
      return TInputImage::New().GetPointer();
      break;
    }
}


template< typename TInputImage >
typename MaximumGradientMagnitudeImageFilter< TInputImage >::DoubleObjectType *
MaximumGradientMagnitudeImageFilter< TInputImage >
::GetMaximumOutput()
{
  return static_cast< DoubleObjectType * >( this->ProcessObject::GetOutput(1) );
}

template< typename TInputImage >
const typename MaximumGradientMagnitudeImageFilter< TInputImage >::DoubleObjectType *
MaximumGradientMagnitudeImageFilter< TInputImage >
::GetMaximumOutput() const
{
  return static_cast< const DoubleObjectType * >( this->ProcessObject::GetOutput(1) );
}

template< typename TInputImage >
void
MaximumGradientMagnitudeImageFilter< TInputImage >
::GenerateInputRequestedRegion()
{
  Superclass::GenerateInputRequestedRegion();
  if ( this->GetInput() )
    {
    InputImagePointer image =
      const_cast< typename Superclass::InputImageType * >( this->GetInput() );
    image->SetRequestedRegionToLargestPossibleRegion();
    }
}

template< typename TInputImage >
void
MaximumGradientMagnitudeImageFilter< TInputImage >
::EnlargeOutputRequestedRegion(DataObject *data)
{
  Superclass::EnlargeOutputRequestedRegion(data);
  data->SetRequestedRegionToLargestPossibleRegion();
}

template< typename TInputImage >
void
MaximumGradientMagnitudeImageFilter< TInputImage >
::AllocateOutputs()
{
  // Pass the input through as the output
  InputImagePointer image =
    const_cast< TInputImage * >( this->GetInput() );

  this->GraftOutput(image);

  // Nothing that needs to be allocated for the remaining outputs
}

template< typename TInputImage >
void
MaximumGradientMagnitudeImageFilter< TInputImage >
::BeforeThreadedGenerateData()
{
  ThreadIdType numberOfThreads = this->GetNumberOfThreads();

  // Create the thread temporaries
  m_ThreadMax = std::vector< double >(numberOfThreads, 0.0);
}

template< typename TInputImage >
void
MaximumGradientMagnitudeImageFilter< TInputImage >
::AfterThreadedGenerateData()
{
  ThreadIdType i;
  ThreadIdType numberOfThreads = this->GetNumberOfThreads();


  // Find the min/max over all threads
  double maximum = 0.0;
  for ( i = 0; i < numberOfThreads; i++ )
    {
    if ( m_ThreadMax[i] > maximum )
      {
      maximum = m_ThreadMax[i];
      }
    }

  // Set the outputs
  this->GetMaximumOutput()->Set(maximum);
}

template< typename TInputImage >
void
MaximumGradientMagnitudeImageFilter< TInputImage >
::ThreadedGenerateData(const RegionType & outputRegionForThread,
                       ThreadIdType threadId)
{
  if ( outputRegionForThread.GetNumberOfPixels() == 0 )
    return;

  typedef float RealType;

  // Local maximum
  double localMax = 0.0;

  unsigned int i;

  ZeroFluxNeumannBoundaryCondition< TInputImage > nbc;

  ConstNeighborhoodIterator< TInputImage > nit;
  ConstNeighborhoodIterator< TInputImage > bit;

  NeighborhoodInnerProduct< TInputImage, RealType > SIP;

  // Allocate output
  typename  InputImageType::ConstPointer input  = this->GetInput();

  // Set up operators
  DerivativeOperator< RealType, ImageDimension > op[ImageDimension];

  for ( i = 0; i < ImageDimension; i++ )
    {
    op[i].SetDirection(0);
    op[i].SetOrder(1);
    op[i].CreateDirectional();

    if ( m_UseImageSpacing == true )
      {
      if ( this->GetInput()->GetSpacing()[i] == 0.0 )
        {
        itkExceptionMacro(<< "Image spacing cannot be zero.");
        }
      else
        {
        op[i].ScaleCoefficients(1.0 / this->GetInput()->GetSpacing()[i]);
        }
      }
    }

  // Calculate iterator radius
  Size< ImageDimension > radius;
  for ( i = 0; i < ImageDimension; ++i )
    {
    radius[i]  = op[0].GetRadius()[0];
    }

  // Find the data-set boundary "faces"
  typename NeighborhoodAlgorithm::ImageBoundaryFacesCalculator< TInputImage >::
  FaceListType faceList;
  NeighborhoodAlgorithm::ImageBoundaryFacesCalculator< TInputImage > bC;
  faceList = bC(input, outputRegionForThread, radius);

  typename NeighborhoodAlgorithm::ImageBoundaryFacesCalculator< TInputImage >::
  FaceListType::iterator fit;
  fit = faceList.begin();

  // support progress methods/callbacks
  ProgressReporter progress( this, threadId, outputRegionForThread.GetNumberOfPixels() );

  // Process non-boundary face
  nit = ConstNeighborhoodIterator< TInputImage >(radius, input, *fit);

  std::slice          x_slice[ImageDimension];
  const SizeValueType center = nit.Size() / 2;
  for ( i = 0; i < ImageDimension; ++i )
    {
    x_slice[i] = std::slice( center - nit.GetStride(i) * radius[i],
                             op[i].GetSize()[0], nit.GetStride(i) );
    }

  // Process each of the boundary faces.  These are N-d regions which border
  // the edge of the buffer.
  for ( fit = faceList.begin(); fit != faceList.end(); ++fit )
    {
    bit = ConstNeighborhoodIterator< InputImageType >(radius,
                                                      input, *fit);
    bit.OverrideBoundaryCondition(&nbc);
    bit.GoToBegin();

    while ( !bit.IsAtEnd() )
      {
      RealType a = NumericTraits< RealType >::Zero;
      for ( i = 0; i < ImageDimension; ++i )
        {
        const RealType g = SIP(x_slice[i], bit, op[i]);
        a += g * g;
        }

      if(localMax < a)
        localMax = a;

      ++bit;
      progress.CompletedPixel();
      }
    }

  m_ThreadMax[threadId] = vcl_sqrt(localMax);
}

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

  os << indent << "Maximum: "
     << static_cast< typename NumericTraits< PixelType >::PrintType >( this->GetMaximum() )
     << std::endl;
}
} // end namespace itk
#endif