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
|
/*
* 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.
*/
#include "otbImageFileReader.h"
#include "otbLineSegmentDetector.h"
#include "otbVectorDataToMapFilter.h"
#include "otbAlphaBlendingFunctor.h"
#include "itkBinaryFunctorImageFilter.h"
#include "otbImageFileWriter.h"
#include "itkRescaleIntensityImageFilter.h"
#include "otbVectorDataFileWriter.h"
// Software Guide : BeginCommandLineArgs
// INPUTS: {Scene.png}
// OUTPUTS: {RighAngleOutput.tif}, {PrettyRighAngleInput.png}, {PrettyRighAngleOutput.png}
// 0.1 20
// Software Guide : EndCommandLineArgs
// Software Guide : BeginLatex
//
// 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.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
#include "otbVectorDataToRightAngleVectorDataFilter.h"
// Software Guide : EndCodeSnippet
int main(int argc, char * argv[])
{
const char * infname = argv[1];
const char * outfname = argv[2];
const char * inprettyfname = argv[3];
const char * outprettyfname = argv[4];
double angleThreshold = atof(argv[5]);
double distanceThreshold = atof(argv[6]);
const unsigned int Dimension = 2;
typedef unsigned char PixelType;
typedef double PrecisionType;
typedef otb::Image<PixelType, Dimension> ImageType;
typedef otb::ImageFileReader<ImageType> ReaderType;
typedef otb::ImageFileWriter<ImageType> WriterType;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(infname);
reader->GenerateOutputInformation();
WriterType::Pointer writer = WriterType::New();
// Software Guide : BeginLatex
//
// 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.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef otb::VectorData<PrecisionType> VectorDataType;
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// 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}.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef otb::LineSegmentDetector<ImageType, PrecisionType> LsdFilterType;
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// 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.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef otb::VectorDataToRightAngleVectorDataFilter<VectorDataType>
RightAngleFilterType;
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// We instantiate the line segment detector and the right angle detector.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
LsdFilterType::Pointer lsdFilter = LsdFilterType::New();
RightAngleFilterType::Pointer rightAngleFilter = RightAngleFilterType::New();
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// We plug the pipeline.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
lsdFilter->SetInput(reader->GetOutput());
rightAngleFilter->SetInput(lsdFilter->GetOutput());
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// 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.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
rightAngleFilter->SetAngleThreshold(angleThreshold);
rightAngleFilter->SetDistanceThreshold(distanceThreshold);
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// We will now draw the right angles on top of the input image. For
// this, we will draw the detected points on top of the input
// image. For this matter, we will use a
// \doxygen{otb}{VectorDataToMapFilter} which is templated over
// the input vector data type and the output image type, and a
// combination of a \doxygen{itk}{binaryFunctorImageFilter}
// and the \doxygen{otb}{Functor}{UnaryFunctorImageFilter}.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef otb::VectorDataToMapFilter<VectorDataType,
ImageType> VectorDataRendererType;
VectorDataRendererType::Pointer vectorDataRenderer = VectorDataRendererType::New();
typedef otb::Functor::AlphaBlendingFunctor<PixelType,
PixelType, PixelType> FunctorType;
typedef itk::BinaryFunctorImageFilter<ImageType, ImageType,
ImageType, FunctorType> BlendingFilterType;
BlendingFilterType::Pointer blendingFilter = BlendingFilterType::New();
vectorDataRenderer->SetInput(1, lsdFilter->GetOutput());
vectorDataRenderer->SetInput(rightAngleFilter->GetOutput());
vectorDataRenderer->SetSize(reader->GetOutput()->GetLargestPossibleRegion().GetSize());
vectorDataRenderer->SetOrigin(reader->GetOutput()->GetOrigin());
vectorDataRenderer->SetSpacing(reader->GetOutput()->GetSignedSpacing());
vectorDataRenderer->SetRenderingStyleType(VectorDataRendererType::Binary);
blendingFilter->SetInput1(reader->GetOutput());
blendingFilter->SetInput2(vectorDataRenderer->GetOutput());
blendingFilter->GetFunctor().SetAlpha(0.25);
writer->SetInput(blendingFilter->GetOutput());
writer->SetFileName(outfname);
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// Before calling the \code{Update()} method of the writer 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.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
reader->GenerateOutputInformation();
writer->Update();
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
// Figure~\ref{fig:RIGHTANGLE_FILTER} shows the result of applying
// the right angle detection filter to an image.
// \begin{figure}
// \center
// \includegraphics[width=0.25\textwidth]{PrettyRighAngleInput.eps}
// \includegraphics[width=0.25\textwidth]{PrettyRighAngleOutput.eps}
// \itkcaption[Right Angle Detection Filter]{Result of applying the
// \doxygen{otb}{LineSegmentDetector} and the
// \doxygen{otb}{VectorDataToRightAngleVectorDataFilter} to an image. From left to right :
// original image, detected right angles.}
// \label{fig:RIGHTANGLE_FILTER}
// \end{figure}
//
// Software Guide : EndLatex
/************** images for printing *********/
typedef unsigned char OutputPixelType;
typedef otb::Image<OutputPixelType,
Dimension> OutputImageType;
typedef otb::ImageFileWriter<OutputImageType>
OutputWriterType;
typedef itk::RescaleIntensityImageFilter<ImageType,
OutputImageType> RescalerType;
RescalerType::Pointer rescaler = RescalerType::New();
OutputWriterType::Pointer outwriter = OutputWriterType::New();
rescaler->SetOutputMinimum(0);
rescaler->SetOutputMaximum(255);
rescaler->SetInput(reader->GetOutput());
outwriter->SetInput(rescaler->GetOutput());
outwriter->SetFileName(inprettyfname);
outwriter->Update();
rescaler->SetInput(blendingFilter->GetOutput());
outwriter->SetInput(rescaler->GetOutput());
outwriter->SetFileName(outprettyfname);
outwriter->Update();
return EXIT_SUCCESS;
}
|