File: itkKLMSegmentationRegion.cxx

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 (607 lines) | stat: -rw-r--r-- 18,260 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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
/*=========================================================================

  Program:   Insight Segmentation & Registration Toolkit
  Module:    $RCSfile: itkKLMSegmentationRegion.cxx,v $
  Language:  C++
  Date:      $Date: 2007-12-23 17:59:28 $
  Version:   $Revision: 1.16 $

  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.

=========================================================================*/
#include "itkKLMSegmentationRegion.h"

namespace itk
{


KLMSegmentationRegion
::KLMSegmentationRegion( void )
{
  m_MeanRegionIntensity = 0;
}


KLMSegmentationRegion
::~KLMSegmentationRegion()
{

}

void
KLMSegmentationRegion
::PrintSelf( std::ostream& os, Indent indent ) const
{

  Superclass::PrintSelf(os,indent);
  os << indent << "Mean region intensity   : " << m_MeanRegionIntensity <<
    std::endl;
  os << indent << "Region border object" << std::endl;

} // end PrintSelf


void
KLMSegmentationRegion
::SetRegionParameters( MeanRegionIntensityType meanRegionIntensity,
                       double                  regionArea,
                       RegionLabelType         label )
{
  // Set the area, mean, and label associated with the region
  this->SetRegionArea( regionArea );
  this->SetMeanRegionIntensity( meanRegionIntensity );
  this->SetRegionLabel( label );

} // end SetRegionParameters


void
KLMSegmentationRegion
::CombineRegionParameters( const Self *region )
{
  // Reset the area and mean associated with the merged region

  MeanRegionIntensityType region1Mean = this->GetMeanRegionIntensity();
  MeanRegionIntensityType region2Mean = region->GetMeanRegionIntensity();

  double region1Area = this->GetRegionArea();
  double region2Area = region->GetRegionArea();

  double mergedRegionArea = region1Area + region2Area;
  MeanRegionIntensityType mergedRegionMean =
    region1Mean * region1Area + region2Mean * region2Area;

  if ( mergedRegionArea <= 0 ) itkExceptionMacro( << "Invalid region area" );
  mergedRegionMean /= mergedRegionArea;
  this->SetRegionArea( mergedRegionArea );
  this->SetMeanRegionIntensity( mergedRegionMean );

} // end CombineRegionParameters


double
KLMSegmentationRegion
::EnergyFunctional(const Self *region)
{

  MeanRegionIntensityType region1_2MeanDiff =
    this->m_MeanRegionIntensity - region->m_MeanRegionIntensity;

  // Assuming equal weights to all the channels
  // FIXME: For different channel weights modify this part of the code.
  double cost = region1_2MeanDiff.squared_magnitude();

  double region1Area = this->GetRegionArea();
  double region2Area = region->GetRegionArea();

  double scaleArea = ( region1Area * region2Area ) /
                     ( region1Area + region2Area );

  return scaleArea * cost;
}


void
KLMSegmentationRegion
::DeleteRegionBorder( KLMSegmentationBorder *pBorderCandidate )
{

  if( pBorderCandidate == NULL )
    {
    itkExceptionMacro( << "Null pointer to segmentation region border" );
    }

  RegionBorderVectorIterator
    regionBorderVectorIt    = m_RegionBorderVector.begin();
  RegionBorderVectorIterator
    regionBorderVectorItEnd = m_RegionBorderVector.end();

  // Erase the region border that matches the pBorderCandidate
  bool foundBorderCandidate = false;
  while( regionBorderVectorIt != regionBorderVectorItEnd )
    {
    if ( *regionBorderVectorIt == pBorderCandidate )
      {
      m_RegionBorderVector.erase( regionBorderVectorIt );
      foundBorderCandidate = true;
      break;
      }
    ++regionBorderVectorIt;
    }

  if ( !foundBorderCandidate )
    {
    itkExceptionMacro(<< "Border candidate not in region borders list" );
    }

} // end DeleteRegionBorder()


void
KLMSegmentationRegion
::PushBackRegionBorder( KLMSegmentationBorder *pBorderCandidate )
{
  if( pBorderCandidate == NULL )
    {
    itkExceptionMacro( << "Null pointer to segmentation region border" );
    }
  m_RegionBorderVector.push_back( pBorderCandidate );
}


void
KLMSegmentationRegion
::PushFrontRegionBorder( KLMSegmentationBorder *pBorderCandidate )
{
  if( pBorderCandidate == NULL )
    {
    itkExceptionMacro( << "Null pointer to segmentation region border" );
    }
  m_RegionBorderVector.insert( m_RegionBorderVector.begin(), pBorderCandidate );
}


void
KLMSegmentationRegion
::InsertRegionBorder( KLMSegmentationBorder *pBorderCandidate )
{
  // Ensure that the border candidate is not a null pointer
  if( pBorderCandidate == NULL )
    {
    itkExceptionMacro( << "Null pointer to segmentation region border" );
    }

  // The m_RegionBorderVec is a ordered vector of pointers to the borders.
  // Ordering is based on regions labels.

  // The region border vector is empty, there is only one region border
  if( m_RegionBorderVector.empty() )
    {
    m_RegionBorderVector.push_back( pBorderCandidate );
    }

  // There are many region borders
  else
    {

    // Iterator set to the first element of the region border element
    RegionBorderVectorIterator
      regionBorderVectorIt    = m_RegionBorderVector.begin();
    RegionBorderVectorIterator
      regionBorderVectorItEnd = m_RegionBorderVector.end();

    bool insertAtEnd = true;
    while( regionBorderVectorIt != regionBorderVectorItEnd )
      {

      // The region border should be inserted.
      if ( (      pBorderCandidate->GetRegion1()->GetRegionLabel() <
           (*regionBorderVectorIt)->GetRegion1()->GetRegionLabel() ) ||
           (      pBorderCandidate->GetRegion1()->GetRegionLabel() ==
           (*regionBorderVectorIt)->GetRegion1()->GetRegionLabel() &&
                  pBorderCandidate->GetRegion2()->GetRegionLabel() <
           (*regionBorderVectorIt)->GetRegion2()->GetRegionLabel() ) )
        {

        m_RegionBorderVector.insert(regionBorderVectorIt,pBorderCandidate);
        insertAtEnd = false;
        break;

        } // end of if

      ++regionBorderVectorIt;
      } // end of while

    // It should be inserted at the end
    if( insertAtEnd )
      {
      m_RegionBorderVector.push_back( pBorderCandidate );
      }

    } // end of else for the case with many region borders

} // end InsertRegionBorder


void
KLMSegmentationRegion
::InsertRegionBorder(RegionBorderVectorIterator RegionBorderVectorIt,
                     KLMSegmentationBorder *pBorderCandidate )
{
  // Ensure that the border candidate is not a null pointer
  if( pBorderCandidate == NULL )
    {
    itkExceptionMacro( << "Null pointer to segmentation region border" );
    }

  // The m_RegionBorderVec is a ordered vector of pointers to the
  // borders. Insert a valid region border into the region border vector
  // assuming calling function has correctly identified the new
  // element position.
  m_RegionBorderVector.insert( RegionBorderVectorIt, pBorderCandidate );

} // end InsertRegionBorder


void
KLMSegmentationRegion
::ResetRegionLabelAndUpdateBorders( Self *region )
{
  // Assign new equivalence label to the old region
  this->SetRegionLabel( region->GetRegionLabel() );

  // Update the region borders

  RegionBorderVectorIterator
    oldRegionBordersIt    = this->GetRegionBorderItBegin();
  RegionBorderVectorIterator
    endOfOldRegionBorders = this->GetRegionBorderItEnd();

  // Ensure that all borders associated with the old region
  // are in the correct order
  while ( oldRegionBordersIt != endOfOldRegionBorders )
    {
    // Make sure the neighbors now point to the new region, and
    // reorder the region borders of the neighbors since they are
    // no longer in ascending order

    // Ensure that region 1 label is less than  region 2 label
    if( (*oldRegionBordersIt)->GetRegion1()->GetRegionLabel() ==
        (*oldRegionBordersIt)->GetRegion2()->GetRegionLabel() )
      {
      itkExceptionMacro( << "Invalid region border list" );
      }

    // Correct order
    if ( (*oldRegionBordersIt)->GetRegion1()->GetRegionLabel()  >
         (*oldRegionBordersIt)->GetRegion2()->GetRegionLabel() )
      {
      // Swap the regions pointed to by the border
      KLMSegmentationRegion *ptmpRegion = (*oldRegionBordersIt)->GetRegion1();
      (*oldRegionBordersIt)->SetRegion1( (*oldRegionBordersIt)->GetRegion2() );
      (*oldRegionBordersIt)->SetRegion2( ptmpRegion );

      } // end if (correct order)

    // Point to the newly merged region, and since the region labels
    // have been switched (if the region 1 label of a border is found to be
    // greater than the corresponding region 2 label), the list of borders for
    // the neighbor region has changed.

    // The border's region1 is the neighbor
    if ( (*oldRegionBordersIt)->GetRegion2() == this )
      {

      // Make the border's region 2 point to the new region
      (*oldRegionBordersIt)->SetRegion2( region );

      // Reorder the region borders of the neighbor:
      // remove the region border, then re-insert it.
      (*oldRegionBordersIt)->GetRegion1()
        ->DeleteRegionBorder( *oldRegionBordersIt );
      (*oldRegionBordersIt)->GetRegion1()
        ->InsertRegionBorder( *oldRegionBordersIt );

      } // end if (the border's region1 is the neighbor)

    // The border's region2 is the neighbor
    else if ( (*oldRegionBordersIt)->GetRegion1() == this )
      {

      // Make the border's region 1 point to the new region
      (*oldRegionBordersIt)->SetRegion1( region );

      // Reorder the region borders of the neighbor
      // remove the region border, then re-insert it
      (*oldRegionBordersIt)->GetRegion2()
        ->DeleteRegionBorder( *oldRegionBordersIt );
      (*oldRegionBordersIt)->GetRegion2()
        ->InsertRegionBorder( *oldRegionBordersIt );

      } // end else if (the border's region2 is the neighbor)

    else
      {
      itkExceptionMacro( << "Invalid region border list" );
      } // end else

    // Go to the next region border pointed by the iterator
    ++oldRegionBordersIt;

    } // end while

} // end ResetRegionLabelAndUpdateBorders


void
KLMSegmentationRegion
::SpliceRegionBorders( Self *region )
{
  // Do the actual union of the borders

  // Copy the container into a temp variable to avoid iterator confusion
  RegionBorderVectorType thisRegionBorder = m_RegionBorderVector;

  // clear output
  m_RegionBorderVector.resize( 0 );

  // Initialize the region iterators

  RegionBorderVectorConstIterator
    thisRegionBordersIt    = thisRegionBorder.begin();
  RegionBorderVectorConstIterator
    endOfThisRegionBorders = thisRegionBorder.end();

  RegionBorderVectorConstIterator
    thatRegionBordersIt    = region->GetRegionBorderConstItBegin();
  RegionBorderVectorConstIterator
    endOfThatRegionBorders = region->GetRegionBorderConstItEnd();

  // Merge the two sets of region borders into the newly merged region

  while ( ( thisRegionBordersIt != endOfThisRegionBorders ) &&
          ( thatRegionBordersIt != endOfThatRegionBorders ) )
    {

    // Ensure that there are no common borders in the new region
    if( ( (*thisRegionBordersIt)->GetRegion1() ==
          (*thisRegionBordersIt)->GetRegion2() ) ||
        ( (*thatRegionBordersIt)->GetRegion1() ==
          (*thatRegionBordersIt)->GetRegion2() ) ||
        ( (*thisRegionBordersIt) == (*thatRegionBordersIt) ) )
      {
      itkExceptionMacro( << "Invalid region border list" );
      }

    // The two borders point to the same regions: DUPLICATE case.
    // They must be merged into one border, and any reference to
    // to the duplicate border must be purged.

    if ( ( (*thisRegionBordersIt)->GetRegion1() ==
           (*thatRegionBordersIt)->GetRegion1() ) &&
         ( (*thisRegionBordersIt)->GetRegion2() ==
           (*thatRegionBordersIt)->GetRegion2() ) )
      {

      // Add the lengths of the borders
      double newLength = (*thatRegionBordersIt)->GetBorderLength() +
                         (*thisRegionBordersIt)->GetBorderLength();

      (*thisRegionBordersIt)->SetBorderLength( newLength );

      m_RegionBorderVector.push_back( *thisRegionBordersIt );

      // The border's region1 is the neighbor
      if ( (*thatRegionBordersIt)->GetRegion2() == this )
        {
        (*thatRegionBordersIt)->GetRegion1()
          ->DeleteRegionBorder( (*thatRegionBordersIt) );

        } // end if (the border's region1 is the neighbor )

      // The border's region2 is the neighbor
      else if ( (*thatRegionBordersIt)->GetRegion1() == this )
        {
        (*thatRegionBordersIt)->GetRegion2()
          ->DeleteRegionBorder( *thatRegionBordersIt );

        } // end else if (the border's region2 is the neighbor )

      else
        {
        itkExceptionMacro( << "Invalid region border list" );
        } // end else

      // Nullify the duplicate border so it can be identified and removed.
      (*thatRegionBordersIt)->SetRegion1( NULL );
      (*thatRegionBordersIt)->SetRegion2( NULL );
      (*thatRegionBordersIt)->SetLambda( -1.0 );

      thisRegionBordersIt++;
      thatRegionBordersIt++;

      } // end if loop for case when two borders point to same region

    // This neighbor region label is less then that neighbor region label
    else if ( (   (*thisRegionBordersIt)->GetRegion1()->GetRegionLabel() <
                  (*thatRegionBordersIt)->GetRegion1()->GetRegionLabel() ) ||

              ( ( (*thisRegionBordersIt)->GetRegion1()->GetRegionLabel() ==
                  (*thatRegionBordersIt)->GetRegion1()->GetRegionLabel() ) &&

                ( (*thisRegionBordersIt)->GetRegion2()->GetRegionLabel() <
                  (*thatRegionBordersIt)->GetRegion2()->GetRegionLabel() ) ) )
      {

      m_RegionBorderVector.push_back( *thisRegionBordersIt );
      thisRegionBordersIt++;

      } // end else if

    // That neighbor region label is less then this neighbor region label
    else if ( (   (*thatRegionBordersIt)->GetRegion1()->GetRegionLabel() <
                  (*thisRegionBordersIt)->GetRegion1()->GetRegionLabel() ) ||

              ( ( (*thatRegionBordersIt)->GetRegion1()->GetRegionLabel() ==
                  (*thisRegionBordersIt)->GetRegion1()->GetRegionLabel() ) &&

                ( (*thatRegionBordersIt)->GetRegion2()->GetRegionLabel() <
                  (*thisRegionBordersIt)->GetRegion2()->GetRegionLabel() ) ) )
      {

      m_RegionBorderVector.push_back( *thatRegionBordersIt );
      thatRegionBordersIt++;

      } // end else if
    else
      {
      itkExceptionMacro( << "Invalid region border" );
      } // end else

    } // end of while

  // If any borders remain in thisRegionBordersIt, put them in the back
  while ( thisRegionBordersIt != endOfThisRegionBorders )
    {
    m_RegionBorderVector.push_back( *thisRegionBordersIt );
    thisRegionBordersIt++;
    }

  // If any borders remain in thatRegionBorders, put them to the back
  while ( thatRegionBordersIt != endOfThatRegionBorders )
    {
    m_RegionBorderVector.push_back( *thatRegionBordersIt );
    thatRegionBordersIt++;
    }

} // end SpliceRegionBorders


void
KLMSegmentationRegion
::UpdateRegionBorderLambda()
{
  // Check if the number of borders for this region is NULL
  if( m_RegionBorderVector.empty() )
    {
    itkExceptionMacro(<< "The region border for computing Lambda is NULL" );
    }

  // Set up the iterator to loop through the region border vector
  RegionBorderVectorIterator
    regionBorderVectorIt    = m_RegionBorderVector.begin();
  RegionBorderVectorIterator
    regionBorderVectorItEnd = m_RegionBorderVector.end();

  // Loop through the entire border list and update the Lambda values
  while( regionBorderVectorIt != regionBorderVectorItEnd )
    {
    (*regionBorderVectorIt)->EvaluateLambda();
    ++regionBorderVectorIt;
    } // End while loop

} // end UpdateRegionBorderLambda


void
KLMSegmentationRegion
::DeleteAllRegionBorders()
{

  m_RegionBorderVector.resize( 0 );

} // end DeleteAllRegionBorders


KLMSegmentationRegion::RegionBorderVectorIterator
KLMSegmentationRegion
::GetRegionBorderItBegin()
{

  return m_RegionBorderVector.begin();

} // end GetRegionBorderItBegin


KLMSegmentationRegion::RegionBorderVectorConstIterator
KLMSegmentationRegion
::GetRegionBorderConstItBegin()
{

  return m_RegionBorderVector.begin();

} // end GetRegionBorderConstItBegin


KLMSegmentationRegion::RegionBorderVectorIterator
KLMSegmentationRegion
::GetRegionBorderItEnd()
{

  return m_RegionBorderVector.end();

} // end GetRegionBorderItEnd


KLMSegmentationRegion::RegionBorderVectorConstIterator
KLMSegmentationRegion
::GetRegionBorderConstItEnd()
{

  return m_RegionBorderVector.end();

} // end GetRegionBorderConstItEnd


KLMSegmentationRegion::RegionBorderVectorSizeType
KLMSegmentationRegion
::GetRegionBorderSize() const
{

  return m_RegionBorderVector.size();

} // end GetRegionBorderSize


void
KLMSegmentationRegion
::PrintRegionInfo()
{
  int region1label;
  int region2label;

  // If there are border pointers print the results
  RegionBorderVectorIterator tempVectorIt;

  std::cout << "------------------------------" << std::endl;
  std::cout << "Location   : " << this << std::endl;
  std::cout << "Label      : " << (this->GetRegionLabel()) << std::endl;
  std::cout << "Area       : " << (this->GetRegionArea()) << std::endl;
  std::cout << "Mean       : " << (this->GetMeanRegionIntensity()) << std::endl;
  std::cout << "Num Borders: " << static_cast<int>( m_RegionBorderVector.size() ) << std::endl;
  std::cout << "++++++++++++++++++++++++++++++" << std::endl;

  // If there are border pointers print the results
  tempVectorIt = m_RegionBorderVector.begin();
  for( unsigned int k = 0; k < m_RegionBorderVector.size(); k++ )
    {
    region1label = (*tempVectorIt)->GetRegion1()->GetRegionLabel();
    region2label = (*tempVectorIt)->GetRegion2()->GetRegionLabel();

    std::cout << "Border Ptr :" << (*tempVectorIt) << "( " <<
      region1label << " - " << region2label << " )" << " Lambda = " <<
      (*tempVectorIt)->GetLambda() << std::endl;

    tempVectorIt++;
    } // end for

  std::cout << "------------------------------" << std::endl;

} //end PrintRegionInfo

} // namespace itk