File: RightAngleDetectionExample.cxx

package info (click to toggle)
otb 8.1.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,030,436 kB
  • sloc: xml: 231,007; cpp: 224,490; ansic: 4,592; sh: 1,790; python: 1,131; perl: 92; makefile: 72
file content (120 lines) | stat: -rw-r--r-- 4,317 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
/*
 * Copyright (C) 2005-2022 Centre National d'Etudes Spatiales (CNES)
 *
 * This file is part of Orfeo Toolbox
 *
 *     https://www.orfeo-toolbox.org/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


#include "otbImageFileReader.h"
#include "otbLineSegmentDetector.h"
#include "otbVectorDataFileWriter.h"

/* Example usage:
./RightAngleDetectionExample Input/Scene.png Output/rightAngleOutput.shp 0.1 20
*/


// This example illustrates the use of the
// \doxygen{otb}{VectorDataToRightAngleVectorDataFilter}.
// This filter detects the right angles in an image by exploiting the
// output of a line detection algorithm. Typically the
// \doxygen{otb}{LineSegmentDetector} class will be used. The right
// angle detection algorithm is described in detail in
// \cite{RightAngleDetection}.
//
// The first step required to use this filter is to include its header file.

#include "otbVectorDataToRightAngleVectorDataFilter.h"

int main(int argc, char* argv[])
{
  if (argc != 5)
  {
    std::cerr << "Usage: ./RightAngleDetectionExample input rightAngleOutput angleThreshold distanceThreshold\n";
    return EXIT_FAILURE;
  }

  const char* infname                  = argv[1];
  const char* rightAngleOutputFilename = argv[2];
  double      angleThreshold           = atof(argv[3]);
  double      distanceThreshold        = atof(argv[4]);

  const unsigned int Dimension = 2;
  using PixelType              = unsigned char;
  using PrecisionType          = double;

  using ImageType  = otb::Image<PixelType, Dimension>;
  using ReaderType = otb::ImageFileReader<ImageType>;

  auto reader = ReaderType::New();
  reader->SetFileName(infname);
  reader->GenerateOutputInformation();

  // After defining, as usual, the types for the input image and the
  // image reader, we define the specific types needed for this
  // example. First of all, we will use a vector data
  // to store the detected lines which will be provided by the line
  // segment detector.

  using VectorDataType = otb::VectorData<PrecisionType>;
  // The right angle detector's output is a vector data where each point
  // gives the coordinate of the detected angle.
  //
  // Next, We define the type for the line segment detector. A detailed
  // example for this detector can be found in section \ref{sec:LSD}.

  using LsdFilterType = otb::LineSegmentDetector<ImageType, PrecisionType>;

  // We can finally define the type for the right angle detection
  // filter. This filter is templated over the input vector data type
  // provided by the line segment detector.

  using RightAngleFilterType = otb::VectorDataToRightAngleVectorDataFilter<VectorDataType>;

  // We instantiate the line segment detector and the right angle detector.

  auto lsdFilter        = LsdFilterType::New();
  auto rightAngleFilter = RightAngleFilterType::New();

  // We plug the pipeline.

  lsdFilter->SetInput(reader->GetOutput());
  rightAngleFilter->SetInput(lsdFilter->GetOutput());

  // You can choose how far the right angle segments can be, and the tolerance
  // to consider an angle between two segments as an right one.

  rightAngleFilter->SetAngleThreshold(angleThreshold);
  rightAngleFilter->SetDistanceThreshold(distanceThreshold);

  using WriterType = otb::VectorDataFileWriter<LsdFilterType::VectorDataType>;

  auto rightAngleWriter = WriterType::New();

  rightAngleWriter->SetInput(rightAngleFilter->GetOutput());
  rightAngleWriter->SetFileName(rightAngleOutputFilename);

  // Before calling the \code{Update()} method of the writers in order to
  // trigger the pipeline execution, we call the
  // \code{GenerateOutputInformation()} of the reader, so the
  // filter gets the information about image size and spacing.

  reader->GenerateOutputInformation();
  rightAngleWriter->Update();

  return EXIT_SUCCESS;
}