File: itkReconstructionImageFilter.txx

package info (click to toggle)
insighttoolkit 3.6.0-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 94,956 kB
  • ctags: 74,981
  • sloc: cpp: 355,621; ansic: 195,070; fortran: 28,713; python: 3,802; tcl: 1,996; sh: 1,175; java: 583; makefile: 415; csh: 184; perl: 175
file content (411 lines) | stat: -rw-r--r-- 12,845 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
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*=========================================================================

  Program:   Insight Segmentation & Registration Toolkit
  Module:    $RCSfile: itkReconstructionImageFilter.txx,v $
  Language:  C++
  Date:      $Date: 2008-01-12 20:27:49 $
  Version:   $Revision: 1.4 $

  Copyright (c) Insight Software Consortium. All rights reserved.
  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even 
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/
#ifndef __itkReconstructionImageFilter_txx
#define __itkReconstructionImageFilter_txx

#include "itkImageRegionConstIterator.h"
#include "itkImageRegionIterator.h"
#include "itkNumericTraits.h"
#include "itkReconstructionImageFilter.h"
#include "itkProgressReporter.h"
#include "itkConstantBoundaryCondition.h"
#include "itkConnectedComponentAlgorithm.h"

#include "itkConstantPadImageFilter.h"
#include "itkCropImageFilter.h"


namespace itk {

template <class TInputImage, class TOutputImage, class TCompare>
ReconstructionImageFilter<TInputImage, TOutputImage, TCompare>
::ReconstructionImageFilter()
{
  m_FullyConnected = false;
  m_UseInternalCopy = true;
}

template <class TInputImage, class TOutputImage, class TCompare>
void
ReconstructionImageFilter<TInputImage, TOutputImage, TCompare>
::GenerateInputRequestedRegion()
{
  // call the superclass' implementation of this method
  Superclass::GenerateInputRequestedRegion();

  // get pointers to the inputs
  MarkerImagePointer  markerPtr =
    const_cast< MarkerImageType * >( this->GetInput(0) );

  MaskImagePointer  maskPtr =
    const_cast< MaskImageType * >( this->GetInput(1) );

  if ( !markerPtr || !maskPtr )
    {
    return;
    }
  markerPtr->SetRequestedRegion(markerPtr->GetLargestPossibleRegion());
  maskPtr->SetRequestedRegion(maskPtr->GetLargestPossibleRegion());
}

template <class TInputImage, class TOutputImage, class TCompare>
void
ReconstructionImageFilter<TInputImage, TOutputImage, TCompare>
::EnlargeOutputRequestedRegion(DataObject *)
{
  this->GetOutput()
    ->SetRequestedRegion( this->GetOutput()->GetLargestPossibleRegion() );
}

template <class TInputImage, class TOutputImage, class TCompare>
void
ReconstructionImageFilter<TInputImage, TOutputImage, TCompare>
::SetMarkerImage(const MarkerImageType* markerImage)
{
  // Process object is not const-correct so the const casting is required.
  this->SetNthInput(0, const_cast<MarkerImageType *>( markerImage ));
}

template <class TInputImage, class TOutputImage, class TCompare>
const typename ReconstructionImageFilter<TInputImage, TOutputImage, TCompare>::MarkerImageType *
ReconstructionImageFilter<TInputImage, TOutputImage, TCompare>::GetMarkerImage()
{
  return this->GetInput(0);
}

template <class TInputImage, class TOutputImage, class TCompare>
void
ReconstructionImageFilter<TInputImage, TOutputImage, TCompare>::SetMaskImage(const MaskImageType* maskImage)
{
  // Process object is not const-correct so the const casting is required.
  this->SetNthInput(1, const_cast<MaskImageType *>( maskImage ));
}

template <class TInputImage, class TOutputImage, class TCompare>
const typename ReconstructionImageFilter<TInputImage, TOutputImage, TCompare>::MaskImageType *
ReconstructionImageFilter<TInputImage, TOutputImage, TCompare>::GetMaskImage()
{
  return this->GetInput(1);
}

// a version that takes a padded copy of mask and marker
template <class TInputImage, class TOutputImage, class TCompare>
void
ReconstructionImageFilter<TInputImage, TOutputImage, TCompare>
::GenerateData()
{
  // Allocate the output
  this->AllocateOutputs();
  // there are 2 passes that use all pixels and a 3rd that uses some
  // subset of the pixels. We'll just pretend that the third pass
  // takes the same as each of the others. Is it OK to update more
  // often than pixels?
  ProgressReporter progress(this, 0, this->GetOutput()->GetRequestedRegion().GetNumberOfPixels()*3);

  TCompare compare;

  MarkerImageConstPointer markerImage = this->GetMarkerImage();
  MaskImageConstPointer   maskImage = this->GetMaskImage();
  OutputImagePointer      output = this->GetOutput();

  // mask and marker must have the same size
  if ( this->GetMarkerImage()->GetRequestedRegion().GetSize() != this->GetMaskImage()->GetRequestedRegion().GetSize() )
    {
    itkExceptionMacro( << "Marker and mask must have the same size." );
    }

  // create padded versions of the marker image and the mask image
  typedef typename itk::ConstantPadImageFilter<InputImageType, InputImageType> PadType;
  
  MarkerImageConstPointer markerImageP;
  MaskImageConstPointer   maskImageP;

  ISizeType padSize;

  if (m_UseInternalCopy)
    {
    typename PadType::Pointer MaskPad = PadType::New();
    typename PadType::Pointer MarkerPad = PadType::New();
    padSize.Fill( 1 );
    
    MaskPad->SetConstant(m_MarkerValue);
    MarkerPad->SetConstant(m_MarkerValue);
    MaskPad->SetPadLowerBound( padSize.m_Size );
    MaskPad->SetPadUpperBound( padSize.m_Size );
    MarkerPad->SetPadLowerBound( padSize.m_Size );
    MarkerPad->SetPadUpperBound( padSize.m_Size );
  
    MaskPad->SetInput(maskImage);
    MarkerPad->SetInput(markerImage);
    MaskPad->Update();
    MarkerPad->Update();
  
    markerImageP = MarkerPad->GetOutput();
    maskImageP = MaskPad->GetOutput();
    }
  else
    {
    maskImageP = this->GetMaskImage();
    InputIteratorType inIt( markerImage,
                            output->GetRequestedRegion() );
    OutputIteratorType outIt( output,
                              output->GetRequestedRegion() );
    // copy marker to output - isn't there a better way?
    while ( !outIt.IsAtEnd() )
      {
      MarkerImagePixelType currentValue = inIt.Get();
      outIt.Set( static_cast<OutputImagePixelType>( currentValue ) );
      ++inIt;
      ++outIt;
      }
    markerImageP = output;
    }



  // declare our queue type
  typedef typename std::queue<OutputImageIndexType> FifoType;
  FifoType IndexFifo;

  NOutputIterator outNIt;
  InputIteratorType mskIt;
  CNInputIterator mskNIt;
  ISizeType kernelRadius;
  kernelRadius.Fill(1);
  if (m_UseInternalCopy)
    {
    FaceCalculatorType faceCalculator;
    
    FaceListType faceList;
    FaceListTypeIt fit;
    
    
    faceList = faceCalculator(maskImageP, maskImageP->GetLargestPossibleRegion(),
                              kernelRadius);
    // we will only be processing the body region
    fit = faceList.begin();
    // must be a better way of doing this
    NOutputIterator tt(kernelRadius,
                       markerImageP,
                       *fit );
    outNIt = tt;
    
    InputIteratorType ttt( maskImageP,
                           *fit );
    mskIt = ttt;
    CNInputIterator tttt(kernelRadius,
                         maskImageP,
                         *fit );
    mskNIt = tttt;
    }
  else
    {
    NOutputIterator tt(kernelRadius,
                       markerImageP,
                       output->GetRequestedRegion());
    outNIt = tt;
    
    InputIteratorType ttt( maskImageP,
                           output->GetRequestedRegion());
    mskIt = ttt;
    CNInputIterator tttt(kernelRadius,
                         maskImageP,
                         output->GetRequestedRegion());
    mskNIt = tttt;    
    }

  setConnectivityPrevious( &outNIt, m_FullyConnected );
  
  ConstantBoundaryCondition<OutputImageType> oBC;
  oBC.SetConstant(m_MarkerValue);
  outNIt.OverrideBoundaryCondition(&oBC);
  
  
  mskIt.GoToBegin();
  // scan in forward raster order
  for (outNIt.GoToBegin(),mskIt.GoToBegin();!outNIt.IsAtEnd(); ++outNIt,++mskIt)
    {
    InputImagePixelType V = outNIt.GetCenterPixel();
    InputImagePixelType iV = static_cast<OutputImagePixelType>( mskIt.Get() );

    // be sure that the pixels in the images follow the preconditions
    if ( compare( V, iV ) )
      {
      if ( compare(0, 1) )
        {
        itkExceptionMacro( << "Marker pixels must be <= mask pixels." );
        }
      else
        {
        itkExceptionMacro( << "Marker pixels must be >= mask pixels." );
        }
      }

    // visit the previous neighbours
    typename NOutputIterator::ConstIterator sIt;
    for( sIt = outNIt.Begin(); !sIt.IsAtEnd(); ++sIt )
      {
      InputImagePixelType VN = sIt.Get();
      if (compare(VN, V)) 
        {
        outNIt.SetCenterPixel(VN);
        V = VN;
        }
      }

    // this step clamps to the mask 
    if (compare(V, iV))
      {
      outNIt.SetCenterPixel(iV);
      }

    progress.CompletedPixel();
    }

  // now for the reverse raster order pass
  // reset the neighborhood
  setConnectivityLater( &outNIt, m_FullyConnected );
  outNIt.OverrideBoundaryCondition(&oBC);
  outNIt.GoToEnd();
  //mskIt.GoToEnd();
  
  ConstantBoundaryCondition<InputImageType> iBC;
  iBC.SetConstant(m_MarkerValue);
  
  setConnectivityLater( &mskNIt, m_FullyConnected );
  mskNIt.OverrideBoundaryCondition(&iBC);
  
  typename NOutputIterator::IndexListType oIndexList, mIndexList;
  typename NOutputIterator::IndexListType::const_iterator oLIt, mLIt;
  
  oIndexList = outNIt.GetActiveIndexList();
  mIndexList = mskNIt.GetActiveIndexList();
  
  mskNIt.GoToEnd();
  while (!outNIt.IsAtBegin())
    {
    --outNIt;
    --mskNIt;
    InputImagePixelType V = outNIt.GetCenterPixel();
    typename NOutputIterator::ConstIterator sIt;
    for (sIt = outNIt.Begin(); !sIt.IsAtEnd();++sIt)
      {
      InputImagePixelType VN = sIt.Get();
      if (compare(VN, V)) 
        {
        outNIt.SetCenterPixel(VN);
        V = VN;
        }
      }
    InputImagePixelType iV = mskNIt.GetCenterPixel();
    if (compare(V, iV))
      {
      outNIt.SetCenterPixel(iV);
      V = iV;
      }

    // now put indexes in the fifo
    //typename CNInputIterator::ConstIterator mIt;
    for (oLIt = oIndexList.begin(), mLIt = mIndexList.begin(); oLIt != oIndexList.end();++oLIt, ++mLIt)
      {
      InputImagePixelType VN = outNIt.GetPixel(*oLIt);
      InputImagePixelType iN = mskNIt.GetPixel(*mLIt);
      if (compare(V, VN) && compare(iN, VN)) 
        {
        IndexFifo.push(outNIt.GetIndex());
        break;
        }
      }
    progress.CompletedPixel();
    }

  
  // Now we want to check the full neighborhood
  setConnectivity( &outNIt, m_FullyConnected );
  setConnectivity( &mskNIt, m_FullyConnected );
  mskNIt.OverrideBoundaryCondition(&iBC);
  outNIt.OverrideBoundaryCondition(&oBC);
  oIndexList = outNIt.GetActiveIndexList();
  mIndexList = mskNIt.GetActiveIndexList();
  // now process the fifo - this fill the parts that weren't dealt
  // with by the raster and anti-raster passes
  //typename NOutputIterator::Iterator sIt;
  typename CNInputIterator::ConstIterator mIt;
  
  while (!IndexFifo.empty())
    {
    InputImageIndexType I = IndexFifo.front();
    IndexFifo.pop();
    // reposition the iterators
    outNIt += I - outNIt.GetIndex();
    mskNIt += I - mskNIt.GetIndex();
    InputImagePixelType V = outNIt.GetCenterPixel();
    for (oLIt = oIndexList.begin(), mLIt = mIndexList.begin(); 
         oLIt != oIndexList.end();
         ++oLIt, ++mLIt)
      {
      InputImagePixelType VN = outNIt.GetPixel(*oLIt);
      InputImagePixelType iN = mskNIt.GetPixel(*mLIt);
      // candidate for dilation via flooding
      if (compare(V, VN) && (iN != VN))
        {
        if (compare(iN, V)) 
          {
          // not clamped by the mask, propogate the center value
          outNIt.SetPixel(*oLIt, V);
          }
        else
          {
          // apply the clamping
          outNIt.SetPixel(*oLIt, iN);
          }
         IndexFifo.push(outNIt.GetIndex(*oLIt));
        }
      }     
    progress.CompletedPixel();
    }

  if (m_UseInternalCopy)
    {
    typedef typename itk::CropImageFilter<InputImageType, OutputImageType> CropType;
    typename CropType::Pointer crop = CropType::New();

    crop->SetInput( markerImageP );
    crop->SetUpperBoundaryCropSize( padSize );
    crop->SetLowerBoundaryCropSize( padSize );
    crop->GraftOutput( this->GetOutput() );
    /** execute the minipipeline */
    crop->Update();

    /** graft the minipipeline output back into this filter's output */
    this->GraftOutput( crop->GetOutput() );
    }
}

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

  os << indent << "FullyConnected: "  << m_FullyConnected << std::endl;
  os << indent << "MarkerValue: " << m_MarkerValue << std::endl;
  os << indent << "UseInternalCopy: " << m_UseInternalCopy << std::endl;
}
}
#endif