Orfeo Toolbox  3.16
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 const_cast<ListSampleType*>(this->GetListSampleObject()->Get());
103 }
104 
105 template <class TInputImage, class TVectorData, class TFunctionType, class TListSample, class TLabelListSample>
109 {
110  return dynamic_cast<ListSampleObjectType*>( this->itk::ProcessObject::GetOutput(1) );
111 }
112 
113 template <class TInputImage, class TVectorData, class TFunctionType, class TListSample, class TLabelListSample>
117 {
118  return const_cast<LabelListSampleType*>(this->GetLabelListSampleObject()->Get());
119 }
120 
121 template <class TInputImage, class TVectorData, class TFunctionType, class TListSample, class TLabelListSample>
125 {
126  return dynamic_cast<LabelListSampleObjectType*>(this->itk::ProcessObject::GetOutput(2));
127 }
128 
129 template <class TInputImage, class TVectorData, class TFunctionType, class TListSample, class TLabelListSample>
133 {
134  return this->GetSamplesPositionsObject()->Get();
135 }
136 
137 template <class TInputImage, class TVectorData, class TFunctionType, class TListSample, class TLabelListSample>
141 {
142  return dynamic_cast<SamplesPositionObjectType*>(this->itk::ProcessObject::GetOutput(3));
143 }
144 
145 template <class TInputImage, class TVectorData, class TFunctionType, class TListSample, class TLabelListSample>
148 ::MakeOutput(unsigned int idx)
149 {
151  switch (idx)
152  {
153  case 0:
154  output = static_cast<itk::DataObject*>(InputImageType::New().GetPointer());
155  break;
156  case 1:
157  {
158  ListSampleObjectPointerType listSample = ListSampleObjectType::New();
159  listSample->Set(ListSampleType::New());
160  output = static_cast<itk::DataObject*>(listSample.GetPointer());
161  break;
162  }
163  case 2:
164  {
165  LabelListSampleObjectPointerType labelListSample = LabelListSampleObjectType::New();
166  labelListSample->Set(LabelListSampleType::New());
167  output = static_cast<itk::DataObject*>(labelListSample.GetPointer());
168  break;
169  }
170  case 3:
171  {
172  SamplesPositionObjectPointerType samplesPositions = SamplesPositionObjectType::New();
173  output = static_cast<itk::DataObject*>(samplesPositions.GetPointer());
174  break;
175  }
176  default:
177  output = static_cast<itk::DataObject*>(InputImageType::New().GetPointer());
178  break;
179  }
180  return output;
181 }
182 
183 template <class TInputImage, class TVectorData, class TFunctionType, class TListSample, class TLabelListSample>
184 void
187 {
188  m_ThreadListSample = ListSampleArray(this->GetNumberOfThreads());
189  for (int i = 0; i < this->GetNumberOfThreads(); ++i)
190  {
191  m_ThreadListSample[i] = ListSampleType::New();
192  }
193 
194  m_ThreadLabelListSample = LabelListSampleArray(this->GetNumberOfThreads());
195  for (int i = 0; i < this->GetNumberOfThreads(); ++i)
196  {
197  m_ThreadLabelListSample[i] = LabelListSampleType::New();
198  }
199 
200  m_ThreadSamplesPosition = SamplesPositionArray(this->GetNumberOfThreads());
201 
202  this->GetListSample()->Clear();
203  this->GetLabelListSample()->Clear();
204  this->GetSamplesPositions().clear();
205 }
206 
207 template <class TInputImage, class TVectorData, class TFunctionType, class TListSample, class TLabelListSample>
208 void
211 {
212  // Merge the ListSample from all the threads
213 
214  ListSampleType* listSample = this->GetListSample();
215  LabelListSampleType* labelListSample = this->GetLabelListSample();
216  SamplesPositionType& samplesPosition = this->GetSamplesPositions();
218 
219  // Copy the first thread elements into lists
220  if( this->GetNumberOfThreads() > 1 )
221  {
222  ListSampleType* threadListSample = m_ThreadListSample[0];
223  LabelListSampleType* threadLabelListSample = m_ThreadLabelListSample[0];
224  SamplesPositionType& threadSamplesPosition = m_ThreadSamplesPosition[0];
225 
226  for (unsigned int i = 0; i < threadListSample->Size(); ++i)
227  {
228  listSample->PushBack( threadListSample->GetMeasurementVector(i) );
229  labelListSample->PushBack( threadLabelListSample->GetMeasurementVector(i) );
230  samplesPosition.push_back( threadSamplesPosition[i] );
231  }
232  }
233 
234  // Add the other thread element checking if the point dosn't already exist
235  for (int threadId = 1; threadId < this->GetNumberOfThreads(); ++threadId )
236  {
237  ListSampleType* threadListSample = m_ThreadListSample[threadId];
238  LabelListSampleType* threadLabelListSample = m_ThreadLabelListSample[threadId];
239  SamplesPositionType& threadSamplesPosition = m_ThreadSamplesPosition[threadId];
240 
241  for (unsigned int i = 0; i < threadListSample->Size(); ++i)
242  {
243  const DescriptorsFunctionPointType & curSamplesPosition = threadSamplesPosition[i];
244  if( std::find(samplesPosition.begin(), samplesPosition.end(), curSamplesPosition) == samplesPosition.end() )
245  {
246  listSample->PushBack( threadListSample->GetMeasurementVector(i) );
247  labelListSample->PushBack( threadLabelListSample->GetMeasurementVector(i) );
248  samplesPosition.push_back( curSamplesPosition );
249  }
250  }
251  }
252 }
253 
254 template <class TInputImage, class TVectorData, class TFunctionType, class TListSample, class TLabelListSample>
255 void
257 ::PrintSelf(std::ostream& os, itk::Indent indent) const
258 {
259  Superclass::PrintSelf(os, indent);
260 }
261 
262 template <class TInputImage, class TVectorData, class TFunctionType, class TListSample, class TLabelListSample>
263 void
266 {
267  Superclass::GenerateInputRequestedRegion();
268 
269  // get pointers to the input and output
270  typename Superclass::InputImagePointer inputPtr =
271  const_cast< TInputImage * >( this->GetInput() );
272  typename Superclass::OutputImagePointer outputPtr = this->GetOutput();
273 
274  if ( !inputPtr || !outputPtr )
275  {
276  return;
277  }
278 
279  // get a copy of the input requested region (should equal the output
280  // requested region)
281  typename TInputImage::RegionType inputRequestedRegion;
282  inputRequestedRegion = inputPtr->GetRequestedRegion();
283 
284  // pad the input requested region by the operator radius
285  inputRequestedRegion.PadByRadius( m_NeighborhoodRadius + 5 );
286 
287  // crop the input requested region at the input's largest possible region
288  if ( inputRequestedRegion.Crop(inputPtr->GetLargestPossibleRegion()) )
289  {
290  inputPtr->SetRequestedRegion( inputRequestedRegion );
291  return;
292  }
293  else
294  {
295  // Couldn't crop the region (requested region is outside the largest
296  // possible region). Throw an exception.
297 
298  // store what we tried to request (prior to trying to crop)
299  inputPtr->SetRequestedRegion( inputRequestedRegion );
300  }
301 }
302 
303 template <class TInputImage, class TVectorData, class TFunctionType, class TListSample, class TLabelListSample>
304 void
307 {}
308 
309 
310 template <class TInputImage, class TVectorData, class TFunctionType, class TListSample, class TLabelListSample>
311 void
313 ::ThreadedGenerateData(const RegionType& outputRegionForThread,
314  int threadId)
315 {
316  ListSampleType* listSample = m_ThreadListSample[threadId];
317  LabelListSampleType* labelListSample = m_ThreadLabelListSample[threadId];
318  SamplesPositionType& samplesPosition = m_ThreadSamplesPosition[threadId];
319 
320  VectorDataTreeIteratorType vectorDataIt(this->GetSamplesLocations()->GetDataTree());
321 
322  for (vectorDataIt.GoToBegin(); !vectorDataIt.IsAtEnd(); ++vectorDataIt)
323  {
324  if (vectorDataIt.Get()->IsPointFeature())
325  {
326  VectorDataPointType point = vectorDataIt.Get()->GetPoint();
327  ContinuousIndexType cidx;
328  this->GetInput()->TransformPhysicalPointToContinuousIndex(point, cidx);
329 
330  // OGR convention : vector data are recorded with a 0.5 shift
331  cidx[0] -= 0.5;
332  cidx[1] -= 0.5;
333 
334  RegionType paddedRegion = outputRegionForThread;
335  paddedRegion.PadByRadius(m_NeighborhoodRadius);
336  if (this->IsInsideWithNeighborhoodRadius(paddedRegion, cidx))
337  {
338  SampleMeasurementVectorType sample(m_DescriptorsFunction->Evaluate(point));
339  listSample->PushBack( sample );
340 
342  label[0] = static_cast<LabelMeasurementType>(vectorDataIt.Get()->GetFieldAsInt("Class"));
343  labelListSample->PushBack( label );
344 
345  samplesPosition.push_back(point);
346  }
347  }
348  }
349 }
350 
351 
352 template <class TInputImage, class TVectorData, class TListSample, class TLabelListSample, class TOutputPrecision, class TCoordRep>
355 {
356 
357 }
358 
359 template <class TInputImage, class TVectorData, class TListSample, class TLabelListSample, class TOutputPrecision, class TCoordRep>
362 {
363 
364 }
365 
366 
367 } // end namespace otb
368 
369 #endif

Generated at Sun Feb 3 2013 00:20:32 for Orfeo Toolbox with doxygen 1.8.1.1