freDemonRegistrationProcessor.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: freDemonRegistrationProcessor.h,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 __freDemonRegistrationProcessor_txx
00023 #define __freDemonRegistrationProcessor_txx
00024 
00025 #include "freDemonRegistrationProcessor.h"
00026 #include "freOptimizerControllerBase.h"
00027 #include "freTransformSetupAdaptor.h"
00028 
00029 #include "itkPasteImageFilter.h"
00030 
00031 namespace FREE
00032 {
00033 
00037 
00038 
00039 template <unsigned int VImageDimension>
00040 void
00041 DemonRegistrationProcessor<VImageDimension>::
00042 ComputeRegistration()
00043 {
00044     long optimizerObserverID = 0;
00045     try
00046     {
00047         Superclass::ComputeRegistration();
00048 
00049         typename HistoMatchFilterType::Pointer histoMatch = HistoMatchFilterType::New();
00050 
00051         ComponentSetup* pSetup = this->GetComponentSetup();
00052         int iNrOfIterations;
00053         if(!pSetup->Parameters().GetParameterValue(cParam_Iterations,iNrOfIterations))
00054             throwExceptionMacro("Missing parameter: " << cParam_Iterations);
00055 
00056         double dStdDev;
00057         if(!pSetup->Parameters().GetParameterValue(cParam_StdDev,dStdDev))
00058             throwExceptionMacro("Missing parameter: " << cParam_StdDev);
00059 
00060         bool bHistoMatch;
00061         if(!pSetup->Parameters().GetParameterValue(cParam_HistoMatch,bHistoMatch))
00062             throwExceptionMacro("Missing parameter: " << cParam_HistoMatch);
00063 
00064         long lHistoLevels;
00065         if(!pSetup->Parameters().GetParameterValue(cParam_HistoLevels,lHistoLevels))
00066             throwExceptionMacro("Missing parameter: " << cParam_HistoLevels);
00067 
00068         long lHistoPoints;
00069         if(!pSetup->Parameters().GetParameterValue(cParam_HistoMatchPoints,lHistoPoints))
00070             throwExceptionMacro("Missing parameter: " << cParam_HistoMatchPoints);
00071 
00072         bool bHistoThreshold;
00073         if(!pSetup->Parameters().GetParameterValue(cParam_HistoThreshold,bHistoThreshold))
00074             throwExceptionMacro("Missing parameter: " << cParam_HistoThreshold);
00075 
00076         //Extract needed ImageParts;
00077         typename ImageType::Pointer smpMovingPart = this->m_smpMovingImage;
00078         typename ImageType::Pointer smpFixedPart = this->m_smpFixedImage;
00079 
00080         //Connect images
00081         if(bHistoMatch)
00082         {
00083             if(this->fnOnProgress.IsNotNull())
00084                 this->fnOnProgress->Execute(RSTInitProcessor, "Histogram match enabled",this);
00085 
00086             histoMatch->SetInput( smpMovingPart );
00087             histoMatch->SetReferenceImage( smpFixedPart );
00088 
00089             histoMatch->SetNumberOfHistogramLevels( lHistoLevels );
00090             histoMatch->SetNumberOfMatchPoints( lHistoPoints );
00091             if ( bHistoThreshold )
00092                 histoMatch->ThresholdAtMeanIntensityOn();
00093 
00094             //Calculate histogram matching
00095             histoMatch->Update();
00096             //Connect it to registration
00097             smpRegistration->SetFixedImage(smpFixedPart);
00098             smpRegistration->SetMovingImage(histoMatch->GetOutput());
00099         }
00100         else
00101         {
00102             smpRegistration->SetFixedImage(smpFixedPart);
00103             smpRegistration->SetMovingImage(smpMovingPart);
00104         };
00105 
00106         smpRegistration->SetNumberOfIterations( iNrOfIterations );
00107         smpRegistration->SetStandardDeviations( dStdDev );
00108 
00109         //Register observer
00110         m_IterationObserver->fnOnNotify = m_IterationNotificationEvent;
00111         optimizerObserverID = this->smpRegistration->AddObserver(itk::IterationEvent(), this->m_IterationObserver);
00112     }
00113     catchAllNPassMacro("Unknown Error while preparing the registration.");
00114 
00115     try
00116     {
00117         //Call the OnNextLevel one time before starting the registration.
00118         //even a singel resolution Registration has at least one level to notify
00119         if(this->fnOnNextLevel.IsNotNull())
00120             this->fnOnNextLevel->Execute(0,this);
00121 
00122         this->smpRegistration->Update();
00123 
00124         this->smpRegistration->RemoveObserver(optimizerObserverID);
00125     }
00126     catchAllNPassMacro("Unknown Error while itk processing the registration.");
00127 };
00128 
00129 template <unsigned int VImageDimension>
00130 DemonRegistrationProcessor<VImageDimension>::
00131 DemonRegistrationProcessor()
00132 {
00133     smpRegistration = itk::DemonsRegistrationFilter< ImageType, ImageType, TransformFieldType >::New();
00134     smpInterpolate = NULL;
00135     m_IterationObserver = IterationObserver::New();
00136     m_IterationNotificationEvent = NotificationEvent<DemonRegistrationProcessor>::New(this, &Self::OnNewIteration);
00137 };
00138 
00139 template <unsigned int VImageDimension>
00140 DemonRegistrationProcessor<VImageDimension>::
00141 ~DemonRegistrationProcessor()
00142 {}
00143 ;
00144 
00145 template <unsigned int VImageDimension>
00146 typename DemonRegistrationProcessor<VImageDimension>::TransformFieldPointer
00147 DemonRegistrationProcessor<VImageDimension>::
00148 ComputeTransformationField(const PointType& origin, const RegionType& region, const SpacingType& spacing)
00149 {
00150     if(this->fnOnProgress.IsNotNull())
00151         this->fnOnProgress->Execute(RSTFinalizing, "Computing transformation field",this);
00152 
00153     TransformFieldPointer smpDemField = smpRegistration->GetDeformationField();
00154 
00155     if (!( (region==smpDemField->GetLargestPossibleRegion())&
00156            (spacing==smpDemField->GetSpacing())&
00157            (origin==smpDemField->GetOrigin()) ) )
00158     { // have to resize the field.
00159 
00160         //Create the field in correct size
00161         typename TransformFieldType::Pointer smpField = TransformFieldType::New();
00162         smpField->SetOrigin(origin);
00163         smpField->SetSpacing(spacing);
00164         smpField->SetRegions(region);
00165         smpField->Allocate();
00166         //Reset the transformation field to no transformation (all vectors components are 0)
00167         typename TransformFieldType::PixelType nullVector;
00168         for (int iIndex=0; iIndex < VImageDimension; iIndex++)
00169             nullVector[iIndex]=0;
00170 
00171         // iterator for the output image
00172         itk::ImageRegionIteratorWithIndex<TransformFieldType> outputIt(
00173           smpField, smpField->GetLargestPossibleRegion() );
00174 
00175         // interpolator
00176         typedef itk::LinearInterpolateImageFunction<TransformFieldType,ScalarType> InterpolatorType;
00177         typename InterpolatorType::Pointer interpolator = InterpolatorType::New();
00178         interpolator->SetInputImage(smpDemField);
00179 
00180         typename ImageType::IndexType index;
00181         typename ImageType::PointType point;
00182         typename TransformFieldType::PixelType value;
00183 
00184         while( !outputIt.IsAtEnd() )
00185         {
00186           // get the output image index
00187           index = outputIt.GetIndex();
00188           smpField->TransformIndexToPhysicalPoint( index, point );
00189 
00190           if( interpolator->IsInsideBuffer( point ) )
00191           {
00192             value = interpolator->Evaluate( point );
00193             outputIt.Set( value );
00194           }
00195           else
00196           {
00197             outputIt.Set( nullVector );
00198           }  
00199 
00200           ++outputIt;
00201         }
00202 
00203         return smpField;
00204     }
00205     else
00206         return smpDemField;
00207 };
00208 
00209 template <unsigned int VImageDimension>
00210 void
00211 DemonRegistrationProcessor<VImageDimension>::
00212 OnNewIteration(void* pSender, long threadID)
00213 {
00214     if (this->fnOnNextIteration)
00215     {
00216         //(*fnOnNextIteration)(lIteration, value, parameters,this, 0);
00217     };
00218 };
00219 
00220 }//End of Namespace free
00221 
00222 #endif

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