MarkovRegularizationExample.cxx 6.22 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.
 */
Cyrille Valladeau's avatar
Cyrille Valladeau committed
20 21


22 23 24
/* Example usage:
./MarkovRegularizationExample Input/ROI_QB_MUL_1_SVN_CLASS_MULTI.png Output/MarkovRegularization.png Output/MarkovRegularization-scaled.png 0.2 20 0.0 1
*/
25

Cyrille Valladeau's avatar
Cyrille Valladeau committed
26

27
// This example illustrates the use of the \doxygen{otb}{MarkovRandomFieldFilter}.
Emmanuel Christophe's avatar
Emmanuel Christophe committed
28 29
// to regularize a classification obtained previously by another classifier. Here
// we will apply the regularization to the output of an SVM classifier presented
30
// in \ref{ssec:LearningFromImages}.
Cyrille Valladeau's avatar
Cyrille Valladeau committed
31
//
32
// The reference image and the starting image are both going to be the original
Cyrille Valladeau's avatar
Cyrille Valladeau committed
33 34 35
// classification. Both regularization and fidelity energy are defined by Potts model.
//
// The convergence of the Markov Random Field is done with a random sampler
36
// and a Metropolis model as in example 1. As you should get use to the general
Cyrille Valladeau's avatar
Cyrille Valladeau committed
37 38 39 40 41 42 43 44
// program structure to use the MRF framework, we are not going to repeat the entire
// example. However, remember you can find the full source code for this example
// in your OTB source directory.

#include "otbImageFileReader.h"
#include "otbImageFileWriter.h"
#include "otbImage.h"
#include "otbMarkovRandomFieldFilter.h"
45
#include "itkUnaryFunctorImageFilter.h"
Cyrille Valladeau's avatar
Cyrille Valladeau committed
46 47 48 49 50 51 52
#include "itkRescaleIntensityImageFilter.h"
#include "itkLabelStatisticsImageFilter.h"

#include "otbMRFEnergyPotts.h"
#include "otbMRFOptimizerMetropolis.h"
#include "otbMRFSamplerRandom.h"

OTB Bot's avatar
STYLE  
OTB Bot committed
53
int main(int argc, char* argv[])
Cyrille Valladeau's avatar
Cyrille Valladeau committed
54
{
55

OTB Bot's avatar
STYLE  
OTB Bot committed
56
  if (argc != 8)
57
  {
Cyrille Valladeau's avatar
Cyrille Valladeau committed
58 59
    std::cerr << "Missing Parameters " << std::endl;
    std::cerr << "Usage: " << argv[0];
60
    std::cerr << " inputClassificationImage outputClassification outputClassificationScaled lambda iterations temperature " << std::endl;
61
    std::cerr << " useRandomValue" << std::endl;
Cyrille Valladeau's avatar
Cyrille Valladeau committed
62
    return 1;
63
  }
Cyrille Valladeau's avatar
Cyrille Valladeau committed
64 65

  const unsigned int Dimension = 2;
66

67 68
  using LabelledPixelType = unsigned char;
  using LabelledImageType = otb::Image<LabelledPixelType, Dimension>;
Cyrille Valladeau's avatar
Cyrille Valladeau committed
69

70 71
  using ReaderType = otb::ImageFileReader<LabelledImageType>;
  using WriterType = otb::ImageFileWriter<LabelledImageType>;
72

Cyrille Valladeau's avatar
Cyrille Valladeau committed
73 74
  ReaderType::Pointer reader = ReaderType::New();
  WriterType::Pointer writer = WriterType::New();
75

76 77 78
  const char* inputFilename        = argv[1];
  const char* outputFilename       = argv[2];
  const char* outputScaledFilename = argv[3];
79

OTB Bot's avatar
STYLE  
OTB Bot committed
80 81
  reader->SetFileName(inputFilename);
  writer->SetFileName(outputFilename);
Cyrille Valladeau's avatar
Cyrille Valladeau committed
82

83
  using MarkovRandomFieldFilterType = otb::MarkovRandomFieldFilter<LabelledImageType, LabelledImageType>;
Cyrille Valladeau's avatar
Cyrille Valladeau committed
84

85
  using SamplerType = otb::MRFSamplerRandom<LabelledImageType, LabelledImageType>;
Cyrille Valladeau's avatar
Cyrille Valladeau committed
86

87
  using OptimizerType = otb::MRFOptimizerMetropolis;
Cyrille Valladeau's avatar
Cyrille Valladeau committed
88

89 90
  using EnergyRegularizationType = otb::MRFEnergyPotts<LabelledImageType, LabelledImageType>;
  using EnergyFidelityType       = otb::MRFEnergyPotts<LabelledImageType, LabelledImageType>;
91 92 93 94 95 96 97 98 99

  MarkovRandomFieldFilterType::Pointer markovFilter         = MarkovRandomFieldFilterType::New();
  EnergyRegularizationType::Pointer    energyRegularization = EnergyRegularizationType::New();
  EnergyFidelityType::Pointer          energyFidelity       = EnergyFidelityType::New();
  OptimizerType::Pointer               optimizer            = OptimizerType::New();
  SamplerType::Pointer                 sampler              = SamplerType::New();

  if ((bool)(atoi(argv[7])) == true)
  {
100 101 102 103
    // Overpass random calculation(for test only):
    sampler->InitializeSeed(0);
    optimizer->InitializeSeed(1);
    markovFilter->InitializeSeed(2);
104
  }
105

Cyrille Valladeau's avatar
Cyrille Valladeau committed
106 107 108
  // To find the number of classes available in the original image we use the
  // \doxygen{itk}{LabelStatisticsImageFilter} and more particularly the method
  // \code{GetNumberOfLabels()}.
109

110 111
  using LabelledStatType                 = itk::LabelStatisticsImageFilter<LabelledImageType, LabelledImageType>;
  LabelledStatType::Pointer labelledStat = LabelledStatType::New();
Cyrille Valladeau's avatar
Cyrille Valladeau committed
112 113 114
  labelledStat->SetInput(reader->GetOutput());
  labelledStat->SetLabelInput(reader->GetOutput());
  labelledStat->Update();
115

Cyrille Valladeau's avatar
Cyrille Valladeau committed
116
  unsigned int nClass = labelledStat->GetNumberOfLabels();
117

118
  optimizer->SetSingleParameter(0.0);
119
  markovFilter->SetNumberOfClasses(nClass);
Cyrille Valladeau's avatar
Cyrille Valladeau committed
120 121 122 123
  markovFilter->SetMaximumNumberOfIterations(atoi(argv[5]));
  markovFilter->SetErrorTolerance(0.0);
  markovFilter->SetLambda(atof(argv[4]));
  markovFilter->SetNeighborhoodRadius(1);
124

Cyrille Valladeau's avatar
Cyrille Valladeau committed
125 126 127 128
  markovFilter->SetEnergyRegularization(energyRegularization);
  markovFilter->SetEnergyFidelity(energyFidelity);
  markovFilter->SetOptimizer(optimizer);
  markovFilter->SetSampler(sampler);
129

Cyrille Valladeau's avatar
Cyrille Valladeau committed
130 131
  markovFilter->SetTrainingInput(reader->GetOutput());
  markovFilter->SetInput(reader->GetOutput());
132

OTB Bot's avatar
STYLE  
OTB Bot committed
133
  writer->SetInput(markovFilter->GetOutput());
134 135 136

  writer->Update();

137 138
  using RescaleType                  = itk::RescaleIntensityImageFilter<LabelledImageType, LabelledImageType>;
  RescaleType::Pointer rescaleFilter = RescaleType::New();
Cyrille Valladeau's avatar
Cyrille Valladeau committed
139 140
  rescaleFilter->SetOutputMinimum(0);
  rescaleFilter->SetOutputMaximum(255);
141

OTB Bot's avatar
STYLE  
OTB Bot committed
142
  rescaleFilter->SetInput(markovFilter->GetOutput());
143

OTB Bot's avatar
STYLE  
OTB Bot committed
144
  writer->SetFileName(outputScaledFilename);
145

OTB Bot's avatar
STYLE  
OTB Bot committed
146
  writer->SetInput(rescaleFilter->GetOutput());
147 148 149

  writer->Update();

Cyrille Valladeau's avatar
Cyrille Valladeau committed
150 151 152 153 154
  // Figure~\ref{fig:MRF_REGULARIZATION} shows the output of the Markov Random
  // Field regularization on the classification output of another method.
  //
  // \begin{figure}
  // \center
155
  // \includegraphics[width=0.44\textwidth]{MarkovRegularization.eps}
Cyrille Valladeau's avatar
Cyrille Valladeau committed
156
  // \includegraphics[width=0.44\textwidth]{MarkovRegularization-scaled.eps}
157
  // \itkcaption[MRF restoration]{Result of applying
Cyrille Valladeau's avatar
Cyrille Valladeau committed
158 159
  // the \doxygen{otb}{MarkovRandomFieldFilter} to regularized the result of another
  // classification. From left to right : original classification, regularized
160 161
  // classification}
  // \label{fig:MRF_REGULARIZATION}
Cyrille Valladeau's avatar
Cyrille Valladeau committed
162
  // \end{figure}
163

164
  return EXIT_SUCCESS;
Cyrille Valladeau's avatar
Cyrille Valladeau committed
165
}