Multispectral.cxx 7.46 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*=========================================================================

  Program:   ORFEO Toolbox
  Language:  C++
  Date:      $Date$
  Version:   $Revision$


  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
  See OTBCopyright.txt for details.


13 14
     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 16 17 18 19 20
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/

//  Software Guide : BeginCommandLineArgs
//    INPUTS: {qb_RoadExtract.tif}
Jordi Inglada's avatar
Jordi Inglada committed
21
//    OUTPUTS: {qb_blue.tif}, {qb_shiftscale.tif}
22 23 24 25 26 27 28
//  Software Guide : EndCommandLineArgs

//  Software Guide : BeginLatex
//
//  First, we are going to use \doxygen{otb}{VectorImage} instead of the now
// traditionnal \doxygen{otb}{Image}. So we include the required header:
//
29
//  Software Guide : EndLatex
30 31 32 33 34 35 36

// Software Guide : BeginCodeSnippet
// Software Guide : EndCodeSnippet

//  Software Guide : BeginLatex
//
// We also include some other header which will be useful later. Note that we
37
// are still using the \doxygen{otb}{Image} in this example for some of the
38 39
// output.
//
40
//  Software Guide : EndLatex
41 42 43

// Software Guide : BeginCodeSnippet
#include "otbImageFileReader.h"
44
#include "otbImageFileWriter.h"
45 46 47 48
#include "otbMultiToMonoChannelExtractROI.h"
#include "itkShiftScaleImageFilter.h"
#include "otbPerBandVectorImageFilter.h"

OTB Bot's avatar
STYLE  
OTB Bot committed
49
int main(int argc, char * argv[])
50
{
51 52 53 54 55 56 57
  if (argc != 4)
    {
    std::cerr << "Usage: "
        << argv[0]
        << " <input_filename> <output_extract> <output_shifted_scaled>"
        << std::endl;
    }
58
  // Software Guide : EndCodeSnippet
59

60 61 62
  //  Software Guide : BeginLatex
  //
  // We want to read a multispectral image so we declare the image type and the
63 64
  // reader. As we have done in the previous example we get the filename from
  // the command line.
65
  //
66 67
  //  Software Guide : EndLatex

68
  // Software Guide : BeginCodeSnippet
OTB Bot's avatar
STYLE  
OTB Bot committed
69
  typedef unsigned short int             PixelType;
70 71 72 73
  typedef otb::VectorImage<PixelType, 2> VectorImageType;

  typedef otb::ImageFileReader<VectorImageType> ReaderType;
  ReaderType::Pointer reader = ReaderType::New();
74

75 76
  reader->SetFileName(argv[1]);
  // Software Guide : EndCodeSnippet
77

78 79 80
  //  Software Guide : BeginLatex
  //
  //  Sometime, you need to process only one spectral band of the image. To get
81
  // only one of the spectral band we use the
Guillaume Pasero's avatar
Guillaume Pasero committed
82
  // \doxygen{otb}{MultiToMonoChannelExtractROI}. The declaration is as usual:
83
  //
84 85
  //  Software Guide : EndLatex

86
  // Software Guide : BeginCodeSnippet
87
  typedef otb::MultiToMonoChannelExtractROI<PixelType, PixelType>
88
  ExtractChannelType;
89
  ExtractChannelType::Pointer extractChannel = ExtractChannelType::New();
Jordi Inglada's avatar
Jordi Inglada committed
90
  // Software Guide : EndCodeSnippet
91 92
  //  Software Guide : BeginLatex
  //
93
  //  We need to pass the parameters to the filter for the extraction. This
94
  // filter also allow extracting only a spatial subset of the image. However,
95 96
  // we will extract the whole channel in this case.
  //
97 98
  // To do that, we need to pass the desired region using the
  // \code{SetExtractionRegion()} (method such as \code{SetStartX},
99
  // \code{SetSizeX} are also available). We get the region from the reader with
100 101 102
  // the \code{GetLargestPossibleRegion()} method. Before doing that we need to
  // read the metadata from the file: this is done by calling the
  // \code{UpdateOutputInformation()} on the reader's output. The difference with the
103 104 105
  // \code{Update()} is that the pixel array is not allocated (yet !) and reduce
  // the memory usage.
  //
106 107
  //  Software Guide : EndLatex

108 109
  // Software Guide : BeginCodeSnippet
  reader->UpdateOutputInformation();
110
  extractChannel->SetExtractionRegion(
111
    reader->GetOutput()->GetLargestPossibleRegion());
112
  // Software Guide : EndCodeSnippet
113

114 115
  //  Software Guide : BeginLatex
  //
116
  //  We chose the channel number to extract (starting from 1) and we plug the
117 118
  // pipeline.
  //
119 120
  //  Software Guide : EndLatex

121 122 123 124
  // Software Guide : BeginCodeSnippet
  extractChannel->SetChannel(3);
  extractChannel->SetInput(reader->GetOutput());
  // Software Guide : EndCodeSnippet
125

126 127 128
  //  Software Guide : BeginLatex
  //
  //  To output this image, we need a writer. As the output of the
129
  // \doxygen{otb}{MultiToMonoChannelExtractROI} is a \doxygen{otb}{Image}, we
130 131
  // need to template the writer with this type.
  //
132 133
  //  Software Guide : EndLatex

134
  // Software Guide : BeginCodeSnippet
OTB Bot's avatar
STYLE  
OTB Bot committed
135
  typedef otb::Image<PixelType, 2>                 ImageType;
136
  typedef otb::ImageFileWriter<ImageType> WriterType;
137 138 139 140
  WriterType::Pointer writer = WriterType::New();

  writer->SetFileName(argv[2]);
  writer->SetInput(extractChannel->GetOutput());
141

142 143
  writer->Update();
  // Software Guide : EndCodeSnippet
144

145 146
  //  Software Guide : BeginLatex
  //
147
  // After this, we have a one band image that we can process with most OTB
148 149
  // filters.
  //
150
  // In some situation, you may want to apply the same process to all bands of
151 152 153 154 155 156 157
  // the image. You don't have to extract each band and process them separately.
  // There is several situations:
  //
  // \begin{itemize}
  // \item the filter (or the combination of filters) you want to use are doing
  // operations that are well defined for \doxygen{itk}{VariableLengthVector}
  // (which is the pixel type), then you don't have to do anything special.
158
  // \item if this is not working, you can look for the equivalent filter
159 160
  // specially designed for vector images.
  // \item some of the filter you need to use applies operations undefined for
161 162
  // \doxygen{itk}{VariableLengthVector}, then you can use the
  // \doxygen{otb}{PerBandVectorImageFilter} specially designed for this
163 164 165 166 167
  // purpose.
  // \end{itemize}
  //
  // Let's see how this filter is working. We chose to apply the
  // \doxygen{itk}{ShiftScaleImageFilter} to each of the spectral band. We start
168
  // by declaring the filter on a normal \doxygen{otb}{Image}. Note that we
169 170
  // don't need to specify any input for this filter.
  //
171 172
  //  Software Guide : EndLatex

173 174 175 176 177 178
  // Software Guide : BeginCodeSnippet
  typedef itk::ShiftScaleImageFilter<ImageType, ImageType> ShiftScaleType;
  ShiftScaleType::Pointer shiftScale = ShiftScaleType::New();
  shiftScale->SetScale(0.5);
  shiftScale->SetShift(10);
  // Software Guide : EndCodeSnippet
179

180 181
  //  Software Guide : BeginLatex
  //
182
  // We declare the \doxygen{otb}{PerBandVectorImageFilter} which has three
183 184 185 186 187 188
  // template: the input image type, the output image type and the filter type
  // to apply to each band.
  //
  // The filter is selected using the \code{SetFilter()} method and the input
  // by the usual \code{SetInput()} method.
  //
189 190
  //  Software Guide : EndLatex

191 192
  // Software Guide : BeginCodeSnippet
  typedef otb::PerBandVectorImageFilter
193
  <VectorImageType, VectorImageType, ShiftScaleType> VectorFilterType;
194 195
  VectorFilterType::Pointer vectorFilter = VectorFilterType::New();
  vectorFilter->SetFilter(shiftScale);
196

197 198
  vectorFilter->SetInput(reader->GetOutput());
  // Software Guide : EndCodeSnippet
199

200 201 202 203 204
  //  Software Guide : BeginLatex
  //
  // Now, we just have to save the image using a writer templated over an
  // \doxygen{otb}{VectorImage}:
  //
205 206
  //  Software Guide : EndLatex

207
  // Software Guide : BeginCodeSnippet
208
  typedef otb::ImageFileWriter<VectorImageType> VectorWriterType;
209 210 211 212
  VectorWriterType::Pointer writerVector = VectorWriterType::New();

  writerVector->SetFileName(argv[3]);
  writerVector->SetInput(vectorFilter->GetOutput());
213

214
  writerVector->Update();
215 216

  return EXIT_SUCCESS;
217 218
}
// Software Guide : EndCodeSnippet