ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkMRFImageFilter.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 #ifndef __itkMRFImageFilter_h
00019 #define __itkMRFImageFilter_h
00020 
00021 #include "vnl/vnl_vector.h"
00022 #include "vnl/vnl_matrix.h"
00023 
00024 #include "itkImageClassifierBase.h"
00025 
00026 #include "itkImageToImageFilter.h"
00027 
00028 #include "itkConstNeighborhoodIterator.h"
00029 #include "itkNeighborhoodAlgorithm.h"
00030 #include "itkSize.h"
00031 
00032 namespace itk
00033 {
00124 template< class TInputImage, class TClassifiedImage >
00125 class ITK_EXPORT MRFImageFilter:
00126   public ImageToImageFilter< TInputImage, TClassifiedImage >
00127 {
00128 public:
00130   typedef MRFImageFilter                                      Self;
00131   typedef ImageToImageFilter< TInputImage, TClassifiedImage > Superclass;
00132   typedef SmartPointer< Self >                                Pointer;
00133   typedef SmartPointer< const Self >                          ConstPointer;
00134   typedef typename Superclass::OutputImagePointer             OutputImagePointer;
00135 
00137   itkNewMacro(Self);
00138 
00140   itkTypeMacro(MRFImageFilter, Object);
00141 
00143   typedef TInputImage                        InputImageType;
00144   typedef typename TInputImage::Pointer      InputImagePointer;
00145   typedef typename TInputImage::ConstPointer InputImageConstPointer;
00146 
00148   typedef typename TInputImage::PixelType InputImagePixelType;
00149 
00151   typedef typename TInputImage::RegionType InputImageRegionType;
00152 
00154   typedef ImageRegionIterator< TInputImage >      InputImageRegionIterator;
00155   typedef ImageRegionConstIterator< TInputImage > InputImageRegionConstIterator;
00156 
00158   itkStaticConstMacro(InputImageDimension, unsigned int,
00159                       TInputImage::ImageDimension);
00160 
00162   typedef typename TClassifiedImage::Pointer TrainingImagePointer;
00163 
00165   typedef typename TClassifiedImage::PixelType TrainingImagePixelType;
00166 
00169   typedef typename TClassifiedImage::Pointer LabelledImagePointer;
00170 
00173   typedef typename TClassifiedImage::PixelType LabelledImagePixelType;
00174 
00177   typedef typename TClassifiedImage::RegionType LabelledImageRegionType;
00178 
00180   typedef typename TClassifiedImage::IndexType            LabelledImageIndexType;
00181   typedef typename LabelledImageIndexType::IndexValueType IndexValueType;
00182 
00184   typedef typename TClassifiedImage::OffsetType LabelledImageOffsetType;
00185 
00187   typedef ImageRegionIterator< TClassifiedImage >
00188   LabelledImageRegionIterator;
00189 
00191   itkStaticConstMacro(ClassifiedImageDimension, unsigned int,
00192                       TClassifiedImage::ImageDimension);
00193 
00195   typedef ImageClassifierBase< TInputImage, TClassifiedImage > ClassifierType;
00196 
00198   typedef typename TInputImage::SizeType SizeType;
00199 
00201   typedef typename TInputImage::SizeType NeighborhoodRadiusType;
00202 
00204   typedef ConstNeighborhoodIterator< TInputImage >
00205   InputImageNeighborhoodIterator;
00206 
00207   typedef typename InputImageNeighborhoodIterator::RadiusType
00208   InputImageNeighborhoodRadiusType;
00209 
00210   typedef NeighborhoodAlgorithm::ImageBoundaryFacesCalculator< TInputImage >
00211   InputImageFacesCalculator;
00212 
00213   typedef typename InputImageFacesCalculator::FaceListType
00214   InputImageFaceListType;
00215 
00216   typedef typename InputImageFaceListType::iterator
00217   InputImageFaceListIterator;
00218 
00220   typedef NeighborhoodIterator< TClassifiedImage >
00221   LabelledImageNeighborhoodIterator;
00222 
00223   typedef typename LabelledImageNeighborhoodIterator::RadiusType
00224   LabelledImageNeighborhoodRadiusType;
00225 
00226   typedef NeighborhoodAlgorithm::ImageBoundaryFacesCalculator< TClassifiedImage >
00227   LabelledImageFacesCalculator;
00228 
00229   typedef typename LabelledImageFacesCalculator::FaceListType
00230   LabelledImageFaceListType;
00231 
00232   typedef typename LabelledImageFaceListType::iterator
00233   LabelledImageFaceListIterator;
00234 
00236   void SetClassifier(typename ClassifierType::Pointer ptrToClassifier);
00237 
00239   itkSetMacro(NumberOfClasses, unsigned int);
00240   itkGetConstMacro(NumberOfClasses, unsigned int);
00242 
00245   itkSetMacro(MaximumNumberOfIterations, unsigned int);
00246   itkGetConstMacro(MaximumNumberOfIterations, unsigned int);
00248 
00251   itkSetMacro(ErrorTolerance, double);
00252   itkGetConstMacro(ErrorTolerance, double);
00254 
00257   itkSetMacro(SmoothingFactor, double);
00258   itkGetConstMacro(SmoothingFactor, double);
00260 
00262   void SetNeighborhoodRadius(const NeighborhoodRadiusType &);
00263 
00267   void SetNeighborhoodRadius(const SizeValueType);
00268 
00269   void SetNeighborhoodRadius(const SizeValueType *radiusArray);
00270 
00272   const NeighborhoodRadiusType GetNeighborhoodRadius() const
00273   {
00274     NeighborhoodRadiusType radius;
00275 
00276     for ( int i = 0; i < InputImageDimension; ++i )
00277       {
00278       radius[i] = m_InputImageNeighborhoodRadius[i];
00279       }
00280     return radius;
00281   }
00282 
00288   virtual void SetMRFNeighborhoodWeight(std::vector< double > BetaMatrix);
00289 
00290   virtual std::vector< double > GetMRFNeighborhoodWeight()
00291   {
00292     return m_MRFNeighborhoodWeight;
00293   }
00294 
00295 //Enum to get the stopping condition of the MRF filter
00296   typedef enum {
00297     MaximumNumberOfIterations = 1,
00298     ErrorTolerance
00299     } StopConditionType;
00300 
00303   itkGetConstReferenceMacro(StopCondition, StopConditionType);
00304 
00305   /* Get macro for number of iterations */
00306   itkGetConstReferenceMacro(NumberOfIterations, unsigned int);
00307 
00308 #ifdef ITK_USE_CONCEPT_CHECKING
00309 
00310   itkConceptMacro( UnsignedIntConvertibleToClassifiedCheck,
00311                    ( Concept::Convertible< unsigned int, LabelledImagePixelType > ) );
00312   itkConceptMacro( ClassifiedConvertibleToUnsignedIntCheck,
00313                    ( Concept::Convertible< LabelledImagePixelType, unsigned int > ) );
00314   itkConceptMacro( ClassifiedConvertibleToIntCheck,
00315                    ( Concept::Convertible< LabelledImagePixelType, int > ) );
00316   itkConceptMacro( IntConvertibleToClassifiedCheck,
00317                    ( Concept::Convertible< int, LabelledImagePixelType > ) );
00318   itkConceptMacro( SameDimensionCheck,
00319                    ( Concept::SameDimension< InputImageDimension, ClassifiedImageDimension > ) );
00320 
00322 #endif
00323 protected:
00324   MRFImageFilter();
00325   ~MRFImageFilter();
00326   void PrintSelf(std::ostream & os, Indent indent) const;
00328 
00330   void Allocate();
00331 
00336   virtual void ApplyMRFImageFilter();
00337 
00339   virtual void MinimizeFunctional();
00340 
00341   typedef Image< int, itkGetStaticConstMacro(InputImageDimension) > LabelStatusImageType;
00342   typedef typename LabelStatusImageType::IndexType                  LabelStatusIndexType;
00343   typedef typename LabelStatusImageType::RegionType                 LabelStatusRegionType;
00344   typedef typename LabelStatusImageType::Pointer                    LabelStatusImagePointer;
00345   typedef ImageRegionIterator< LabelStatusImageType >               LabelStatusImageIterator;
00346 
00348   typedef NeighborhoodIterator< LabelStatusImageType >
00349   LabelStatusImageNeighborhoodIterator;
00350   //Function implementing the neighborhood operation
00351 
00352   virtual void DoNeighborhoodOperation(const InputImageNeighborhoodIterator & imageIter,
00353                                        LabelledImageNeighborhoodIterator & labelledIter,
00354                                        LabelStatusImageNeighborhoodIterator & labelStatusIter);
00355 
00356   virtual void GenerateData();
00357 
00358   virtual void GenerateInputRequestedRegion();
00359 
00360   virtual void EnlargeOutputRequestedRegion(DataObject *);
00361 
00362   virtual void GenerateOutputInformation();
00363 
00364 private:
00365   MRFImageFilter(const Self &); //purposely not implemented
00366   void operator=(const Self &); //purposely not implemented
00367 
00368   typedef typename TInputImage::SizeType InputImageSizeType;
00369 
00370   typedef typename LabelStatusImageNeighborhoodIterator::RadiusType
00371   LabelStatusImageNeighborhoodRadiusType;
00372 
00373   typedef NeighborhoodAlgorithm::ImageBoundaryFacesCalculator< LabelStatusImageType >
00374   LabelStatusImageFacesCalculator;
00375 
00376   typedef typename LabelStatusImageFacesCalculator::FaceListType
00377   LabelStatusImageFaceListType;
00378 
00379   typedef typename LabelStatusImageFaceListType::iterator
00380   LabelStatusImageFaceListIterator;
00381 
00382   InputImageNeighborhoodRadiusType       m_InputImageNeighborhoodRadius;
00383   LabelledImageNeighborhoodRadiusType    m_LabelledImageNeighborhoodRadius;
00384   LabelStatusImageNeighborhoodRadiusType m_LabelStatusImageNeighborhoodRadius;
00385 
00386   unsigned int m_NumberOfClasses;
00387   unsigned int m_MaximumNumberOfIterations;
00388   unsigned int m_KernelSize;
00389 
00390   int               m_ErrorCounter;
00391   int               m_NeighborhoodSize;
00392   int               m_TotalNumberOfValidPixelsInOutputImage;
00393   int               m_TotalNumberOfPixelsInInputImage;
00394   double            m_ErrorTolerance;
00395   double            m_SmoothingFactor;
00396   double *          m_ClassProbability;         //Class liklihood
00397   unsigned int      m_NumberOfIterations;
00398   StopConditionType m_StopCondition;
00399 
00400   LabelStatusImagePointer m_LabelStatusImage;
00401 
00402   std::vector< double > m_MRFNeighborhoodWeight;
00403   std::vector< double > m_NeighborInfluence;
00404   std::vector< double > m_MahalanobisDistance;
00405   std::vector< double > m_DummyVector;
00406 
00408   typename ClassifierType::Pointer m_ClassifierPtr;
00409 
00413   virtual void SetDefaultMRFNeighborhoodWeight();
00414 
00415   //Function implementing the ICM algorithm to label the images
00416   void ApplyICMLabeller();
00417 }; // class MRFImageFilter
00418 } // namespace itk
00419 
00420 #ifndef ITK_MANUAL_INSTANTIATION
00421 #include "itkMRFImageFilter.hxx"
00422 #endif
00423 
00424 #endif
00425