KWStyle - itkKLMSegmentationRegion.cxx
 
Matrix View
Description

1 /*=========================================================================
2
3   Program:   Insight Segmentation & Registration Toolkit
4   Module:    $RCSfile: itkKLMSegmentationRegion.cxx.html,v $
5   Language:  C++
6   Date:      $Date: 2006/01/17 19:15:40 $
7   Version:   $Revision: 1.4 $
8
9   Copyright (c) Insight Software Consortium. All rights reserved.
10   See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11
12      This software is distributed WITHOUT ANY WARRANTY; without even
13      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14      PURPOSE.  See the above copyright notices for more information.
15
16 =========================================================================*/
17 DEF #ifndef _itkKLMSegmentationRegion_cxx
18 DEF #define _itkKLMSegmentationRegion_cxx
19
20 #include "itkKLMSegmentationRegion.h"
21
22 namespace itk
23 {
24
25
26 KLMSegmentationRegion
27 ::KLMSegmentationRegion( void )
28 {
29   m_MeanRegionIntensity = 0;
30 }
31
32
33 KLMSegmentationRegion
34 ::~KLMSegmentationRegion()
35 {
36
37 }
38
39 void
40 KLMSegmentationRegion
41 ::PrintSelf( std::ostream& os, Indent indent ) const
42 {
43
44   Superclass::PrintSelf(os,indent);
45   os << indent << "Mean region intensity   : " << m_MeanRegionIntensity <<
46     std::endl;
47   os << indent << "Region border object" << std::endl;
48
49 // end PrintSelf
50
51
52 void
53 KLMSegmentationRegion
54 ::SetRegionParameters( MeanRegionIntensityType meanRegionIntensity,
55                        double                  regionArea,
56                        RegionLabelType         label )
57 {
58   // Set the area, mean, and label associated with the region
59   this->SetRegionArea( regionArea );
60   this->SetMeanRegionIntensity( meanRegionIntensity );
61   this->SetRegionLabel( label );
62
63 // end SetRegionParameters
64
65
66 void
67 KLMSegmentationRegion
68 ::CombineRegionParameters( const Self *region )
69 {
70   // Reset the area and mean associated with the merged region
71
72   MeanRegionIntensityType region1Mean = this->GetMeanRegionIntensity();
73   MeanRegionIntensityType region2Mean = region->GetMeanRegionIntensity();
74
75   double region1Area = this->GetRegionArea();
76   double region2Area = region->GetRegionArea();
77
78   double mergedRegionArea = region1Area + region2Area;
79   MeanRegionIntensityType mergedRegionMean =
80 IND ****region1Mean * region1Area + region2Mean * region2Area;
81
82   if ( mergedRegionArea <= 0 ) itkExceptionMacro( << "Invalid region area" );
83   mergedRegionMean /= mergedRegionArea;
84   this->SetRegionArea( mergedRegionArea );
85   this->SetMeanRegionIntensity( mergedRegionMean );
86
87 // end CombineRegionParameters
88
89
90 double
91 KLMSegmentationRegion
92 ::EnergyFunctional(const Self *region)
93 {
94
95   MeanRegionIntensityType region1_2MeanDiff =
96 IND ****this->m_MeanRegionIntensity - region->m_MeanRegionIntensity;
97
98   // Assuming equal weights to all the channels
99   // FIXME: For different channel weights modify this part of the code.
100   double cost = region1_2MeanDiff.squared_magnitude();
101
102   double region1Area = this->GetRegionArea();
103   double region2Area = region->GetRegionArea();
104
105   double scaleArea = ( region1Area * region2Area ) /
106 IND *********************( region1Area + region2Area );
107
108   return scaleArea * cost;
109 }
110
111
112 void
113 KLMSegmentationRegion
114 ::DeleteRegionBorder( KLMSegmentationBorder *pBorderCandidate )
115 {
116
117   if( pBorderCandidate == NULL )
118     {
119     itkExceptionMacro( << "Null pointer to segmentation region border" );
120     }
121
122   RegionBorderVectorIterator
123     regionBorderVectorIt    = m_RegionBorderVector.begin();
124   RegionBorderVectorIterator
125     regionBorderVectorItEnd = m_RegionBorderVector.end();
126
127   // Erase the region border that matches the pBorderCandidate
128   while( regionBorderVectorIt != regionBorderVectorItEnd )
129     {
130     if ( *regionBorderVectorIt == pBorderCandidate ) {
131       m_RegionBorderVector.erase( regionBorderVectorIt );
132       break;
133     }
134     ++regionBorderVectorIt;
135     }
136
137   if ( regionBorderVectorIt == regionBorderVectorItEnd )
138     {
139     itkExceptionMacro(<< "Border candidate not in region borders list" );
140     }
141
142 // end DeleteRegionBorder()
143
144
145 void
146 KLMSegmentationRegion
147 ::PushBackRegionBorder( KLMSegmentationBorder *pBorderCandidate )
148 {
149   if( pBorderCandidate == NULL )
150     itkExceptionMacro( << "Null pointer to segmentation region border" );
151
152   m_RegionBorderVector.push_back( pBorderCandidate );
153 }
154
155
156 void
157 KLMSegmentationRegion
158 ::PushFrontRegionBorder( KLMSegmentationBorder *pBorderCandidate )
159 {
160   if( pBorderCandidate == NULL )
161     itkExceptionMacro( << "Null pointer to segmentation region border" );
162
163   m_RegionBorderVector.insert( m_RegionBorderVector.begin(), pBorderCandidate );
164 }
165
166
167 void
168 KLMSegmentationRegion
169 ::InsertRegionBorder( KLMSegmentationBorder *pBorderCandidate )
170 {
171   // Ensure that the border candidate is not a null pointer
172   if( pBorderCandidate == NULL )
173     itkExceptionMacro( << "Null pointer to segmentation region border" );
174
175   // The m_RegionBorderVec is a ordered vector of pointers to the borders.
176   // Ordering is based on regions labels.
177
178   // The region border vector is empty, there is only one region border
179   if( m_RegionBorderVector.empty() )
180     {
181     m_RegionBorderVector.push_back( pBorderCandidate );
182     }
183
184   // There are many region borders
185   else
186     {
187
188     // Iterator set to the first element of the region border element
189     RegionBorderVectorIterator
190       regionBorderVectorIt    = m_RegionBorderVector.begin();
191     RegionBorderVectorIterator
192       regionBorderVectorItEnd = m_RegionBorderVector.end();
193
194     while( regionBorderVectorIt != regionBorderVectorItEnd )
195       {
196
197       // The region border should be inserted.
198       if ( (      pBorderCandidate->GetRegion1()->GetRegionLabel() <
199            (*regionBorderVectorIt)->GetRegion1()->GetRegionLabel() ) ||
200 IND ***********(      pBorderCandidate->GetRegion1()->GetRegionLabel() ==
201 IND ***********(*regionBorderVectorIt)->GetRegion1()->GetRegionLabel() &&
202 IND ******************pBorderCandidate->GetRegion2()->GetRegionLabel() <
203            (*regionBorderVectorIt)->GetRegion2()->GetRegionLabel() ) )
204         {
205
206         m_RegionBorderVector.insert(regionBorderVectorIt,pBorderCandidate);
207         break;
208
209         } // end of if
210
211       ++regionBorderVectorIt;
212       } // end of while
213
214     // It should be inserted at the end
215     if( regionBorderVectorIt == regionBorderVectorItEnd )
216 IND ******m_RegionBorderVector.push_back( pBorderCandidate );
217
218     } // end of else for the case with many region borders
219
220 // end InsertRegionBorder
221
222
223 void
224 KLMSegmentationRegion
225 ::InsertRegionBorder(RegionBorderVectorIterator RegionBorderVectorIt,
226                      KLMSegmentationBorder *pBorderCandidate )
227 {
228   // Ensure that the border candidate is not a null pointer
229   if( pBorderCandidate == NULL )
230 IND ****itkExceptionMacro( << "Null pointer to segmentation region border" );
231
232   // The m_RegionBorderVec is a ordered vector of pointers to the
233   // borders. Insert a valid region border into the region border vector
234   // assuming calling function has correctly identified the new
235   // element position.
236   m_RegionBorderVector.insert( RegionBorderVectorIt, pBorderCandidate );
237
238 // end InsertRegionBorder
239
240
241 void
242 KLMSegmentationRegion
243 ::ResetRegionLabelAndUpdateBorders( Self *region )
244 {
245   // Assign new equivalence label to the old region
246   this->SetRegionLabel( region->GetRegionLabel() );
247
248   // Update the region borders
249
250   RegionBorderVectorIterator
251 IND ****oldRegionBordersIt    = this->GetRegionBorderItBegin();
252   RegionBorderVectorIterator
253 IND ****endOfOldRegionBorders = this->GetRegionBorderItEnd();
254
255   // Ensure that all borders associated with the old region
256   // are in the correct order
257   while ( oldRegionBordersIt != endOfOldRegionBorders )
258     {
259     // Make sure the neighbors now point to the new region, and
260     // reorder the region borders of the neighbors since they are
261     // no longer in ascending order
262
263     // Ensure that region 1 label is less than  region 2 label
264     if( (*oldRegionBordersIt)->GetRegion1()->GetRegionLabel() ==
265 IND ********(*oldRegionBordersIt)->GetRegion2()->GetRegionLabel() )
266       {
267       itkExceptionMacro( << "Invalid region border list" );
268       }
269
270     // Correct order
271     if ( (*oldRegionBordersIt)->GetRegion1()->GetRegionLabel()  >
272 IND *********(*oldRegionBordersIt)->GetRegion2()->GetRegionLabel() )
273       {
274       // Swap the regions pointed to by the border
275       KLMSegmentationRegion *ptmpRegion = (*oldRegionBordersIt)->GetRegion1();
276       (*oldRegionBordersIt)->SetRegion1( (*oldRegionBordersIt)->GetRegion2() );
277       (*oldRegionBordersIt)->SetRegion2( ptmpRegion );
278
279       } // end if (correct order)
280
281     // Point to the newly merged region, and since the region labels
282     // have been switched (if the region 1 label of a border is found to be
283     // greater than the corresponding region 2 label), the list of borders for
284     // the neighbor region has changed.
285
286     // The border's region1 is the neighbor
287     if ( (*oldRegionBordersIt)->GetRegion2() == this )
288       {
289
290       // Make the border's region 2 point to the new region
291       (*oldRegionBordersIt)->SetRegion2( region );
292
293       // Reorder the region borders of the neighbor:
294       // remove the region border, then re-insert it.
295       (*oldRegionBordersIt)->GetRegion1()
296 IND ********->DeleteRegionBorder( *oldRegionBordersIt );
297       (*oldRegionBordersIt)->GetRegion1()
298 IND ********->InsertRegionBorder( *oldRegionBordersIt );
299
300       } // end if (the border's region1 is the neighbor)
301
302     // The border's region2 is the neighbor
303     else if ( (*oldRegionBordersIt)->GetRegion1() == this )
304       {
305
306       // Make the border's region 1 point to the new region
307       (*oldRegionBordersIt)->SetRegion1( region );
308
309       // Reorder the region borders of the neighbor
310       // remove the region border, then re-insert it
311       (*oldRegionBordersIt)->GetRegion2()
312 IND ********->DeleteRegionBorder( *oldRegionBordersIt );
313       (*oldRegionBordersIt)->GetRegion2()
314 IND ********->InsertRegionBorder( *oldRegionBordersIt );
315
316       } // end else if (the border's region2 is the neighbor)
317
318     else
319       {
320       itkExceptionMacro( << "Invalid region border list" );
321       } // end else
322
323     // Go to the next region border pointed by the iterator
324     ++oldRegionBordersIt;
325
326     } // end while
327
328 // end ResetRegionLabelAndUpdateBorders
329
330
331 void
332 KLMSegmentationRegion
333 ::SpliceRegionBorders( Self *region )
334 {
335   // Do the actual union of the borders
336
337   // Copy the container into a temp variable to avoid iterator confusion
338   RegionBorderVectorType thisRegionBorder = m_RegionBorderVector;
339
340   // clear output
341   m_RegionBorderVector.resize( 0 );
342
343   // Initialize the region iterators
344
345   RegionBorderVectorConstIterator
346     thisRegionBordersIt    = thisRegionBorder.begin();
347   RegionBorderVectorConstIterator
348     endOfThisRegionBorders = thisRegionBorder.end();
349
350   RegionBorderVectorConstIterator
351     thatRegionBordersIt    = region->GetRegionBorderConstItBegin();
352   RegionBorderVectorConstIterator
353 IND ****endOfThatRegionBorders = region->GetRegionBorderConstItEnd();
354
355   // Merge the two sets of region borders into the newly merged region
356
357   while ( ( thisRegionBordersIt != endOfThisRegionBorders ) &&
358 IND **********( thatRegionBordersIt != endOfThatRegionBorders ) )
359     {
360
361     // Ensure that there are no common borders in the new region
362     if( ( (*thisRegionBordersIt)->GetRegion1() ==
363 IND **********(*thisRegionBordersIt)->GetRegion2() ) ||
364 IND ********( (*thatRegionBordersIt)->GetRegion1() ==
365 IND **********(*thatRegionBordersIt)->GetRegion2() ) ||
366 IND ********( (*thisRegionBordersIt) == (*thatRegionBordersIt) ) )
367       {
368       itkExceptionMacro( << "Invalid region border list" );
369       }
370
371     // The two borders point to the same regions: DUPLICATE case.
372     // They must be merged into one border, and any reference to
373     // to the duplicate border must be purged.
374
375     if ( ( (*thisRegionBordersIt)->GetRegion1() ==
376 IND ***********(*thatRegionBordersIt)->GetRegion1() ) &&
377 IND *********( (*thisRegionBordersIt)->GetRegion2() ==
378 IND ***********(*thatRegionBordersIt)->GetRegion2() ) )
379       {
380
381       // Add the lengths of the borders
382       double newLength = (*thatRegionBordersIt)->GetBorderLength() +
383 IND *************************(*thisRegionBordersIt)->GetBorderLength();
384
385       (*thisRegionBordersIt)->SetBorderLength( newLength );
386
387       m_RegionBorderVector.push_back( *thisRegionBordersIt );
388
389       // The border's region1 is the neighbor
390       if ( (*thatRegionBordersIt)->GetRegion2() == this )
391         {
392         (*thatRegionBordersIt)->GetRegion1()
393 IND **********->DeleteRegionBorder( (*thatRegionBordersIt) );
394
395         } // end if (the border's region1 is the neighbor )
396
397       // The border's region2 is the neighbor
398       else if ( (*thatRegionBordersIt)->GetRegion1() == this )
399         {
400         (*thatRegionBordersIt)->GetRegion2()
401 IND **********->DeleteRegionBorder( *thatRegionBordersIt );
402
403         } // end else if (the border's region2 is the neighbor )
404
405       else
406         {
407         itkExceptionMacro( << "Invalid region border list" );
408         } // end else
409
410       // Nullify the duplicate border so it can be identified and removed.
411       (*thatRegionBordersIt)->SetRegion1( NULL );
412       (*thatRegionBordersIt)->SetRegion2( NULL );
413       (*thatRegionBordersIt)->SetLambda( -1.0 );
414
415       thisRegionBordersIt++;
416       thatRegionBordersIt++;
417
418 IND ****} // end if loop for case when two borders point to same region
419
420     // This neighbor region label is less then that neighbor region label
421     else if ( (   (*thisRegionBordersIt)->GetRegion1()->GetRegionLabel() <
422                   (*thatRegionBordersIt)->GetRegion1()->GetRegionLabel() ) ||
423
424 IND **************( ( (*thisRegionBordersIt)->GetRegion1()->GetRegionLabel() ==
425 IND ******************(*thatRegionBordersIt)->GetRegion1()->GetRegionLabel() ) &&
426
427 IND ****************( (*thisRegionBordersIt)->GetRegion2()->GetRegionLabel() <
428                   (*thatRegionBordersIt)->GetRegion2()->GetRegionLabel() ) ) )
429       {
430
431       m_RegionBorderVector.push_back( *thisRegionBordersIt );
432       thisRegionBordersIt++;
433
434       } // end else if
435
436     // That neighbor region label is less then this neighbor region label
437     else if ( (   (*thatRegionBordersIt)->GetRegion1()->GetRegionLabel() <
438                   (*thisRegionBordersIt)->GetRegion1()->GetRegionLabel() ) ||
439
440 IND **************( ( (*thatRegionBordersIt)->GetRegion1()->GetRegionLabel() ==
441 IND ******************(*thisRegionBordersIt)->GetRegion1()->GetRegionLabel() ) &&
442
443 IND ****************( (*thatRegionBordersIt)->GetRegion2()->GetRegionLabel() <
444                   (*thisRegionBordersIt)->GetRegion2()->GetRegionLabel() ) ) )
445       {
446
447       m_RegionBorderVector.push_back( *thatRegionBordersIt );
448       thatRegionBordersIt++;
449
450       } // end else if
451     else
452       {
453       itkExceptionMacro( << "Invalid region border" );
454       } // end else
455
456     } // end of while
457
458   // If any borders remain in thisRegionBordersIt, put them in the back
459   while ( thisRegionBordersIt != endOfThisRegionBorders )
460     {
461     m_RegionBorderVector.push_back( *thisRegionBordersIt );
462     thisRegionBordersIt++;
463     }
464
465   // If any borders remain in thatRegionBorders, put them to the back
466   while ( thatRegionBordersIt != endOfThatRegionBorders )
467     {
468     m_RegionBorderVector.push_back( *thatRegionBordersIt );
469     thatRegionBordersIt++;
470     }
471
472 // end SpliceRegionBorders
473
474
475 void
476 KLMSegmentationRegion
477 ::UpdateRegionBorderLambda()
478 {
479   // Check if the number of borders for this region is NULL
480   if( m_RegionBorderVector.empty() )
481     {
482     itkExceptionMacro(<< "The region border for computing Lambda is NULL" );
483     }
484
485   // Set up the iterator to loop through the region border vector
486   RegionBorderVectorIterator
487     regionBorderVectorIt    = m_RegionBorderVector.begin();
488   RegionBorderVectorIterator
489     regionBorderVectorItEnd = m_RegionBorderVector.end();
490
491   // Loop through the entire border list and update the Lambda values
492   while( regionBorderVectorIt != regionBorderVectorItEnd )
493     {
494     (*regionBorderVectorIt)->EvaluateLambda();
495     ++regionBorderVectorIt;
496     } // End while loop
497
498 // end UpdateRegionBorderLambda
499
500
501 void
502 KLMSegmentationRegion
503 ::DeleteAllRegionBorders()
504 {
505
506   m_RegionBorderVector.resize( 0 );
507
508 // end DeleteAllRegionBorders
509
510
511 KLMSegmentationRegion::RegionBorderVectorIterator
512 KLMSegmentationRegion
513 ::GetRegionBorderItBegin()
514 {
515
516   return m_RegionBorderVector.begin();
517
518 // end GetRegionBorderItBegin
519
520
521 KLMSegmentationRegion::RegionBorderVectorConstIterator
522 KLMSegmentationRegion
523 ::GetRegionBorderConstItBegin()
524 {
525
526   return m_RegionBorderVector.begin();
527
528 // end GetRegionBorderConstItBegin
529
530
531 KLMSegmentationRegion::RegionBorderVectorIterator
532 KLMSegmentationRegion
533 ::GetRegionBorderItEnd()
534 {
535
536   return m_RegionBorderVector.end();
537
538 // end GetRegionBorderItEnd
539
540
541 KLMSegmentationRegion::RegionBorderVectorConstIterator
542 KLMSegmentationRegion
543 ::GetRegionBorderConstItEnd()
544 {
545
546   return m_RegionBorderVector.end();
547
548 // end GetRegionBorderConstItEnd
549
550
551 int
552 KLMSegmentationRegion
553 ::GetRegionBorderSize() const
554 {
555
556   return m_RegionBorderVector.size();
557
558 // end GetRegionBorderSize
559
560
561 void
562 KLMSegmentationRegion
563 ::PrintRegionInfo()
564 {
565   int region1label;
566   int region2label;
567
568   // If there are border pointers print the results
569   RegionBorderVectorIterator tempVectorIt;
570
571   std::cout << "------------------------------" << std::endl;
572   std::cout << "Location   : " << this << std::endl;
573   std::cout << "Label      : " << (this->GetRegionLabel()) << std::endl;
574   std::cout << "Area       : " << (this->GetRegionArea()) << std::endl;
575   std::cout << "Mean       : " << (this->GetMeanRegionIntensity()) << std::endl;
576 LEN   std::cout << "Num Borders: " << static_cast<int>( m_RegionBorderVector.size() ) << std::endl;
577   std::cout << "++++++++++++++++++++++++++++++" << std::endl;
578
579   // If there are border pointers print the results
580   tempVectorIt = m_RegionBorderVector.begin();
581   for( unsigned int k = 0; k < m_RegionBorderVector.size(); k++ )
582     {
583     region1label = (*tempVectorIt)->GetRegion1()->GetRegionLabel();
584     region2label = (*tempVectorIt)->GetRegion2()->GetRegionLabel();
585
586     std::cout << "Border Ptr :" << (*tempVectorIt) << "( " <<
587       region1label << " - " << region2label << " )" << " Lambda = " <<
588       (*tempVectorIt)->GetLambda() << std::endl;
589
590     tempVectorIt++;
591     } // end for
592
593   std::cout << "------------------------------" << std::endl;
594
595 //end PrintRegionInfo
596
597 // namespace itk
598
599
600 #endif
601

Generated by KWStyle 1.0b on Tuesday January,17 at 02:14:18PM
© Kitware Inc.