ITK  6.0.0
Insight Toolkit
itkMRFImageFilter.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * https://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef itkMRFImageFilter_h
19 #define itkMRFImageFilter_h
20 
21 #include "vnl/vnl_vector.h"
22 #include "vnl/vnl_matrix.h"
23 
24 #include "itkImageClassifierBase.h"
25 
26 #include "itkImageToImageFilter.h"
27 
30 #include "itkSize.h"
31 #include "ITKMarkovRandomFieldsClassifiersExport.h"
32 
33 namespace itk
34 {
40 {
41 public:
47  enum class MRFStop : uint8_t
48  {
51  };
52 };
53 
54 // Define how to print enumeration
55 extern ITKMarkovRandomFieldsClassifiers_EXPORT std::ostream &
56  operator<<(std::ostream & out, const MRFImageFilterEnums::MRFStop value);
148 template <typename TInputImage, typename TClassifiedImage>
149 class ITK_TEMPLATE_EXPORT MRFImageFilter : public ImageToImageFilter<TInputImage, TClassifiedImage>
150 {
151 public:
152  ITK_DISALLOW_COPY_AND_MOVE(MRFImageFilter);
160  using typename Superclass::OutputImagePointer;
161 
163  itkNewMacro(Self);
164 
166  itkOverrideGetNameOfClassMacro(MRFImageFilter);
167 
169  using InputImageType = TInputImage;
172 
174  using InputImagePixelType = typename TInputImage::PixelType;
175 
178 
182 
184  static constexpr unsigned int InputImageDimension = TInputImage::ImageDimension;
185 
188 
190  using TrainingImagePixelType = typename TClassifiedImage::PixelType;
191 
195 
198  using LabelledImagePixelType = typename TClassifiedImage::PixelType;
199 
203 
207 
209  using LabelledImageOffsetType = typename TClassifiedImage::OffsetType;
210 
213 
215  static constexpr unsigned int ClassifiedImageDimension = TClassifiedImage::ImageDimension;
216 
219 
221  using SizeType = typename TInputImage::SizeType;
222 
225 
228 
230 
232 
234 
235  using InputImageFaceListIterator = typename InputImageFaceListType::iterator;
236 
239 
241 
243 
245 
246  using LabelledImageFaceListIterator = typename LabelledImageFaceListType::iterator;
247 
249  void
250  SetClassifier(typename ClassifierType::Pointer ptrToClassifier);
251 
253  itkSetMacro(NumberOfClasses, unsigned int);
254  itkGetConstMacro(NumberOfClasses, unsigned int);
259  itkSetMacro(MaximumNumberOfIterations, unsigned int);
260  itkGetConstMacro(MaximumNumberOfIterations, unsigned int);
265  itkSetMacro(ErrorTolerance, double);
266  itkGetConstMacro(ErrorTolerance, double);
271  itkSetMacro(SmoothingFactor, double);
272  itkGetConstMacro(SmoothingFactor, double);
276  void
277  SetNeighborhoodRadius(const NeighborhoodRadiusType &);
278 
283  void
284  SetNeighborhoodRadius(const SizeValueType);
285 
286  void
287  SetNeighborhoodRadius(const SizeValueType * radiusArray);
288 
292  {
293  NeighborhoodRadiusType radius;
294 
295  for (int i = 0; i < InputImageDimension; ++i)
296  {
297  radius[i] = m_InputImageNeighborhoodRadius[i];
298  }
299  return radius;
300  }
301 
307  virtual void
308  SetMRFNeighborhoodWeight(std::vector<double> betaMatrix);
309 
310  virtual std::vector<double>
312  {
313  return m_MRFNeighborhoodWeight;
314  }
315 
317 #if !defined(ITK_LEGACY_REMOVE)
318 
319  // We need to expose the enum values at the class level
320  // for backwards compatibility
321  static constexpr MRFStopEnum MaximumNumberOfIterations = MRFStopEnum::MaximumNumberOfIterations;
322  static constexpr MRFStopEnum ErrorTolerance = MRFStopEnum::ErrorTolerance;
323 #endif
324 
327  itkGetConstReferenceMacro(StopCondition, MRFStopEnum);
328 
329  /* Get macro for number of iterations */
330  itkGetConstReferenceMacro(NumberOfIterations, unsigned int);
331 
332 #ifdef ITK_USE_CONCEPT_CHECKING
333  // Begin concept checking
334  itkConceptMacro(UnsignedIntConvertibleToClassifiedCheck,
336  itkConceptMacro(ClassifiedConvertibleToUnsignedIntCheck,
338  itkConceptMacro(ClassifiedConvertibleToIntCheck, (Concept::Convertible<LabelledImagePixelType, int>));
339  itkConceptMacro(IntConvertibleToClassifiedCheck, (Concept::Convertible<int, LabelledImagePixelType>));
341  // End concept checking
342 #endif
343 
344 protected:
345  MRFImageFilter();
346  ~MRFImageFilter() override = default;
347  void
348  PrintSelf(std::ostream & os, Indent indent) const override;
349 
351  void
352  Allocate();
353 
358  virtual void
359  ApplyMRFImageFilter();
360 
362  virtual void
363  MinimizeFunctional();
364 
370 
373 
375  virtual void
376  DoNeighborhoodOperation(const InputImageNeighborhoodIterator & imageIter,
377  LabelledImageNeighborhoodIterator & labelledIter,
378  LabelStatusImageNeighborhoodIterator & labelStatusIter);
379 
380  void
381  GenerateData() override;
382 
383  void
384  GenerateInputRequestedRegion() override;
385 
386  void
387  EnlargeOutputRequestedRegion(DataObject *) override;
388 
389  void
390  GenerateOutputInformation() override;
391 
392 private:
394 
396 
398 
400 
401  using LabelStatusImageFaceListIterator = typename LabelStatusImageFaceListType::iterator;
402 
403  InputImageNeighborhoodRadiusType m_InputImageNeighborhoodRadius{};
404  LabelledImageNeighborhoodRadiusType m_LabelledImageNeighborhoodRadius{};
405  LabelStatusImageNeighborhoodRadiusType m_LabelStatusImageNeighborhoodRadius{};
406 
407  unsigned int m_NumberOfClasses{ 0 };
408  unsigned int m_MaximumNumberOfIterations{ 50 };
409  unsigned int m_KernelSize{};
410 
411  int m_ErrorCounter{ 0 };
412  int m_NeighborhoodSize{ 27 };
413  int m_TotalNumberOfValidPixelsInOutputImage{ 1 };
414  int m_TotalNumberOfPixelsInInputImage{ 1 };
415  double m_ErrorTolerance{ 0.2 };
416  double m_SmoothingFactor{ 1 };
417  double * m_ClassProbability{ nullptr }; // Class likelihood
418  unsigned int m_NumberOfIterations{ 0 };
419  MRFStopEnum m_StopCondition{ MRFStopEnum::MaximumNumberOfIterations };
420 
421  LabelStatusImagePointer m_LabelStatusImage{};
422 
423  std::vector<double> m_MRFNeighborhoodWeight{};
424  std::vector<double> m_NeighborInfluence{};
425  std::vector<double> m_MahalanobisDistance{};
426  std::vector<double> m_DummyVector{};
427 
429  typename ClassifierType::Pointer m_ClassifierPtr{};
430 
434  virtual void
435  SetDefaultMRFNeighborhoodWeight();
436 
438  void
439  ApplyICMLabeller();
440 }; // class MRFImageFilter
441 } // namespace itk
442 
443 #ifndef ITK_MANUAL_INSTANTIATION
444 # include "itkMRFImageFilter.hxx"
445 #endif
446 
447 #endif
itk::NeighborhoodAlgorithm::ImageBoundaryFacesCalculator
Splits an image into a main region and several "face" regions which are used to handle computations o...
Definition: itkNeighborhoodAlgorithm.h:63
Pointer
SmartPointer< Self > Pointer
Definition: itkAddImageFilter.h:93
ConstPointer
SmartPointer< const Self > ConstPointer
Definition: itkAddImageFilter.h:94
itkConstNeighborhoodIterator.h
itkNeighborhoodAlgorithm.h
itk::MRFImageFilter
Implementation of a labeler object that uses Markov Random Fields to classify pixels in an image data...
Definition: itkMRFImageFilter.h:149
itk::MRFImageFilterEnums::MRFStop::ErrorTolerance
itk::Size
Represent a n-dimensional size (bounds) of a n-dimensional image.
Definition: itkSize.h:69
itk::MRFImageFilter::LabelledImageNeighborhoodRadiusType
typename LabelledImageNeighborhoodIterator::RadiusType LabelledImageNeighborhoodRadiusType
Definition: itkMRFImageFilter.h:240
itk::MRFImageFilter::LabelStatusIndexType
typename LabelStatusImageType::IndexType LabelStatusIndexType
Definition: itkMRFImageFilter.h:366
itk::MRFImageFilter::InputImageConstPointer
typename TInputImage::ConstPointer InputImageConstPointer
Definition: itkMRFImageFilter.h:171
itk::MRFImageFilter::InputImageNeighborhoodRadiusType
typename InputImageNeighborhoodIterator::RadiusType InputImageNeighborhoodRadiusType
Definition: itkMRFImageFilter.h:229
itk::MRFImageFilter::LabelledImageFaceListType
typename LabelledImageFacesCalculator::FaceListType LabelledImageFaceListType
Definition: itkMRFImageFilter.h:244
itk::MRFImageFilterEnums::MRFStop
MRFStop
Definition: itkMRFImageFilter.h:47
itk::GTest::TypedefsAndConstructors::Dimension2::SizeType
ImageBaseType::SizeType SizeType
Definition: itkGTestTypedefsAndConstructors.h:49
itk::MRFImageFilterEnums::MRFStop::MaximumNumberOfIterations
itk::MRFImageFilterEnums
Contains all enum classes in MRFImageFilter class;.
Definition: itkMRFImageFilter.h:39
itk::operator<<
ITKCommon_EXPORT std::ostream & operator<<(std::ostream &out, typename AnatomicalOrientation::CoordinateEnum value)
itk::MRFImageFilter::LabelStatusImageFaceListIterator
typename LabelStatusImageFaceListType::iterator LabelStatusImageFaceListIterator
Definition: itkMRFImageFilter.h:401
itk::SmartPointer< Self >
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::MRFImageFilter::LabelStatusImageNeighborhoodRadiusType
typename LabelStatusImageNeighborhoodIterator::RadiusType LabelStatusImageNeighborhoodRadiusType
Definition: itkMRFImageFilter.h:395
itk::MRFImageFilter::LabelledImagePointer
typename TClassifiedImage::Pointer LabelledImagePointer
Definition: itkMRFImageFilter.h:194
itk::IndexValueType
long IndexValueType
Definition: itkIntTypes.h:93
itk::MRFImageFilter::TrainingImagePointer
typename TClassifiedImage::Pointer TrainingImagePointer
Definition: itkMRFImageFilter.h:187
itk::Concept::SameDimension
Definition: itkConceptChecking.h:697
itk::ImageRegionIterator
A multi-dimensional iterator templated over image type that walks a region of pixels.
Definition: itkImageRegionIterator.h:80
itk::GTest::TypedefsAndConstructors::Dimension2::IndexType
ImageBaseType::IndexType IndexType
Definition: itkGTestTypedefsAndConstructors.h:50
itk::MRFImageFilter::LabelStatusImageFaceListType
typename LabelStatusImageFacesCalculator::FaceListType LabelStatusImageFaceListType
Definition: itkMRFImageFilter.h:399
itk::ImageToImageFilter
Base class for filters that take an image as input and produce an image as output.
Definition: itkImageToImageFilter.h:108
itk::LightObject
Light weight base class for most itk classes.
Definition: itkLightObject.h:55
itk::MRFImageFilter::IndexValueType
typename LabelledImageIndexType::IndexValueType IndexValueType
Definition: itkMRFImageFilter.h:206
itk::MRFImageFilter::LabelledImageIndexType
typename TClassifiedImage::IndexType LabelledImageIndexType
Definition: itkMRFImageFilter.h:205
itk::MRFImageFilter::InputImageRegionType
typename TInputImage::RegionType InputImageRegionType
Definition: itkMRFImageFilter.h:177
itk::ImageClassifierBase
Base class for the ImageClassifierBase object.
Definition: itkImageClassifierBase.h:71
itk::GTest::TypedefsAndConstructors::Dimension2::RegionType
ImageBaseType::RegionType RegionType
Definition: itkGTestTypedefsAndConstructors.h:54
itk::MRFImageFilter::InputImageSizeType
typename TInputImage::SizeType InputImageSizeType
Definition: itkMRFImageFilter.h:393
itk::MRFImageFilter::LabelStatusImagePointer
typename LabelStatusImageType::Pointer LabelStatusImagePointer
Definition: itkMRFImageFilter.h:368
itk::MRFImageFilter::InputImagePixelType
typename TInputImage::PixelType InputImagePixelType
Definition: itkMRFImageFilter.h:174
itk::MRFImageFilter::GetMRFNeighborhoodWeight
virtual std::vector< double > GetMRFNeighborhoodWeight()
Definition: itkMRFImageFilter.h:311
MRFStop
itk::MRFImageFilter::InputImagePointer
typename TInputImage::Pointer InputImagePointer
Definition: itkMRFImageFilter.h:170
itkImageToImageFilter.h
itk::MRFImageFilter::GetNeighborhoodRadius
const NeighborhoodRadiusType GetNeighborhoodRadius() const
Definition: itkMRFImageFilter.h:291
itk::MRFImageFilter::InputImageFaceListIterator
typename InputImageFaceListType::iterator InputImageFaceListIterator
Definition: itkMRFImageFilter.h:235
itk::MRFImageFilter::LabelledImageRegionType
typename TClassifiedImage::RegionType LabelledImageRegionType
Definition: itkMRFImageFilter.h:202
itk::MRFImageFilter::InputImageFaceListType
typename InputImageFacesCalculator::FaceListType InputImageFaceListType
Definition: itkMRFImageFilter.h:233
itk::MRFImageFilter::InputImageType
TInputImage InputImageType
Definition: itkMRFImageFilter.h:169
itk::NeighborhoodIterator
Defines iteration of a local N-dimensional neighborhood of pixels across an itk::Image.
Definition: itkNeighborhoodIterator.h:212
itk::MRFImageFilter::NeighborhoodRadiusType
typename TInputImage::SizeType NeighborhoodRadiusType
Definition: itkMRFImageFilter.h:224
itkConceptMacro
#define itkConceptMacro(name, concept)
Definition: itkConceptChecking.h:65
itk::NeighborhoodAlgorithm::ImageBoundaryFacesCalculator::FaceListType
std::list< RegionType > FaceListType
Definition: itkNeighborhoodAlgorithm.h:69
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnatomicalOrientation.h:29
itk::ConstNeighborhoodIterator< TInputImage >
itkImageClassifierBase.h
itk::MRFImageFilter::SizeType
typename TInputImage::SizeType SizeType
Definition: itkMRFImageFilter.h:221
itk::Concept::Convertible
Definition: itkConceptChecking.h:217
itk::MRFImageFilter::LabelledImagePixelType
typename TClassifiedImage::PixelType LabelledImagePixelType
Definition: itkMRFImageFilter.h:198
itk::ImageRegionConstIterator
A multi-dimensional iterator templated over image type that walks a region of pixels.
Definition: itkImageRegionConstIterator.h:109
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:88
itk::MRFImageFilter::LabelledImageOffsetType
typename TClassifiedImage::OffsetType LabelledImageOffsetType
Definition: itkMRFImageFilter.h:209
itk::MRFImageFilter::TrainingImagePixelType
typename TClassifiedImage::PixelType TrainingImagePixelType
Definition: itkMRFImageFilter.h:190
StopCondition
itk::MRFImageFilter::LabelStatusRegionType
typename LabelStatusImageType::RegionType LabelStatusRegionType
Definition: itkMRFImageFilter.h:367
itk::MRFImageFilter::LabelledImageFaceListIterator
typename LabelledImageFaceListType::iterator LabelledImageFaceListIterator
Definition: itkMRFImageFilter.h:246
itk::SizeValueType
unsigned long SizeValueType
Definition: itkIntTypes.h:86
itkSize.h
itk::DataObject
Base class for all data objects in ITK.
Definition: itkDataObject.h:293