From 8ca23efa2da542c89f7ed351d4e6329dfeef2e8d Mon Sep 17 00:00:00 2001 From: Cyrille Valladeau <cyrille.valladeau@c-s.fr> Date: Mon, 5 May 2008 15:22:41 +0000 Subject: [PATCH] Tests Markov, troisieme et derniere vague. --- Code/Markov/otbMarkovClassificationFilter.h | 594 +++++++++--------- Code/Markov/otbMarkovClassificationFilter.txx | 138 ++-- Testing/Code/Markov/CMakeLists.txt | 18 +- .../Markov/otbMarkovClassificationFilter.cxx | 14 +- Testing/Code/Markov/otbMarkovTests1.cxx | 1 + Testing/Code/Markov/otbtutuNew.cxx | 49 -- 6 files changed, 376 insertions(+), 438 deletions(-) delete mode 100644 Testing/Code/Markov/otbtutuNew.cxx diff --git a/Code/Markov/otbMarkovClassificationFilter.h b/Code/Markov/otbMarkovClassificationFilter.h index 33687209a5..7232b56b78 100644 --- a/Code/Markov/otbMarkovClassificationFilter.h +++ b/Code/Markov/otbMarkovClassificationFilter.h @@ -53,321 +53,293 @@ namespace otb template <class TInputImage, class TClassifiedImage> class ITK_EXPORT MarkovClassificationFilter : public itk::ImageToImageFilter<TInputImage,TClassifiedImage> - { - public: - /** Standard class typedefs. */ - typedef MarkovClassificationFilter Self; - typedef itk::ImageToImageFilter<TInputImage,TClassifiedImage> Superclass; - typedef itk::SmartPointer<Self> Pointer; - typedef itk::SmartPointer<const Self> ConstPointer; - typedef typename Superclass::OutputImagePointer OutputImagePointer; - - /** Run-time type information (and related methods). */ - itkTypeMacro(MarkovClassificationFilter,itk::ImageToImageFilter); - - /** Method for creation through the object factory. */ - itkNewMacro(Self); - - - /** Type definition for the input image. */ - typedef TInputImage InputImageType; - typedef typename TInputImage::Pointer InputImagePointer; - typedef typename TInputImage::ConstPointer InputImageConstPointer; - - /** Type definition for the input image pixel type. */ - typedef typename TInputImage::PixelType InputImagePixelType; - - /** Type definition for the input image region type. */ - typedef typename TInputImage::RegionType InputImageRegionType; - - /** Type definition for the input image region iterator */ - typedef itk::ImageRegionIterator<TInputImage> InputImageRegionIterator; - typedef itk::ImageRegionConstIterator<TInputImage> InputImageRegionConstIterator; - - /** Image dimension */ - itkStaticConstMacro(InputImageDimension, unsigned int, TInputImage::ImageDimension); - - /** Type definitions for the training image. */ - typedef TClassifiedImage TrainingImageType; - typedef typename TClassifiedImage::Pointer TrainingImagePointer; - - /** Type definitions for the training image pixel type. */ - typedef typename TClassifiedImage::PixelType TrainingImagePixelType; - - /** Type definitions for the labelled image. - * It is derived from the training image. */ - typedef TClassifiedImage LabelledImageType; - typedef typename TClassifiedImage::Pointer LabelledImagePointer; - - /** Type definitions for the classified image pixel type. - * It has to be the same type as the training image. */ - typedef typename TClassifiedImage::PixelType LabelledImagePixelType; - +{ + public: + /** Standard class typedefs. */ + typedef MarkovClassificationFilter Self; + typedef itk::ImageToImageFilter<TInputImage,TClassifiedImage> Superclass; + typedef itk::SmartPointer<Self> Pointer; + typedef itk::SmartPointer<const Self> ConstPointer; + typedef typename Superclass::OutputImagePointer OutputImagePointer; + + /** Run-time type information (and related methods). */ + itkTypeMacro(MarkovClassificationFilter,itk::ImageToImageFilter); + + /** Method for creation through the object factory. */ + itkNewMacro(Self); + + /** Type definition for the input image. */ + typedef TInputImage InputImageType; + typedef typename TInputImage::Pointer InputImagePointer; + typedef typename TInputImage::ConstPointer InputImageConstPointer; + + /** Type definition for the input image pixel type. */ + typedef typename TInputImage::PixelType InputImagePixelType; + + /** Type definition for the input image region type. */ + typedef typename TInputImage::RegionType InputImageRegionType; + + /** Type definition for the input image region iterator */ + typedef itk::ImageRegionIterator<TInputImage> InputImageRegionIterator; + typedef itk::ImageRegionConstIterator<TInputImage> InputImageRegionConstIterator; + + /** Image dimension */ + itkStaticConstMacro(InputImageDimension, unsigned int, TInputImage::ImageDimension); + + /** Type definitions for the training image. */ + typedef TClassifiedImage TrainingImageType; + typedef typename TClassifiedImage::Pointer TrainingImagePointer; + + /** Type definitions for the training image pixel type. */ + typedef typename TClassifiedImage::PixelType TrainingImagePixelType; + + /** Type definitions for the labelled image. + * It is derived from the training image. */ + typedef TClassifiedImage LabelledImageType; + typedef typename TClassifiedImage::Pointer LabelledImagePointer; /** Type definitions for the classified image pixel type. - * It has to be the same type as the training image. */ - typedef typename TClassifiedImage::RegionType LabelledImageRegionType; - - /** Type definition for the classified image index type. */ - typedef typename TClassifiedImage::IndexType LabelledImageIndexType; - typedef typename LabelledImageIndexType::IndexValueType IndexValueType; + * It has to be the same type as the training image. */ + typedef typename TClassifiedImage::PixelType LabelledImagePixelType; + + /** Type definitions for the classified image pixel type. + * It has to be the same type as the training image. */ + typedef typename TClassifiedImage::RegionType LabelledImageRegionType; + + /** Type definition for the classified image index type. */ + typedef typename TClassifiedImage::IndexType LabelledImageIndexType; + typedef typename LabelledImageIndexType::IndexValueType IndexValueType; + + /** Type definition for the classified image offset type. */ + typedef typename TClassifiedImage::OffsetType LabelledImageOffsetType; + + /** Type definition for the input image region iterator */ + typedef itk::ImageRegionIterator<TClassifiedImage> LabelledImageRegionIterator; + + /** Labelled Image dimension */ + itkStaticConstMacro(ClassifiedImageDimension, unsigned int, TClassifiedImage::ImageDimension); + + /** Type definitions for classifier to be used for the MRF lavbelling. */ + typedef itk::ImageClassifierBase<TInputImage,TClassifiedImage> ClassifierType; + + /** Size and value typedef support. */ + typedef typename TInputImage::SizeType SizeType; + + /** Radius typedef support. */ + typedef typename TInputImage::SizeType NeighborhoodRadiusType; + + /** Input image neighborhood iterator and kernel size typedef */ + typedef itk::ConstNeighborhoodIterator< TInputImage > InputImageNeighborhoodIterator; + typedef typename InputImageNeighborhoodIterator::RadiusType InputImageNeighborhoodRadiusType; + typedef itk::NeighborhoodAlgorithm::ImageBoundaryFacesCalculator< TInputImage > InputImageFacesCalculator; + typedef typename InputImageFacesCalculator::FaceListType InputImageFaceListType; + typedef typename InputImageFaceListType::iterator InputImageFaceListIterator; + + /** Labelled image neighborhood interator typedef */ + typedef itk::NeighborhoodIterator< TClassifiedImage > LabelledImageNeighborhoodIterator; + typedef typename LabelledImageNeighborhoodIterator::RadiusType LabelledImageNeighborhoodRadiusType; + typedef itk::NeighborhoodAlgorithm::ImageBoundaryFacesCalculator< TClassifiedImage > LabelledImageFacesCalculator; + typedef typename LabelledImageFacesCalculator::FaceListType LabelledImageFaceListType; + typedef typename LabelledImageFaceListType::iterator LabelledImageFaceListIterator; + + /** Set the pointer to the classifer being used. */ + void SetClassifier( typename ClassifierType::Pointer ptrToClassifier ); + + + /** Set pipeline elements */ + // typedef GaussianMRFEnergy< TInputImage, TClassifiedImage> EnergyType; + typedef MRFEnergy< TClassifiedImage, TClassifiedImage> EnergyRegularizationType; + typedef MRFEnergy< TInputImage, TClassifiedImage> EnergyFidelityType; + typedef typename EnergyRegularizationType::Pointer EnergyRegularizationPointer; + typedef typename EnergyFidelityType::Pointer EnergyFidelityPointer; + + // typedef RandomMRFSampler< TInputImage, TClassifiedImage> SamplerType; + // typedef MRFSamplerMAP< TInputImage, TClassifiedImage> SamplerType; + typedef MRFSampler< TInputImage, TClassifiedImage> SamplerType; + typedef typename SamplerType::Pointer SamplerPointer; + + // typedef MetropolisMRFOptimizer OptimizerType; + // typedef MRFOptimizerICM OptimizerType; + typedef MRFOptimizer OptimizerType; + typedef typename OptimizerType::Pointer OptimizerPointer; + - /** Type definition for the classified image offset type. */ - typedef typename TClassifiedImage::OffsetType LabelledImageOffsetType; - - /** Type definition for the input image region iterator */ - typedef itk::ImageRegionIterator<TClassifiedImage> - LabelledImageRegionIterator; - - /** Labelled Image dimension */ - itkStaticConstMacro(ClassifiedImageDimension, unsigned int, - TClassifiedImage::ImageDimension); - - /** Type definitions for classifier to be used for the MRF lavbelling. */ - typedef itk::ImageClassifierBase<TInputImage,TClassifiedImage> ClassifierType; - - /** Size and value typedef support. */ - typedef typename TInputImage::SizeType SizeType; - - /** Radius typedef support. */ - typedef typename TInputImage::SizeType NeighborhoodRadiusType; - - /** Input image neighborhood iterator and kernel size typedef */ - typedef itk::ConstNeighborhoodIterator< TInputImage > - InputImageNeighborhoodIterator; - - - typedef typename InputImageNeighborhoodIterator::RadiusType - InputImageNeighborhoodRadiusType; - - typedef itk::NeighborhoodAlgorithm::ImageBoundaryFacesCalculator< TInputImage > - InputImageFacesCalculator; - - typedef typename InputImageFacesCalculator::FaceListType - InputImageFaceListType; - - typedef typename InputImageFaceListType::iterator - InputImageFaceListIterator; - - /** Labelled image neighborhood interator typedef */ - typedef itk::NeighborhoodIterator< TClassifiedImage > - LabelledImageNeighborhoodIterator; - - typedef typename LabelledImageNeighborhoodIterator::RadiusType - LabelledImageNeighborhoodRadiusType; - - typedef itk::NeighborhoodAlgorithm::ImageBoundaryFacesCalculator< TClassifiedImage > - LabelledImageFacesCalculator; - - typedef typename LabelledImageFacesCalculator::FaceListType - LabelledImageFaceListType; - - typedef typename LabelledImageFaceListType::iterator - LabelledImageFaceListIterator; - - /** Set the pointer to the classifer being used. */ - void SetClassifier( typename ClassifierType::Pointer ptrToClassifier ); - - - /** Set pipeline elements */ - // typedef GaussianMRFEnergy< TInputImage, TClassifiedImage> EnergyType; - typedef MRFEnergy< TClassifiedImage, TClassifiedImage> EnergyRegularizationType; - typedef MRFEnergy< TInputImage, TClassifiedImage> EnergyFidelityType; - - typedef typename EnergyRegularizationType::Pointer EnergyRegularizationPointer; - typedef typename EnergyFidelityType::Pointer EnergyFidelityPointer; - - // typedef RandomMRFSampler< TInputImage, TClassifiedImage> SamplerType; - // typedef MRFSamplerMAP< TInputImage, TClassifiedImage> SamplerType; - typedef MRFSampler< TInputImage, TClassifiedImage> SamplerType; - typedef typename SamplerType::Pointer SamplerPointer; - - // typedef MetropolisMRFOptimizer OptimizerType; - // typedef MRFOptimizerICM OptimizerType; - typedef MRFOptimizer OptimizerType; - typedef typename OptimizerType::Pointer OptimizerPointer; - - itkSetObjectMacro( EnergyRegularization, EnergyRegularizationType); - itkGetObjectMacro( EnergyRegularization, EnergyRegularizationType); - - itkSetObjectMacro( EnergyFidelity, EnergyFidelityType); - itkGetObjectMacro( EnergyFidelity, EnergyFidelityType); - - itkSetObjectMacro( Sampler, SamplerType); - itkGetObjectMacro( Sampler, SamplerType); - - itkSetObjectMacro( Optimizer, OptimizerType); - itkGetObjectMacro( Optimizer, OptimizerType); - - - - /** Set/Get the number of classes. */ - itkSetMacro(NumberOfClasses, unsigned int); - itkGetMacro(NumberOfClasses, unsigned int); - - /** Set/Get the number of iteration of the Iterated Conditional Mode - * (ICM) algorithm. A default value is set at 50 iterations. */ - itkSetMacro(MaximumNumberOfIterations, unsigned int); - itkGetMacro(MaximumNumberOfIterations, unsigned int); - - /** Set/Get the error tollerance level which is used as a threshold - * to quit the iterations */ - itkSetMacro(ErrorTolerance, double); - itkGetMacro(ErrorTolerance, double); - - /** Set/Get the degree of smoothing desired - * */ - itkSetMacro(SmoothingFactor, double); - itkGetMacro(SmoothingFactor, double); - - /** Set/Get the temperature - * */ - // itkSetMacro(Temperature, double); - // itkGetMacro(Temperature, double); - - /** Set/Get the regularization coefficient - * */ - itkSetMacro(Lambda, double); - itkGetMacro(Lambda, double); - - - /** Set the neighborhood radius */ - void SetNeighborhoodRadius(const NeighborhoodRadiusType &); - - /** Sets the radius for the neighborhood, calculates size from the - * radius, and allocates storage. */ - - void SetNeighborhoodRadius( const unsigned long ); - void SetNeighborhoodRadius( const unsigned long *radiusArray ); - - /** Get the neighborhood radius */ - const NeighborhoodRadiusType GetNeighborhoodRadius() const - { - NeighborhoodRadiusType m_NeighborhoodRadius; - - for(int i=0; i<InputImageDimension; ++i) - m_NeighborhoodRadius[i] = m_InputImageNeighborhoodRadius[i]; - - return m_NeighborhoodRadius; - } - - - /** Set training image */ - // virtual void SetTrainingImage (const TrainingImagePointer _arg) - // { - // itkDebugMacro("setting TrainingImage to " << _arg); - // if (this->m_TrainingImage != _arg) - // { - // // this->SetInput(1,_arg); - // // this->m_TrainingImage = this->GetInput(1); - // this->m_TrainingImage = _arg; - // this->Modified(); - // } - // m_ExternalClassificationSet = true; - // } - // itkSetMacro(TrainingImage,TrainingImagePointer); - // itkGetMacro(TrainingImage,TrainingImagePointer); - // itkSetInputMacro(TrainingImage,TrainingImagePointer,1); - // itkGetInputMacro(TrainingImage,TrainingImagePointer,1); - virtual void SetTrainingInput( const TrainingImageType * trainningImage); - TrainingImageType* GetTrainingInput(void); - - - //Enum to get the stopping condition of the MRF filter - typedef enum{ - MaximumNumberOfIterations=1, - ErrorTolerance - } StopConditionType; - - /** Get condition that stops the MRF filter (Number of Iterations - * / Error tolerance ) */ - itkGetConstReferenceMacro( StopCondition, StopConditionType ); - - /* Get macro for number of iterations */ - itkGetConstReferenceMacro( NumberOfIterations, unsigned int ); - + + /* + ************ ACCESSORS ************ + */ + itkSetObjectMacro( EnergyRegularization, EnergyRegularizationType); + itkGetObjectMacro( EnergyRegularization, EnergyRegularizationType); + + itkSetObjectMacro( EnergyFidelity, EnergyFidelityType); + itkGetObjectMacro( EnergyFidelity, EnergyFidelityType); + + itkSetObjectMacro( Sampler, SamplerType); + itkGetObjectMacro( Sampler, SamplerType); + + itkSetObjectMacro( Optimizer, OptimizerType); + itkGetObjectMacro( Optimizer, OptimizerType); + + /** Set/Get the number of classes. */ + itkSetMacro(NumberOfClasses, unsigned int); + itkGetMacro(NumberOfClasses, unsigned int); + + /** Set/Get the number of iteration of the Iterated Conditional Mode + * (ICM) algorithm. A default value is set at 50 iterations. */ + itkSetMacro(MaximumNumberOfIterations, unsigned int); + itkGetMacro(MaximumNumberOfIterations, unsigned int); + + /** Set/Get the error tollerance level which is used as a threshold + * to quit the iterations */ + itkSetMacro(ErrorTolerance, double); + itkGetMacro(ErrorTolerance, double); + + /** Set/Get the degree of smoothing desired + * */ + itkSetMacro(SmoothingFactor, double); + itkGetMacro(SmoothingFactor, double); + + /** Set/Get the temperature + * */ + // itkSetMacro(Temperature, double); + // itkGetMacro(Temperature, double); + + /** Set/Get the regularization coefficient + * */ + itkSetMacro(Lambda, double); + itkGetMacro(Lambda, double); + + + /** Set the neighborhood radius */ + void SetNeighborhoodRadius(const NeighborhoodRadiusType &); + + /** Sets the radius for the neighborhood, calculates size from the + * radius, and allocates storage. */ + + void SetNeighborhoodRadius( const unsigned long ); + void SetNeighborhoodRadius( const unsigned long *radiusArray ); + + /** Get the neighborhood radius */ + const NeighborhoodRadiusType GetNeighborhoodRadius() const + { + NeighborhoodRadiusType m_NeighborhoodRadius; + + for(int i=0; i<InputImageDimension; ++i) + m_NeighborhoodRadius[i] = m_InputImageNeighborhoodRadius[i]; + + return m_NeighborhoodRadius; + } + + + /** Set training image */ + // virtual void SetTrainingImage (const TrainingImagePointer _arg) + // { + // itkDebugMacro("setting TrainingImage to " << _arg); + // if (this->m_TrainingImage != _arg) + // { + // // this->SetInput(1,_arg); + // // this->m_TrainingImage = this->GetInput(1); + // this->m_TrainingImage = _arg; + // this->Modified(); + // } + // m_ExternalClassificationSet = true; + // } + // itkSetMacro(TrainingImage,TrainingImagePointer); + // itkGetMacro(TrainingImage,TrainingImagePointer); + // itkSetInputMacro(TrainingImage,TrainingImagePointer,1); + // itkGetInputMacro(TrainingImage,TrainingImagePointer,1); + virtual void SetTrainingInput( const TrainingImageType * trainningImage); + TrainingImageType* GetTrainingInput(void); + + /** Store a value to be used instead of random value.. FOR TEST ONLY*/ + void SetValueInsteadRandom( int val ) + { + m_ValueInsteadRandom = val; + std::cout<<"The m_ValueInsteadRandom varaible has to be used only for tests..."<<std::endl; + this->Modified(); + }; + + //Enum to get the stopping condition of the MRF filter + typedef enum{ MaximumNumberOfIterations=1, ErrorTolerance } StopConditionType; + + /** Get condition that stops the MRF filter (Number of Iterations + * / Error tolerance ) */ + itkGetConstReferenceMacro( StopCondition, StopConditionType ); + + /* Get macro for number of iterations */ + itkGetConstReferenceMacro( NumberOfIterations, unsigned int ); + #ifdef ITK_USE_CONCEPT_CHECKING - /** Begin concept checking */ - itkConceptMacro(UnsignedIntConvertibleToClassifiedCheck, - (itk::Concept::Convertible<unsigned int, LabelledImagePixelType>)); - itkConceptMacro(ClassifiedConvertibleToUnsignedIntCheck, - (itk::Concept::Convertible<LabelledImagePixelType, unsigned int> )); - itkConceptMacro(ClassifiedConvertibleToIntCheck, - (itk::Concept::Convertible<LabelledImagePixelType, int> )); - itkConceptMacro(IntConvertibleToClassifiedCheck, - (itk::Concept::Convertible<int, LabelledImagePixelType>)); - itkConceptMacro(SameDimensionCheck, - (itk::Concept::SameDimension<InputImageDimension, ClassifiedImageDimension>)); - /** End concept checking */ + /** Begin concept checking */ + itkConceptMacro(UnsignedIntConvertibleToClassifiedCheck, (itk::Concept::Convertible<unsigned int, LabelledImagePixelType>)); + itkConceptMacro(ClassifiedConvertibleToUnsignedIntCheck, (itk::Concept::Convertible<LabelledImagePixelType, unsigned int> )); + itkConceptMacro(ClassifiedConvertibleToIntCheck, (itk::Concept::Convertible<LabelledImagePixelType, int> )); + itkConceptMacro(IntConvertibleToClassifiedCheck, (itk::Concept::Convertible<int, LabelledImagePixelType>)); + itkConceptMacro(SameDimensionCheck, (itk::Concept::SameDimension<InputImageDimension, ClassifiedImageDimension>)); + /** End concept checking */ #endif - - - protected: - MarkovClassificationFilter(); - ~MarkovClassificationFilter(); - void PrintSelf(std::ostream& os, itk::Indent indent) const; - - /** Allocate memory for labelled images. */ - void Allocate(); - - void Initialize() throw (itk::ExceptionObject); - - - virtual void ApplyMarkovClassificationFilter(); - - - virtual void GenerateData(); - virtual void GenerateInputRequestedRegion(); - virtual void EnlargeOutputRequestedRegion( itk::DataObject * ); - virtual void GenerateOutputInformation(); - - - MarkovClassificationFilter(const Self&); //purposely not implemented - void operator=(const Self&); //purposely not implemented - - typedef typename TInputImage::SizeType InputImageSizeType; - - - InputImageNeighborhoodRadiusType m_InputImageNeighborhoodRadius; - LabelledImageNeighborhoodRadiusType m_LabelledImageNeighborhoodRadius; - - unsigned int m_NumberOfClasses; - unsigned int m_MaximumNumberOfIterations; - unsigned int m_KernelSize; - - int m_ErrorCounter; - // int m_NeighborhoodSize; - int m_NeighborhoodRadius; - int m_TotalNumberOfValidPixelsInOutputImage; - int m_TotalNumberOfPixelsInInputImage; - double m_ErrorTolerance; - double m_SmoothingFactor; - // double *m_ClassProbability; //Class liklihood - unsigned int m_NumberOfIterations; - StopConditionType m_StopCondition; - - // double m_Temperature; - double m_Lambda; - bool m_ExternalClassificationSet; - - TrainingImagePointer m_TrainingImage; - - std::vector<double> m_MRFNeighborhoodWeight; - std::vector<double> m_NeighborInfluence; - std::vector<double> m_DummyVector; - - - /** Pointer to different elements */ - - EnergyRegularizationPointer m_EnergyRegularization; - EnergyFidelityPointer m_EnergyFidelity; - OptimizerPointer m_Optimizer; - SamplerPointer m_Sampler; - - virtual void MinimizeOnce(); - - private: - - }; // class MarkovClassificationFilter + + + protected: + MarkovClassificationFilter(); + ~MarkovClassificationFilter(); + void PrintSelf(std::ostream& os, itk::Indent indent) const; + + /** Allocate memory for labelled images. */ + void Allocate(); + + void Initialize() throw (itk::ExceptionObject); + + virtual void ApplyMarkovClassificationFilter(); + virtual void GenerateData(); + virtual void GenerateInputRequestedRegion(); + virtual void EnlargeOutputRequestedRegion( itk::DataObject * ); + virtual void GenerateOutputInformation(); + + + MarkovClassificationFilter(const Self&); //purposely not implemented + void operator=(const Self&); //purposely not implemented + + typedef typename TInputImage::SizeType InputImageSizeType; + + + InputImageNeighborhoodRadiusType m_InputImageNeighborhoodRadius; + LabelledImageNeighborhoodRadiusType m_LabelledImageNeighborhoodRadius; + + unsigned int m_NumberOfClasses; + unsigned int m_MaximumNumberOfIterations; + unsigned int m_KernelSize; + unsigned int m_NumberOfIterations; + int m_ErrorCounter; + // int m_NeighborhoodSize; + int m_NeighborhoodRadius; + int m_TotalNumberOfValidPixelsInOutputImage; + int m_TotalNumberOfPixelsInInputImage; + double m_ErrorTolerance; + double m_SmoothingFactor; + double m_Lambda; + // double *m_ClassProbability; //Class liklihood + // double m_Temperature; + bool m_ExternalClassificationSet; + StopConditionType m_StopCondition; + std::vector<double> m_MRFNeighborhoodWeight; + std::vector<double> m_NeighborInfluence; + std::vector<double> m_DummyVector; + + + /** Pointer to different elements */ + // TrainingImagePointer m_TrainingImage; + EnergyRegularizationPointer m_EnergyRegularization; + EnergyFidelityPointer m_EnergyFidelity; + OptimizerPointer m_Optimizer; + SamplerPointer m_Sampler; + + virtual void MinimizeOnce(); + + private: + /** Store a value to be used instead of random value.. FOR TEST ONLY*/ + int m_ValueInsteadRandom; + +}; // class MarkovClassificationFilter } // namespace itk diff --git a/Code/Markov/otbMarkovClassificationFilter.txx b/Code/Markov/otbMarkovClassificationFilter.txx index a4e06dfd20..3c0771ea57 100644 --- a/Code/Markov/otbMarkovClassificationFilter.txx +++ b/Code/Markov/otbMarkovClassificationFilter.txx @@ -32,13 +32,16 @@ MarkovClassificationFilter<TInputImage,TClassifiedImage> m_NeighborhoodRadius(1), m_TotalNumberOfValidPixelsInOutputImage(1), m_TotalNumberOfPixelsInInputImage(1), - m_ErrorTolerance(0.2), - m_SmoothingFactor(1), - m_NumberOfIterations(0), - //m_ExternalClassificationSet(false), when not comment, generates a warning ("defini plus loin") - m_StopCondition(MaximumNumberOfIterations)//, + //m_ErrorTolerance(0.2), + //m_SmoothingFactor(1), + //m_NumberOfIterations(0), + m_ExternalClassificationSet(false), + m_StopCondition(MaximumNumberOfIterations) { m_ExternalClassificationSet = false; + m_SmoothingFactor = 1; + m_NumberOfIterations = 0; + m_ErrorTolerance = 0.2; this->SetNumberOfRequiredInputs(2); if( (int)InputImageDimension != (int)ClassifiedImageDimension ) { @@ -47,11 +50,18 @@ MarkovClassificationFilter<TInputImage,TClassifiedImage> throw itk::ExceptionObject(__FILE__, __LINE__,msg.str().c_str(),ITK_LOCATION); } m_InputImageNeighborhoodRadius.Fill(m_NeighborhoodRadius); - // m_MRFNeighborhoodWeight.resize(0); - // m_NeighborInfluence.resize(0); - // m_DummyVector.resize(0); - // this->SetMRFNeighborhoodWeight( m_DummyVector ); - // this->SetDefaultMRFNeighborhoodWeight(); + // m_MRFNeighborhoodWeight.resize(0); + // m_NeighborInfluence.resize(0); + // m_DummyVector.resize(0); + // this->SetMRFNeighborhoodWeight( m_DummyVector ); + // this->SetDefaultMRFNeighborhoodWeight(); + + EnergyRegularizationPointer m_EnergyRegularization = EnergyRegularizationType::New();; + EnergyFidelityPointer m_EnergyFidelity = EnergyFidelityType::New();; + OptimizerPointer m_Optimizer = OptimizerType::New();; + SamplerPointer m_Sampler = SamplerType::New(); + + m_ValueInsteadRandom = itk::NumericTraits<int>::min(); } template<class TInputImage, class TClassifiedImage> @@ -92,25 +102,13 @@ MarkovClassificationFilter<TInputImage, TClassifiedImage> ::PrintSelf( std::ostream& os, itk::Indent indent ) const { Superclass::PrintSelf(os,indent); - os << indent <<" MRF Image filter object " << std::endl; - os << indent <<" Number of classes: " << m_NumberOfClasses << std::endl; - - os << indent <<" Maximum number of iterations: " << - m_MaximumNumberOfIterations << std::endl; - - os << indent <<" Error tolerance for convergence: " << - m_ErrorTolerance << std::endl; - - os << indent <<" Size of the MRF neighborhood radius:" << - m_InputImageNeighborhoodRadius << std::endl; - - os << indent << "StopCondition: " - << m_StopCondition << std::endl; - - os << indent <<" Number of iterations: " << - m_NumberOfIterations << std::endl; + os << indent <<" Maximum number of iterations: " << m_MaximumNumberOfIterations << std::endl; + os << indent <<" Error tolerance for convergence: " << m_ErrorTolerance << std::endl; + os << indent <<" Size of the MRF neighborhood radius:" << m_InputImageNeighborhoodRadius << std::endl; + os << indent << "StopCondition: " << m_StopCondition << std::endl; + os << indent <<" Number of iterations: " << m_NumberOfIterations << std::endl; }// end PrintSelf @@ -123,12 +121,10 @@ template <class TInputImage, class TClassifiedImage> void MarkovClassificationFilter<TInputImage, TClassifiedImage> ::GenerateInputRequestedRegion() -{ - +{ // this filter requires the all of the input images // to be at the size of the output requested region - InputImagePointer inputPtr = - const_cast< InputImageType * >( this->GetInput() ); + InputImagePointer inputPtr = const_cast< InputImageType * >( this->GetInput() ); OutputImagePointer outputPtr = this->GetOutput(); inputPtr->SetRequestedRegion( outputPtr->GetRequestedRegion() ); } @@ -159,7 +155,7 @@ void MarkovClassificationFilter<TInputImage, TClassifiedImage> ::GenerateOutputInformation() { - typename TInputImage::ConstPointer input = this->GetInput(); + typename TInputImage::ConstPointer input = this->GetInput(); typename TClassifiedImage::Pointer output = this->GetOutput(); output->SetLargestPossibleRegion( input->GetLargestPossibleRegion() ); } @@ -186,7 +182,6 @@ MarkovClassificationFilter<TInputImage, TClassifiedImage> this->ApplyMarkovClassificationFilter(); - }// end GenerateData @@ -270,9 +265,7 @@ MarkovClassificationFilter<TInputImage, TClassifiedImage> for( unsigned int i=0; i < InputImageDimension; i++ ) { tmp = static_cast<int>(inputImageSize[i]); - m_TotalNumberOfPixelsInInputImage *= tmp; - m_TotalNumberOfValidPixelsInOutputImage *= ( tmp - 2*m_InputImageNeighborhoodRadius[i] ); } @@ -285,8 +278,7 @@ MarkovClassificationFilter<TInputImage, TClassifiedImage> //Copy input data in the output buffer memory or //initialize to random values if not set - LabelledImageRegionIterator - outImageIt( outputPtr, outputPtr->GetRequestedRegion() ); + LabelledImageRegionIterator outImageIt( outputPtr, outputPtr->GetRequestedRegion() ); m_ExternalClassificationSet = true; //TODO switch to random if the ref image is not specified if (m_ExternalClassificationSet) @@ -305,14 +297,27 @@ MarkovClassificationFilter<TInputImage, TClassifiedImage> } else //set to random value { - srand((unsigned)time(0)); - - while ( !outImageIt.IsAtEnd() ) + // if it is a test, cancel the rand() + if( m_ValueInsteadRandom == itk::NumericTraits<int>::min() ) { - LabelledImagePixelType randomvalue = static_cast<LabelledImagePixelType>(rand() % static_cast<int>(m_NumberOfClasses)); - outImageIt.Set( randomvalue ); - ++outImageIt; - }// end while + srand((unsigned)time(0)); + + while ( !outImageIt.IsAtEnd() ) + { + LabelledImagePixelType randomvalue = static_cast<LabelledImagePixelType>(rand() % static_cast<int>(m_NumberOfClasses)); + outImageIt.Set( randomvalue ); + ++outImageIt; + }// end while + } + else + { + while ( !outImageIt.IsAtEnd() ) + { + LabelledImagePixelType randomvalue = static_cast<LabelledImagePixelType>(m_ValueInsteadRandom % static_cast<int>(m_NumberOfClasses)); + outImageIt.Set( randomvalue ); + ++outImageIt; + } + } } @@ -323,8 +328,11 @@ template<class TInputImage, class TClassifiedImage> void MarkovClassificationFilter<TInputImage, TClassifiedImage> ::Initialize() throw (itk::ExceptionObject) -{ - srand((unsigned)time(0)); +{ + if( m_ValueInsteadRandom == itk::NumericTraits<int>::min() ) + { + srand((unsigned)time(0)); + } if ( !m_EnergyRegularization ) { @@ -364,26 +372,19 @@ void MarkovClassificationFilter<TInputImage, TClassifiedImage> ::ApplyMarkovClassificationFilter() { - InputImageSizeType inputImageSize = this->GetInput()->GetBufferedRegion().GetSize(); - int totalNumberOfPixelsInInputImage = 1; - for( unsigned int i = 0; i < InputImageDimension; i++ ) { totalNumberOfPixelsInInputImage *= static_cast<int>(inputImageSize[ i ]) ; } - int maxNumPixelError = (int) ( vnl_math_rnd (m_ErrorTolerance * - m_TotalNumberOfValidPixelsInOutputImage) ); - + int maxNumPixelError = (int) ( vnl_math_rnd (m_ErrorTolerance * m_TotalNumberOfValidPixelsInOutputImage) ); m_NumberOfIterations = 0; do { - otbMsgDebugMacro(<< "Iteration No." << m_NumberOfIterations); - + otbMsgDebugMacro(<< "Iteration No." << m_NumberOfIterations); this->MinimizeOnce(); - otbMsgDebugMacro(<< "m_ErrorCounter/m_TotalNumberOfPixelsInInputImage: " << m_ErrorCounter/((double)(m_TotalNumberOfPixelsInInputImage))); @@ -391,7 +392,7 @@ MarkovClassificationFilter<TInputImage, TClassifiedImage> // m_ErrorCounter = m_TotalNumberOfValidPixelsInOutputImage - // totalNumberOfPixelsInInputImage; - } + } while(( m_NumberOfIterations < m_MaximumNumberOfIterations ) && ( m_ErrorCounter > maxNumPixelError ) ); @@ -425,20 +426,19 @@ MarkovClassificationFilter<TInputImage, TClassifiedImage> InputImageNeighborhoodIterator dataIterator(m_InputImageNeighborhoodRadius, this->GetInput(), this->GetInput()->GetLargestPossibleRegion() ); m_ErrorCounter = 0; //WARNING, is it the same size ?? - for (labelledIterator.GoToBegin(), dataIterator.GoToBegin(); - !labelledIterator.IsAtEnd(); - ++labelledIterator, ++dataIterator){ - - LabelledImagePixelType value; - bool changeValue; - m_Sampler->Compute(dataIterator,labelledIterator); - value=m_Sampler->GetValue(); - changeValue= m_Optimizer->Compute(m_Sampler->GetDeltaEnergy()); - if (changeValue){ - labelledIterator.SetCenterPixel(value); - ++m_ErrorCounter; + for (labelledIterator.GoToBegin(), dataIterator.GoToBegin(); !labelledIterator.IsAtEnd(); ++labelledIterator, ++dataIterator) + { + LabelledImagePixelType value; + bool changeValue; + m_Sampler->Compute(dataIterator,labelledIterator); + value=m_Sampler->GetValue(); + changeValue= m_Optimizer->Compute(m_Sampler->GetDeltaEnergy()); + if (changeValue) + { + labelledIterator.SetCenterPixel(value); + ++m_ErrorCounter; + } } - } } } // namespace otb diff --git a/Testing/Code/Markov/CMakeLists.txt b/Testing/Code/Markov/CMakeLists.txt index 0b180b6130..bad6e443f8 100755 --- a/Testing/Code/Markov/CMakeLists.txt +++ b/Testing/Code/Markov/CMakeLists.txt @@ -23,12 +23,17 @@ SET(MARKOV_TESTS2 ${CXX_TEST_PATH}/otbMarkovTests2) ADD_TEST(maTuMarkovClassificationFilterNew ${MARKOV_TESTS1} otbMarkovClassificationFilterNew ) -# ADD_TEST(maTuMarkovClassificationFilter ${MARKOV_TESTS1} -# --compare-ascii ${TOL} ${BASELINE_FILES}/maTuMarkovClassificationFilter.txt -# ${TEMP}/maTuMarkovClassificationFilter.txt -# otbMarkovClassificationFilter -# ${TEMP}/maTuMarkovClassificationFilter.txt -# ) +ADD_TEST(maTuMarkovClassificationFilter ${MARKOV_TESTS1} + --compare-image ${TOL} ${BASELINE}/maTvMarkovClassification.png + ${TEMP}/maTvMarkovClassification.png + otbMarkovClassificationFilter + ${INPUTDATA}/QB_Suburb.png + ${INPUTDATA}/RandomImage.png + ${TEMP}/maTvMarkovClassification.png + 1.0 + 30 + 1.0 + ) # ------- otb::MRFEnergy ------------------------------ ADD_TEST(maTuMRFEnergyNew ${MARKOV_TESTS1} @@ -160,6 +165,7 @@ ADD_TEST(maTvMRFSamplerRandomMAP ${MARKOV_TESTS2} # A enrichir SET(Markov_SRCS1 otbMarkovClassificationFilterNew.cxx +otbMarkovClassificationFilter.cxx otbMRFEnergyNew.cxx otbMRFEnergy.cxx otbMRFEnergyPottsNew.cxx diff --git a/Testing/Code/Markov/otbMarkovClassificationFilter.cxx b/Testing/Code/Markov/otbMarkovClassificationFilter.cxx index 4f360d97d7..be38a43040 100644 --- a/Testing/Code/Markov/otbMarkovClassificationFilter.cxx +++ b/Testing/Code/Markov/otbMarkovClassificationFilter.cxx @@ -82,8 +82,15 @@ int otbMarkovClassificationFilter( int argc, char* argv[] ) itk::VariableLengthVector<double> stdDev; mean.SetSize(nClass); stdDev.SetSize(nClass); - mean[0]=10;mean[1]=80;mean[2]=150;mean[3]=220; - stdDev[0]=10;stdDev[1]=10;stdDev[2]=10;stdDev[3]=10; + mean[0]=10; + mean[1]=80; + mean[2]=150; + mean[3]=220; + + stdDev[0]=10; + stdDev[1]=10; + stdDev[2]=10; + stdDev[3]=10; energyFidelity->SetNumberOfParameters(2*nClass); EnergyFidelityType::ParametersType parameters; @@ -98,7 +105,8 @@ int otbMarkovClassificationFilter( int argc, char* argv[] ) parameters[7]=10.0; //Class 3 stde energyFidelity->SetParameters(parameters); optimizer->SetTemperature(atof(argv[6])); - markovFilter->SetNumberOfClasses(nClass); + markovFilter->SetNumberOfClasses(nClass); + markovFilter->SetValueInsteadRandom(500); // Unable rand() calculation markovFilter->SetMaximumNumberOfIterations(atoi(argv[5])); markovFilter->SetErrorTolerance(-1.0); markovFilter->SetLambda(atof(argv[4])); diff --git a/Testing/Code/Markov/otbMarkovTests1.cxx b/Testing/Code/Markov/otbMarkovTests1.cxx index ee0dc9b1f1..36c1c3d784 100755 --- a/Testing/Code/Markov/otbMarkovTests1.cxx +++ b/Testing/Code/Markov/otbMarkovTests1.cxx @@ -28,6 +28,7 @@ void RegisterTests() { REGISTER_TEST(otbMarkovClassificationFilterNew); +REGISTER_TEST(otbMarkovClassificationFilter); REGISTER_TEST(otbMRFEnergyNew); REGISTER_TEST(otbMRFEnergy); REGISTER_TEST(otbMRFEnergyEdgeFidelityNew); diff --git a/Testing/Code/Markov/otbtutuNew.cxx b/Testing/Code/Markov/otbtutuNew.cxx deleted file mode 100644 index 8041238b9c..0000000000 --- a/Testing/Code/Markov/otbtutuNew.cxx +++ /dev/null @@ -1,49 +0,0 @@ -/*========================================================================= - - Program: ORFEO Toolbox - Language: C++ - Date: $Date$ - Version: $Revision$ - - - Copyright (c) Centre National d'Etudes Spatiales. All rights reserved. - See OTBCopyright.txt for details. - - - This software is distributed WITHOUT ANY WARRANTY; without even - the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR - PURPOSE. See the above copyright notices for more information. - -=========================================================================*/ -#if defined(_MSC_VER) -#pragma warning ( disable : 4786 ) -#endif - -#include "itkExceptionObject.h" - -#include "otbAtmosphericRadiativeTerms.h" - -int otbAtmosphericRadiativeTermsNew(int argc, char * argv[]) -{ - try - { - typedef otb::AtmosphericRadiativeTerms AtmosphericRadiativeTermsType; - - // Instantiating object - AtmosphericRadiativeTermsType::Pointer object = AtmosphericRadiativeTermsType::New(); - } - - catch( itk::ExceptionObject & err ) - { - std::cout << "Exception itk::ExceptionObject thrown !" << std::endl; - std::cout << err << std::endl; - return EXIT_FAILURE; - } - - catch( ... ) - { - std::cout << "Unknown exception thrown !" << std::endl; - return EXIT_FAILURE; - } - return EXIT_SUCCESS; -} -- GitLab