OTB  10.0.0
Orfeo Toolbox
otbMRFSamplerRandom.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2024 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
This is the base class for sampler methods used in the MRF framework.
itk::SmartPointer< Self > Pointer
Superclass::EnergyRegularizationType EnergyRegularizationType
RandomGeneratorType::Pointer m_Generator
Superclass::LabelledImagePixelType LabelledImagePixelType
Superclass::LabelledImageNeighborhoodIterator LabelledImageNeighborhoodIterator
Superclass::EnergyRegularizationPointer EnergyRegularizationPointer
Superclass::EnergyFidelityType EnergyFidelityType
Superclass::InputImagePixelType InputImagePixelType
Superclass::EnergyFidelityPointer EnergyFidelityPointer
void InitializeSeed(int seed)
otb::MRFSampler< TInput1, TInput2 > Superclass
itk::SmartPointer< const Self > ConstPointer
int Compute(const InputImageNeighborhoodIterator &itData, const LabelledImageNeighborhoodIterator &itRegul) override
itk::Statistics::MersenneTwisterRandomVariateGenerator RandomGeneratorType
Superclass::InputImageNeighborhoodIterator InputImageNeighborhoodIterator
This is the base class for sampler methods used in the MRF framework.
Definition: otbMRFSampler.h:45
itk::ConstNeighborhoodIterator< TInput1 > InputImageNeighborhoodIterator
Definition: otbMRFSampler.h:52
itk::NeighborhoodIterator< TInput2 > LabelledImageNeighborhoodIterator
Definition: otbMRFSampler.h:55
TInput2::PixelType LabelledImagePixelType
Definition: otbMRFSampler.h:56
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.