Orfeo Toolbox  3.16
itkMergeLabelMapFilter.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Insight Segmentation & Registration Toolkit
4  Module: $RCSfile: itkMergeLabelMapFilter.txx,v $
5  Language: C++
6  Date: $Date: 2009-07-24 12:03:35 $
7  Version: $Revision: 1.2 $
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 #ifndef __itkMergeLabelMapFilter_txx
18 #define __itkMergeLabelMapFilter_txx
19 
20 #include "itkMergeLabelMapFilter.h"
21 #include "itkProgressReporter.h"
22 #include <deque>
23 
24 
25 namespace itk {
26 
27 template <class TImage>
30 {
31  this->m_Method = KEEP;
32 }
33 
34 template <class TImage>
35 void
38 {
39  // Allocate the output
40  this->AllocateOutputs();
41 
42  switch( this->m_Method )
43  {
44  case KEEP:
45  {
46  this->MergeWithKeep();
47  break;
48  }
49  case AGGREGATE:
50  {
51  this->MergeWithAggregate();
52  break;
53  }
54  case PACK:
55  {
56  this->MergeWithPack();
57  break;
58  }
59  case STRICT:
60  {
61  this->MergeWithStrict();
62  break;
63  }
64  default:
65  {
66  itkExceptionMacro(<< "No such method: " << this->m_Method );
67  }
68  }
69 }
70 
71 template <class TImage>
72 void
75 {
76 
77  ImageType * output = this->GetOutput();
78 
79  typedef std::deque< LabelObjectPointer > VectorType;
80  VectorType labelObjects;
81 
82  ProgressReporter progress( this, 0, 1 );
83 
84  for( unsigned int i=1; i < this->GetNumberOfInputs(); i++ )
85  {
86 
87  const LabelObjectContainerType & otherLabelObjects = this->GetInput(i)->GetLabelObjectContainer();
88  typename LabelObjectContainerType::const_iterator it2 = otherLabelObjects.begin();
89 
90  while( it2 != otherLabelObjects.end() )
91  {
92  const LabelObjectType * lo = it2->second;
93  LabelObjectPointer newLo = LabelObjectType::New();
94  newLo->CopyAllFrom( lo );
95 
96  if( ! output->HasLabel( newLo->GetLabel() ) )
97  {
98  // we can keep the label
99  output->AddLabelObject( newLo );
100  }
101  else
102  {
103  // store the label object to read it later with another label
104  labelObjects.push_back( newLo );
105  }
106 
107  // go to the next label
108  progress.CompletedPixel();
109  it2++;
110  }
111 
112  // add the other label objects, with a different label
113  typename VectorType::iterator it = labelObjects.begin();
114  while( it != labelObjects.end() )
115  {
116  output->PushLabelObject( *it );
117  it++;
118  }
119  }
120 }
121 
122 
123 template <class TImage>
124 void
127 {
128 
129  ImageType * output = this->GetOutput();
130 
131  ProgressReporter progress( this, 0, 1 );
132 
133  for( unsigned int i=1; i<this->GetNumberOfInputs(); i++ )
134  {
135  const LabelObjectContainerType & otherLabelObjects = this->GetInput(i)->GetLabelObjectContainer();
136  typename LabelObjectContainerType::const_iterator it2 = otherLabelObjects.begin();
137 
138  while( it2 != otherLabelObjects.end() )
139  {
140  const LabelObjectType * lo = it2->second;
141  LabelObjectPointer newLo = LabelObjectType::New();
142  newLo->CopyAllFrom( lo );
143 
144  if( ! output->HasLabel( newLo->GetLabel() ) )
145  {
146  // we can keep the label
147  output->AddLabelObject( newLo );
148  }
149  else
150  {
151  itkExceptionMacro(<< "Label "
152  << static_cast< typename itk::NumericTraits< PixelType >::PrintType >( newLo->GetLabel() )
153  << " from input " << i
154  << " is already in use.");
155  }
156 
157  // go to the next label
158  progress.CompletedPixel();
159  it2++;
160  }
161  }
162 }
163 
164 
165 template <class TImage>
166 void
169 {
170 
171  ImageType * output = this->GetOutput();
172 
173  ProgressReporter progress( this, 0, 1 );
174 
175  for( unsigned int i=1; i<this->GetNumberOfInputs(); i++ )
176  {
177  const LabelObjectContainerType & otherLabelObjects = this->GetInput(i)->GetLabelObjectContainer();
178 
179  typename LabelObjectContainerType::const_iterator it2 = otherLabelObjects.begin();
180 
181  while( it2 != otherLabelObjects.end() )
182  {
183  const LabelObjectType * lo = it2->second;
184 
185  if( ! output->HasLabel( lo->GetLabel() ) )
186  {
187  // we can keep the label
188  LabelObjectPointer newLo = LabelObjectType::New();
189  newLo->CopyAllFrom( lo );
190  output->AddLabelObject( newLo );
191  }
192  else
193  {
194  if( lo->GetLabel() != output->GetBackgroundValue() )
195  {
196  // add the lines of that object to the one already in the output
197  LabelObjectType * mainLo = output->GetLabelObject( lo->GetLabel() );
198  const LineContainerType & lineContainer = lo->GetLineContainer();
199 
200  LineContainerIterator lit = lineContainer.begin();
201  while( lit != lineContainer.end() )
202  {
203  mainLo->AddLine( *lit );
204  lit++;
205  }
206 
207  // be sure to have the lines well organized
208  mainLo->Optimize();
209  }
210  }
211 
212  // go to the next label
213  progress.CompletedPixel();
214  it2++;
215  }
216  }
217 }
218 
219 
220 template <class TImage>
221 void
224 {
225 
226  ProgressReporter progress( this, 0, 1 );
227 
228  ImageType * output = this->GetOutput();
229 
230  // get the label objects of the first input
231  LabelObjectContainerType labelObjects = output->GetLabelObjectContainer();
232 
233  // and put back the objects in the map
234  output->ClearLabels();
235 
236  typename LabelObjectContainerType::iterator it = labelObjects.begin();
237 
238  while( it != labelObjects.end() )
239  {
240  output->PushLabelObject( it->second );
241 
242  // go to the next label
243  progress.CompletedPixel();
244  it++;
245  }
246 
247  // now, the next images
248  for( unsigned int i=1; i<this->GetNumberOfInputs(); i++ )
249  {
250  const LabelObjectContainerType & otherLabelObjects = this->GetInput(i)->GetLabelObjectContainer();
251  typename LabelObjectContainerType::const_iterator it2 = otherLabelObjects.begin();
252 
253  while( it2 != otherLabelObjects.end() )
254  {
255  const LabelObjectType * lo = it2->second;
256  LabelObjectPointer newLo = LabelObjectType::New();
257  newLo->CopyAllFrom( lo );
258  output->PushLabelObject( newLo );
259 
260  // go to the next label
261  progress.CompletedPixel();
262  it2++;
263  }
264  }
265 }
266 
267 
268 template <class TImage>
269 void
271 ::PrintSelf(std::ostream& os, Indent indent) const
272 {
273  Superclass::PrintSelf(os,indent);
274 
275  os << indent << "Method: " << this->m_Method << std::endl;
276 }
277 
278 }// end namespace itk
279 #endif

Generated at Sat May 18 2013 23:53:34 for Orfeo Toolbox with doxygen 1.8.3.1