freImageDifferenceSOMetricThread.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: freImageDifferenceSOMetricThread.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 __freImageDifferenceSOMetricThread_txx
00023 #define __freImageDifferenceSOMetricThread_txx
00024 
00025 #include "freImageDifferenceSOMetricThread.h"
00026 
00027 #include "freSessionProcessor.h"
00028 #include "freImageSampleCharacteristicsCalculator.h"
00029 
00030 #include "itkNumericTraits.h"
00031 #include "itkAbsoluteValueDifferenceImageFilter.h"
00032 
00033 namespace FREE
00034 {
00035 
00036 template <unsigned int VImageDimension>
00037 void
00038 ImageDifferenceSOMetricThread<VImageDimension>:: 
00039 SetResultImagePath(const IDPath& path)
00040 {
00041   this->LockExecutionMutex();
00042     this->m_ResultImagePath = path;
00043   this->UnlockExecutionMutex();
00044 };
00045 
00046 template <unsigned int VImageDimension>
00047 const IDPath&
00048 ImageDifferenceSOMetricThread<VImageDimension>:: 
00049 GetResultImagePath() const {return m_ResultImagePath;};
00050 
00051 template <unsigned int VImageDimension>
00052 void
00053 ImageDifferenceSOMetricThread<VImageDimension>:: 
00054 SetReferenceImagePath(const IDPath& path)
00055 {
00056   this->LockExecutionMutex();
00057     this->m_ReferenceImagePath = path;
00058   this->UnlockExecutionMutex();
00059 };
00060 
00061 template <unsigned int VImageDimension>
00062 const IDPath&
00063 ImageDifferenceSOMetricThread<VImageDimension>:: 
00064 GetReferenceImagePath() const {return m_ReferenceImagePath;};
00065 
00066 template <unsigned int VImageDimension>
00067 ImageDifferenceSOMetricThread<VImageDimension>:: 
00068 ImageDifferenceSOMetricThread()
00069 {
00070   m_ReferenceImagePath.Reset();
00071   m_ResultImagePath.Reset();
00072 };
00073 
00074 template <unsigned int VImageDimension>
00075 ImageDifferenceSOMetricThread<VImageDimension>:: 
00076 ~ImageDifferenceSOMetricThread() {};
00077 
00078 template <unsigned int VImageDimension>
00079 bool
00080 ImageDifferenceSOMetricThread<VImageDimension>:: 
00081 ProcessSetup(Setup* pAdaptationSetup) throw()
00082 {
00083   bool result = false;
00084 
00085   ControllerCentral::AddOnProgressEvent(this->m_EvaluatedSessionProgressEvent);
00086 
00087   try
00088         {
00089     SessionProcessor processor;
00090 
00091     processor.SetSetup(pAdaptationSetup);
00092     processor.DefineOutput("resultImage", this->m_ResultImagePath);
00093 
00094     processor.InitializeSession();
00095         
00096     this->m_EvaluatedSessionProgressEvent->SetACR(processor.GetSessionInfo()->GetSessionCache());
00097 
00098     clock_t start = clock();
00099 
00100     typename ImageType::Pointer smpResult = processor.GetCastedOutput<ImageType>("resultImage");
00101                 
00102     clock_t stop = clock();
00103     double dDuration = (stop-start) / (CLOCKS_PER_SEC/10); //convert clocks to thenth second
00104 
00105     //evaluating registration result
00106     typename ImageType::Pointer smpReference = NULL;
00107 
00108     if (m_ReferenceImagePath.IsEmpty()) throwExceptionMacro("Error: Reference image path is null, but should be used to measure the adaptation. Ensure correct initialisation of the metric.");
00109     try
00110     {
00111       typename SessionAccessor::GenericMediaPointer smpGenReference = SessionAccessor::GetMedia(m_ReferenceImagePath, processor.GetSessionInfo());
00112       smpReference = static_cast<ImageType*>(smpGenReference.GetPointer());
00113     }
00114     catchAllNPassMacro("Error: cannot retrieve reference image. Ensure correct IDPath. Used path: "<<m_ReferenceImagePath.ToStr());
00115 
00116     EvaluateResult(smpResult, smpReference);
00117                 m_Results.SetDuration((unsigned long)dDuration);
00118 
00119     result = true;
00120         }
00121         catch( FREE::ExceptionBase & err ) 
00122         { 
00123                 this->m_FailureComment = std::string(err.what());
00124         } 
00125         catch(...)
00126         {
00127                 this->m_FailureComment = "unkown error";
00128         }
00129 
00130   ControllerCentral::RemoveOnProgressEvent(m_EvaluatedSessionProgressEvent);
00131 
00132   return result;
00133 };
00134 
00135 template <unsigned int VImageDimension>
00136 void
00137 ImageDifferenceSOMetricThread<VImageDimension>:: 
00138 EvaluateResult(ImageType* pResultImage, ImageType* pReferenceImage)
00139 {
00140   //calculating difference between reference and final image
00141         typedef itk::AbsoluteValueDifferenceImageFilter< ImageType, ImageType, ImageType > AbsDiffFilterType;
00142         typename AbsDiffFilterType::Pointer smpDiffFilter = AbsDiffFilterType::New();
00143 
00144         smpDiffFilter->SetInput1(pReferenceImage);
00145         smpDiffFilter->SetInput2(pResultImage);
00146         smpDiffFilter->Update();
00147   typename ImageType::Pointer smpDifImage = smpDiffFilter->GetOutput();
00148 
00149         //computing mean and variance of the field difference
00150         typedef ImageSampleCharacteristicsCalculator<ImageType> CharCalculator;
00151 
00152   CharCalculator calculator;
00153         calculator.SetImage(smpDifImage);
00154         calculator.Compute();
00155 
00156         this->m_Results.SetError(calculator.GetMean());
00157         this->m_Results.SetVariance(calculator.GetVariance());
00158         this->m_Results.SetSamplesize(pResultImage->GetLargestPossibleRegion().GetNumberOfPixels());
00159 
00160         //checking for minimum or maximum and calculate image error;
00161   double dImageError = 0.0;
00162   double dMin = itk::NumericTraits< double >::max();
00163   double dMax = 0.0;
00164 
00165   typedef itk::ImageRegionConstIterator<ImageType> ImageIterator;
00166         ImageIterator it(smpDifImage,smpDifImage->GetBufferedRegion());
00167 
00168   for ( it.GoToBegin(); !it.IsAtEnd(); ++it )
00169         {
00170     dImageError += it.Get();
00171                 if (it.Get()<dMin) dMin = it.Get();
00172                 if (it.Get()>dMax) dMax = it.Get();
00173         }
00174 
00175   this->m_Results.SetImageError(dImageError);
00176   this->m_Results.SetMinError(dMin);
00177   this->m_Results.SetMaxError(dMax);
00178 };
00179 
00180 } // end namespace FREE
00181 
00182 #endif

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