Orfeo Toolbox  3.16
HyperspectralUnmixingExample.cxx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: ORFEO Toolbox
4  Language: C++
5  Date: $Date$
6  Version: $Revision$
7 
8 
9  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
10  See OTBCopyright.txt for details.
11 
12 
13  This software is distributed WITHOUT ANY WARRANTY; without even
14  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  PURPOSE. See the above copyright notices for more information.
16 
17 =========================================================================*/
18 #include "otbVectorImage.h"
19 #include "otbImageFileReader.h"
20 #include "otbImageFileWriter.h"
26 // Software Guide : BeginCommandLineArgs
27 // INPUTS: {Indian_pines_corrected.tif}
28 // OUTPUTS: {Indian_pines_Unmixing_Output.tif}, {UnmixingbandOutput1.png}, {UnmixingbandOutput2.png}, {UnmixingbandOutput3.png}
29 // 16
30 // Software Guide : EndCommandLineArgs
31 
32 // Software Guide : BeginLatex
33 //
34 // This example illustrates the use of the
35 // \doxygen{otb}{VcaImageFilter} and \doxygen{otb}{UnConstrainedLeastSquareImageFilter}.
36 // The VCA filter computes endmembers using the Vertex Component Analysis
37 // and UCLS performs unmixing on the input image using these endmembers.
38 //
39 // The first step required to use these filters is to include its header files.
40 //
41 // Software Guide : EndLatex
42 
43 // Software Guide : BeginCodeSnippet
44 #include "otbVcaImageFilter.h"
46 // Software Guide : EndCodeSnippet
47 
48 int main(int argc, char * argv[])
49 {
50  if (argc != 7)
51  {
52  std::cerr << "Usage: " << argv[0];
53  std::cerr << "inputFileName outputFileName outputPrettyFilename1 outputPrettyFilename2 outputPrettyFilename3 EstimatenumberOfEndmembers" << std::endl;
54  return EXIT_FAILURE;
55  }
56 
57  const char * infname = argv[1];
58  const char * outfname = argv[2];
59  const unsigned int estimateNumberOfEndmembers = atoi(argv[6]);
60  const unsigned int Dimension = 2;
61 
62  typedef double PixelType;
63  typedef otb::VectorImage<unsigned char, 2> OutputPrettyImageType;
64 
65  // Software Guide : BeginLatex
66  //
67  // We start by defining the types for the images and the reader and
68  // the writer. We choose to work with a \doxygen{otb}{VectorImage},
69  // since we will produce a multi-channel image (the principal
70  // components) from a multi-channel input image.
71  //
72  // Software Guide : EndLatex
73 
74  // Software Guide : BeginCodeSnippet
76  typedef otb::ImageFileReader<ImageType> ReaderType;
77  typedef otb::ImageFileWriter<ImageType> WriterType;
78  // Software Guide : EndCodeSnippet
79 
81  typedef otb::StreamingStatisticsVectorImageFilter<ImageType> StreamingStatisticsVectorImageFilterType;
82  typedef otb::VectorImageToMatrixImageFilter<ImageType> VectorImageToMatrixImageFilterType;
83 
86 
87  // Software Guide : BeginLatex
88  //
89  // We instantiate now the image reader and we set the image file name.
90  //
91  // Software Guide : EndLatex
92 
93  // Software Guide : BeginCodeSnippet
94  ReaderType::Pointer reader = ReaderType::New();
95  reader->SetFileName(infname);
96  // Software Guide : EndCodeSnippet
97 
98  reader->UpdateOutputInformation();
99 
100 
101  // Software Guide : BeginLatex
102  //
103  // For now we need to rescale the input image between 0 and 1 to
104  // perform the unmixing algorithm. We use the
105  // \doxygen{otb}{VectorRescaleIntensityImageFilter}.
106  //
107  // Software Guide : EndLatex
108 
109  // Software Guide : BeginCodeSnippet
110  RescalerType::Pointer rescaler = RescalerType::New();
111  rescaler->SetInput(reader->GetOutput());
112 
113  ImageType::PixelType minPixel, maxPixel;
114  minPixel.SetSize(reader->GetOutput()->GetNumberOfComponentsPerPixel());
115  maxPixel.SetSize(reader->GetOutput()->GetNumberOfComponentsPerPixel());
116  minPixel.Fill(0.);
117  maxPixel.Fill(1.);
118 
119  rescaler->SetOutputMinimum(minPixel);
120  rescaler->SetOutputMaximum(maxPixel);
121  // Software Guide : EndCodeSnippet
122 
123  // Software Guide : BeginLatex
124  //
125  // We define the type for the VCA filter. It is templated over the input
126  // image type. The only parameter is the number of endmembers which
127  // needs to be estimated.
128  // We can now the instantiate the filter.
129  //
130  // Software Guide : EndLatex
131 
132  // Software Guide : BeginCodeSnippet
133  typedef otb::VCAImageFilter<ImageType> VCAFilterType;
134  VCAFilterType::Pointer vca = VCAFilterType::New();
135 
136  vca->SetNumberOfEndmembers(estimateNumberOfEndmembers);
137  vca->SetInput(rescaler->GetOutput());
138  // Software Guide : EndCodeSnippet
139 
140 
141  // Software Guide : BeginLatex
142  //
143  // We transform the output estimate endmembers to a matrix representation
144  //
145  // Software Guide : EndLatex
146 
147  // Software Guide : BeginCodeSnippet
148  VectorImageToMatrixImageFilterType::Pointer
149  endMember2Matrix = VectorImageToMatrixImageFilterType::New();
150  endMember2Matrix->SetInput(vca->GetOutput());
151  endMember2Matrix->Update();
152 
153  // Software Guide : EndCodeSnippet
154 
155  // Software Guide : BeginLatex
156  //
157  // We can now procedd to the unmixing algorithm.Parameters
158  // needed are the input image and the endmembers matrix.
159  //
160  // Software Guide : EndLatex
161 
162  // Software Guide : BeginCodeSnippet
164  UCLSUnmixingFilterType::Pointer
165  unmixer = UCLSUnmixingFilterType::New();
166  unmixer->SetInput(rescaler->GetOutput());
167  unmixer->SetMatrix(endMember2Matrix->GetMatrix());
168  // Software Guide : EndCodeSnippet
169 
170  unmixer->SetNumberOfThreads(1); // FIXME : currently buggy
171 
172  // Software Guide : BeginLatex
173  //
174  // We now instantiate the writer and set the file name for the
175  // output image and trigger the unmixing computation with the \code{Update()} of the writer.
176  //
177  // Software Guide : EndLatex
178 
179  // Software Guide : BeginCodeSnippet
180  WriterType::Pointer writer = WriterType::New();
181  writer->SetInput(unmixer->GetOutput());
182  writer->SetFileName(outfname);
183  writer->Update();
184  // Software Guide : EndCodeSnippet
185 
186  // Software Guide : BeginLatex
187  // Figure~\ref{fig:UNMIXING_FILTER} shows the result of applying unmixing
188  // to an AVIRIS image (220 bands). This dataset is freely available
189  // at \url{http://www.ehu.es/ccwintco/index.php/Hyperspectral_Remote_Sensing_Scenes#Indian_Pines}
190  // \begin{figure}
191  // \center
192  // \includegraphics[width=0.25\textwidth]{UnmixingbandOutput1.eps}
193  // \includegraphics[width=0.25\textwidth]{UnmixingbandOutput2.eps}
194  // \includegraphics[width=0.25\textwidth]{UnmixingbandOutput3.eps}
195  // \itkcaption[Unmixing Filter]{Result of applying the
196  // \doxygen{otb}{VcaImageFilter} and \doxygen{otb}{UnConstrainedLeastSquareImageFilter} to an image. From left
197  // to right and top to bottom:
198  // first abundance map band, second abundance map band, third abundance map band.}
199  // \label{fig:UNMIXING_FILTER}
200  // \end{figure}
201  //
202  // Software Guide : EndLatex
203 
204  typedef otb::Image<PixelType, Dimension> MonoImageType;
205  typedef otb::MultiToMonoChannelExtractROI<PixelType, PixelType> ExtractROIFilterType;
206  typedef otb::Image<unsigned char, 2> OutputImageType;
207  typedef itk::RescaleIntensityImageFilter<MonoImageType,
208  OutputImageType> PrettyRescalerType;
209  typedef otb::ImageFileWriter<OutputImageType> WriterType2;
210 
211  for (unsigned int cpt = 0; cpt < 3; ++cpt)
212  {
213  ExtractROIFilterType::Pointer extractROIFilter = ExtractROIFilterType::New();
214  PrettyRescalerType::Pointer prettyRescaler = PrettyRescalerType::New();
215  WriterType2::Pointer writer2 = WriterType2::New();
216 
217  extractROIFilter->SetInput(unmixer->GetOutput());
218  extractROIFilter->SetChannel(cpt + 1);
219 
220  prettyRescaler->SetInput(extractROIFilter->GetOutput());
221  prettyRescaler->SetOutputMinimum(0);
222  prettyRescaler->SetOutputMaximum(255);
223 
224  writer2->SetInput(prettyRescaler->GetOutput());
225  writer2->SetFileName(argv[cpt + 3]);
226  writer2->Update();
227  }
228 
229  return EXIT_SUCCESS;
230 }

Generated at Sat Feb 2 2013 23:22:53 for Orfeo Toolbox with doxygen 1.8.1.1