LocalHoughExample.cxx 6.55 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright (C) 2005-2017 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.
 */
Patrick Imbo's avatar
nomsg  
Patrick Imbo committed
20

Jordi Inglada's avatar
Jordi Inglada committed
21 22 23 24 25 26 27 28 29

//  Software Guide : BeginCommandLineArgs
//  INPUTS: {detected_lines.png}
//  OUTPUTS: {detected_local_hough.png}
//  30 10 1 50
//  Software Guide : EndCommandLineArgs

// Software Guide : BeginLatex
//
30
// This example illustrates the use of the \doxygen{otb}{ExtractSegmentsImageFilter}.
Jordi Inglada's avatar
Jordi Inglada committed
31
//
32
// The first step required to use this filter is to include its header file.
Jordi Inglada's avatar
Jordi Inglada committed
33
//
34
// Software Guide : EndLatex
Jordi Inglada's avatar
Jordi Inglada committed
35 36 37 38 39 40

// Software Guide : BeginCodeSnippet
#include "otbLocalHoughFilter.h"
#include "otbDrawLineSpatialObjectListFilter.h"
// Software Guide : EndCodeSnippet

41
#include "otbImage.h"
Jordi Inglada's avatar
Jordi Inglada committed
42 43 44 45
#include "otbImageFileReader.h"
#include "itkRescaleIntensityImageFilter.h"
#include "otbImageFileWriter.h"

OTB Bot's avatar
STYLE  
OTB Bot committed
46
int main(int argc, char * argv[])
Jordi Inglada's avatar
Jordi Inglada committed
47 48
{

OTB Bot's avatar
STYLE  
OTB Bot committed
49 50
  if (argc != 7)
    {
Jordi Inglada's avatar
Jordi Inglada committed
51
    std::cerr << "Usage: " << argv[0] << " inputImageFile ";
OTB Bot's avatar
STYLE  
OTB Bot committed
52 53 54
    std::cerr <<
    " outputImageFile  LocalHoughRadius LocalHoughOverlap LocalHoughNumberOfLines LocalHoughThreshold"
              << std::endl;
55

Jordi Inglada's avatar
Jordi Inglada committed
56
    return EXIT_FAILURE;
OTB Bot's avatar
STYLE  
OTB Bot committed
57
    }
58

Jordi Inglada's avatar
Jordi Inglada committed
59 60 61 62 63 64
  //  Software Guide : BeginLatex
  //
  //  Then we must decide what pixel type to use for the image. We
  //  choose to make all computations with floating point precision
  //  and rescale the results between 0 and 255 in order to export PNG images.
  //
65
  //  Software Guide : EndLatex
Jordi Inglada's avatar
Jordi Inglada committed
66 67

  // Software Guide : BeginCodeSnippet
OTB Bot's avatar
STYLE  
OTB Bot committed
68 69
  typedef  float         InternalPixelType;
  typedef  unsigned char OutputPixelType;
Jordi Inglada's avatar
Jordi Inglada committed
70 71 72 73 74 75
  // Software Guide : EndCodeSnippet

  //  Software Guide : BeginLatex
  //
  //  The images are defined using the pixel type and the dimension.
  //
76
  //  Software Guide : EndLatex
Jordi Inglada's avatar
Jordi Inglada committed
77 78

  // Software Guide : BeginCodeSnippet
OTB Bot's avatar
STYLE  
OTB Bot committed
79 80
  typedef otb::Image<InternalPixelType,  2> InternalImageType;
  typedef otb::Image<OutputPixelType,  2>   OutputImageType;
Jordi Inglada's avatar
Jordi Inglada committed
81 82 83 84 85 86
  // Software Guide : EndCodeSnippet

  //  Software Guide : BeginLatex
  //
  //  The filter can be instantiated using the image types defined above.
  //
87
  //  Software Guide : EndLatex
Jordi Inglada's avatar
Jordi Inglada committed
88 89

  // Software Guide : BeginCodeSnippet
OTB Bot's avatar
STYLE  
OTB Bot committed
90 91
  typedef otb::LocalHoughFilter<InternalImageType> LocalHoughType;
  typedef otb::DrawLineSpatialObjectListFilter<InternalImageType,
OTB Bot's avatar
STYLE  
OTB Bot committed
92
      OutputImageType>
OTB Bot's avatar
STYLE  
OTB Bot committed
93
  DrawLineListType;
Jordi Inglada's avatar
Jordi Inglada committed
94 95
  // Software Guide : EndCodeSnippet

OTB Bot's avatar
STYLE  
OTB Bot committed
96
  typedef itk::RescaleIntensityImageFilter<InternalImageType,
OTB Bot's avatar
STYLE  
OTB Bot committed
97
      OutputImageType> RescalerType;
Jordi Inglada's avatar
Jordi Inglada committed
98 99 100

  RescalerType::Pointer rescaler = RescalerType::New();

OTB Bot's avatar
STYLE  
OTB Bot committed
101 102
  rescaler->SetOutputMinimum(itk::NumericTraits<OutputPixelType>::min());
  rescaler->SetOutputMaximum(itk::NumericTraits<OutputPixelType>::max());
103

Jordi Inglada's avatar
Jordi Inglada committed
104 105
  //  Software Guide : BeginLatex
  //
106
  //  An \doxygen{otb}{ImageFileReader} class is also instantiated in order to read
107
  //  image data from a file.
Jordi Inglada's avatar
Jordi Inglada committed
108
  //
109
  //  Software Guide : EndLatex
Jordi Inglada's avatar
Jordi Inglada committed
110 111

  // Software Guide : BeginCodeSnippet
OTB Bot's avatar
STYLE  
OTB Bot committed
112
  typedef otb::ImageFileReader<InternalImageType> ReaderType;
Jordi Inglada's avatar
Jordi Inglada committed
113 114 115
  // Software Guide : EndCodeSnippet

  //  Software Guide : BeginLatex
116
  //
117
  // An \doxygen{otb}{ImageFileWriter} is instantiated in order to write the
Jordi Inglada's avatar
Jordi Inglada committed
118 119
  // output image to a file.
  //
120
  //  Software Guide : EndLatex
Jordi Inglada's avatar
Jordi Inglada committed
121 122

  // Software Guide : BeginCodeSnippet
OTB Bot's avatar
STYLE  
OTB Bot committed
123
  typedef otb::ImageFileWriter<OutputImageType> WriterType;
Jordi Inglada's avatar
Jordi Inglada committed
124 125 126 127 128 129 130
  // Software Guide : EndCodeSnippet

  //  Software Guide : BeginLatex
  //
  //  Both the filter and the reader are created by invoking their \code{New()}
  //  methods and assigning the result to SmartPointers.
  //
131
  //  Software Guide : EndLatex
Jordi Inglada's avatar
Jordi Inglada committed
132 133 134 135

  // Software Guide : BeginCodeSnippet
  ReaderType::Pointer reader = ReaderType::New();

OTB Bot's avatar
STYLE  
OTB Bot committed
136
  LocalHoughType::Pointer localHough = LocalHoughType::New();
Jordi Inglada's avatar
Jordi Inglada committed
137

OTB Bot's avatar
STYLE  
OTB Bot committed
138
  DrawLineListType::Pointer drawLineList = DrawLineListType::New();
Jordi Inglada's avatar
Jordi Inglada committed
139 140 141 142 143
  // Software Guide : EndCodeSnippet

  //  Software Guide : BeginLatex
  //
  //  The same is done for the writer.
144 145
  //
  //  Software Guide : EndLatex
Jordi Inglada's avatar
Jordi Inglada committed
146

147
  // Software Guide : BeginCodeSnippet
Jordi Inglada's avatar
Jordi Inglada committed
148 149 150
  WriterType::Pointer writer = WriterType::New();
  // Software Guide : EndCodeSnippet

OTB Bot's avatar
STYLE  
OTB Bot committed
151
  reader->SetFileName(argv[1]);
152

OTB Bot's avatar
STYLE  
OTB Bot committed
153 154 155 156 157 158
  unsigned int LocalHoughRadiusX((unsigned int) ::atoi(argv[3]));
  unsigned int LocalHoughRadiusY((unsigned int) ::atoi(argv[3]));
  unsigned int LocalHoughOverlapX((unsigned int) ::atoi(argv[4]));
  unsigned int LocalHoughOverlapY((unsigned int) ::atoi(argv[4]));
  unsigned int LocalHoughNumberOfLines((unsigned int) ::atoi(argv[5]));
  float        LocalHoughThreshold((float) ::atoi(argv[6]));
159

Jordi Inglada's avatar
Jordi Inglada committed
160 161 162 163 164 165 166 167
  LocalHoughType::SizeType LocalHoughRadius;
  LocalHoughRadius[0] = LocalHoughRadiusX;
  LocalHoughRadius[1] = LocalHoughRadiusY;

  LocalHoughType::SizeType LocalHoughOverlap;
  LocalHoughOverlap[0] = LocalHoughOverlapX;
  LocalHoughOverlap[1] = LocalHoughOverlapY;

OTB Bot's avatar
STYLE  
OTB Bot committed
168 169 170 171
  localHough->SetRadius(LocalHoughRadius);
  localHough->SetOverlap(LocalHoughOverlap);
  localHough->SetNumberOfLines(LocalHoughNumberOfLines);
  localHough->SetThreshold(LocalHoughThreshold);
172

Jordi Inglada's avatar
Jordi Inglada committed
173
  //  Software Guide : BeginLatex
174
  //
Jordi Inglada's avatar
Jordi Inglada committed
175
  //  The image obtained with the reader is passed as input to the
176
  //  \doxygen{otb}{ExtractSegmentsImageFilter}. The pipeline is built as follows.
Jordi Inglada's avatar
Jordi Inglada committed
177 178 179
  //
  //  \index{otb::ExtractSegmentsImageFilter!SetInput()}
  //
180
  //  Software Guide : EndLatex
Jordi Inglada's avatar
Jordi Inglada committed
181 182

  // Software Guide : BeginCodeSnippet
OTB Bot's avatar
STYLE  
OTB Bot committed
183
  localHough->SetInput(reader->GetOutput());
184

OTB Bot's avatar
STYLE  
OTB Bot committed
185 186 187 188
  drawLineList->SetInput(reader->GetOutput());
  drawLineList->SetInputLineSpatialObjectList(localHough->GetOutput());
  writer->SetFileName(argv[2]);
  writer->SetInput(drawLineList->GetOutput());
Jordi Inglada's avatar
Jordi Inglada committed
189
  writer->Update();
190
// Software Guide : EndCodeSnippet
Jordi Inglada's avatar
Jordi Inglada committed
191

Jordi Inglada's avatar
Jordi Inglada committed
192 193
  //  Software Guide : BeginLatex
  // Figure~\ref{fig:LOCAL_HOUGH}
194
  // shows the result of applying the \doxygen{otb}{LocalHoughImageFilter}.
Jordi Inglada's avatar
Jordi Inglada committed
195
  // \begin{figure} \center
Jordi Inglada's avatar
Jordi Inglada committed
196 197 198
  // \includegraphics[width=0.25\textwidth]{detected_lines.eps}
  // \includegraphics[width=0.25\textwidth]{detected_local_hough.eps}
  // \itkcaption[Line Correlation Detector Application]{Result of applying
199
  // the \doxygen{otb}{LocalHoughImageFilter}. From left to right :
Jordi Inglada's avatar
Jordi Inglada committed
200
  // original image, extracted segments.}  \label{fig:LOCAL_HOUGH} \end{figure}
Jordi Inglada's avatar
Jordi Inglada committed
201
  //
202
  //  Software Guide : EndLatex
Jordi Inglada's avatar
Jordi Inglada committed
203 204 205

  return EXIT_SUCCESS;
}