ScalingPipeline.cxx 3.74 KB
Newer Older
1
/*
Julien Michel's avatar
Julien Michel committed
2
 * Copyright (C) 2005-2019 Centre National d'Etudes Spatiales (CNES)
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * 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.
 */
20 21


22 23 24 25
/* Example usage:
./ScalingPipeline Input/QB_Suburb.png Output/TutorialsScalingPipelineOutput.png
*/

26

27
//  This example illustrates the use of the
28 29 30
// \doxygen{itk}{RescaleIntensityImageFilter} to convert
// the result for proper display.
//
31
// We include the required header including the header
32
// for the \doxygen{itk}{CannyEdgeDetectionImageFilter} and the
33 34 35 36
// \doxygen{itk}{RescaleIntensityImageFilter}.

#include "otbImage.h"
#include "otbImageFileReader.h"
37
#include "otbImageFileWriter.h"
38
#include "itkUnaryFunctorImageFilter.h"
39 40 41
#include "itkCannyEdgeDetectionImageFilter.h"
#include "itkRescaleIntensityImageFilter.h"

42
int main(int argc, char* argv[])
43
{
44
  if (argc != 3)
45 46 47 48
  {
    std::cerr << "Usage: " << argv[0] << " <input_filename> <output_filename>" << std::endl;
  }

49
  //  We need to declare two different image types, one for the internal
50
  // processing and one to output the results:
51

52 53
  using PixelType = double;
  using ImageType = otb::Image<PixelType, 2>;
54

55 56
  using OutputPixelType = unsigned char;
  using OutputImageType = otb::Image<OutputPixelType, 2>;
57

58
  //  We declare the reader with the image template using the pixel type
59
  // double. It is worth noticing that this instantiation does not imply
60 61 62 63 64
  // anything about the type of the input image. The original image can be
  // anything, the reader will just convert the result to double.
  //
  //  The writer is templated with the unsigned char image to be able to save
  // the result on one byte images (like png for example).
65

66 67
  using ReaderType           = otb::ImageFileReader<ImageType>;
  ReaderType::Pointer reader = ReaderType::New();
68

69 70
  using WriterType           = otb::ImageFileWriter<OutputImageType>;
  WriterType::Pointer writer = WriterType::New();
71 72 73

  reader->SetFileName(argv[1]);
  writer->SetFileName(argv[2]);
74 75

  // Now we are declaring the edge detection filter which is going to work with
76
  // double input and output.
77

78 79
  using FilterType           = itk::CannyEdgeDetectionImageFilter<ImageType, ImageType>;
  FilterType::Pointer filter = FilterType::New();
80 81

  // Here comes the interesting part: we declare the
82
  // \doxygen{itk}{RescaleIntensityImageFilter}. The input
83 84 85
  // image type is the output type of the edge detection
  // filter. The output type is the same as the input type
  // of the writer.
86 87
  //
  // Desired minimum and maximum values for the output are
88
  // specified by the methods \code{SetOutputMinimum()} and
89 90
  // \code{SetOutputMaximum()}.
  //
91
  // This filter will actually rescale all the pixels of
92
  // the image but also cast the type of these pixels.
93

94 95
  using RescalerType             = itk::RescaleIntensityImageFilter<ImageType, OutputImageType>;
  RescalerType::Pointer rescaler = RescalerType::New();
96

97 98
  rescaler->SetOutputMinimum(0);
  rescaler->SetOutputMaximum(255);
99

100
  // Let's plug the pipeline:
101

102 103 104
  filter->SetInput(reader->GetOutput());
  rescaler->SetInput(filter->GetOutput());
  writer->SetInput(rescaler->GetOutput());
105

106 107
  // And finally, we trigger the pipeline execution calling the Update()
  // method on the writer
108

109 110
  writer->Update();

111
  return EXIT_SUCCESS;
112
}