File: otbDescriptorsListSampleGenerator.txx

package info (click to toggle)
otb 5.8.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 38,496 kB
  • ctags: 40,282
  • sloc: cpp: 306,573; ansic: 3,575; python: 450; sh: 214; perl: 74; java: 72; makefile: 70
file content (362 lines) | stat: -rw-r--r-- 14,775 bytes parent folder | download
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
/*=========================================================================

  Program:   ORFEO Toolbox
  Language:  C++
  Date:      $Date$
  Version:   $Revision$


  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
  See OTBCopyright.txt 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.

=========================================================================*/
#ifndef otbDescriptorsListSampleGenerator_txx
#define otbDescriptorsListSampleGenerator_txx

#include "otbDescriptorsListSampleGenerator.h"

#include "itkContinuousIndex.h"

namespace otb
{

template <class TInputImage, class TVectorData, class TFunctionType, class TListSample, class TLabelListSample>
PersistentDescriptorsListSampleGenerator<TInputImage, TVectorData, TFunctionType, TListSample, TLabelListSample>
::PersistentDescriptorsListSampleGenerator()
  : m_NeighborhoodRadius(0)
{
  // Need 2 inputs : a vector image and a vectordata
  this->SetNumberOfRequiredInputs(2);

  // Have 4 outputs : the image created by Superclass,
  // the sample list, the label sample list, the positions of the samples
  this->SetNumberOfRequiredOutputs(3);
  this->itk::ProcessObject::SetNthOutput(1, this->MakeOutput(1).GetPointer());
  this->itk::ProcessObject::SetNthOutput(2, this->MakeOutput(2).GetPointer());
  this->itk::ProcessObject::SetNthOutput(3, this->MakeOutput(3).GetPointer());
}

template <class TInputImage, class TVectorData, class TFunctionType, class TListSample, class TLabelListSample>
PersistentDescriptorsListSampleGenerator<TInputImage, TVectorData, TFunctionType, TListSample, TLabelListSample>
::~PersistentDescriptorsListSampleGenerator()
{
}

template <class TInputImage, class TVectorData, class TFunctionType, class TListSample, class TLabelListSample>
void
PersistentDescriptorsListSampleGenerator<TInputImage, TVectorData, TFunctionType, TListSample, TLabelListSample>
::AllocateOutputs()
{
  // This is commented to prevent the streaming of the whole image for the first stream strip
  // It shall not cause any problem because the output image of this filter is not intended to be used.
  //InputImagePointer image = const_cast< TInputImage * >( this->GetInput() );
  //this->GraftOutput( image );
  // Nothing that needs to be allocated for the remaining outputs
}

template <class TInputImage, class TVectorData, class TFunctionType, class TListSample, class TLabelListSample>
void
PersistentDescriptorsListSampleGenerator<TInputImage, TVectorData, TFunctionType, TListSample, TLabelListSample>
::GenerateOutputInformation()
{
  Superclass::GenerateOutputInformation();
  if (this->GetInput())
    {
    this->GetOutput()->CopyInformation(this->GetInput());
    this->GetOutput()->SetLargestPossibleRegion(this->GetInput()->GetLargestPossibleRegion());

    if (this->GetOutput()->GetRequestedRegion().GetNumberOfPixels() == 0)
      {
      this->GetOutput()->SetRequestedRegion(this->GetOutput()->GetLargestPossibleRegion());
      }
    }
}

template <class TInputImage, class TVectorData, class TFunctionType, class TListSample, class TLabelListSample>
void
PersistentDescriptorsListSampleGenerator<TInputImage, TVectorData, TFunctionType, TListSample, TLabelListSample>
::SetSamplesLocations(VectorDataType* location)
{
  this->SetNthInput(1, location);
}

template <class TInputImage, class TVectorData, class TFunctionType, class TListSample, class TLabelListSample>
typename PersistentDescriptorsListSampleGenerator<TInputImage, TVectorData, TFunctionType, TListSample, TLabelListSample>::VectorDataType*
PersistentDescriptorsListSampleGenerator<TInputImage, TVectorData, TFunctionType, TListSample, TLabelListSample>
::GetSamplesLocations()
{

  return static_cast<VectorDataType*>(this->itk::ProcessObject::GetInput(1));
}

template <class TInputImage, class TVectorData, class TFunctionType, class TListSample, class TLabelListSample>
typename PersistentDescriptorsListSampleGenerator<TInputImage, TVectorData, TFunctionType, TListSample, TLabelListSample>::ListSampleType*
PersistentDescriptorsListSampleGenerator<TInputImage, TVectorData, TFunctionType, TListSample, TLabelListSample>
::GetListSample()
{
  return dynamic_cast<ListSampleType*>(this->itk::ProcessObject::GetOutput(1));
}

template <class TInputImage, class TVectorData, class TFunctionType, class TListSample, class TLabelListSample>
typename PersistentDescriptorsListSampleGenerator<TInputImage, TVectorData, TFunctionType, TListSample, TLabelListSample>::LabelListSampleType*
PersistentDescriptorsListSampleGenerator<TInputImage, TVectorData, TFunctionType, TListSample, TLabelListSample>
::GetLabelListSample()
{
  return dynamic_cast<LabelListSampleType*>( this->itk::ProcessObject::GetOutput(2) );
}


template <class TInputImage, class TVectorData, class TFunctionType, class TListSample, class TLabelListSample>
typename PersistentDescriptorsListSampleGenerator<TInputImage, TVectorData, TFunctionType, TListSample, TLabelListSample>::SamplesPositionType&
PersistentDescriptorsListSampleGenerator<TInputImage, TVectorData, TFunctionType, TListSample, TLabelListSample>
::GetSamplesPositions()
{
  return this->GetSamplesPositionsObject()->Get();
}

template <class TInputImage, class TVectorData, class TFunctionType, class TListSample, class TLabelListSample>
typename PersistentDescriptorsListSampleGenerator<TInputImage, TVectorData, TFunctionType, TListSample, TLabelListSample>::SamplesPositionObjectType*
PersistentDescriptorsListSampleGenerator<TInputImage, TVectorData, TFunctionType, TListSample, TLabelListSample>
::GetSamplesPositionsObject()
{
  return dynamic_cast<SamplesPositionObjectType*>(this->itk::ProcessObject::GetOutput(3));
}

template <class TInputImage, class TVectorData, class TFunctionType, class TListSample, class TLabelListSample>
itk::DataObject::Pointer
PersistentDescriptorsListSampleGenerator<TInputImage, TVectorData, TFunctionType, TListSample, TLabelListSample>
::MakeOutput(DataObjectPointerArraySizeType idx)
{
  itk::DataObject::Pointer output;
  switch (idx)
    {
    case 0:
      output = static_cast<itk::DataObject*>(InputImageType::New().GetPointer());
      break;
    case 1:
      {
      ListSamplePointerType listSample = ListSampleType::New();
      output = static_cast<itk::DataObject*>(listSample.GetPointer());
      break;
      }
    case 2:
      {
      LabelListSamplePointerType labelListSample = LabelListSampleType::New();
      output = static_cast<itk::DataObject*>(labelListSample.GetPointer());
      break;
      }
    case 3:
      {
      SamplesPositionObjectPointerType samplesPositions = SamplesPositionObjectType::New();
      output = static_cast<itk::DataObject*>(samplesPositions.GetPointer());
      break;
      }
    default:
      output = static_cast<itk::DataObject*>(InputImageType::New().GetPointer());
      break;
    }
  return output;
}

template <class TInputImage, class TVectorData, class TFunctionType, class TListSample, class TLabelListSample>
void
PersistentDescriptorsListSampleGenerator<TInputImage, TVectorData, TFunctionType, TListSample, TLabelListSample>
::Reset()
{
  m_ThreadListSample = ListSampleArray(this->GetNumberOfThreads());
  for (unsigned int i = 0; i < this->GetNumberOfThreads(); ++i)
    {
    m_ThreadListSample[i] = ListSampleType::New();
    }

  m_ThreadLabelListSample = LabelListSampleArray(this->GetNumberOfThreads());
  for (unsigned int i = 0; i < this->GetNumberOfThreads(); ++i)
    {
    m_ThreadLabelListSample[i] = LabelListSampleType::New();
    }

  m_ThreadSamplesPosition = SamplesPositionArray(this->GetNumberOfThreads());

  this->GetListSample()->Clear();
  this->GetLabelListSample()->Clear();
  this->GetSamplesPositions().clear();
}

template <class TInputImage, class TVectorData, class TFunctionType, class TListSample, class TLabelListSample>
void
PersistentDescriptorsListSampleGenerator<TInputImage, TVectorData, TFunctionType, TListSample, TLabelListSample>
::Synthetize()
{
  // Merge the ListSample from all the threads
  ListSampleType* listSample = this->GetListSample();
  LabelListSampleType* labelListSample = this->GetLabelListSample();
  SamplesPositionType& samplesPosition = this->GetSamplesPositions();
  VectorDataPointType ref;

  // Get the output list measurement vector sample sizes once
  listSample->SetMeasurementVectorSize(m_ThreadListSample[0]->GetMeasurementVectorSize());
  labelListSample->SetMeasurementVectorSize(m_ThreadLabelListSample[0]->GetMeasurementVectorSize());

  // Copy the first thread elements into lists
  if( this->GetNumberOfThreads() > 1 )
    {
      ListSampleType* threadListSample = m_ThreadListSample[0];
      LabelListSampleType* threadLabelListSample = m_ThreadLabelListSample[0];
      SamplesPositionType& threadSamplesPosition = m_ThreadSamplesPosition[0];

      for (unsigned int i = 0; i < threadListSample->Size(); ++i)
      {
        listSample->PushBack( threadListSample->GetMeasurementVector(i) );
        labelListSample->PushBack( threadLabelListSample->GetMeasurementVector(i) );
        samplesPosition.push_back( threadSamplesPosition[i] );
      }
    }

  // Add the other thread element checking if the point dosn't already exist
  for (itk::ThreadIdType threadId = 1; threadId < this->GetNumberOfThreads(); ++threadId )
    {
    ListSampleType* threadListSample = m_ThreadListSample[threadId];
    LabelListSampleType* threadLabelListSample = m_ThreadLabelListSample[threadId];
    SamplesPositionType& threadSamplesPosition = m_ThreadSamplesPosition[threadId];

    for (unsigned int i = 0; i < threadListSample->Size(); ++i)
      {
        const DescriptorsFunctionPointType & curSamplesPosition = threadSamplesPosition[i];
        if( std::find(samplesPosition.begin(), samplesPosition.end(), curSamplesPosition) == samplesPosition.end() )
          {
            listSample->PushBack( threadListSample->GetMeasurementVector(i) );
            labelListSample->PushBack( threadLabelListSample->GetMeasurementVector(i) );
            samplesPosition.push_back( curSamplesPosition );
          }
      }
    }
}

template <class TInputImage, class TVectorData, class TFunctionType, class TListSample, class TLabelListSample>
void
PersistentDescriptorsListSampleGenerator<TInputImage, TVectorData, TFunctionType, TListSample, TLabelListSample>
::PrintSelf(std::ostream& os, itk::Indent indent) const
{
  Superclass::PrintSelf(os, indent);
}

template <class TInputImage, class TVectorData, class TFunctionType, class TListSample, class TLabelListSample>
void
PersistentDescriptorsListSampleGenerator<TInputImage, TVectorData, TFunctionType, TListSample, TLabelListSample>
::GenerateInputRequestedRegion()
{
  Superclass::GenerateInputRequestedRegion();

  // get pointers to the input and output
  typename Superclass::InputImagePointer inputPtr =
    const_cast< TInputImage * >( this->GetInput() );
  typename Superclass::OutputImagePointer outputPtr = this->GetOutput();

  if ( !inputPtr || !outputPtr )
    {
    return;
    }

  // get a copy of the input requested region (should equal the output
  // requested region)
  typename TInputImage::RegionType inputRequestedRegion;
  inputRequestedRegion = inputPtr->GetRequestedRegion();

  // pad the input requested region by the operator radius
  inputRequestedRegion.PadByRadius( m_NeighborhoodRadius + 5 );

  // crop the input requested region at the input's largest possible region
  if ( inputRequestedRegion.Crop(inputPtr->GetLargestPossibleRegion()) )
    {
    inputPtr->SetRequestedRegion( inputRequestedRegion );
    return;
    }
  else
    {
    // Couldn't crop the region (requested region is outside the largest
    // possible region).  Throw an exception.

    // store what we tried to request (prior to trying to crop)
    inputPtr->SetRequestedRegion( inputRequestedRegion );
    }
}

template <class TInputImage, class TVectorData, class TFunctionType, class TListSample, class TLabelListSample>
void
PersistentDescriptorsListSampleGenerator<TInputImage, TVectorData, TFunctionType, TListSample, TLabelListSample>
::BeforeThreadedGenerateData()
{}


template <class TInputImage, class TVectorData, class TFunctionType, class TListSample, class TLabelListSample>
void
PersistentDescriptorsListSampleGenerator<TInputImage, TVectorData, TFunctionType, TListSample, TLabelListSample>
::ThreadedGenerateData(const RegionType&  outputRegionForThread,
                       itk::ThreadIdType threadId)
{
  ListSampleType* listSample = m_ThreadListSample[threadId];
  LabelListSampleType* labelListSample = m_ThreadLabelListSample[threadId];
  SamplesPositionType& samplesPosition = m_ThreadSamplesPosition[threadId];

  VectorDataTreeIteratorType vectorDataIt(this->GetSamplesLocations()->GetDataTree());
  bool isInitialized = false;

  for (vectorDataIt.GoToBegin(); !vectorDataIt.IsAtEnd(); ++vectorDataIt)
    {
    if (vectorDataIt.Get()->IsPointFeature())
      {
      VectorDataPointType point = vectorDataIt.Get()->GetPoint();
      ContinuousIndexType cidx;
      this->GetInput()->TransformPhysicalPointToContinuousIndex(point, cidx);

      RegionType paddedRegion = outputRegionForThread;
      paddedRegion.PadByRadius(m_NeighborhoodRadius);
      if (this->IsInsideWithNeighborhoodRadius(paddedRegion, cidx))
        {
        // Set the Measurement Vector Size of the samplelists, once
        if(!isInitialized)
          {
          // output list sample
          listSample->SetMeasurementVectorSize(m_DescriptorsFunction->Evaluate(point).Size());
          labelListSample->SetMeasurementVectorSize(LabelMeasurementVectorType::Length);
          isInitialized = true;
          }

        SampleMeasurementVectorType sample(m_DescriptorsFunction->Evaluate(point));
        listSample->PushBack( sample );

        LabelMeasurementVectorType label;
        label[0] = static_cast<LabelMeasurementType>(vectorDataIt.Get()->GetFieldAsInt("Class"));
        labelListSample->PushBack( label );

        samplesPosition.push_back(point);
        }
      }
    }
}


template <class TInputImage, class TVectorData, class TListSample, class TLabelListSample, class TOutputPrecision, class TCoordRep>
DescriptorsListSampleGenerator<TInputImage, TVectorData, TListSample, TLabelListSample, TOutputPrecision, TCoordRep>
::DescriptorsListSampleGenerator()
{


}

template <class TInputImage, class TVectorData, class TListSample, class TLabelListSample, class TOutputPrecision, class TCoordRep>
DescriptorsListSampleGenerator<TInputImage, TVectorData, TListSample, TLabelListSample, TOutputPrecision, TCoordRep>
::~DescriptorsListSampleGenerator()
{

}


} // end namespace otb

#endif