OTB  9.0.0
Orfeo Toolbox
otbMRFSamplerRandom.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2022 Centre National d'Etudes Spatiales (CNES)
3  *
4  * This file is part of Orfeo Toolbox
5  *
6  * https://www.orfeo-toolbox.org/
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #ifndef otbMRFSamplerRandom_h
22 #define otbMRFSamplerRandom_h
23 
24 #include "otbMRFSampler.h"
25 #include "itkMersenneTwisterRandomVariateGenerator.h"
26 #include "itkNumericTraits.h"
27 
28 namespace otb
29 {
45 template <class TInput1, class TInput2>
46 class ITK_EXPORT MRFSamplerRandom : public MRFSampler<TInput1, TInput2>
47 {
48 public:
51  typedef itk::SmartPointer<Self> Pointer;
52  typedef itk::SmartPointer<const Self> ConstPointer;
53 
54  typedef typename Superclass::InputImageNeighborhoodIterator InputImageNeighborhoodIterator;
55  typedef typename Superclass::LabelledImageNeighborhoodIterator LabelledImageNeighborhoodIterator;
56  typedef typename Superclass::LabelledImagePixelType LabelledImagePixelType;
57  typedef typename Superclass::InputImagePixelType InputImagePixelType;
58  typedef typename Superclass::EnergyFidelityType EnergyFidelityType;
59  typedef typename Superclass::EnergyRegularizationType EnergyRegularizationType;
60  typedef typename Superclass::EnergyFidelityPointer EnergyFidelityPointer;
61  typedef typename Superclass::EnergyRegularizationPointer EnergyRegularizationPointer;
62 
63  typedef itk::Statistics::MersenneTwisterRandomVariateGenerator RandomGeneratorType;
64 
65  itkNewMacro(Self);
66 
67  itkTypeMacro(MRFSamplerRandom, MRFSampler);
68 
69  inline int Compute(const InputImageNeighborhoodIterator& itData, const LabelledImageNeighborhoodIterator& itRegul) override
70  {
71  this->m_EnergyBefore = this->m_EnergyFidelity->GetValue(itData, itRegul.GetCenterPixel());
72  this->m_EnergyBefore += this->m_Lambda * this->m_EnergyRegularization->GetValue(itRegul, itRegul.GetCenterPixel());
73 
74  this->m_Value = static_cast<LabelledImagePixelType>(m_Generator->GetIntegerVariate() % this->m_NumberOfClasses);
75  this->m_EnergyAfter = this->m_EnergyFidelity->GetValue(itData, this->m_Value);
76  this->m_EnergyAfter += this->m_Lambda * this->m_EnergyRegularization->GetValue(itRegul, this->m_Value);
77  this->m_DeltaEnergy = this->m_EnergyAfter - this->m_EnergyBefore;
78 
79  return 0;
80  }
81 
83  void InitializeSeed(int seed)
84  {
85  m_Generator->SetSeed(seed);
86  }
88  {
89  m_Generator->SetSeed();
90  }
92 
93 protected:
94  // The constructor and destructor.
96  {
97  m_Generator = RandomGeneratorType::GetInstance();
98  m_Generator->SetSeed();
99  }
100  ~MRFSamplerRandom() override
101  {
102  }
103 
104 private:
105  RandomGeneratorType::Pointer m_Generator;
106 };
107 }
108 
109 #endif
otb::MRFSamplerRandom::InitializeSeed
void InitializeSeed(int seed)
Definition: otbMRFSamplerRandom.h:83
otb::MRFSamplerRandom::RandomGeneratorType
itk::Statistics::MersenneTwisterRandomVariateGenerator RandomGeneratorType
Definition: otbMRFSamplerRandom.h:63
otb::MRFSamplerRandom::Compute
int Compute(const InputImageNeighborhoodIterator &itData, const LabelledImageNeighborhoodIterator &itRegul) override
Definition: otbMRFSamplerRandom.h:69
otbMRFSampler.h
otb::MRFSamplerRandom::InputImageNeighborhoodIterator
Superclass::InputImageNeighborhoodIterator InputImageNeighborhoodIterator
Definition: otbMRFSamplerRandom.h:54
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::MRFSamplerRandom::~MRFSamplerRandom
~MRFSamplerRandom() override
Definition: otbMRFSamplerRandom.h:100
otb::MRFSamplerRandom::m_Generator
RandomGeneratorType::Pointer m_Generator
Definition: otbMRFSamplerRandom.h:105
otb::MRFSampler::InputImageNeighborhoodIterator
itk::ConstNeighborhoodIterator< TInput1 > InputImageNeighborhoodIterator
Definition: otbMRFSampler.h:52
otb::MRFSamplerRandom::EnergyFidelityType
Superclass::EnergyFidelityType EnergyFidelityType
Definition: otbMRFSamplerRandom.h:58
otb::MRFSamplerRandom::InputImagePixelType
Superclass::InputImagePixelType InputImagePixelType
Definition: otbMRFSamplerRandom.h:57
otb::MRFSamplerRandom::EnergyFidelityPointer
Superclass::EnergyFidelityPointer EnergyFidelityPointer
Definition: otbMRFSamplerRandom.h:60
otb::MRFSamplerRandom::MRFSamplerRandom
MRFSamplerRandom()
Definition: otbMRFSamplerRandom.h:95
otb::MRFSampler
This is the base class for sampler methods used in the MRF framework.
Definition: otbMRFSampler.h:44
otb::MRFSamplerRandom::ConstPointer
itk::SmartPointer< const Self > ConstPointer
Definition: otbMRFSamplerRandom.h:52
otb::MRFSamplerRandom::EnergyRegularizationType
Superclass::EnergyRegularizationType EnergyRegularizationType
Definition: otbMRFSamplerRandom.h:59
otb::MRFSamplerRandom::LabelledImagePixelType
Superclass::LabelledImagePixelType LabelledImagePixelType
Definition: otbMRFSamplerRandom.h:56
otb::MRFSampler::LabelledImagePixelType
TInput2::PixelType LabelledImagePixelType
Definition: otbMRFSampler.h:56
otb::MRFSamplerRandom::Superclass
otb::MRFSampler< TInput1, TInput2 > Superclass
Definition: otbMRFSamplerRandom.h:50
otb::MRFSamplerRandom::LabelledImageNeighborhoodIterator
Superclass::LabelledImageNeighborhoodIterator LabelledImageNeighborhoodIterator
Definition: otbMRFSamplerRandom.h:55
otb::MRFSamplerRandom::Pointer
itk::SmartPointer< Self > Pointer
Definition: otbMRFSamplerRandom.h:51
otb::MRFSampler::LabelledImageNeighborhoodIterator
itk::NeighborhoodIterator< TInput2 > LabelledImageNeighborhoodIterator
Definition: otbMRFSampler.h:55
otb::MRFSamplerRandom::Self
MRFSamplerRandom Self
Definition: otbMRFSamplerRandom.h:49
otb::MRFSamplerRandom::EnergyRegularizationPointer
Superclass::EnergyRegularizationPointer EnergyRegularizationPointer
Definition: otbMRFSamplerRandom.h:61
otb::MRFSamplerRandom::InitializeSeed
void InitializeSeed()
Definition: otbMRFSamplerRandom.h:87
otb::MRFSamplerRandom
This is the base class for sampler methods used in the MRF framework.
Definition: otbMRFSamplerRandom.h:46