ITK  5.0.0
Insight Segmentation and Registration Toolkit
itkMRFImageFilter.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
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 
32 namespace itk
33 {
124 template< typename TInputImage, typename TClassifiedImage >
125 class ITK_TEMPLATE_EXPORT MRFImageFilter:
126  public ImageToImageFilter< TInputImage, TClassifiedImage >
127 {
128 public:
129  ITK_DISALLOW_COPY_AND_ASSIGN(MRFImageFilter);
130 
136  using OutputImagePointer = typename Superclass::OutputImagePointer;
137 
139  itkNewMacro(Self);
140 
142  itkTypeMacro(MRFImageFilter, Object);
143 
145  using InputImageType = TInputImage;
146  using InputImagePointer = typename TInputImage::Pointer;
147  using InputImageConstPointer = typename TInputImage::ConstPointer;
148 
150  using InputImagePixelType = typename TInputImage::PixelType;
151 
154 
158 
160  static constexpr unsigned int InputImageDimension = TInputImage::ImageDimension;
161 
163  using TrainingImagePointer = typename TClassifiedImage::Pointer;
164 
166  using TrainingImagePixelType = typename TClassifiedImage::PixelType;
167 
170  using LabelledImagePointer = typename TClassifiedImage::Pointer;
171 
174  using LabelledImagePixelType = typename TClassifiedImage::PixelType;
175 
179 
183 
185  using LabelledImageOffsetType = typename TClassifiedImage::OffsetType;
186 
189 
191  static constexpr unsigned int ClassifiedImageDimension = TClassifiedImage::ImageDimension;
192 
195 
197  using SizeType = typename TInputImage::SizeType;
198 
201 
204 
205  using InputImageNeighborhoodRadiusType = typename InputImageNeighborhoodIterator::RadiusType;
206 
208 
210 
211  using InputImageFaceListIterator = typename InputImageFaceListType::iterator;
212 
215 
217 
219 
221 
222  using LabelledImageFaceListIterator = typename LabelledImageFaceListType::iterator;
223 
225  void SetClassifier(typename ClassifierType::Pointer ptrToClassifier);
226 
228  itkSetMacro(NumberOfClasses, unsigned int);
229  itkGetConstMacro(NumberOfClasses, unsigned int);
231 
234  itkSetMacro(MaximumNumberOfIterations, unsigned int);
235  itkGetConstMacro(MaximumNumberOfIterations, unsigned int);
237 
240  itkSetMacro(ErrorTolerance, double);
241  itkGetConstMacro(ErrorTolerance, double);
243 
246  itkSetMacro(SmoothingFactor, double);
247  itkGetConstMacro(SmoothingFactor, double);
249 
251  void SetNeighborhoodRadius(const NeighborhoodRadiusType &);
252 
256  void SetNeighborhoodRadius(const SizeValueType);
257 
258  void SetNeighborhoodRadius(const SizeValueType *radiusArray);
259 
262  {
263  NeighborhoodRadiusType radius;
264 
265  for ( int i = 0; i < InputImageDimension; ++i )
266  {
267  radius[i] = m_InputImageNeighborhoodRadius[i];
268  }
269  return radius;
270  }
271 
277  virtual void SetMRFNeighborhoodWeight(std::vector< double > BetaMatrix);
278 
279  virtual std::vector< double > GetMRFNeighborhoodWeight()
280  {
281  return m_MRFNeighborhoodWeight;
282  }
283 
284 //Enum to get the stopping condition of the MRF filter
285  typedef enum {
286  MaximumNumberOfIterations = 1,
287  ErrorTolerance
288  } StopConditionType;
289 
292  itkGetConstReferenceMacro(StopCondition, StopConditionType);
293 
294  /* Get macro for number of iterations */
295  itkGetConstReferenceMacro(NumberOfIterations, unsigned int);
296 
297 #ifdef ITK_USE_CONCEPT_CHECKING
298  // Begin concept checking
299  itkConceptMacro( UnsignedIntConvertibleToClassifiedCheck,
301  itkConceptMacro( ClassifiedConvertibleToUnsignedIntCheck,
303  itkConceptMacro( ClassifiedConvertibleToIntCheck,
305  itkConceptMacro( IntConvertibleToClassifiedCheck,
307  itkConceptMacro( SameDimensionCheck,
309  // End concept checking
310 #endif
311 
312 protected:
313  MRFImageFilter();
314  ~MRFImageFilter() override = default;
315  void PrintSelf(std::ostream & os, Indent indent) const override;
316 
318  void Allocate();
319 
324  virtual void ApplyMRFImageFilter();
325 
327  virtual void MinimizeFunctional();
328 
334 
337  //Function implementing the neighborhood operation
338 
339  virtual void DoNeighborhoodOperation(const InputImageNeighborhoodIterator & imageIter,
340  LabelledImageNeighborhoodIterator & labelledIter,
341  LabelStatusImageNeighborhoodIterator & labelStatusIter);
342 
343  void GenerateData() override;
344 
345  void GenerateInputRequestedRegion() override;
346 
347  void EnlargeOutputRequestedRegion(DataObject *) override;
348 
349  void GenerateOutputInformation() override;
350 
351 private:
353 
355 
357 
359 
360  using LabelStatusImageFaceListIterator = typename LabelStatusImageFaceListType::iterator;
361 
365 
366  unsigned int m_NumberOfClasses{0};
367  unsigned int m_MaximumNumberOfIterations{50};
368  unsigned int m_KernelSize;
369 
370  int m_ErrorCounter{0};
371  int m_NeighborhoodSize{27};
372  int m_TotalNumberOfValidPixelsInOutputImage{1};
373  int m_TotalNumberOfPixelsInInputImage{1};
374  double m_ErrorTolerance{0.2};
375  double m_SmoothingFactor{1};
376  double * m_ClassProbability{nullptr}; //Class liklihood
377  unsigned int m_NumberOfIterations{0};
379 
381 
382  std::vector< double > m_MRFNeighborhoodWeight;
383  std::vector< double > m_NeighborInfluence;
384  std::vector< double > m_MahalanobisDistance;
385  std::vector< double > m_DummyVector;
386 
389 
393  virtual void SetDefaultMRFNeighborhoodWeight();
394 
395  //Function implementing the ICM algorithm to label the images
396  void ApplyICMLabeller();
397 }; // class MRFImageFilter
398 } // namespace itk
399 
400 #ifndef ITK_MANUAL_INSTANTIATION
401 #include "itkMRFImageFilter.hxx"
402 #endif
403 
404 #endif
typename TInputImage::ConstPointer InputImageConstPointer
typename TClassifiedImage::RegionType LabelledImageRegionType
Light weight base class for most itk classes.
typename TClassifiedImage::IndexType LabelledImageIndexType
typename LabelledImageFaceListType::iterator LabelledImageFaceListIterator
typename TInputImage::SizeType InputImageSizeType
std::vector< double > m_MahalanobisDistance
unsigned long SizeValueType
Definition: itkIntTypes.h:83
typename TClassifiedImage::Pointer LabelledImagePointer
std::vector< double > m_MRFNeighborhoodWeight
typename TInputImage::PixelType InputImagePixelType
typename TClassifiedImage::PixelType TrainingImagePixelType
typename TClassifiedImage::OffsetType LabelledImageOffsetType
LabelStatusImagePointer m_LabelStatusImage
Base class for the ImageClassifierBase object.
std::vector< double > m_DummyVector
Implementation of a labeller object that uses Markov Random Fields to classify pixels in an image dat...
typename TInputImage::SizeType SizeType
ClassifierType::Pointer m_ClassifierPtr
Splits an image into a main region and several &quot;face&quot; regions which are used to handle computations o...
typename TClassifiedImage::Pointer TrainingImagePointer
typename InputImageFacesCalculator::FaceListType InputImageFaceListType
typename LabelStatusImageType::IndexType LabelStatusIndexType
typename LabelStatusImageFacesCalculator::FaceListType LabelStatusImageFaceListType
A multi-dimensional iterator templated over image type that walks a region of pixels.
signed long IndexValueType
Definition: itkIntTypes.h:90
typename InputImageNeighborhoodIterator::RadiusType InputImageNeighborhoodRadiusType
LabelStatusImageNeighborhoodRadiusType m_LabelStatusImageNeighborhoodRadius
typename Superclass::OutputImagePointer OutputImagePointer
InputImageNeighborhoodRadiusType m_InputImageNeighborhoodRadius
virtual std::vector< double > GetMRFNeighborhoodWeight()
const NeighborhoodRadiusType GetNeighborhoodRadius() const
typename TInputImage::SizeType NeighborhoodRadiusType
typename TInputImage::RegionType InputImageRegionType
typename Superclass::RadiusType RadiusType
LabelledImageNeighborhoodRadiusType m_LabelledImageNeighborhoodRadius
typename TInputImage::Pointer InputImagePointer
typename TClassifiedImage::PixelType LabelledImagePixelType
typename LabelStatusImageType::RegionType LabelStatusRegionType
typename LabelStatusImageType::Pointer LabelStatusImagePointer
Base class for filters that take an image as input and produce an image as output.
typename InputImageFaceListType::iterator InputImageFaceListIterator
Control indentation during Print() invocation.
Definition: itkIndent.h:49
typename LabelledImageIndexType::IndexValueType IndexValueType
typename LabelledImageFacesCalculator::FaceListType LabelledImageFaceListType
std::vector< double > m_NeighborInfluence
typename LabelStatusImageFaceListType::iterator LabelStatusImageFaceListIterator
StopConditionType m_StopCondition
Base class for most ITK classes.
Definition: itkObject.h:60
#define itkConceptMacro(name, concept)
typename LabelStatusImageNeighborhoodIterator::RadiusType LabelStatusImageNeighborhoodRadiusType
Base class for all data objects in ITK.
Defines iteration of a local N-dimensional neighborhood of pixels across an itk::Image.
Templated n-dimensional image class.
Definition: itkImage.h:75
A multi-dimensional iterator templated over image type that walks a region of pixels.
typename LabelledImageNeighborhoodIterator::RadiusType LabelledImageNeighborhoodRadiusType