OTB  9.0.0
Orfeo Toolbox
otbMarkovRandomFieldFilter.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 otbMarkovRandomFieldFilter_h
22 #define otbMarkovRandomFieldFilter_h
23 
24 #include "otbMacro.h"
25 
26 #include "vnl/vnl_vector.h"
27 #include "vnl/vnl_matrix.h"
28 #include "itkMersenneTwisterRandomVariateGenerator.h"
29 
30 #include "itkImageToImageFilter.h"
31 #include "itkImageRegionIterator.h"
32 
33 #include "itkNeighborhoodAlgorithm.h"
34 #include "itkNeighborhood.h"
35 #include "itkSize.h"
36 #include "otbMRFOptimizer.h"
37 #include "otbMRFSampler.h"
38 
39 namespace otb
40 {
84 
85 template <class TInputImage, class TClassifiedImage>
86 class ITK_EXPORT MarkovRandomFieldFilter : public itk::ImageToImageFilter<TInputImage, TClassifiedImage>
87 {
88 public:
91  typedef itk::ImageToImageFilter<TInputImage, TClassifiedImage> Superclass;
92  typedef itk::SmartPointer<Self> Pointer;
93  typedef itk::SmartPointer<const Self> ConstPointer;
94  typedef typename Superclass::OutputImagePointer OutputImagePointer;
95 
97  itkTypeMacro(MarkovRandomFieldFilter, itk::ImageToImageFilter);
98 
100  itkNewMacro(Self);
101 
103  typedef TInputImage InputImageType;
104  typedef typename TInputImage::Pointer InputImagePointer;
105  typedef typename TInputImage::ConstPointer InputImageConstPointer;
106 
108  typedef typename TInputImage::PixelType InputImagePixelType;
109 
111  typedef typename TInputImage::RegionType InputImageRegionType;
112 
114  typedef itk::ImageRegionIterator<TInputImage> InputImageRegionIterator;
115  typedef itk::ImageRegionConstIterator<TInputImage> InputImageRegionConstIterator;
116 
118  itkStaticConstMacro(InputImageDimension, unsigned int, TInputImage::ImageDimension);
119 
121  typedef TClassifiedImage TrainingImageType;
122  typedef typename TClassifiedImage::Pointer TrainingImagePointer;
123 
125  typedef typename TClassifiedImage::PixelType TrainingImagePixelType;
126 
129  typedef TClassifiedImage LabelledImageType;
130  typedef typename TClassifiedImage::Pointer LabelledImagePointer;
131 
134  typedef typename TClassifiedImage::PixelType LabelledImagePixelType;
135 
138  typedef typename TClassifiedImage::RegionType LabelledImageRegionType;
139 
141  typedef typename TClassifiedImage::IndexType LabelledImageIndexType;
142  typedef typename LabelledImageIndexType::IndexValueType IndexValueType;
143 
145  typedef typename TClassifiedImage::OffsetType LabelledImageOffsetType;
146 
148  typedef itk::ImageRegionIterator<TClassifiedImage> LabelledImageRegionIterator;
149 
150  typedef itk::ImageRegionConstIterator<TClassifiedImage> LabelledImageRegionConstIterator;
151 
153  itkStaticConstMacro(ClassifiedImageDimension, unsigned int, TClassifiedImage::ImageDimension);
154 
156  typedef typename TInputImage::SizeType SizeType;
157 
159  typedef typename TInputImage::SizeType NeighborhoodRadiusType;
160 
162  typedef itk::ConstNeighborhoodIterator<TInputImage> InputImageNeighborhoodIterator;
163 
164  typedef typename InputImageNeighborhoodIterator::RadiusType InputImageNeighborhoodRadiusType;
165 
166  typedef itk::NeighborhoodAlgorithm::ImageBoundaryFacesCalculator<TInputImage> InputImageFacesCalculator;
167 
168  typedef typename InputImageFacesCalculator::FaceListType InputImageFaceListType;
169 
170  typedef typename InputImageFaceListType::iterator InputImageFaceListIterator;
171 
173  typedef itk::NeighborhoodIterator<TClassifiedImage> LabelledImageNeighborhoodIterator;
174 
175  typedef typename LabelledImageNeighborhoodIterator::RadiusType LabelledImageNeighborhoodRadiusType;
176 
177  typedef itk::NeighborhoodAlgorithm::ImageBoundaryFacesCalculator<TClassifiedImage> LabelledImageFacesCalculator;
178 
179  typedef typename LabelledImageFacesCalculator::FaceListType LabelledImageFaceListType;
180 
181  typedef typename LabelledImageFaceListType::iterator LabelledImageFaceListIterator;
182 
184  typedef itk::Statistics::MersenneTwisterRandomVariateGenerator RandomGeneratorType;
185 
189 
192 
195 
198 
202  itkSetObjectMacro(EnergyRegularization, EnergyRegularizationType);
203  itkGetObjectMacro(EnergyRegularization, EnergyRegularizationType);
205 
206  itkSetObjectMacro(EnergyFidelity, EnergyFidelityType);
207  itkGetObjectMacro(EnergyFidelity, EnergyFidelityType);
208 
209  itkSetObjectMacro(Sampler, SamplerType);
210  itkGetObjectMacro(Sampler, SamplerType);
211 
212  itkSetObjectMacro(Optimizer, OptimizerType);
213  itkGetObjectMacro(Optimizer, OptimizerType);
214 
216  itkSetMacro(NumberOfClasses, unsigned int);
217  itkGetMacro(NumberOfClasses, unsigned int);
219 
222  itkSetMacro(MaximumNumberOfIterations, unsigned int);
223  itkGetMacro(MaximumNumberOfIterations, unsigned int);
225 
228  itkSetMacro(ErrorTolerance, double);
229  itkGetMacro(ErrorTolerance, double);
231 
234  itkSetMacro(SmoothingFactor, double);
235  itkGetMacro(SmoothingFactor, double);
237 
240  itkSetMacro(Lambda, double);
241  itkGetMacro(Lambda, double);
243 
245  void SetNeighborhoodRadius(const NeighborhoodRadiusType&);
246 
250  void SetNeighborhoodRadius(const unsigned long);
251  void SetNeighborhoodRadius(const unsigned long* radiusArray);
252 
255  {
256  NeighborhoodRadiusType neighborhoodRadius;
257 
258  for (int i = 0; i < InputImageDimension; ++i)
259  neighborhoodRadius[i] = m_InputImageNeighborhoodRadius[i];
260 
261  return neighborhoodRadius;
262  }
263 
271  virtual void SetTrainingInput(const TrainingImageType* trainingImage);
272  const TrainingImageType* GetTrainingInput(void);
274 
275  // Enum to get the stopping condition of the MRF filter
276  typedef enum { MaximumNumberOfIterations = 1, ErrorTolerance } StopConditionType;
277 
280  itkGetConstReferenceMacro(StopCondition, StopConditionType);
281 
283  itkGetConstReferenceMacro(NumberOfIterations, unsigned int);
284 
285 #ifdef ITK_USE_CONCEPT_CHECKING
286 
287  itkConceptMacro(UnsignedIntConvertibleToClassifiedCheck, (itk::Concept::Convertible<unsigned int, LabelledImagePixelType>));
288  itkConceptMacro(ClassifiedConvertibleToUnsignedIntCheck, (itk::Concept::Convertible<LabelledImagePixelType, unsigned int>));
289  itkConceptMacro(ClassifiedConvertibleToIntCheck, (itk::Concept::Convertible<LabelledImagePixelType, int>));
290  itkConceptMacro(IntConvertibleToClassifiedCheck, (itk::Concept::Convertible<int, LabelledImagePixelType>));
291  itkConceptMacro(SameDimensionCheck, (itk::Concept::SameDimension<InputImageDimension, ClassifiedImageDimension>));
292 
294 #endif
295 
297  void InitializeSeed(int seed)
298  {
299  m_Generator->SetSeed(seed);
300  }
302  {
303  m_Generator->SetSeed();
304  }
306 
307 protected:
310  {
311  }
312  void PrintSelf(std::ostream& os, itk::Indent indent) const override;
313 
317  void Allocate();
318 
322  void Initialize();
323 
324  virtual void ApplyMarkovRandomFieldFilter();
325 
326  void GenerateData() override;
327  void GenerateInputRequestedRegion() override;
328  void EnlargeOutputRequestedRegion(itk::DataObject*) override;
329  void GenerateOutputInformation() override;
330 
331  MarkovRandomFieldFilter(const Self&) = delete;
332  void operator=(const Self&) = delete;
333 
334  typedef typename TInputImage::SizeType InputImageSizeType;
335 
338 
339  unsigned int m_NumberOfClasses;
341 
344 
350 
351  unsigned int m_NumberOfIterations;
352 
353  double m_Lambda;
356 
358 
359  std::vector<double> m_MRFNeighborhoodWeight;
360  std::vector<double> m_NeighborInfluence;
361  std::vector<double> m_DummyVector;
362 
363  RandomGeneratorType::Pointer m_Generator;
364 
371 
372  virtual void MinimizeOnce();
373 
374 private:
375 }; // class MarkovRandomFieldFilter
376 
377 } // namespace otb
378 
379 #ifndef OTB_MANUAL_INSTANTIATION
381 #endif
382 
383 #endif
otb::MarkovRandomFieldFilter::m_StopCondition
StopConditionType m_StopCondition
Definition: otbMarkovRandomFieldFilter.h:355
otb::MarkovRandomFieldFilter::InputImageFaceListType
InputImageFacesCalculator::FaceListType InputImageFaceListType
Definition: otbMarkovRandomFieldFilter.h:168
otb::MarkovRandomFieldFilter::m_NumberOfClasses
unsigned int m_NumberOfClasses
Definition: otbMarkovRandomFieldFilter.h:339
otb::MRFOptimizer::Pointer
itk::SmartPointer< Self > Pointer
Definition: otbMRFOptimizer.h:48
otb::MarkovRandomFieldFilter::TrainingImagePixelType
TClassifiedImage::PixelType TrainingImagePixelType
Definition: otbMarkovRandomFieldFilter.h:125
otb::MarkovRandomFieldFilter::LabelledImageOffsetType
TClassifiedImage::OffsetType LabelledImageOffsetType
Definition: otbMarkovRandomFieldFilter.h:145
otb::MarkovRandomFieldFilter::LabelledImagePixelType
TClassifiedImage::PixelType LabelledImagePixelType
Definition: otbMarkovRandomFieldFilter.h:134
otb::MarkovRandomFieldFilter::m_Generator
RandomGeneratorType::Pointer m_Generator
Definition: otbMarkovRandomFieldFilter.h:363
otb::MarkovRandomFieldFilter::TrainingImageType
TClassifiedImage TrainingImageType
Definition: otbMarkovRandomFieldFilter.h:121
otb::MRFEnergy::Pointer
itk::SmartPointer< Self > Pointer
Definition: otbMRFEnergy.h:48
otb::MarkovRandomFieldFilter::GetNeighborhoodRadius
const NeighborhoodRadiusType GetNeighborhoodRadius() const
Definition: otbMarkovRandomFieldFilter.h:254
otb::MarkovRandomFieldFilter::OptimizerType
MRFOptimizer OptimizerType
Definition: otbMarkovRandomFieldFilter.h:196
otb::MRFSampler::Pointer
itk::SmartPointer< Self > Pointer
Definition: otbMRFSampler.h:49
otb::MarkovRandomFieldFilter::InputImagePointer
TInputImage::Pointer InputImagePointer
Definition: otbMarkovRandomFieldFilter.h:104
otb::MarkovRandomFieldFilter::m_MRFNeighborhoodWeight
std::vector< double > m_MRFNeighborhoodWeight
Definition: otbMarkovRandomFieldFilter.h:359
otb::MarkovRandomFieldFilter::Superclass
itk::ImageToImageFilter< TInputImage, TClassifiedImage > Superclass
Definition: otbMarkovRandomFieldFilter.h:91
otb::MarkovRandomFieldFilter::m_NumberOfIterations
unsigned int m_NumberOfIterations
Definition: otbMarkovRandomFieldFilter.h:351
otb::MarkovRandomFieldFilter::TrainingImagePointer
TClassifiedImage::Pointer TrainingImagePointer
Definition: otbMarkovRandomFieldFilter.h:122
otbMRFSampler.h
otb::MarkovRandomFieldFilter::LabelledImageType
TClassifiedImage LabelledImageType
Definition: otbMarkovRandomFieldFilter.h:129
otb::MarkovRandomFieldFilter::LabelledImageNeighborhoodIterator
itk::NeighborhoodIterator< TClassifiedImage > LabelledImageNeighborhoodIterator
Definition: otbMarkovRandomFieldFilter.h:173
otbMarkovRandomFieldFilter.hxx
otb::MarkovRandomFieldFilter::LabelledImageRegionType
TClassifiedImage::RegionType LabelledImageRegionType
Definition: otbMarkovRandomFieldFilter.h:138
otb::MarkovRandomFieldFilter::OutputImagePointer
Superclass::OutputImagePointer OutputImagePointer
Definition: otbMarkovRandomFieldFilter.h:94
otb::MarkovRandomFieldFilter::Pointer
itk::SmartPointer< Self > Pointer
Definition: otbMarkovRandomFieldFilter.h:92
otb::MarkovRandomFieldFilter::InputImagePixelType
TInputImage::PixelType InputImagePixelType
Definition: otbMarkovRandomFieldFilter.h:108
otb::MarkovRandomFieldFilter::m_ExternalClassificationSet
bool m_ExternalClassificationSet
Definition: otbMarkovRandomFieldFilter.h:354
otb::MarkovRandomFieldFilter::m_Lambda
double m_Lambda
Definition: otbMarkovRandomFieldFilter.h:353
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::MarkovRandomFieldFilter::m_TotalNumberOfPixelsInInputImage
int m_TotalNumberOfPixelsInInputImage
Definition: otbMarkovRandomFieldFilter.h:347
otb::MarkovRandomFieldFilter::m_Optimizer
OptimizerPointer m_Optimizer
Definition: otbMarkovRandomFieldFilter.h:369
otb::MarkovRandomFieldFilter::LabelledImageFaceListType
LabelledImageFacesCalculator::FaceListType LabelledImageFaceListType
Definition: otbMarkovRandomFieldFilter.h:179
otbMacro.h
otb::MarkovRandomFieldFilter::LabelledImageRegionConstIterator
itk::ImageRegionConstIterator< TClassifiedImage > LabelledImageRegionConstIterator
Definition: otbMarkovRandomFieldFilter.h:150
otb::MarkovRandomFieldFilter::LabelledImageFacesCalculator
itk::NeighborhoodAlgorithm::ImageBoundaryFacesCalculator< TClassifiedImage > LabelledImageFacesCalculator
Definition: otbMarkovRandomFieldFilter.h:177
otb::MarkovRandomFieldFilter::InputImageConstPointer
TInputImage::ConstPointer InputImageConstPointer
Definition: otbMarkovRandomFieldFilter.h:105
otb::MarkovRandomFieldFilter::m_EnergyRegularization
EnergyRegularizationPointer m_EnergyRegularization
Definition: otbMarkovRandomFieldFilter.h:367
otb::MarkovRandomFieldFilter::SizeType
TInputImage::SizeType SizeType
Definition: otbMarkovRandomFieldFilter.h:156
otb::MarkovRandomFieldFilter::IndexValueType
LabelledImageIndexType::IndexValueType IndexValueType
Definition: otbMarkovRandomFieldFilter.h:142
otb::MarkovRandomFieldFilter::ConstPointer
itk::SmartPointer< const Self > ConstPointer
Definition: otbMarkovRandomFieldFilter.h:93
otb::MarkovRandomFieldFilter::~MarkovRandomFieldFilter
~MarkovRandomFieldFilter() override
Definition: otbMarkovRandomFieldFilter.h:309
otb::MarkovRandomFieldFilter::StopConditionType
StopConditionType
Definition: otbMarkovRandomFieldFilter.h:276
otb::MarkovRandomFieldFilter::m_ImageDeltaEnergy
double m_ImageDeltaEnergy
Definition: otbMarkovRandomFieldFilter.h:343
otb::MarkovRandomFieldFilter::SamplerPointer
SamplerType::Pointer SamplerPointer
Definition: otbMarkovRandomFieldFilter.h:194
otb::MarkovRandomFieldFilter::m_MaximumNumberOfIterations
unsigned int m_MaximumNumberOfIterations
Definition: otbMarkovRandomFieldFilter.h:340
otb::MarkovRandomFieldFilter::InputImageRegionIterator
itk::ImageRegionIterator< TInputImage > InputImageRegionIterator
Definition: otbMarkovRandomFieldFilter.h:114
otb::MarkovRandomFieldFilter::InputImageFacesCalculator
itk::NeighborhoodAlgorithm::ImageBoundaryFacesCalculator< TInputImage > InputImageFacesCalculator
Definition: otbMarkovRandomFieldFilter.h:166
otb::MarkovRandomFieldFilter::m_InputImageNeighborhoodRadius
InputImageNeighborhoodRadiusType m_InputImageNeighborhoodRadius
Definition: otbMarkovRandomFieldFilter.h:336
otb::MarkovRandomFieldFilter::m_LabelledImageNeighborhoodRadius
LabelledImageNeighborhoodRadiusType m_LabelledImageNeighborhoodRadius
Definition: otbMarkovRandomFieldFilter.h:337
otb::MarkovRandomFieldFilter::InputImageType
TInputImage InputImageType
Definition: otbMarkovRandomFieldFilter.h:100
otb::MarkovRandomFieldFilter::InputImageFaceListIterator
InputImageFaceListType::iterator InputImageFaceListIterator
Definition: otbMarkovRandomFieldFilter.h:170
otb::MarkovRandomFieldFilter::m_Sampler
SamplerPointer m_Sampler
Definition: otbMarkovRandomFieldFilter.h:370
otb::MarkovRandomFieldFilter::m_ErrorTolerance
double m_ErrorTolerance
Definition: otbMarkovRandomFieldFilter.h:348
otb::MarkovRandomFieldFilter::LabelledImagePointer
TClassifiedImage::Pointer LabelledImagePointer
Definition: otbMarkovRandomFieldFilter.h:130
otb::MarkovRandomFieldFilter::m_TrainingImage
TrainingImagePointer m_TrainingImage
Definition: otbMarkovRandomFieldFilter.h:357
otb::MarkovRandomFieldFilter::LabelledImageNeighborhoodRadiusType
LabelledImageNeighborhoodIterator::RadiusType LabelledImageNeighborhoodRadiusType
Definition: otbMarkovRandomFieldFilter.h:175
otb::MarkovRandomFieldFilter::LabelledImageRegionIterator
itk::ImageRegionIterator< TClassifiedImage > LabelledImageRegionIterator
Definition: otbMarkovRandomFieldFilter.h:148
otb::MarkovRandomFieldFilter::InputImageRegionConstIterator
itk::ImageRegionConstIterator< TInputImage > InputImageRegionConstIterator
Definition: otbMarkovRandomFieldFilter.h:115
otb::MarkovRandomFieldFilter::m_NeighborhoodRadius
int m_NeighborhoodRadius
Definition: otbMarkovRandomFieldFilter.h:345
otb::MarkovRandomFieldFilter::RandomGeneratorType
itk::Statistics::MersenneTwisterRandomVariateGenerator RandomGeneratorType
Definition: otbMarkovRandomFieldFilter.h:184
otb::MarkovRandomFieldFilter::m_EnergyFidelity
EnergyFidelityPointer m_EnergyFidelity
Definition: otbMarkovRandomFieldFilter.h:368
otb::MarkovRandomFieldFilter::LabelledImageIndexType
TClassifiedImage::IndexType LabelledImageIndexType
Definition: otbMarkovRandomFieldFilter.h:141
otb::MarkovRandomFieldFilter::m_ErrorCounter
int m_ErrorCounter
Definition: otbMarkovRandomFieldFilter.h:342
otb::MarkovRandomFieldFilter::NeighborhoodRadiusType
TInputImage::SizeType NeighborhoodRadiusType
Definition: otbMarkovRandomFieldFilter.h:159
otb::MRFSampler
This is the base class for sampler methods used in the MRF framework.
Definition: otbMRFSampler.h:44
otb::MarkovRandomFieldFilter::InitializeSeed
void InitializeSeed()
Definition: otbMarkovRandomFieldFilter.h:301
otb::MarkovRandomFieldFilter::SamplerType
MRFSampler< TInputImage, TClassifiedImage > SamplerType
Definition: otbMarkovRandomFieldFilter.h:193
otb::MarkovRandomFieldFilter::InputImageNeighborhoodIterator
itk::ConstNeighborhoodIterator< TInputImage > InputImageNeighborhoodIterator
Definition: otbMarkovRandomFieldFilter.h:162
otb::MarkovRandomFieldFilter::m_DummyVector
std::vector< double > m_DummyVector
Definition: otbMarkovRandomFieldFilter.h:361
otb::MarkovRandomFieldFilter::OptimizerPointer
OptimizerType::Pointer OptimizerPointer
Definition: otbMarkovRandomFieldFilter.h:197
otb::MRFOptimizer
This is the base class for optimizer used in the MRF framework.
Definition: otbMRFOptimizer.h:43
otb::MarkovRandomFieldFilter::EnergyFidelityPointer
EnergyFidelityType::Pointer EnergyFidelityPointer
Definition: otbMarkovRandomFieldFilter.h:191
otb::MarkovRandomFieldFilter
This is the class to use the Markov Random Field framework in OTB.
Definition: otbMarkovRandomFieldFilter.h:86
otb::MarkovRandomFieldFilter::EnergyRegularizationPointer
EnergyRegularizationType::Pointer EnergyRegularizationPointer
Definition: otbMarkovRandomFieldFilter.h:190
otb::MarkovRandomFieldFilter::EnergyRegularizationType
MRFEnergy< TClassifiedImage, TClassifiedImage > EnergyRegularizationType
Definition: otbMarkovRandomFieldFilter.h:187
otb::MarkovRandomFieldFilter::InputImageRegionType
TInputImage::RegionType InputImageRegionType
Definition: otbMarkovRandomFieldFilter.h:111
otb::MarkovRandomFieldFilter::EnergyFidelityType
MRFEnergy< TInputImage, TClassifiedImage > EnergyFidelityType
Definition: otbMarkovRandomFieldFilter.h:188
otbMRFOptimizer.h
otb::MarkovRandomFieldFilter::m_SmoothingFactor
double m_SmoothingFactor
Definition: otbMarkovRandomFieldFilter.h:349
otb::MarkovRandomFieldFilter::m_NeighborInfluence
std::vector< double > m_NeighborInfluence
Definition: otbMarkovRandomFieldFilter.h:360
otb::MRFEnergy
This is the base class for energy function used in the MRF framework.
Definition: otbMRFEnergy.h:43
otb::MarkovRandomFieldFilter::InputImageNeighborhoodRadiusType
InputImageNeighborhoodIterator::RadiusType InputImageNeighborhoodRadiusType
Definition: otbMarkovRandomFieldFilter.h:164
otb::MarkovRandomFieldFilter::InputImageSizeType
TInputImage::SizeType InputImageSizeType
Definition: otbMarkovRandomFieldFilter.h:334
otb::MarkovRandomFieldFilter::Self
MarkovRandomFieldFilter Self
Definition: otbMarkovRandomFieldFilter.h:90
otb::MarkovRandomFieldFilter::LabelledImageFaceListIterator
LabelledImageFaceListType::iterator LabelledImageFaceListIterator
Definition: otbMarkovRandomFieldFilter.h:181
otb::MarkovRandomFieldFilter::m_TotalNumberOfValidPixelsInOutputImage
int m_TotalNumberOfValidPixelsInOutputImage
Definition: otbMarkovRandomFieldFilter.h:346
otb::MarkovRandomFieldFilter::InitializeSeed
void InitializeSeed(int seed)
Definition: otbMarkovRandomFieldFilter.h:297