Orfeo Toolbox  3.16
BayesianFusionImageFilter.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  Some parts of this code are derived from ITK. See ITKCopyright.txt
13  for details.
14 
15 
16  This software is distributed WITHOUT ANY WARRANTY; without even
17  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
18  PURPOSE. See the above copyright notices for more information.
19 
20 =========================================================================*/
21 
22 
23 // Software Guide : BeginCommandLineArgs
24 // INPUTS: {multiSpect.tif} , {multiSpectInterp.tif}, {panchro.tif}
25 // OUTPUTS: {BayesianFusion_0.9999.tif} , {pretty_BayesianFusion_0.9999.png} , {pretty_multiSpect_0.9999.png} , {pretty_multiSpectInterp_0.9999.png} , {pretty_panchro_0.9999.png}
26 // 0.9999
27 // Software Guide : EndCommandLineArgs
28 
29 // Software Guide : BeginCommandLineArgs
30 // INPUTS: {multiSpect.tif} , {multiSpectInterp.tif}, {panchro.tif}
31 // OUTPUTS: {BayesianFusion_0.5.tif} , {pretty_BayesianFusion_0.5.png} , {pretty_multiSpect_0.5.png} , {pretty_multiSpectInterp_0.5.png} , {pretty_panchro_0.5.png}
32 // 0.5
33 // Software Guide : EndCommandLineArgs
34 
35 // Software Guide : BeginLatex
36 //
37 // \index{otb::BayesianFusionFilter}
38 // \index{otb::BayesianFusionFilter!header}
39 //
40 // The following example illustrates the use of the
41 // \doxygen{otb}{BayesianFusionFilter}. The Bayesian data fusion
42 // relies on the idea that variables of interest, denoted as vector $\mathbf{Z}$,
43 // cannot be directly observed. They are linked to the observable variables
44 // $\mathbf{Y}$ through the following error-like model.
45 //
46 // \begin{equation}
47 // \mathbf{Y} = g(\mathbf{Z}) + \mathbf{E}
48 // \end{equation}
49 //
50 // where g($\mathbf{Z}$) is a set of functionals and $\mathbf{E}$ is a
51 // vector of random errors that are stochastically independent from $\mathbf{Z}$.
52 // This algorithm uses elementary probability calculus, and several assumptions to compute
53 // the data fusion. For more explication see Fasbender, Radoux and Bogaert's
54 // publication \cite{JRadoux}.
55 // Three images are used :
56 // \begin{itemize}
57 // \item a panchromatic image,
58 // \item a multispectral image resampled at the panchromatic image spatial resolution,
59 // \item a multispectral image resampled at the panchromatic image spatial resolution,
60 // using, e.g. a cubic interpolator.
61 // \item a float : $\lambda$, the meaning of the weight to be given to the panchromatic
62 // image compared to the multispectral one.
63 // \end{itemize}
64 //
65 // Let's look at the minimal code required to use this algorithm. First, the following header
66 // defining the otb::BayesianFusionFilter class must be included.
67 // Software Guide : EndLatex
68 
69 // Software Guide : BeginCodeSnippet
71 // Software Guide : EndCodeSnippet
72 
73 #include "otbImage.h"
74 #include "otbVectorImage.h"
75 #include "itkCastImageFilter.h"
76 #include "otbImageFileReader.h"
77 #include "otbImageFileWriter.h"
82 
83 int main(int argc, char *argv[])
84 {
85  if (argc < 10)
86  {
87  std::cerr << "Missing Parameters " << std::endl;
88  std::cerr << "Usage: " << argv[0];
89  std::cerr << " inputMultiSpectralImage inputMultiSpectralInterpolatedImage "
90  << "inputPanchromatiqueImage outputImage outputImagePrinted "
91  << "msPrinted msiPrinted panchroPrinted lambda"
92  << std::endl;
93  return 1;
94  }
95 
96  // Software Guide : BeginLatex
97  //
98  // The image types are now defined using pixel types and particular
99  // dimension. The panchromatic image is defined as an \doxygen{otb}{Image}
100  // and the multispectral one as \doxygen{otb}{VectorImage}.
101  //
102  // Software Guide : EndLatex
103 
104  // Software Guide : BeginCodeSnippet
105  typedef double InternalPixelType;
106  const unsigned int Dimension = 2;
107  typedef otb::Image<InternalPixelType, Dimension> PanchroImageType;
108  typedef otb::VectorImage<InternalPixelType, Dimension> MultiSpecImageType;
109  // Software Guide : EndCodeSnippet
110 
111  typedef double OutputPixelType;
112  typedef otb::VectorImage<OutputPixelType, Dimension> OutputImageType;
113 
114  // We instantiate reader and writer types
115  //
116  typedef otb::ImageFileReader<MultiSpecImageType> ReaderVectorType;
117  typedef otb::ImageFileReader<PanchroImageType> ReaderType;
118  typedef otb::ImageFileWriter<OutputImageType> WriterType;
119 
120  ReaderVectorType::Pointer multiSpectReader = ReaderVectorType::New();
121  ReaderVectorType::Pointer multiSpectInterpReader = ReaderVectorType::New();
122  ReaderType::Pointer panchroReader = ReaderType::New();
123  WriterType::Pointer writer = WriterType::New();
124 
125  multiSpectReader->SetFileName(argv[1]);
126  multiSpectInterpReader->SetFileName(argv[2]);
127  panchroReader->SetFileName(argv[3]);
128  writer->SetFileName(argv[4]);
129 
130  // Software Guide : BeginLatex
131  //
132  // The Bayesian data fusion filter type is instantiated using the images types as
133  // a template parameters.
134  //
135  // Software Guide : EndLatex
136 
137  // Software Guide : BeginCodeSnippet
138  typedef otb::BayesianFusionFilter<MultiSpecImageType,
139  MultiSpecImageType,
140  PanchroImageType,
141  OutputImageType>
142  BayesianFusionFilterType;
143  // Software Guide : EndCodeSnippet
144 
145  // Software Guide : BeginLatex
146  //
147  // Next the filter is created by invoking the \code{New()} method and
148  // assigning the result to a \doxygen{itk}{SmartPointer}.
149  //
150  // Software Guide : EndLatex
151 
152  // Software Guide : BeginCodeSnippet
153  BayesianFusionFilterType::Pointer bayesianFilter =
154  BayesianFusionFilterType::New();
155  // Software Guide : EndCodeSnippet
156 
157  // Software Guide : BeginLatex
158  //
159  // Now the multi spectral image, the interpolated multi spectral image and
160  // the panchromatic image are given as inputs to the filter.
161  //
162  // Software Guide : EndLatex
163 
164  // Software Guide : BeginCodeSnippet
165  bayesianFilter->SetMultiSpect(multiSpectReader->GetOutput());
166  bayesianFilter->SetMultiSpectInterp(multiSpectInterpReader->GetOutput());
167  bayesianFilter->SetPanchro(panchroReader->GetOutput());
168 
169  writer->SetInput(bayesianFilter->GetOutput());
170  // Software Guide : EndCodeSnippet
171 
172  // Software Guide : BeginLatex
173  // The BayesianFusionFilter requires defining one parameter : $\lambda$.
174  // The $\lambda$ parameter can be used to tune the fusion toward either a high color
175  // consistency or sharp details. Typical $\lambda$ value range in $[0.5, 1[$, where higher
176  // values yield sharper details. by default $\lambda$ is set at 0.9999.
177  //
178  // Software Guide : EndLatex
179 
180  // Software Guide : BeginCodeSnippet
181  bayesianFilter->SetLambda(atof(argv[9]));
182  // Software Guide : EndCodeSnippet
183 
184  // Software Guide : BeginLatex
185  //
186  // The invocation of the \code{Update()} method on the writer triggers the
187  // execution of the pipeline. It is recommended to place update calls in a
188  // \code{try/catch} block in case errors occur and exceptions are thrown.
189  //
190  // Software Guide : EndLatex
191 
192  // Software Guide : BeginCodeSnippet
193  try
194  {
195  writer->Update();
196  }
197  catch (itk::ExceptionObject& excep)
198  {
199  std::cerr << "Exception caught !" << std::endl;
200  std::cerr << excep << std::endl;
201  }
202  // Software Guide : EndCodeSnippet
203 
204  // Create an 3 band images for the software guide
205  typedef unsigned char OutputPixelType2;
206  typedef otb::VectorImage<OutputPixelType2, Dimension> OutputVectorImageType;
207  typedef otb::ImageFileWriter<OutputVectorImageType> VectorWriterType;
208  typedef otb::VectorRescaleIntensityImageFilter<MultiSpecImageType,
209  OutputVectorImageType>
210  VectorRescalerType;
211  typedef otb::VectorRescaleIntensityImageFilter<OutputImageType,
212  OutputVectorImageType>
213  VectorRescalerBayesianType;
214  typedef otb::Image<OutputPixelType2,
215  Dimension>
216  PanchroOutputImageType;
217  typedef otb::ImageToVectorImageCastFilter<PanchroImageType,
218  MultiSpecImageType> CasterType;
219  typedef otb::MultiChannelExtractROI<OutputPixelType2,
220  OutputPixelType2>
221  ChannelExtractorType;
223  WriterType2;
224 
225  multiSpectReader->GenerateOutputInformation();
226  multiSpectInterpReader->GenerateOutputInformation();
227 
228  CasterType::Pointer cast = CasterType::New();
229  cast->SetInput(panchroReader->GetOutput());
230 
231  OutputVectorImageType::PixelType minimum, maximum;
232  minimum.SetSize(multiSpectReader->GetOutput()->GetNumberOfComponentsPerPixel());
233  maximum.SetSize(multiSpectReader->GetOutput()->GetNumberOfComponentsPerPixel());
234  minimum.Fill(0);
235  maximum.Fill(255);
236 
237  VectorRescalerType::Pointer vrms = VectorRescalerType::New();
238  VectorRescalerType::Pointer vrmsi = VectorRescalerType::New();
239  VectorRescalerBayesianType::Pointer vrb = VectorRescalerBayesianType::New();
240 
241  vrms->SetInput(multiSpectReader->GetOutput());
242  vrms->SetOutputMinimum(minimum);
243  vrms->SetOutputMaximum(maximum);
244  vrms->SetClampThreshold(0.01);
245 
246  vrmsi->SetInput(multiSpectInterpReader->GetOutput());
247  vrmsi->SetOutputMinimum(minimum);
248  vrmsi->SetOutputMaximum(maximum);
249  vrmsi->SetClampThreshold(0.01);
250 
251  vrb->SetInput(bayesianFilter->GetOutput());
252  vrb->SetOutputMinimum(minimum);
253  vrb->SetOutputMaximum(maximum);
254  vrb->SetClampThreshold(0.01);
255 
256  VectorRescalerType::Pointer rp = VectorRescalerType::New();
257  rp->SetInput(cast->GetOutput());
258  minimum.SetSize(1);
259  maximum.SetSize(1);
260  minimum.Fill(0);
261  maximum.Fill(255);
262  rp->SetOutputMinimum(minimum);
263  rp->SetOutputMaximum(maximum);
264  rp->SetClampThreshold(0.01);
265 
266  ChannelExtractorType::Pointer selecterms = ChannelExtractorType::New();
267  ChannelExtractorType::Pointer selectermsi = ChannelExtractorType::New();
268  ChannelExtractorType::Pointer selecterf = ChannelExtractorType::New();
269 
270  selecterms->SetInput(vrms->GetOutput());
271 // selecterms->SetExtractionRegion(multiSpectReader->GetOutput()->GetLargestPossibleRegion());
272  selecterms->SetChannel(2);
273  selecterms->SetChannel(3);
274  selecterms->SetChannel(4);
275 
276  selectermsi->SetInput(vrmsi->GetOutput());
277 // selectermsi->SetExtractionRegion(multiSpectInterpReader->GetOutput()->GetLargestPossibleRegion());
278  selectermsi->SetChannel(2);
279  selectermsi->SetChannel(3);
280  selectermsi->SetChannel(4);
281 
282  selecterf->SetInput(vrb->GetOutput());
283  //selecterf->SetExtractionRegion(bayesianFilter->GetOutput()->GetLargestPossibleRegion());
284  selecterf->SetChannel(2);
285  selecterf->SetChannel(3);
286  selecterf->SetChannel(4);
287 
288  VectorWriterType::Pointer vectWriterms = VectorWriterType::New();
289  VectorWriterType::Pointer vectWritermsi = VectorWriterType::New();
290  VectorWriterType::Pointer vectWriterf = VectorWriterType::New();
291  VectorWriterType::Pointer vectWriterp = VectorWriterType::New();
292 
293  vectWriterf->SetFileName(argv[5]);
294  vectWriterf->SetInput(selecterf->GetOutput());
295  vectWriterms->SetFileName(argv[6]);
296  vectWriterms->SetInput(selecterms->GetOutput());
297  vectWritermsi->SetFileName(argv[7]);
298  vectWritermsi->SetInput(selectermsi->GetOutput());
299  vectWriterp->SetFileName(argv[8]);
300  vectWriterp->SetInput(rp->GetOutput());
301 
302  try
303  {
304  vectWriterms->Update();
305  vectWritermsi->Update();
306  vectWriterf->Update();
307  vectWriterp->Update();
308  }
309  catch (itk::ExceptionObject& excep)
310  {
311  std::cerr << "Exception caught !" << std::endl;
312  std::cerr << excep << std::endl;
313  }
314  catch (...)
315  {
316  std::cout << "Unknown exception !" << std::endl;
317  return EXIT_FAILURE;
318  }
319 
320  // Software Guide : BeginLatex
321  //
322  // Let's now run this example using as input the images
323  // \code{multiSpect.tif} , \code{multiSpectInterp.tif} and \code{panchro.tif}
324  // provided in the directory \code{Examples/Data}. The results
325  // obtained for 2 different values for $\lambda$ are shown in figure
326  // \ref{fig:BayesianImageFusionFilterInput}.
327  //
328  //
329  // \begin{figure} \center
330  // \includegraphics[width=0.24\textwidth]{pretty_multiSpect_0.5.eps}
331  // \includegraphics[width=0.24\textwidth]{pretty_multiSpectInterp_0.5.eps}
332  // \includegraphics[width=0.24\textwidth]{pretty_panchro_0.5.eps}
333  // \itkcaption[Bayesian Data Fusion Example inputs]{Input
334  // images used for this example (\copyright European Space Imaging).}
335  // \label{fig:BayesianImageFusionFilterInput}
336  // \end{figure}
337 
338  // \begin{figure} \center
339  // \includegraphics[width=0.24\textwidth]{pretty_BayesianFusion_0.5.eps}
340  // \includegraphics[width=0.24\textwidth]{pretty_BayesianFusion_0.9999.eps}
341  // \itkcaption[Bayesian Data Fusion results]{Fusion results
342  // for the Bayesian Data Fusion filter for $\lambda = 0.5$ on the left and $\lambda = 0.9999$ on the right.}
343  // \label{fig:BayesianImageFusionFilterOutput}
344  // \end{figure}
345  //
346  //
347  // Software Guide : EndLatex
348 
349  return EXIT_SUCCESS;
350 }

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