freConsistencySOMetric.txx

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   F.R.E.E. - flexible registration evaluation engine
00004   Version:   v.1.0.0
00005   Date:      $Date: 2006/09/01 12:00:00 $
00006   Module:    $RCSfile: freConsistencySOMetric.txx,v $
00007   Language:  C++
00008 
00009 
00010 
00011   Copyright (c) 2007 Ralf o Floca (Department of Medical Informatics,
00012   Institute for Medical Biometry and Informatics, University of Heidelberg,
00013   Germany). All rights reserved.
00014   See FREECopyright.txt or http://www.mi.med.uni-hd.de/free/copyright.htm
00015   for details.
00016 
00017      This software is distributed WITHOUT ANY WARRANTY; without even 
00018      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
00019      PURPOSE.  See the above copyright notices for more information.
00020 
00021 =========================================================================*/
00022 #ifndef __freConsistencyRegistrationMetric_txx
00023 #define __freConsistencyRegistrationMetric_txx
00024 
00025 #include "freConsistencySOMetric.h"
00026 #include "freGenericSetupToImageAdaptor.h"
00027 #include "freVectorToNormImageFilter.h"
00028 #include "freImageSampleCharacteristicsCalculator.h"
00029 #include "freImageAdaptationInverter.h"
00030 
00031 #include "itkNumericTraits.h"
00032 #include "itkAddImageFilter.h"
00033 #include "itkImageAdaptor.h"
00034 #include "itkPointSet.h"
00035 #include "itkListSample.h"
00036 
00037 namespace FREE
00038 {
00039 
00040   template <unsigned int VImageDimension>
00041   typename ConsistencyRegistrationMetric<VImageDimension>::DecomposedMeasureType
00042         ConsistencyRegistrationMetric<VImageDimension>::
00043         ComputeDecomposedValue( const ParametersType & parameters ) const
00044         {
00045                 if (this->m_smpAdaptations.IsNull()) throwExceptionMacro("Error. Cannot calculate metric value, adaptiation list is null.");
00046                 if (this->m_Transform.IsNull()) throwExceptionMacro("Error. Cannot calculate metric value, no setup transform set.");
00047 
00048                 if (this->m_Transform->GetNumberOfParameters() != parameters.size()) throwExceptionMacro("Error. Cannot calculate metric value, number of passed parameters is not equal to the parameter size of the transform. Passed parameter count: "<< parameters.size());
00049 
00050                 m_dMinError = itk::NumericTraits< double >::max();
00051                 m_dMaxError = 0.0;
00052                 m_lFailedRegistrations = 0;
00053                 m_lActAdaptation = 0;
00054                 m_dErrorMean = 0.0;
00055                 m_dErrorVar = 0.0;
00056 
00057                 m_Errors.clear();
00058                 m_Vars.clear();
00059                 m_Samplesize.clear();
00060 
00061                 m_Transform->SetParameters(parameters);
00062                 m_smpSetup = m_Transform->GenerateTransformedSetup();
00063 
00064                 for (unsigned int iAdaptID = 0; iAdaptID<m_smpAdaptations->Size(); iAdaptID++)
00065                 {
00066                         if (this->fnOnNextAdaptation.IsNotNull()) this->fnOnNextAdaptation->Execute(iAdaptID,"",(void*)this);
00067                         this->InvokeEvent( NextAdaptationObserverEvent() );
00068 
00069       Adaptation::Adaptation* pAdaptation = m_smpAdaptations->GetElement(iAdaptID);
00070       Adaptation::Adaptation::Pointer smpReverseAdaptation = ImageAdaptationInverter::InvertAdaptation(*pAdaptation);
00071 
00072       Setup::Pointer smpSetup = Setup::New();
00073       Setup::Pointer smpReverseSetup = Setup::New();
00074       smpSetup->operator =(*m_smpSetup);
00075       smpReverseSetup->operator =(*m_smpSetup);
00076       
00077       GenericSetupToImageAdaptor::AdaptSetup(*pAdaptation, *smpSetup);
00078       GenericSetupToImageAdaptor::AdaptSetup(*(smpReverseAdaptation.GetPointer()), *smpReverseSetup);
00079 
00080                         ComputeRegistrations(smpSetup, smpReverseSetup);
00081 
00082       if (fnOnEvaluationDone.IsNotNull()) fnOnEvaluationDone->Execute(iAdaptID,"",(void*)this);
00083                   this->InvokeEvent( EvaluationDoneObserverEvent() );
00084     };
00085 
00086                 unsigned long lTotalSamples = 0;
00087                 for (unsigned int iIndex=0; iIndex<m_Errors.size(); iIndex++)
00088                 {
00089                         m_dErrorMean += m_Errors[iIndex] * m_Samplesize[iIndex];
00090                         m_dErrorVar += m_Vars[iIndex] * (m_Samplesize[iIndex]-1);
00091                         lTotalSamples += m_Samplesize[iIndex];
00092                 }
00093 
00094     if (lTotalSamples>0) m_dErrorMean /= lTotalSamples;
00095     if (lTotalSamples>1) m_dErrorVar /= lTotalSamples-1;
00096 
00097     DecomposedMeasureType results(5);
00098                 results.SetElement(0,m_dErrorMean);
00099                 results.SetElement(1,std::sqrt(m_dErrorVar));
00100                 results.SetElement(2,m_dMinError);
00101                 results.SetElement(3,m_dMaxError);
00102                 results.SetElement(4,m_lFailedRegistrations);
00103 
00104                 return results;
00105         };
00106 
00107 template <unsigned int VImageDimension>
00108 ConsistencyRegistrationMetric<VImageDimension>::
00109 ConsistencyRegistrationMetric()
00110 {
00111   //m_IterationEvent = FREE::IterationEvent<Self>::New(this,&ConsistencyRegistrationMetric<VImageDimension>::OnNextIteration);
00112 };
00113 
00114 template <unsigned int VImageDimension>
00115 void
00116 ConsistencyRegistrationMetric<VImageDimension>::
00117 OnNextIteration(const long lIteration, Statistics* pStatistic, void* pSender)
00118 {
00119         //if (fnOnEvaluatu.IsNotNull()) fnOnNextIteration->Execute(lIteration, 0, pSender);
00120         //this->InvokeEvent( itk::IterationEvent() );
00121 }
00122 
00123 template <unsigned int VImageDimension>
00124 void
00125 ConsistencyRegistrationMetric<VImageDimension>::
00126 ComputeRegistrations(Setup* pSetup, Setup* pReverseSetup) const
00127 {
00129  // RegistratorType registrator;
00130  // registrator.fnOnNextIteration= m_IterationEvent;
00131 
00132  // try
00133         //{
00134  //   registrator.SetSetup(pSetup);
00135         //  registrator.InitializeRegistration();
00136 
00137  //   registrator.StartRegistration(); 
00138 
00139  //   RegistratorType::TransformFieldPointer smpField = registrator.GetTransformationField();
00140  //   registrator.Reset();
00141 
00142  //   registrator.SetSetup(pReverseSetup);
00143         //  registrator.InitializeRegistration();
00144 
00145  //   registrator.StartRegistration(); 
00146 
00147  //   RegistratorType::TransformFieldPointer smpReverseField = registrator.GetTransformationField();
00148  //   registrator.Reset();
00149 
00150  //   RegistratorType::TransformFieldType::RegionType region1 = smpField->GetLargestPossibleRegion();
00151  //   RegistratorType::TransformFieldType::RegionType region2 = smpReverseField->GetLargestPossibleRegion();
00152 
00153  //   region1.Crop(smpReverseField->GetLargestPossibleRegion());
00154  //   region2.Crop(smpField->GetLargestPossibleRegion());
00155 
00156  //   smpField->SetRequestedRegion(region1);
00157  //   smpReverseField->SetRequestedRegion(region2);
00158 
00159  //   //TODO::Evaluierung so nicht richtig noch mal in Ruhe berprfen.
00160  //   typedef itk::AddImageFilter< RegistratorType::TransformFieldType,
00161  //                                RegistratorType::TransformFieldType,
00162  //                                RegistratorType::TransformFieldType > AddFilterType;
00163         //  AddFilterType::Pointer smpAddFilter = AddFilterType::New();
00164  //     
00165         //  smpAddFilter->SetInput1(smpField);
00166         //  smpAddFilter->SetInput2(smpReverseField);
00167         //  smpAddFilter->Update();
00168  //   smpField = smpAddFilter->GetOutput();
00169 
00170         //  //computing mean and variance of the field difference
00171         //  typedef itk::Image<double,VImageDimension> NormImageType;
00172         //  typedef VectorToNormImageFilter<RegistratorType::TransformFieldType,
00173  //                                   NormImageType > NormImageFilterType;
00174         //  typedef ImageSampleCharacteristicsCalculator<NormImageType> CharCalculator;
00175 
00176         //  NormImageFilterType::Pointer smpNormFilter = NormImageFilterType::New();
00177         //  smpNormFilter->SetInput(smpField);
00178         //  smpNormFilter->Update();
00179         //  NormImageType::Pointer smpNormImage = smpNormFilter->GetOutput();
00180 
00181         //  CharCalculator calculator;
00182         //  calculator.SetImage(smpNormImage);
00183         //  calculator.Compute();
00184 
00185         //  m_Errors.push_back(calculator.GetMean());
00186         //  m_Vars.push_back(calculator.GetVariance());
00187         //  m_Samplesize.push_back(smpNormImage->GetLargestPossibleRegion().GetNumberOfPixels());
00188 
00189         //  //checking for minimum or maximum
00190         //  typedef itk::ImageRegionConstIterator<NormImageType> NormIterator;
00191 
00192         //  NormIterator it(smpNormImage,smpField->GetBufferedRegion());
00193         //  for ( it.GoToBegin(); !it.IsAtEnd(); ++it )
00194         //  {
00195         //        if (it.Get()<m_dMinError) m_dMinError = it.Get();
00196         //        if (it.Get()>m_dMaxError) m_dMaxError = it.Get();
00197         //  }
00198         //}
00199         //catch( FREE::ExceptionBase & err ) 
00200         //{ 
00201         //      if (this->fnOnEvaluationFailed.IsNotNull()) this->fnOnEvaluationFailed->Execute(m_lActAdaptation,(void*)this,std::string(err.what()));
00202         //      m_lFailedRegistrations++;
00203         //} 
00204         //catch(...)
00205         //{
00206         //      if (this->fnOnEvaluationFailed.IsNotNull()) this->fnOnEvaluationFailed->Execute(m_lActAdaptation,(void*)this,"unkown error");
00207         //      m_lFailedRegistrations++;
00208         //}
00209 };
00210 
00211 } // end namespace FREE
00212 
00213 #endif

Generated at Sat Oct 13 15:39:20 2007 for f.r.e.e. - Flexible Registration and Evaluation Engine by doxygen 1.5.3 written by Dimitri van Heesch, © 1997-2000