DEMToRainbowExample.cxx 6.19 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
/* Example usage:
./DEMToRainbowExample Output/DEMToRainbowImageGenerator.png 6.5 45.5 500 500 0.002 -0.002 Input/DEM_srtm
*/
25 26


27 28 29 30 31 32 33 34
/* Example usage:
./DEMToRainbowExample Output/DEMToHotImageGenerator.png 6.5 45.5 500 500 0.002 -0.002 Input/DEM_srtm hot
*/


/* Example usage:
./DEMToRainbowExample Output/DEMToReliefImageGenerator.png 6.5 45.5 500 500 0.002 -0.002 Input/DEM_srtm relief
*/
35 36


37
// In some situation, it is desirable to represent a gray scale image in color for easier
38
// interpretation. This is particularly the case if pixel values in the image are used
39 40 41
// to represent some data such as elevation, deformation map,
// interferogram. In this case, it is important to ensure that similar
// values will get similar colors. You can notice how this requirement
42
// differs from the previous case.
43
//
44
// The following example illustrates the use of the \doxygen{otb}{DEMToImageGenerator} class
45 46 47
// combined with the \doxygen{otb}{ScalarToRainbowRGBPixelFunctor}. You can refer to the
// source code or to section \ref{sec:ReadDEM} for the DEM conversion to image,
// we will focus on the color conversion part here.
48 49

#include "otbImageFileReader.h"
50
#include "otbImageFileWriter.h"
51

52
#include "itkScalarToRGBColormapImageFilter.h"
53 54
#include "otbScalarToRainbowRGBPixelFunctor.h"
#include "otbDEMToImageGenerator.h"
55
#include "otbReliefColormapFunctor.h"
56

57
int main(int argc, char* argv[])
58
{
59

OTB Bot's avatar
OTB Bot committed
60
  if (argc < 9)
61 62 63 64
  {
    std::cout << argv[0] << " <output_filename> <Longitude Output Origin point>";
    std::cout << " <Latitude Output Origin point> <X Output Size> <Y Output size>";
    std::cout << " <X Spacing> <Y Spacing> <DEM folder path>" << std::endl;
65
    return EXIT_FAILURE;
66
  }
67

68 69 70 71 72
  typedef double                             PixelType;
  typedef unsigned char                      UCharPixelType;
  typedef itk::RGBPixel<UCharPixelType>      RGBPixelType;
  typedef otb::Image<PixelType, 2>           ImageType;
  typedef otb::Image<RGBPixelType, 2>        RGBImageType;
73
  typedef otb::ImageFileWriter<RGBImageType> WriterType;
74

75 76
  WriterType::Pointer writer = WriterType::New();
  writer->SetFileName(argv[1]);
77

OTB Bot's avatar
OTB Bot committed
78
  typedef otb::DEMToImageGenerator<ImageType> DEMToImageGeneratorType;
79 80 81

  DEMToImageGeneratorType::Pointer demToImage = DEMToImageGeneratorType::New();

82 83 84
  typedef DEMToImageGeneratorType::SizeType    SizeType;
  typedef DEMToImageGeneratorType::SpacingType SpacingType;
  typedef DEMToImageGeneratorType::PointType   PointType;
85

86
  otb::DEMHandler::Instance()->OpenDEMDirectory(argv[8]);
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

  PointType origin;
  origin[0] = ::atof(argv[2]);
  origin[1] = ::atof(argv[3]);

  demToImage->SetOutputOrigin(origin);

  SizeType size;
  size[0] = ::atoi(argv[4]);
  size[1] = ::atoi(argv[5]);

  demToImage->SetOutputSize(size);

  SpacingType spacing;
  spacing[0] = ::atof(argv[6]);
  spacing[1] = ::atof(argv[7]);

  demToImage->SetOutputSpacing(spacing);
105

106
  // As in the previous example, the \doxygen{itk}{ScalarToRGBColormapImageFilter} is
107 108 109
  // the filter in charge of calling the functor we specify to do the work for
  // each pixel. Here it is the \doxygen{otb}{ScalarToRainbowRGBPixelFunctor}.

110 111
  typedef itk::ScalarToRGBColormapImageFilter<ImageType, RGBImageType> ColorMapFilterType;
  ColorMapFilterType::Pointer                                          colormapper = ColorMapFilterType::New();
112
  colormapper->UseInputImageExtremaForScalingOff();
113

114
  if (argc == 9)
115 116 117
  {
    typedef otb::Functor::ScalarToRainbowRGBPixelFunctor<PixelType, RGBPixelType> ColorMapFunctorType;
    ColorMapFunctorType::Pointer                                                  colormap = ColorMapFunctorType::New();
118 119 120
    colormap->SetMinimumInputValue(0);
    colormap->SetMaximumInputValue(4000);
    colormapper->SetColormap(colormap);
121
  }
122 123

  else
124
  {
OTB Bot's avatar
OTB Bot committed
125
    if (strcmp(argv[9], "hot") == 0)
126 127 128
    {
      typedef itk::Function::HotColormapFunction<PixelType, RGBPixelType> ColorMapFunctorType;
      ColorMapFunctorType::Pointer                                        colormap = ColorMapFunctorType::New();
129 130 131
      colormap->SetMinimumInputValue(0);
      colormap->SetMaximumInputValue(4000);
      colormapper->SetColormap(colormap);
132
    }
133
    else
134 135 136
    {
      typedef otb::Functor::ReliefColormapFunctor<PixelType, RGBPixelType> ColorMapFunctorType;
      ColorMapFunctorType::Pointer                                         colormap = ColorMapFunctorType::New();
137 138 139 140
      colormap->SetMinimumInputValue(0);
      colormap->SetMaximumInputValue(4000);
      colormapper->SetColormap(colormap);
    }
141
  }
142 143 144
  // And we connect the color mapper filter with the filter producing
  // the image of the DEM:

145
  colormapper->SetInput(demToImage->GetOutput());
146

147
  writer->SetInput(colormapper->GetOutput());
148

149
  try
150
  {
151
    writer->Update();
152
  }
OTB Bot's avatar
OTB Bot committed
153
  catch (itk::ExceptionObject& excep)
154
  {
155 156
    std::cerr << "Exception caught !" << std::endl;
    std::cerr << excep << std::endl;
157
  }
OTB Bot's avatar
OTB Bot committed
158
  catch (...)
159
  {
160
    std::cout << "Unknown exception !" << std::endl;
161
    return EXIT_FAILURE;
162
  }
163

164 165
  // Figure~\ref{fig:RAINBOW_FILTER} shows the effect of applying the filter to
  // a gray scale image.
166
  //
167 168 169 170 171 172 173 174 175 176
  // \begin{figure}
  // \center
  // \includegraphics[width=0.44\textwidth]{pretty_DEMToImageGenerator.eps}
  // \includegraphics[width=0.44\textwidth]{DEMToRainbowImageGenerator.eps}
  // \includegraphics[width=0.44\textwidth]{DEMToHotImageGenerator.eps}
  // \includegraphics[width=0.44\textwidth]{DEMToReliefImageGenerator.eps}
  // \itkcaption[Grayscale to color]{The gray level DEM extracted from SRTM
  // data (top-left) and the same area represented in color.}
  // \label{fig:RAINBOW_FILTER}
  // \end{figure}
177

178
  return EXIT_SUCCESS;
179
}