Orfeo Toolbox  4.0
otbDescriptorsListSampleGenerator.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: ORFEO Toolbox
4  Language: C++
5  Date: $Date$
6  Version: $Revision$
7 
8 
9  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
10  See OTBCopyright.txt for details.
11 
12 
13  This software is distributed WITHOUT ANY WARRANTY; without even
14  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  PURPOSE. See the above copyright notices for more information.
16 
17 =========================================================================*/
18 #ifndef __otbDescriptorsListSampleGenerator_txx
19 #define __otbDescriptorsListSampleGenerator_txx
20 
22 
23 #include "itkContinuousIndex.h"
24 
25 namespace otb
26 {
27 
28 template <class TInputImage, class TVectorData, class TFunctionType, class TListSample, class TLabelListSample>
31  : m_NeighborhoodRadius(0)
32 {
33  // Need 2 inputs : a vector image and a vectordata
34  this->SetNumberOfRequiredInputs(2);
35 
36  // Have 4 outputs : the image created by Superclass,
37  // the sample list, the label sample list, the positions of the samples
38  this->SetNumberOfRequiredOutputs(3);
39  this->itk::ProcessObject::SetNthOutput(1, this->MakeOutput(1).GetPointer());
40  this->itk::ProcessObject::SetNthOutput(2, this->MakeOutput(2).GetPointer());
41  this->itk::ProcessObject::SetNthOutput(3, this->MakeOutput(3).GetPointer());
42 }
43 
44 template <class TInputImage, class TVectorData, class TFunctionType, class TListSample, class TLabelListSample>
47 {
48 }
49 
50 template <class TInputImage, class TVectorData, class TFunctionType, class TListSample, class TLabelListSample>
51 void
54 {
55  // This is commented to prevent the streaming of the whole image for the first stream strip
56  // It shall not cause any problem because the output image of this filter is not intended to be used.
57  //InputImagePointer image = const_cast< TInputImage * >( this->GetInput() );
58  //this->GraftOutput( image );
59  // Nothing that needs to be allocated for the remaining outputs
60 }
61 
62 template <class TInputImage, class TVectorData, class TFunctionType, class TListSample, class TLabelListSample>
63 void
66 {
67  Superclass::GenerateOutputInformation();
68  if (this->GetInput())
69  {
70  this->GetOutput()->CopyInformation(this->GetInput());
71  this->GetOutput()->SetLargestPossibleRegion(this->GetInput()->GetLargestPossibleRegion());
72 
73  if (this->GetOutput()->GetRequestedRegion().GetNumberOfPixels() == 0)
74  {
75  this->GetOutput()->SetRequestedRegion(this->GetOutput()->GetLargestPossibleRegion());
76  }
77  }
78 }
79 
80 template <class TInputImage, class TVectorData, class TFunctionType, class TListSample, class TLabelListSample>
81 void
84 {
85  this->SetNthInput(1, location);
86 }
87 
88 template <class TInputImage, class TVectorData, class TFunctionType, class TListSample, class TLabelListSample>
92 {
93 
94  return static_cast<VectorDataType*>(this->itk::ProcessObject::GetInput(1));
95 }
96 
97 template <class TInputImage, class TVectorData, class TFunctionType, class TListSample, class TLabelListSample>
101 {
102  return dynamic_cast<ListSampleType*>(this->itk::ProcessObject::GetOutput(1));
103 }
104 
105 template <class TInputImage, class TVectorData, class TFunctionType, class TListSample, class TLabelListSample>
109 {
110  return dynamic_cast<LabelListSampleType*>( this->itk::ProcessObject::GetOutput(2) );
111 }
112 
113 
114 template <class TInputImage, class TVectorData, class TFunctionType, class TListSample, class TLabelListSample>
118 {
119  return this->GetSamplesPositionsObject()->Get();
120 }
121 
122 template <class TInputImage, class TVectorData, class TFunctionType, class TListSample, class TLabelListSample>
126 {
127  return dynamic_cast<SamplesPositionObjectType*>(this->itk::ProcessObject::GetOutput(3));
128 }
129 
130 template <class TInputImage, class TVectorData, class TFunctionType, class TListSample, class TLabelListSample>
133 ::MakeOutput(unsigned int idx)
134 {
136  switch (idx)
137  {
138  case 0:
139  output = static_cast<itk::DataObject*>(InputImageType::New().GetPointer());
140  break;
141  case 1:
142  {
143  ListSamplePointerType listSample = ListSampleType::New();
144  output = static_cast<itk::DataObject*>(listSample.GetPointer());
145  break;
146  }
147  case 2:
148  {
149  LabelListSamplePointerType labelListSample = LabelListSampleType::New();
150  output = static_cast<itk::DataObject*>(labelListSample.GetPointer());
151  break;
152  }
153  case 3:
154  {
155  SamplesPositionObjectPointerType samplesPositions = SamplesPositionObjectType::New();
156  output = static_cast<itk::DataObject*>(samplesPositions.GetPointer());
157  break;
158  }
159  default:
160  output = static_cast<itk::DataObject*>(InputImageType::New().GetPointer());
161  break;
162  }
163  return output;
164 }
165 
166 template <class TInputImage, class TVectorData, class TFunctionType, class TListSample, class TLabelListSample>
167 void
170 {
171  m_ThreadListSample = ListSampleArray(this->GetNumberOfThreads());
172  for (unsigned int i = 0; i < this->GetNumberOfThreads(); ++i)
173  {
174  m_ThreadListSample[i] = ListSampleType::New();
175  }
176 
177  m_ThreadLabelListSample = LabelListSampleArray(this->GetNumberOfThreads());
178  for (unsigned int i = 0; i < this->GetNumberOfThreads(); ++i)
179  {
180  m_ThreadLabelListSample[i] = LabelListSampleType::New();
181  }
182 
183  m_ThreadSamplesPosition = SamplesPositionArray(this->GetNumberOfThreads());
184 
185  this->GetListSample()->Clear();
186  this->GetLabelListSample()->Clear();
187  this->GetSamplesPositions().clear();
188 }
189 
190 template <class TInputImage, class TVectorData, class TFunctionType, class TListSample, class TLabelListSample>
191 void
194 {
195  // Merge the ListSample from all the threads
196  ListSampleType* listSample = this->GetListSample();
197  LabelListSampleType* labelListSample = this->GetLabelListSample();
198  SamplesPositionType& samplesPosition = this->GetSamplesPositions();
200 
201  // Get the output list measurement vector sample sizes once
202  listSample->SetMeasurementVectorSize(m_ThreadListSample[0]->GetMeasurementVectorSize());
203  labelListSample->SetMeasurementVectorSize(m_ThreadLabelListSample[0]->GetMeasurementVectorSize());
204 
205  // Copy the first thread elements into lists
206  if( this->GetNumberOfThreads() > 1 )
207  {
208  ListSampleType* threadListSample = m_ThreadListSample[0];
209  LabelListSampleType* threadLabelListSample = m_ThreadLabelListSample[0];
210  SamplesPositionType& threadSamplesPosition = m_ThreadSamplesPosition[0];
211 
212  for (unsigned int i = 0; i < threadListSample->Size(); ++i)
213  {
214  listSample->PushBack( threadListSample->GetMeasurementVector(i) );
215  labelListSample->PushBack( threadLabelListSample->GetMeasurementVector(i) );
216  samplesPosition.push_back( threadSamplesPosition[i] );
217  }
218  }
219 
220  // Add the other thread element checking if the point dosn't already exist
221  for (itk::ThreadIdType threadId = 1; threadId < this->GetNumberOfThreads(); ++threadId )
222  {
223  ListSampleType* threadListSample = m_ThreadListSample[threadId];
224  LabelListSampleType* threadLabelListSample = m_ThreadLabelListSample[threadId];
225  SamplesPositionType& threadSamplesPosition = m_ThreadSamplesPosition[threadId];
226 
227  for (unsigned int i = 0; i < threadListSample->Size(); ++i)
228  {
229  const DescriptorsFunctionPointType & curSamplesPosition = threadSamplesPosition[i];
230  if( std::find(samplesPosition.begin(), samplesPosition.end(), curSamplesPosition) == samplesPosition.end() )
231  {
232  listSample->PushBack( threadListSample->GetMeasurementVector(i) );
233  labelListSample->PushBack( threadLabelListSample->GetMeasurementVector(i) );
234  samplesPosition.push_back( curSamplesPosition );
235  }
236  }
237  }
238 }
239 
240 template <class TInputImage, class TVectorData, class TFunctionType, class TListSample, class TLabelListSample>
241 void
243 ::PrintSelf(std::ostream& os, itk::Indent indent) const
244 {
245  Superclass::PrintSelf(os, indent);
246 }
247 
248 template <class TInputImage, class TVectorData, class TFunctionType, class TListSample, class TLabelListSample>
249 void
252 {
253  Superclass::GenerateInputRequestedRegion();
254 
255  // get pointers to the input and output
256  typename Superclass::InputImagePointer inputPtr =
257  const_cast< TInputImage * >( this->GetInput() );
258  typename Superclass::OutputImagePointer outputPtr = this->GetOutput();
259 
260  if ( !inputPtr || !outputPtr )
261  {
262  return;
263  }
264 
265  // get a copy of the input requested region (should equal the output
266  // requested region)
267  typename TInputImage::RegionType inputRequestedRegion;
268  inputRequestedRegion = inputPtr->GetRequestedRegion();
269 
270  // pad the input requested region by the operator radius
271  inputRequestedRegion.PadByRadius( m_NeighborhoodRadius + 5 );
272 
273  // crop the input requested region at the input's largest possible region
274  if ( inputRequestedRegion.Crop(inputPtr->GetLargestPossibleRegion()) )
275  {
276  inputPtr->SetRequestedRegion( inputRequestedRegion );
277  return;
278  }
279  else
280  {
281  // Couldn't crop the region (requested region is outside the largest
282  // possible region). Throw an exception.
283 
284  // store what we tried to request (prior to trying to crop)
285  inputPtr->SetRequestedRegion( inputRequestedRegion );
286  }
287 }
288 
289 template <class TInputImage, class TVectorData, class TFunctionType, class TListSample, class TLabelListSample>
290 void
293 {}
294 
295 
296 template <class TInputImage, class TVectorData, class TFunctionType, class TListSample, class TLabelListSample>
297 void
299 ::ThreadedGenerateData(const RegionType& outputRegionForThread,
300  itk::ThreadIdType threadId)
301 {
302  ListSampleType* listSample = m_ThreadListSample[threadId];
303  LabelListSampleType* labelListSample = m_ThreadLabelListSample[threadId];
304  SamplesPositionType& samplesPosition = m_ThreadSamplesPosition[threadId];
305 
306  VectorDataTreeIteratorType vectorDataIt(this->GetSamplesLocations()->GetDataTree());
307  bool isInitialized = false;
308 
309  for (vectorDataIt.GoToBegin(); !vectorDataIt.IsAtEnd(); ++vectorDataIt)
310  {
311  if (vectorDataIt.Get()->IsPointFeature())
312  {
313  VectorDataPointType point = vectorDataIt.Get()->GetPoint();
314  ContinuousIndexType cidx;
315  this->GetInput()->TransformPhysicalPointToContinuousIndex(point, cidx);
316 
317  // OGR convention : vector data are recorded with a 0.5 shift
318  cidx[0] -= 0.5;
319  cidx[1] -= 0.5;
320 
321  RegionType paddedRegion = outputRegionForThread;
322  paddedRegion.PadByRadius(m_NeighborhoodRadius);
323  if (this->IsInsideWithNeighborhoodRadius(paddedRegion, cidx))
324  {
325  // Set the Measurement Vector Size of the samplelists, once
326  if(!isInitialized)
327  {
328  // output list sample
329  listSample->SetMeasurementVectorSize(m_DescriptorsFunction->Evaluate(point).Size());
330  labelListSample->SetMeasurementVectorSize(LabelMeasurementVectorType::Length);
331  isInitialized = true;
332  }
333 
334  SampleMeasurementVectorType sample(m_DescriptorsFunction->Evaluate(point));
335  listSample->PushBack( sample );
336 
338  label[0] = static_cast<LabelMeasurementType>(vectorDataIt.Get()->GetFieldAsInt("Class"));
339  labelListSample->PushBack( label );
340 
341  samplesPosition.push_back(point);
342  }
343  }
344  }
345 }
346 
347 
348 template <class TInputImage, class TVectorData, class TListSample, class TLabelListSample, class TOutputPrecision, class TCoordRep>
351 {
352 
353 
354 }
355 
356 template <class TInputImage, class TVectorData, class TListSample, class TLabelListSample, class TOutputPrecision, class TCoordRep>
359 {
360 
361 }
362 
363 
364 } // end namespace otb
365 
366 #endif

Generated at Sat Mar 8 2014 15:53:43 for Orfeo Toolbox with doxygen 1.8.3.1