NDVIRAndNIRVegetationIndexImageFilter.cxx 7.83 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 26 27 28 29
/* Example usage:
./NDVIRAndNIRVegetationIndexImageFilter Input/NDVI_2.hdr \
                                        Input/NDVI_3.hdr \
                                        Output/NDVIRAndNIRVegetationIndex.tif \
                                        Output/pretty_Red.png \
                                        Output/pretty_NIR.png \
                                        Output/pretty_NDVIRAndNIRVegetationIndex.png
*/
30 31


32 33
// \index{otb::VegetationIndicesFunctor}
// \index{otb::VegetationIndicesFunctor!header}
34
//
35
// The following example illustrates the use of the
36
// \doxygen{itk}{BinaryFunctorImageFilter} with the use of the Normalized
37
// Difference Vegatation Index (NDVI).
38
// NDVI computes the difference between the NIR channel, noted $L_{NIR}$, and the red channel,
Jordi Inglada's avatar
Jordi Inglada committed
39
// noted $L_{r}$ radiances reflected from the surface and transmitted through the atmosphere:
40 41 42 43 44
//
// \begin{equation}
// \mathbf{NDVI} = \frac{L_{NIR}-L_{r}}{L_{NIR}+L_{r}}
// \end{equation}
//
45 46 47 48 49 50 51 52 53 54 55 56
//  \relatedClasses
//  \begin{itemize}
//  \item \subdoxygen{otb}{Functor}{RVI}
//  \item \subdoxygen{otb}{Functor}{PVI}
//  \item \subdoxygen{otb}{Functor}{SAVI}
//  \item \subdoxygen{otb}{Functor}{TSAVI}
//  \item \subdoxygen{otb}{Functor}{MSAVI}
//  \item \subdoxygen{otb}{Functor}{GEMI}
//  \item \subdoxygen{otb}{Functor}{WDVI}
//  \item \subdoxygen{otb}{Functor}{IPVI}
//  \item \subdoxygen{otb}{Functor}{TNDVI}
//  \end{itemize}
57
//
58
// Let's look at the minimal code required to use this algorithm.
59

60
#include "itkMacro.h"
61 62 63
#include "otbImage.h"
#include "otbImageFileReader.h"
#include "otbImageFileWriter.h"
64 65
#include "itkBinaryFunctorImageFilter.h"
#include "otbVegetationIndicesFunctor.h"
66 67
#include "itkRescaleIntensityImageFilter.h"

68
int main(int argc, char* argv[])
69
{
OTB Bot's avatar
STYLE  
OTB Bot committed
70
  if (argc < 6)
71
  {
72 73
    std::cerr << "Missing Parameters " << std::endl;
    std::cerr << "Usage: " << argv[0];
74
    std::cerr << " inputImage1 , inputImage2 , outputImage , prettyinputImage1 , prettyinputImage2 , prettyOutput" << std::endl;
75
    return 1;
76
  }
77

Jordi Inglada's avatar
Jordi Inglada committed
78
  // The image types are now defined using pixel types the
79
  // dimension. Input and output images are defined as \doxygen{otb}{Image}.
80

81
  const unsigned int                             Dimension = 2;
OTB Bot's avatar
STYLE  
OTB Bot committed
82 83 84 85 86
  typedef double                                 InputPixelType;
  typedef float                                  OutputPixelType;
  typedef otb::Image<InputPixelType, Dimension>  InputRImageType;
  typedef otb::Image<InputPixelType, Dimension>  InputNIRImageType;
  typedef otb::Image<OutputPixelType, Dimension> OutputImageType;
87

88 89 90 91
  // We instantiate reader and writer types
  typedef otb::ImageFileReader<InputRImageType>   RReaderType;
  typedef otb::ImageFileReader<InputNIRImageType> NIRReaderType;
  typedef otb::ImageFileWriter<OutputImageType>   WriterType;
92 93

  // The NDVI (Normalized Difference Vegetation Index) is instantiated using
Emmanuel Christophe's avatar
Emmanuel Christophe committed
94
  // the images pixel type as template parameters. It is
Jordi Inglada's avatar
Jordi Inglada committed
95
  // implemented as a functor class which will be passed as a
96
  // parameter to an \doxygen{itk}{BinaryFunctorImageFilter}.
97

98
  typedef otb::Functor::NDVI<InputPixelType, InputPixelType, OutputPixelType> FunctorType;
99

100
  // The \doxygen{itk}{BinaryFunctorImageFilter} type is instantiated using the images
101
  // types and the NDVI functor as template parameters.
102

103
  typedef itk::BinaryFunctorImageFilter<InputRImageType, InputNIRImageType, OutputImageType, FunctorType> NDVIImageFilterType;
104

105
  // Instantiating object
106
  NDVIImageFilterType::Pointer filter            = NDVIImageFilterType::New();
107 108 109
  RReaderType::Pointer                 readerR   = RReaderType::New();
  NIRReaderType::Pointer               readerNIR = NIRReaderType::New();
  WriterType::Pointer                  writer    = WriterType::New();
110

Jordi Inglada's avatar
Jordi Inglada committed
111
  //  Now the input images are set and a name is given to the output image.
112

OTB Bot's avatar
STYLE  
OTB Bot committed
113 114 115
  readerR->SetFileName(argv[1]);
  readerNIR->SetFileName(argv[2]);
  writer->SetFileName(argv[3]);
116

Emmanuel Christophe's avatar
Emmanuel Christophe committed
117 118
  // We set the processing pipeline: filter inputs are linked to
  // the reader output and the filter output is linked to the writer
Jordi Inglada's avatar
Jordi Inglada committed
119
  // input.
120

121 122
  filter->SetInput1(readerR->GetOutput());
  filter->SetInput2(readerNIR->GetOutput());
123

OTB Bot's avatar
STYLE  
OTB Bot committed
124
  writer->SetInput(filter->GetOutput());
125

Emmanuel Christophe's avatar
Emmanuel Christophe committed
126 127
  // Invocation of the \code{Update()} method on the writer triggers the
  // execution of the pipeline.  It is recommended to place \code{update()} calls in a
128
  // \code{try/catch} block in case errors occur and exceptions are thrown.
129

130
  try
131
  {
132
    writer->Update();
133
  }
OTB Bot's avatar
STYLE  
OTB Bot committed
134
  catch (itk::ExceptionObject& excep)
135
  {
136 137
    std::cerr << "Exception caught !" << std::endl;
    std::cerr << excep << std::endl;
138
  }
OTB Bot's avatar
STYLE  
OTB Bot committed
139
  catch (...)
140
  {
141 142
    std::cout << "Unknown exception !" << std::endl;
    return EXIT_FAILURE;
143
  }
144

145
  // Pretty image creation for the printing
146 147 148 149 150
  typedef otb::Image<unsigned char, Dimension>                                       OutputPrettyImageType;
  typedef otb::ImageFileWriter<OutputPrettyImageType>                                WriterPrettyType;
  typedef itk::RescaleIntensityImageFilter<OutputImageType, OutputPrettyImageType>   RescalerType;
  typedef itk::RescaleIntensityImageFilter<InputRImageType, OutputPrettyImageType>   RescalerRType;
  typedef itk::RescaleIntensityImageFilter<InputNIRImageType, OutputPrettyImageType> RescalerNIRType;
151

152
  RescalerType::Pointer     rescaler     = RescalerType::New();
153
  WriterPrettyType::Pointer prettyWriter = WriterPrettyType::New();
OTB Bot's avatar
STYLE  
OTB Bot committed
154
  rescaler->SetInput(filter->GetOutput());
155 156
  rescaler->SetOutputMinimum(0);
  rescaler->SetOutputMaximum(255);
OTB Bot's avatar
STYLE  
OTB Bot committed
157 158
  prettyWriter->SetFileName(argv[6]);
  prettyWriter->SetInput(rescaler->GetOutput());
159

160 161
  RescalerRType::Pointer    rescalerR       = RescalerRType::New();
  RescalerNIRType::Pointer  rescalerNIR     = RescalerNIRType::New();
162
  WriterPrettyType::Pointer prettyWriterR   = WriterPrettyType::New();
163
  WriterPrettyType::Pointer prettyWriterNIR = WriterPrettyType::New();
OTB Bot's avatar
STYLE  
OTB Bot committed
164
  rescalerR->SetInput(readerR->GetOutput());
165 166
  rescalerR->SetOutputMinimum(0);
  rescalerR->SetOutputMaximum(255);
OTB Bot's avatar
STYLE  
OTB Bot committed
167 168
  prettyWriterR->SetFileName(argv[4]);
  prettyWriterR->SetInput(rescalerR->GetOutput());
169

OTB Bot's avatar
STYLE  
OTB Bot committed
170
  rescalerNIR->SetInput(readerNIR->GetOutput());
171 172
  rescalerNIR->SetOutputMinimum(0);
  rescalerNIR->SetOutputMaximum(255);
OTB Bot's avatar
STYLE  
OTB Bot committed
173 174
  prettyWriterNIR->SetFileName(argv[5]);
  prettyWriterNIR->SetInput(rescalerNIR->GetOutput());
175

176
  try
177
  {
178 179 180
    prettyWriter->Update();
    prettyWriterNIR->Update();
    prettyWriterR->Update();
181
  }
OTB Bot's avatar
STYLE  
OTB Bot committed
182
  catch (itk::ExceptionObject& excep)
183
  {
184 185
    std::cerr << "Exception caught !" << std::endl;
    std::cerr << excep << std::endl;
186
  }
OTB Bot's avatar
STYLE  
OTB Bot committed
187
  catch (...)
188
  {
189 190
    std::cout << "Unknown exception !" << std::endl;
    return EXIT_FAILURE;
191
  }
192 193 194 195 196 197 198 199 200 201 202

  // Let's now run this example using as input the images
  // \code{NDVI\_3.hdr} and  \code{NDVI\_4.hdr} (images kindly and free of charge given by SISA and CNES)
  // provided in the directory \code{Examples/Data}.
  //
  //
  // \begin{figure} \center
  // \includegraphics[width=0.24\textwidth]{pretty_Red.eps}
  // \includegraphics[width=0.24\textwidth]{pretty_NIR.eps}
  // \includegraphics[width=0.24\textwidth]{pretty_NDVIRAndNIRVegetationIndex.eps}
  // \itkcaption[ARVI Example]{NDVI input images on the left (Red channel and NIR channel), on the right the result of the algorithm.}
203
  // \label{fig:NDVIRAndNIRIndex}
204 205 206
  // \end{figure}

  return EXIT_SUCCESS;
207
}