Orfeo Toolbox  3.16
MarkovClassification3Example.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 
19 
20 // Software Guide : BeginCommandLineArgs
21 // INPUTS: {QB_Suburb.png}
22 // OUTPUTS: {MarkovRandomField3_gray_value.png}, {MarkovRandomField3_color_value.png}
23 // 1.0 20 1.0 1
24 // Software Guide : EndCommandLineArgs
25 
26 // Software Guide : BeginLatex
27 //
28 // This example illustrates the details of the MarkovRandomFieldFilter by using the Fisher distribution
29 // to model the likelihood energy.
30 // This filter is an application of the Markov Random Fields for classification.
31 //
32 // This example applies the MarkovRandomFieldFilter to
33 // classify an image into four classes defined by their Fisher distribution parameters L, M and mu.
34 // The optimization is done using a Metropolis algorithm with a random sampler. The
35 // regularization energy is defined by a Potts model and the fidelity or likelihood energy is modelled by a
36 // Fisher distribution.
37 // The parameter of the Fisher distribution was determined for each class in a supervised step.
38 // ( See the File OtbParameterEstimatioOfFisherDistribution )
39 // This example is a contribution from Jan Wegner.
40 //
41 // Software Guide : EndLatex
42 
43 
44 #include "otbImageFileReader.h"
45 #include "otbImageFileWriter.h"
46 #include "otbImage.h"
49 
50 
53 
54 
55 #include "otbMRFEnergyPotts.h"
58 #include "otbMRFSamplerRandom.h"
59 
60 
61 int main(int argc, char* argv[] )
62 {
63  if( argc != 8 )
64  {
65  std::cerr << "Missing Parameters "<< argc << std::endl;
66  std::cerr << "Usage: " << argv[0];
67  std::cerr << " inputImage output_gray_label output_color_label lambda iterations "
68  "optimizerTemperature useRandomValue " << std::endl;
69  return 1;
70  }
71  // Software Guide : BeginLatex
72  //
73  // Then we must decide what pixel type to use for the image. We
74  // choose to make all computations with double precision.
75  // The labeled image is of type unsigned char which allows up to 256 different
76  // classes.
77  //
78  // Software Guide : EndLatex
79 
80  // Software Guide : BeginCodeSnippet
81  const unsigned int Dimension = 2;
82  typedef double InternalPixelType;
83  typedef unsigned char LabelledPixelType;
84 
85  typedef otb::Image<InternalPixelType, Dimension> InputImageType;
86  typedef otb::Image<LabelledPixelType, Dimension> LabelledImageType;
87  // Software Guide : EndCodeSnippet
88 
89  // Software Guide : BeginLatex
90  //
91  // We define a reader for the image to be classified, an initialization for the
92  // classification (which could be random) and a writer for the final
93  // classification.
94  //
95  // Software Guide : EndLatex
96 
97  // Software Guide : BeginCodeSnippet
98  typedef otb::ImageFileReader< InputImageType > ReaderType;
100 
101  ReaderType::Pointer reader = ReaderType::New();
102  WriterType::Pointer writer = WriterType::New();
103  // Software Guide : EndCodeSnippet
104 
105  const char * inputFilename = argv[1];
106  const char * outputFilename = argv[2];
107  const char * outputRescaledImageFileName = argv[3];
108 
109  reader->SetFileName( inputFilename );
110  writer->SetFileName( outputFilename );
111 
112  // Software Guide : BeginLatex
113  //
114  // Finally, we define the different classes necessary for the Markov classification.
115  // A MarkovRandomFieldFilter is instantiated, this is the
116  // main class which connect the other to do the Markov classification.
117  //
118  // Software Guide : EndLatex
119 
120  // Software Guide : BeginCodeSnippet
122  <InputImageType, LabelledImageType> MarkovRandomFieldFilterType;
123  // Software Guide : EndCodeSnippet
124 
125  // Software Guide : BeginLatex
126  //
127  // An MRFSamplerRandomMAP, which derives from the
128  // MRFSampler, is instanciated. The sampler is in charge of
129  // proposing a modification for a given site. The
130  // MRFSamplerRandomMAP, randomly pick one possible value
131  // according to the MAP probability.
132  //
133  // Software Guide : EndLatex
134 
135  // Software Guide : BeginCodeSnippet
137  // Software Guide : EndCodeSnippet
138 
139  // Software Guide : BeginLatex
140  //
141  // An MRFOptimizerMetropolis, which derives from the
142  // MRFOptimizer, is instanciated. The optimizer is in charge
143  // of accepting or rejecting the value proposed by the sampler. The
144  // MRFSamplerRandomMAP, accept the proposal according to the
145  // variation of energy it causes and a temperature parameter.
146  //
147  // Software Guide : EndLatex
148 
149  // Software Guide : BeginCodeSnippet
150  typedef otb::MRFOptimizerMetropolis OptimizerType;
151  // Software Guide : EndCodeSnippet
152 
153  // Software Guide : BeginLatex
154  //
155  // Two energy, deriving from the MRFEnergy class need to be instantiated. One energy
156  // is required for the regularization, taking into account the relationship between neighboring pixels
157  // in the classified image. Here it is done with the MRFEnergyPotts, which implements
158  // a Potts model.
159  //
160  // The second energy is used for the fidelity to the original data. Here it is done with a
161  // MRFEnergyFisherClassification class, which defines a Fisher distribution to model the data.
162  //
163  // Software Guide : EndLatex
164 
165  // Software Guide : BeginCodeSnippet
166  typedef otb::MRFEnergyPotts
167  <LabelledImageType, LabelledImageType> EnergyRegularizationType;
169  <InputImageType, LabelledImageType> EnergyFidelityType;
170  // Software Guide : EndCodeSnippet
171 
172  // Software Guide : BeginLatex
173  //
174  // The different filters composing our pipeline are created by invoking their
175  // New() methods, assigning the results to smart pointers.
176  //
177  // Software Guide : EndLatex
178 
179  // Software Guide : BeginCodeSnippet
180  MarkovRandomFieldFilterType::Pointer markovFilter = MarkovRandomFieldFilterType::New();
181  EnergyRegularizationType::Pointer energyRegularization = EnergyRegularizationType::New();
182  EnergyFidelityType::Pointer energyFidelity = EnergyFidelityType::New();
183  OptimizerType::Pointer optimizer = OptimizerType::New();
184  SamplerType::Pointer sampler = SamplerType::New();
185  // Software Guide : EndCodeSnippet
186 
187  // Software Guide : BeginLatex
188  //
189  // Parameter for the MRFEnergyFisherClassification class are created. The shape parameters M, L
190  // and the weighting parameter mu are computed in a supervised step
191  //
192  // Software Guide : EndLatex
193 
194 
195  if ((bool)(atoi(argv[6])) == true)
196  {
197  // Overpass random calculation(for test only):
198  sampler->InitializeSeed(0);
199  optimizer->InitializeSeed(1);
200  markovFilter->InitializeSeed(1);
201  }
202 
203  // Software Guide : BeginCodeSnippet
204  unsigned int nClass =4;
205  energyFidelity->SetNumberOfParameters(3*nClass);
206  EnergyFidelityType::ParametersType parameters;
207  parameters.SetSize(energyFidelity->GetNumberOfParameters());
208  //Class 0
209  parameters[0] = 12.353042; //Class 0 mu
210  parameters[1] = 2.156422; //Class 0 L
211  parameters[2] = 4.920403; //Class 0 M
212  //Class 1
213  parameters[3] = 72.068291; //Class 1 mu
214  parameters[4] = 11.000000; //Class 1 L
215  parameters[5] = 50.950001; //Class 1 M
216  //Class 2
217  parameters[6] = 146.665985; //Class 2 mu
218  parameters[7] = 11.000000; //Class 2 L
219  parameters[8] = 50.900002; //Class 2 M
220  //Class 3
221  parameters[9] = 200.010132; //Class 3 mu
222  parameters[10] = 11.000000; //Class 3 L
223  parameters[11] = 50.950001; //Class 3 M
224 
225  energyFidelity->SetParameters(parameters);
226  // Software Guide : EndCodeSnippet
227 
228  // Software Guide : BeginLatex
229  //
230  // Parameters are given to the different classes and the sampler, optimizer and
231  // energies are connected with the Markov filter.
232  //
233  // Software Guide : EndLatex
234 
235  // Software Guide : BeginCodeSnippet
236  OptimizerType::ParametersType param(1);
237  param.Fill(atof(argv[6]));
238  optimizer->SetParameters(param);
239  markovFilter->SetNumberOfClasses(nClass);
240  markovFilter->SetMaximumNumberOfIterations(atoi(argv[5]));
241  markovFilter->SetErrorTolerance(0.0);
242  markovFilter->SetLambda(atof(argv[4]));
243  markovFilter->SetNeighborhoodRadius(1);
244 
245  markovFilter->SetEnergyRegularization(energyRegularization);
246  markovFilter->SetEnergyFidelity(energyFidelity);
247  markovFilter->SetOptimizer(optimizer);
248  markovFilter->SetSampler(sampler);
249  // Software Guide : EndCodeSnippet
250 
251  // Software Guide : BeginLatex
252  //
253  // The pipeline is connected. An itkRescaleIntensityImageFilter
254  // rescales the classified image before saving it.
255  //
256  // Software Guide : EndLatex
257 
258  // Software Guide : BeginCodeSnippet
259  markovFilter->SetInput(reader->GetOutput());
260 
262  < LabelledImageType, LabelledImageType > RescaleType;
263  RescaleType::Pointer rescaleFilter = RescaleType::New();
264  rescaleFilter->SetOutputMinimum(0);
265  rescaleFilter->SetOutputMaximum(255);
266 
267  rescaleFilter->SetInput( markovFilter->GetOutput() );
268 
269  writer->SetInput( rescaleFilter->GetOutput() );
270  writer->Update();
271  // Software Guide : EndCodeSnippet
272 
273  //convert output image to color
274  typedef itk::RGBPixel<unsigned char> RGBPixelType;
275  typedef otb::Image<RGBPixelType, 2> RGBImageType;
276  typedef itk::Functor::ScalarToRGBPixelFunctor<unsigned long> ColorMapFunctorType;
277 
279  ColorMapFilterType::Pointer colormapper = ColorMapFilterType::New();
280 
281  colormapper->SetInput( rescaleFilter->GetOutput() );
282  // Software Guide : BeginLatex
283  //
284  // We can now create an image file writer and save the image.
285  //
286  // Software Guide : EndLatex
287 
288  // Software Guide : BeginCodeSnippet
289  typedef otb::ImageFileWriter<RGBImageType> WriterRescaledType;
290 
291  WriterRescaledType::Pointer writerRescaled = WriterRescaledType::New();
292 
293  writerRescaled->SetFileName( outputRescaledImageFileName );
294  writerRescaled->SetInput( colormapper->GetOutput() );
295 
296  writerRescaled->Update();
297  // Software Guide : EndCodeSnippet
298 
299  // Software Guide : BeginLatex
300  //
301  // Figure~\ref{fig:MRF_CLASSIFICATION3} shows the output of the Markov Random
302  // Field classification into four classes using the
303  // Fisher-distribution as likelihood term.
304  //
305  // \begin{figure}
306  // \center
307  // \includegraphics[width=0.44\textwidth]{QB_Suburb.eps}
308  // \includegraphics[width=0.44\textwidth]{MarkovRandomField3_color_value.eps}
309  // \itkcaption[MRF restauration]{Result of applying
310  // the \doxygen{otb}{MarkovRandomFieldFilter} to an extract from a PAN Quickbird
311  // image for classification into four classes using the Fisher-distribution as
312  // likehood term. From left to right : original image,
313  // classification.}
314  // \label{fig:MRF_CLASSIFICATION3}
315  // \end{figure}
316  //
317  // Software Guide : EndLatex
318 
319  return EXIT_SUCCESS;
320 
321 }

Generated at Sun Feb 3 2013 00:15:39 for Orfeo Toolbox with doxygen 1.8.1.1