ITK  5.2.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  * http://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_ASSIGN(MRFImageFilter);
154 
160  using OutputImagePointer = typename Superclass::OutputImagePointer;
161 
163  itkNewMacro(Self);
164 
166  itkTypeMacro(MRFImageFilter, Object);
167 
169  using InputImageType = TInputImage;
170  using InputImagePointer = typename TInputImage::Pointer;
171  using InputImageConstPointer = typename TInputImage::ConstPointer;
172 
174  using InputImagePixelType = typename TInputImage::PixelType;
175 
178 
182 
184  static constexpr unsigned int InputImageDimension = TInputImage::ImageDimension;
185 
187  using TrainingImagePointer = typename TClassifiedImage::Pointer;
188 
190  using TrainingImagePixelType = typename TClassifiedImage::PixelType;
191 
194  using LabelledImagePointer = typename TClassifiedImage::Pointer;
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 
229  using InputImageNeighborhoodRadiusType = typename InputImageNeighborhoodIterator::RadiusType;
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);
256 
259  itkSetMacro(MaximumNumberOfIterations, unsigned int);
260  itkGetConstMacro(MaximumNumberOfIterations, unsigned int);
262 
265  itkSetMacro(ErrorTolerance, double);
266  itkGetConstMacro(ErrorTolerance, double);
268 
271  itkSetMacro(SmoothingFactor, double);
272  itkGetConstMacro(SmoothingFactor, double);
274 
276  void
277  SetNeighborhoodRadius(const NeighborhoodRadiusType &);
278 
282  void
283  SetNeighborhoodRadius(const SizeValueType);
284 
285  void
286  SetNeighborhoodRadius(const SizeValueType * radiusArray);
287 
291  {
292  NeighborhoodRadiusType radius;
293 
294  for (int i = 0; i < InputImageDimension; ++i)
295  {
296  radius[i] = m_InputImageNeighborhoodRadius[i];
297  }
298  return radius;
299  }
300 
306  virtual void
307  SetMRFNeighborhoodWeight(std::vector<double> BetaMatrix);
308 
309  virtual std::vector<double>
311  {
312  return m_MRFNeighborhoodWeight;
313  }
314 
316 #if !defined(ITK_LEGACY_REMOVE)
317 
318  // We need to expose the enum values at the class level
319  // for backwards compatibility
320  static constexpr MRFStopEnum MaximumNumberOfIterations = MRFStopEnum::MaximumNumberOfIterations;
321  static constexpr MRFStopEnum ErrorTolerance = MRFStopEnum::ErrorTolerance;
322 #endif
323 
326  itkGetConstReferenceMacro(StopCondition, MRFStopEnum);
327 
328  /* Get macro for number of iterations */
329  itkGetConstReferenceMacro(NumberOfIterations, unsigned int);
330 
331 #ifdef ITK_USE_CONCEPT_CHECKING
332  // Begin concept checking
333  itkConceptMacro(UnsignedIntConvertibleToClassifiedCheck,
335  itkConceptMacro(ClassifiedConvertibleToUnsignedIntCheck,
337  itkConceptMacro(ClassifiedConvertibleToIntCheck, (Concept::Convertible<LabelledImagePixelType, int>));
338  itkConceptMacro(IntConvertibleToClassifiedCheck, (Concept::Convertible<int, LabelledImagePixelType>));
340  // End concept checking
341 #endif
342 
343 protected:
344  MRFImageFilter();
345  ~MRFImageFilter() override = default;
346  void
347  PrintSelf(std::ostream & os, Indent indent) const override;
348 
350  void
351  Allocate();
352 
357  virtual void
358  ApplyMRFImageFilter();
359 
361  virtual void
362  MinimizeFunctional();
363 
369 
372  // Function implementing the neighborhood operation
373 
374  virtual void
375  DoNeighborhoodOperation(const InputImageNeighborhoodIterator & imageIter,
376  LabelledImageNeighborhoodIterator & labelledIter,
377  LabelStatusImageNeighborhoodIterator & labelStatusIter);
378 
379  void
380  GenerateData() override;
381 
382  void
383  GenerateInputRequestedRegion() override;
384 
385  void
386  EnlargeOutputRequestedRegion(DataObject *) override;
387 
388  void
389  GenerateOutputInformation() override;
390 
391 private:
393 
395 
397 
399 
400  using LabelStatusImageFaceListIterator = typename LabelStatusImageFaceListType::iterator;
401 
405 
406  unsigned int m_NumberOfClasses{ 0 };
407  unsigned int m_MaximumNumberOfIterations{ 50 };
408  unsigned int m_KernelSize;
409 
410  int m_ErrorCounter{ 0 };
411  int m_NeighborhoodSize{ 27 };
412  int m_TotalNumberOfValidPixelsInOutputImage{ 1 };
413  int m_TotalNumberOfPixelsInInputImage{ 1 };
414  double m_ErrorTolerance{ 0.2 };
415  double m_SmoothingFactor{ 1 };
416  double * m_ClassProbability{ nullptr }; // Class liklihood
417  unsigned int m_NumberOfIterations{ 0 };
418  MRFStopEnum m_StopCondition{ MRFStopEnum::MaximumNumberOfIterations };
419 
421 
422  std::vector<double> m_MRFNeighborhoodWeight;
423  std::vector<double> m_NeighborInfluence;
424  std::vector<double> m_MahalanobisDistance;
425  std::vector<double> m_DummyVector;
426 
429 
433  virtual void
434  SetDefaultMRFNeighborhoodWeight();
435 
436  // Function implementing the ICM algorithm to label the images
437  void
438  ApplyICMLabeller();
439 }; // class MRFImageFilter
440 } // namespace itk
441 
442 #ifndef ITK_MANUAL_INSTANTIATION
443 # include "itkMRFImageFilter.hxx"
444 #endif
445 
446 #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
itk::uint8_t
::uint8_t uint8_t
Definition: itkIntTypes.h:29
itk::MRFImageFilter::m_MahalanobisDistance
std::vector< double > m_MahalanobisDistance
Definition: itkMRFImageFilter.h:424
itk::MRFImageFilter::m_LabelledImageNeighborhoodRadius
LabelledImageNeighborhoodRadiusType m_LabelledImageNeighborhoodRadius
Definition: itkMRFImageFilter.h:403
itkConstNeighborhoodIterator.h
itkNeighborhoodAlgorithm.h
itk::MRFImageFilter::m_DummyVector
std::vector< double > m_DummyVector
Definition: itkMRFImageFilter.h:425
itk::MRFImageFilter
Implementation of a labeller object that uses Markov Random Fields to classify pixels in an image dat...
Definition: itkMRFImageFilter.h:149
itk::MRFImageFilterEnums::MRFStop::ErrorTolerance
itk::MRFImageFilter::LabelledImageNeighborhoodRadiusType
typename LabelledImageNeighborhoodIterator::RadiusType LabelledImageNeighborhoodRadiusType
Definition: itkMRFImageFilter.h:240
itk::MRFImageFilter::LabelStatusIndexType
typename LabelStatusImageType::IndexType LabelStatusIndexType
Definition: itkMRFImageFilter.h:365
itk::operator<<
std::ostream & operator<<(std::ostream &os, const Array< TValue > &arr)
Definition: itkArray.h:213
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::MRFImageFilter::LabelStatusImageFaceListIterator
typename LabelStatusImageFaceListType::iterator LabelStatusImageFaceListIterator
Definition: itkMRFImageFilter.h:400
itk::SmartPointer< Self >
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::MRFImageFilter::LabelStatusImageNeighborhoodRadiusType
typename LabelStatusImageNeighborhoodIterator::RadiusType LabelStatusImageNeighborhoodRadiusType
Definition: itkMRFImageFilter.h:394
itk::MRFImageFilter::LabelledImagePointer
typename TClassifiedImage::Pointer LabelledImagePointer
Definition: itkMRFImageFilter.h:194
itk::MRFImageFilter::m_MRFNeighborhoodWeight
std::vector< double > m_MRFNeighborhoodWeight
Definition: itkMRFImageFilter.h:422
itk::MRFImageFilter::m_LabelStatusImage
LabelStatusImagePointer m_LabelStatusImage
Definition: itkMRFImageFilter.h:420
itk::MRFImageFilter::m_NeighborInfluence
std::vector< double > m_NeighborInfluence
Definition: itkMRFImageFilter.h:423
itk::MRFImageFilter::TrainingImagePointer
typename TClassifiedImage::Pointer TrainingImagePointer
Definition: itkMRFImageFilter.h:187
itk::Concept::SameDimension
Definition: itkConceptChecking.h:692
itk::ImageRegionIterator
A multi-dimensional iterator templated over image type that walks a region of pixels.
Definition: itkImageRegionIterator.h:78
itk::GTest::TypedefsAndConstructors::Dimension2::IndexType
ImageBaseType::IndexType IndexType
Definition: itkGTestTypedefsAndConstructors.h:50
itk::MRFImageFilter::LabelStatusImageFaceListType
typename LabelStatusImageFacesCalculator::FaceListType LabelStatusImageFaceListType
Definition: itkMRFImageFilter.h:398
itk::ImageToImageFilter
Base class for filters that take an image as input and produce an image as output.
Definition: itkImageToImageFilter.h:108
itk::MRFImageFilter::OutputImagePointer
typename Superclass::OutputImagePointer OutputImagePointer
Definition: itkMRFImageFilter.h:160
itk::LightObject
Light weight base class for most itk classes.
Definition: itkLightObject.h:59
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::MRFImageFilter::m_LabelStatusImageNeighborhoodRadius
LabelStatusImageNeighborhoodRadiusType m_LabelStatusImageNeighborhoodRadius
Definition: itkMRFImageFilter.h:404
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:392
itk::NeighborhoodIterator::RadiusType
typename Superclass::RadiusType RadiusType
Definition: itkNeighborhoodIterator.h:226
itk::MRFImageFilter::LabelStatusImagePointer
typename LabelStatusImageType::Pointer LabelStatusImagePointer
Definition: itkMRFImageFilter.h:367
itk::MRFImageFilter::InputImagePixelType
typename TInputImage::PixelType InputImagePixelType
Definition: itkMRFImageFilter.h:174
itk::MRFImageFilter::GetMRFNeighborhoodWeight
virtual std::vector< double > GetMRFNeighborhoodWeight()
Definition: itkMRFImageFilter.h:310
MRFStop
itk::MRFImageFilter::InputImagePointer
typename TInputImage::Pointer InputImagePointer
Definition: itkMRFImageFilter.h:170
itkImageToImageFilter.h
itk::MRFImageFilter::GetNeighborhoodRadius
const NeighborhoodRadiusType GetNeighborhoodRadius() const
Definition: itkMRFImageFilter.h:290
itk::MRFImageFilter::InputImageFaceListIterator
typename InputImageFaceListType::iterator InputImageFaceListIterator
Definition: itkMRFImageFilter.h:235
itk::MRFImageFilter::LabelledImageRegionType
typename TClassifiedImage::RegionType LabelledImageRegionType
Definition: itkMRFImageFilter.h:202
itk::MRFImageFilter::m_ClassifierPtr
ClassifierType::Pointer m_ClassifierPtr
Definition: itkMRFImageFilter.h:428
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:211
itk::MRFImageFilter::NeighborhoodRadiusType
typename TInputImage::SizeType NeighborhoodRadiusType
Definition: itkMRFImageFilter.h:224
itkConceptMacro
#define itkConceptMacro(name, concept)
Definition: itkConceptChecking.h:64
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: itkAnnulusOperator.h:24
itk::ConstNeighborhoodIterator< TInputImage >
itkImageClassifierBase.h
itk::MRFImageFilter::SizeType
typename TInputImage::SizeType SizeType
Definition: itkMRFImageFilter.h:221
itk::MRFImageFilter::m_InputImageNeighborhoodRadius
InputImageNeighborhoodRadiusType m_InputImageNeighborhoodRadius
Definition: itkMRFImageFilter.h:402
itk::Concept::Convertible
Definition: itkConceptChecking.h:214
itk::IndexValueType
signed long IndexValueType
Definition: itkIntTypes.h:90
itk::Object
Base class for most ITK classes.
Definition: itkObject.h:60
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:107
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:86
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::m_KernelSize
unsigned int m_KernelSize
Definition: itkMRFImageFilter.h:408
itk::MRFImageFilter::LabelStatusRegionType
typename LabelStatusImageType::RegionType LabelStatusRegionType
Definition: itkMRFImageFilter.h:366
itk::MRFImageFilter::LabelledImageFaceListIterator
typename LabelledImageFaceListType::iterator LabelledImageFaceListIterator
Definition: itkMRFImageFilter.h:246
itk::SizeValueType
unsigned long SizeValueType
Definition: itkIntTypes.h:83
itkSize.h
itk::DataObject
Base class for all data objects in ITK.
Definition: itkDataObject.h:293