File: ParallelLineDetectionExample.cxx

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 (245 lines) | stat: -rw-r--r-- 6,595 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
/*=========================================================================

  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.

=========================================================================*/


//  Software Guide : BeginCommandLineArgs
//    OUTPUTS: {Lines.png}, {ParallelLines.png}
//    20 2 10
//  Software Guide : EndCommandLineArgs

// Software Guide : BeginLatex
//
// This example illustrates the details of the \doxygen{otb}{ParallelLinePathListFilter}.
//
//
// Software Guide : EndLatex

#include "itkPolyLineParametricPath.h"
#include "otbDrawPathListFilter.h"

#include "otbParallelLinePathListFilter.h"

#include "otbImage.h"
#include "otbImageFileWriter.h"

int main(int argc, char * argv[])
{
  if (argc != 6)
    {
    std::cerr << "Usage: " << std::endl;
    std::cerr << argv[0] << " outputImage ";
    std::cerr << " outputParallelLineImage ";
    std::cerr <<
    " distThreshParallel angThreshParallel commonDistThreshParallel" <<
    std::endl;
    return EXIT_FAILURE;
    }

  double distThreshParallel = atof(argv[3]);
  double angThreshParallel = atof(argv[4]);
  double commonDistThreshParallel = atof(argv[5]);

  //We start by creating an empty image
  const unsigned int Dimension = 2;
  typedef unsigned char PixelType;

  typedef otb::Image<PixelType, Dimension> ImageType;

  ImageType::Pointer image = ImageType::New();

  ImageType::IndexType start;

  start[0] =   0;
  start[1] =   0;

  ImageType::SizeType size;
  size[0]  = 600;
  size[1]  = 300;

  ImageType::RegionType region;

  region.SetSize(size);
  region.SetIndex(start);
  image->SetRegions(region);
  image->Allocate();
  image->FillBuffer(itk::NumericTraits<PixelType>::Zero);

  // We create some lines
  typedef itk::PolyLineParametricPath<Dimension> PathType;
  typedef otb::ObjectList<PathType>              PathListType;

  PathListType::Pointer lineList = PathListType::New();

  typedef PathType::ContinuousIndexType ContinuousIndexType;
  ContinuousIndexType cindex;

  /*-----*/
  PathType::Pointer aLine = PathType::New();
  aLine->Initialize();
  cindex[0] = 1;
  cindex[1] = 41;
  aLine->AddVertex(cindex);

  cindex[0] = 175;
  cindex[1] = 204;
  aLine->AddVertex(cindex);

  lineList->PushBack(aLine);

  /*-----*/
  aLine = PathType::New();
  aLine->Initialize();
  cindex[0] = 60;
  cindex[1] = 18;
  aLine->AddVertex(cindex);

  cindex[0] = 203;
  cindex[1] = 164;
  aLine->AddVertex(cindex);

  lineList->PushBack(aLine);

  /*-----*/
  aLine = PathType::New();
  aLine->Initialize();
  cindex[0] = 174;
  cindex[1] = 99;
  aLine->AddVertex(cindex);

  cindex[0] = 281;
  cindex[1] = 1;
  aLine->AddVertex(cindex);

  lineList->PushBack(aLine);

  /*-----*/
  aLine = PathType::New();
  aLine->Initialize();
  cindex[0] = 3;
  cindex[1] = 233;
  aLine->AddVertex(cindex);

  cindex[0] = 191;
  cindex[1] = 227;
  aLine->AddVertex(cindex);

  lineList->PushBack(aLine);

  /*-----*/
  aLine = PathType::New();
  aLine->Initialize();
  cindex[0] = 254;
  cindex[1] = 279;
  aLine->AddVertex(cindex);

  cindex[0] = 351;
  cindex[1] = 110;
  aLine->AddVertex(cindex);

  lineList->PushBack(aLine);

  /*-----*/
  aLine = PathType::New();
  aLine->Initialize();
  cindex[0] = 270;
  cindex[1] = 287;
  aLine->AddVertex(cindex);

  cindex[0] = 368;
  cindex[1] = 120;
  aLine->AddVertex(cindex);

  lineList->PushBack(aLine);

  /*-----*/
  aLine = PathType::New();
  aLine->Initialize();
  cindex[0] = 355;
  cindex[1] = 204;
  aLine->AddVertex(cindex);

  cindex[0] = 528;
  cindex[1] = 199;
  aLine->AddVertex(cindex);

  lineList->PushBack(aLine);

  /*-----*/
  aLine = PathType::New();
  aLine->Initialize();
  cindex[0] = 437;
  cindex[1] = 243;
  aLine->AddVertex(cindex);

  cindex[0] = 591;
  cindex[1] = 237;
  aLine->AddVertex(cindex);

  lineList->PushBack(aLine);

  // Polylines are drawn on a black
  typedef otb::DrawPathListFilter<ImageType, PathType, ImageType> DrawPathType;
  DrawPathType::Pointer drawPathListFilter = DrawPathType::New();
  drawPathListFilter->SetInput(image);
  drawPathListFilter->SetInputPath(lineList);
  drawPathListFilter->SetPathValue(itk::NumericTraits<PixelType>::max());

  typedef otb::ImageFileWriter<ImageType> WriterType;
  WriterType::Pointer writer = WriterType::New();
  writer->SetInput(drawPathListFilter->GetOutput());
  writer->SetFileName(argv[1]);
  writer->Update();

  // Parallel lines are detected. A minimum common length, an angular
  // threshold and a maximum distance threshold have to specified.
  // The input is a pathList of the previously extracted line segments.
  typedef otb::ParallelLinePathListFilter<PathType> ParallelLinePathType;
  ParallelLinePathType::Pointer parallelLinePathListFilter =
    ParallelLinePathType::New();
  parallelLinePathListFilter->SetDistanceThreshold(distThreshParallel);
  parallelLinePathListFilter->SetAngularThreshold(angThreshParallel);
  parallelLinePathListFilter->SetCommonDistanceThreshold(
    commonDistThreshParallel);
  parallelLinePathListFilter->SetInput(lineList);
  parallelLinePathListFilter->Update();

  // A black background image is built to draw the path on.
  ImageType::Pointer outputParallel = ImageType::New();
  outputParallel->SetRegions(image->GetLargestPossibleRegion());
  outputParallel->Allocate();
  outputParallel->FillBuffer(itk::NumericTraits<PixelType>::Zero);

  // Parallel lines are drawn on a black background image with \doxygen{otb}{DrawPathListFilter}.
  // The \code{SetUseIternalValues()} tells the drawing filter to draw the path with its likelihood
  // value.
  //typedef otb::DrawPathListFilter<ImageType, PathType, ImageType> DrawPathType;
  DrawPathType::Pointer drawPathListFilterParallel = DrawPathType::New();
  drawPathListFilterParallel->SetInput(outputParallel);
  drawPathListFilterParallel->SetInputPath(
    parallelLinePathListFilter->GetOutput());
  drawPathListFilter->SetPathValue(itk::NumericTraits<PixelType>::max());
  drawPathListFilterParallel->SetUseInternalPathValue(false);

  // Write the Line image
  WriterType::Pointer writerParallel = WriterType::New();
  writerParallel->SetInput(drawPathListFilterParallel->GetOutput());
  writerParallel->SetFileName(argv[2]);
  writerParallel->Update();

  return EXIT_SUCCESS;
}