File: RLERegionOfInterestImageFilter.txx

package info (click to toggle)
itksnap 3.6.0-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 22,132 kB
  • sloc: cpp: 91,089; ansic: 1,994; sh: 327; makefile: 16
file content (533 lines) | stat: -rw-r--r-- 19,158 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
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
#ifndef RLERegionOfInterestImageFilter_txx
#define RLERegionOfInterestImageFilter_txx

#include "itkRegionOfInterestImageFilter.h"
#include "itkImageAlgorithm.h"
#include "itkObjectFactory.h"
#include "itkProgressReporter.h"
#include "itkImage.h"

namespace itk
{
template< typename TPixel, unsigned int VImageDimension, typename CounterType >
void RegionOfInterestImageFilter<RLEImage<TPixel, VImageDimension, CounterType>,
    RLEImage<TPixel, VImageDimension, CounterType> >
    ::PrintSelf(std::ostream & os, Indent indent) const
{
    Superclass::PrintSelf(os, indent);

    os << indent << "RegionOfInterest: " << m_RegionOfInterest << std::endl;
}

template< typename TPixel, unsigned int VImageDimension, typename CounterType >
void RegionOfInterestImageFilter<RLEImage<TPixel, VImageDimension, CounterType>,
    RLEImage<TPixel, VImageDimension, CounterType> >
    ::GenerateInputRequestedRegion()
{
    // call the superclass' implementation of this method
    Superclass::GenerateInputRequestedRegion();

    // get pointer to the input
    typename Superclass::InputImagePointer inputPtr =
        const_cast< RLEImageType * >(this->GetInput());

    if (inputPtr)
    {
        // request the region of interest
        inputPtr->SetRequestedRegion(m_RegionOfInterest);
    }
}

template< typename TPixel, unsigned int VImageDimension, typename CounterType >
void RegionOfInterestImageFilter<RLEImage<TPixel, VImageDimension, CounterType>,
    RLEImage<TPixel, VImageDimension, CounterType> >
    ::EnlargeOutputRequestedRegion(DataObject *output)
{
    // call the superclass' implementation of this method
    Superclass::EnlargeOutputRequestedRegion(output);

    // generate everything in the region of interest
    output->SetRequestedRegionToLargestPossibleRegion();
}

/**
* RegionOfInterestImageFilter can produce an image which is a different size
* than its input image.  As such, RegionOfInterestImageFilter needs to provide an
* implementation for GenerateOutputInformation() in order to inform
* the pipeline execution model.  The original documentation of this
* method is below.
*
* \sa ProcessObject::GenerateOutputInformaton()
*/
template< typename TPixel, unsigned int VImageDimension, typename CounterType >
void RegionOfInterestImageFilter<RLEImage<TPixel, VImageDimension, CounterType>,
    RLEImage<TPixel, VImageDimension, CounterType> >
    ::GenerateOutputInformation()
{
    // do not call the superclass' implementation of this method since
    // this filter allows the input the output to be of different dimensions

    // get pointers to the input and output
    typename Superclass::OutputImagePointer outputPtr = this->GetOutput();
    typename Superclass::InputImageConstPointer inputPtr = this->GetInput();

    if (!outputPtr || !inputPtr)
    {
        return;
    }

    // Set the output image size to the same value as the region of interest.
    RegionType region;
    IndexType  start;
    start.Fill(0);

    region.SetSize(m_RegionOfInterest.GetSize());
    region.SetIndex(start);

    // Copy Information without modification.
    outputPtr->CopyInformation(inputPtr);

    // Adjust output region
    outputPtr->SetLargestPossibleRegion(region);

    // Correct origin of the extracted region.
    IndexType roiStart(m_RegionOfInterest.GetIndex());
    typename Superclass::OutputImageType::PointType outputOrigin;
    inputPtr->TransformIndexToPhysicalPoint(roiStart, outputOrigin);
    outputPtr->SetOrigin(outputOrigin);
}

/**
* RegionOfInterestImageFilter can be implemented as a multithreaded filter.
* Therefore, this implementation provides a ThreadedGenerateData()
* routine which is called for each processing thread. The output
* image data is allocated automatically by the superclass prior to
* calling ThreadedGenerateData().  ThreadedGenerateData can only
* write to the portion of the output image specified by the
* parameter "outputRegionForThread"
*
* \sa ImageToImageFilter::ThreadedGenerateData(),
*     ImageToImageFilter::GenerateData()
*/
template< typename TPixel, unsigned int VImageDimension, typename CounterType >
void RegionOfInterestImageFilter<RLEImage<TPixel, VImageDimension, CounterType>,
    RLEImage<TPixel, VImageDimension, CounterType> >
    ::ThreadedGenerateData(const RegionType & outputRegionForThread,
    ThreadIdType threadId)
{
    // Get the input and output pointers
    const RLEImageType *in = this->GetInput();
    RLEImageType *out = this->GetOutput();

    // Define the portion of the input to walk for this thread
    InputImageRegionType inputRegionForThread;
    inputRegionForThread.SetSize(outputRegionForThread.GetSize());

    IndexType start, end;
    IndexType roiStart(m_RegionOfInterest.GetIndex());
    IndexType threadStart(outputRegionForThread.GetIndex());
    for (unsigned int i = 0; i < VImageDimension; i++)
    {
        start[i] = roiStart[i] + threadStart[i];
        end[i] = roiStart[i] + threadStart[i] + outputRegionForThread.GetSize(i);
    }
    inputRegionForThread.SetIndex(start);

    bool copyLines = (in->GetLargestPossibleRegion().GetSize(0) == outputRegionForThread.GetSize(0));
    typename ImageType::BufferType::RegionType oReg = ImageType::truncateRegion(outputRegionForThread),
        iReg = ImageType::truncateRegion(inputRegionForThread);
    ImageRegionConstIterator<typename ImageType::BufferType> iIt(in->GetBuffer(), iReg);
    ImageRegionIterator<typename ImageType::BufferType> oIt(out->GetBuffer(), oReg);

    while (!oIt.IsAtEnd())
    {
        if (copyLines)
            oIt.Set(iIt.Get());
        else //determine begin and end iterator and copy range
        {
            typename RLEImageType::RLLine &oLine = oIt.Value();
            oLine.clear();
            const typename RLEImageType::RLLine &iLine = iIt.Value();
            CounterType t = 0;
            SizeValueType x = 0;
            //find start
            for (; x < iLine.size(); x++)
            {
                t += iLine[x].first;
                if (t > start[0])
                    break;
            }
            assert(x < iLine.size());

            SizeValueType begin = x;
            if (t >= end[0]) //both begin and end are in this segment
            {
                oLine.push_back(
                    typename RLEImageType::RLSegment(end[0] - start[0], iLine[x].second));
                ++iIt;
                ++oIt;
                continue; //next line
            }
            else if (t - start[0] < iLine[x].first) //not the first pixel in segment
            {
                oLine.push_back(typename RLEImageType::RLSegment(t - start[0], iLine[x].second));
                begin++; //start copying from next segment
            }

            //if (t < end[0])
                for (x++; x < iLine.size(); x++)
                {
                    t += iLine[x].first;
                    if (t >= end[0])
                        break;
                }
            if (t == end[0])
                oLine.insert(oLine.end(), iLine.begin() + begin, iLine.begin() + x + 1);
            else //we need to take special care of the last segment
            {
                oLine.insert(oLine.end(), iLine.begin() + begin, iLine.begin() + x);
                oLine.push_back(
                    typename RLEImageType::RLSegment(end[0] + iLine[x].first - t, iLine[x].second));
            }
        }
        ++iIt;
        ++oIt;
    }
}


template< typename TPixel, unsigned int VImageDimension, typename CounterType >
void RegionOfInterestImageFilter<Image<TPixel, VImageDimension>,
    RLEImage<TPixel, VImageDimension, CounterType> >
    ::PrintSelf(std::ostream & os, Indent indent) const
{
    Superclass::PrintSelf(os, indent);

    os << indent << "RegionOfInterest: " << m_RegionOfInterest << std::endl;
}

template< typename TPixel, unsigned int VImageDimension, typename CounterType >
void RegionOfInterestImageFilter<Image<TPixel, VImageDimension>,
    RLEImage<TPixel, VImageDimension, CounterType> >
    ::GenerateInputRequestedRegion()
{
    // call the superclass' implementation of this method
    Superclass::GenerateInputRequestedRegion();

    // get pointer to the input
    typename Superclass::InputImagePointer inputPtr =
        const_cast< ImageType * >(this->GetInput());

    if (inputPtr)
    {
        // request the region of interest
        inputPtr->SetRequestedRegion(m_RegionOfInterest);
    }
}

template< typename TPixel, unsigned int VImageDimension, typename CounterType >
void RegionOfInterestImageFilter<Image<TPixel, VImageDimension>,
    RLEImage<TPixel, VImageDimension, CounterType> >
    ::EnlargeOutputRequestedRegion(DataObject *output)
{
    // call the superclass' implementation of this method
    Superclass::EnlargeOutputRequestedRegion(output);

    // generate everything in the region of interest
    output->SetRequestedRegionToLargestPossibleRegion();
}

/**
* RegionOfInterestImageFilter can produce an image which is a different size
* than its input image.  As such, RegionOfInterestImageFilter needs to provide an
* implementation for GenerateOutputInformation() in order to inform
* the pipeline execution model.  The original documentation of this
* method is below.
*
* \sa ProcessObject::GenerateOutputInformaton()
*/
template< typename TPixel, unsigned int VImageDimension, typename CounterType >
void RegionOfInterestImageFilter<Image<TPixel, VImageDimension>,
    RLEImage<TPixel, VImageDimension, CounterType> >
    ::GenerateOutputInformation()
{
    // do not call the superclass' implementation of this method since
    // this filter allows the input the output to be of different dimensions

    // get pointers to the input and output
    typename Superclass::OutputImagePointer outputPtr = this->GetOutput();
    typename Superclass::InputImageConstPointer inputPtr = this->GetInput();

    if (!outputPtr || !inputPtr)
    {
        return;
    }

    // Set the output image size to the same value as the region of interest.
    RegionType region;
    IndexType  start;
    start.Fill(0);

    region.SetSize(m_RegionOfInterest.GetSize());
    region.SetIndex(start);

    // Copy Information without modification.
    outputPtr->CopyInformation(inputPtr);

    // Adjust output region
    outputPtr->SetLargestPossibleRegion(region);

    // Correct origin of the extracted region.
    IndexType roiStart(m_RegionOfInterest.GetIndex());
    typename Superclass::OutputImageType::PointType outputOrigin;
    inputPtr->TransformIndexToPhysicalPoint(roiStart, outputOrigin);
    outputPtr->SetOrigin(outputOrigin);
}

/**
* RegionOfInterestImageFilter can be implemented as a multithreaded filter.
* Therefore, this implementation provides a ThreadedGenerateData()
* routine which is called for each processing thread. The output
* image data is allocated automatically by the superclass prior to
* calling ThreadedGenerateData().  ThreadedGenerateData can only
* write to the portion of the output image specified by the
* parameter "outputRegionForThread"
*
* \sa ImageToImageFilter::ThreadedGenerateData(),
*     ImageToImageFilter::GenerateData()
*/
template< typename TPixel, unsigned int VImageDimension, typename CounterType >
void RegionOfInterestImageFilter<Image<TPixel, VImageDimension>,
    RLEImage<TPixel, VImageDimension, CounterType> >
    ::ThreadedGenerateData(const RegionType & outputRegionForThread,
    ThreadIdType threadId)
{
    // Get the input and output pointers
    const ImageType *in = this->GetInput();
    RLEImageType *out = this->GetOutput();

    // Define the portion of the input to walk for this thread
    InputImageRegionType inputRegionForThread;
    inputRegionForThread.SetSize(outputRegionForThread.GetSize());

    IndexType start, end;
    IndexType roiStart(m_RegionOfInterest.GetIndex());
    IndexType threadStart(outputRegionForThread.GetIndex());
    for (unsigned int i = 0; i < VImageDimension; i++)
    {
        start[i] = roiStart[i] + threadStart[i];
        end[i] = roiStart[i] + threadStart[i] + outputRegionForThread.GetSize(i);
    }
    inputRegionForThread.SetIndex(start);

    typename RLEImageType::BufferType::RegionType oReg = RLEImageType::truncateRegion(outputRegionForThread);
    ImageRegionConstIterator<ImageType> iIt(in, inputRegionForThread);
    ImageRegionIterator<typename RLEImageType::BufferType> oIt(out->GetBuffer(), oReg);
    SizeValueType size0 = outputRegionForThread.GetSize(0);
    typename RLEImageType::RLLine temp;
    temp.reserve(size0); //pessimistically preallocate buffer, otherwise reallocations can occur

    while (!oIt.IsAtEnd())
    {
        SizeValueType x = 0;
        temp.clear();
        while (x < size0)
        {
            typename RLEImageType::RLSegment s(0, iIt.Value());
            while (x < size0 && iIt.Value() == s.second)
            {
                x++;
                s.first++;
                ++(iIt);
            }
            temp.push_back(s);
        }
        oIt.Value() = temp;
        ++oIt;
    }
}


template< typename TPixel, unsigned int VImageDimension, typename CounterType >
void RegionOfInterestImageFilter<RLEImage<TPixel, VImageDimension, CounterType>,
    Image<TPixel, VImageDimension> >
::PrintSelf(std::ostream & os, Indent indent) const
{
  Superclass::PrintSelf(os, indent);

  os << indent << "RegionOfInterest: " << m_RegionOfInterest << std::endl;
}

template< typename TPixel, unsigned int VImageDimension, typename CounterType >
void RegionOfInterestImageFilter<RLEImage<TPixel, VImageDimension, CounterType>,
    Image<TPixel, VImageDimension> >
::GenerateInputRequestedRegion()
{
  // call the superclass' implementation of this method
  Superclass::GenerateInputRequestedRegion();

  // get pointer to the input
  typename Superclass::InputImagePointer inputPtr =
    const_cast< RLEImageType * >( this->GetInput() );

  if ( inputPtr )
    {
    // request the region of interest
    inputPtr->SetRequestedRegion(m_RegionOfInterest);
    }
}

template< typename TPixel, unsigned int VImageDimension, typename CounterType >
void RegionOfInterestImageFilter<RLEImage<TPixel, VImageDimension, CounterType>,
    Image<TPixel, VImageDimension> >
::EnlargeOutputRequestedRegion(DataObject *output)
{
  // call the superclass' implementation of this method
  Superclass::EnlargeOutputRequestedRegion(output);

  // generate everything in the region of interest
  output->SetRequestedRegionToLargestPossibleRegion();
}

/**
 * RegionOfInterestImageFilter can produce an image which is a different size
 * than its input image.  As such, RegionOfInterestImageFilter needs to provide an
 * implementation for GenerateOutputInformation() in order to inform
 * the pipeline execution model.  The original documentation of this
 * method is below.
 *
 * \sa ProcessObject::GenerateOutputInformaton()
 */
template< typename TPixel, unsigned int VImageDimension, typename CounterType >
void RegionOfInterestImageFilter<RLEImage<TPixel, VImageDimension, CounterType>,
    Image<TPixel, VImageDimension> >
::GenerateOutputInformation()
{
  // do not call the superclass' implementation of this method since
  // this filter allows the input the output to be of different dimensions

  // get pointers to the input and output
  typename Superclass::OutputImagePointer outputPtr = this->GetOutput();
  typename Superclass::InputImageConstPointer inputPtr  = this->GetInput();

  if ( !outputPtr || !inputPtr )
    {
    return;
    }

  // Set the output image size to the same value as the region of interest.
  RegionType region;
  IndexType  start;
  start.Fill(0);

  region.SetSize( m_RegionOfInterest.GetSize() );
  region.SetIndex(start);

  // Copy Information without modification.
  outputPtr->CopyInformation(inputPtr);

  // Adjust output region
  outputPtr->SetLargestPossibleRegion(region);

  // Correct origin of the extracted region.
  IndexType roiStart( m_RegionOfInterest.GetIndex() );
  typename Superclass::OutputImageType::PointType outputOrigin;
  inputPtr->TransformIndexToPhysicalPoint(roiStart, outputOrigin);
  outputPtr->SetOrigin(outputOrigin);
}

/**
   * RegionOfInterestImageFilter can be implemented as a multithreaded filter.
   * Therefore, this implementation provides a ThreadedGenerateData()
   * routine which is called for each processing thread. The output
   * image data is allocated automatically by the superclass prior to
   * calling ThreadedGenerateData().  ThreadedGenerateData can only
   * write to the portion of the output image specified by the
   * parameter "outputRegionForThread"
   *
   * \sa ImageToImageFilter::ThreadedGenerateData(),
   *     ImageToImageFilter::GenerateData()
   */
template< typename TPixel, unsigned int VImageDimension, typename CounterType >
void RegionOfInterestImageFilter<RLEImage<TPixel, VImageDimension, CounterType>,
    Image<TPixel, VImageDimension> >
::ThreadedGenerateData(const RegionType & outputRegionForThread,
                       ThreadIdType threadId)
{
  // Get the input and output pointers
  const RLEImageType *in = this->GetInput();
  ImageType *out = this->GetOutput();

  // Define the portion of the input to walk for this thread
  InputImageRegionType inputRegionForThread;
  inputRegionForThread.SetSize(outputRegionForThread.GetSize());

  IndexType start, end;
  IndexType roiStart( m_RegionOfInterest.GetIndex() );
  IndexType threadStart( outputRegionForThread.GetIndex() );
  for (unsigned int i = 0; i < VImageDimension; i++)
  {
      start[i] = roiStart[i] + threadStart[i];
      end[i] = roiStart[i] + threadStart[i] + outputRegionForThread.GetSize(i);
  }
  inputRegionForThread.SetIndex(start);

  typename RLEImageType::BufferType::RegionType iReg = RLEImageType::truncateRegion(inputRegionForThread);
  ImageRegionConstIterator<typename RLEImageType::BufferType> iIt(in->GetBuffer(), iReg);
  ImageRegionIterator<ImageType> oIt(out, outputRegionForThread);

  while (!iIt.IsAtEnd())
  {
    const typename RLEImageType::RLLine &iLine = iIt.Value();
    CounterType t = 0;
    SizeValueType x = 0;
    //find start
    for (; x < iLine.size(); x++)
    {
        t += iLine[x].first;
        if (t > start[0])
            break;
    }
    assert(x < iLine.size());

    SizeValueType begin = x;
    if (t >= end[0]) //both begin and end are in this segment
    {
        for (SizeValueType i = start[0]; i < end[0]; i++)
        {
            oIt.Set(iLine[x].second);
            ++oIt;
        }
        ++iIt;
        continue; //next line
    }
    //else handle the beginning segment
    for (SizeValueType i = start[0]; i < t; i++)
    {
        oIt.Set(iLine[x].second);
        ++oIt;
    }
    //now handle middle segments
    for (x++; x < iLine.size(); x++)
    {
        t += iLine[x].first;
        if (t >= end[0])
            break;
        for (SizeValueType i = 0; i < iLine[x].first; i++)
        {
            oIt.Set(iLine[x].second);
            ++oIt;
        }
    }
    //handle the last segment
    for (SizeValueType i = 0; i < end[0] + iLine[x].first - t; i++)
    {
        oIt.Set(iLine[x].second);
        ++oIt;
    }
    ++iIt;
  }
}
} // end namespace itk

#endif //RLERegionOfInterestImageFilter_txx