00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef __freRigidRegistrationProcessor_txx
00023 #define __freRigidRegistrationProcessor_txx
00024
00025 #include "freRigidRegistrationProcessor.h"
00026 #include "freOptimizerControllerBase.h"
00027 #include "freTransformSetupAdaptor.h"
00028
00029 namespace FREE
00030 {
00034
00035
00036
00037 template <class TRegistrationMethod, class TTransformBase, class TInterpolateBase,
00038 class TMetricBase, class TOptimizerBase, unsigned int VImageDimension>
00039 void
00040 SingleRigidRegistrationProcessorBase<TRegistrationMethod, TTransformBase, TInterpolateBase, TMetricBase, TOptimizerBase, VImageDimension>::
00041 ComputeRegistration()
00042 {
00043 long optimizerObserverID = 0;
00044
00045 try
00046 {
00047 RigidRegistrationProcessorBase<TRegistrationMethod, TTransformBase, TInterpolateBase, TMetricBase, TOptimizerBase, VImageDimension>::ComputeRegistration();
00048
00049 this->m_Registration->SetMetric(this->m_Metric);
00050 this->m_Registration->SetOptimizer(this->m_Optimizer);
00051 this->m_Registration->SetTransform(this->m_Transform);
00052 this->m_Registration->SetInterpolator(this->m_Interpolator);
00053
00054
00055 if(this->fnOnProgress.IsNotNull())
00056 this->fnOnProgress->Execute(RSTInitProcessor, "Setting initial transform parameters and setup values of components",this);
00057
00058 SessionBuilder::ActualizeComponent(this->m_pComponentCache, this->m_pSessionInfo);
00059 ComponentSetup* pTransformSetup = this->GetComponentSetup()->Components().GetElement(cComp_MainTransform);
00060
00061 if (!pTransformSetup)
00062 throwExceptionMacro("Component setup of MainTransform does not exist.");
00063
00064 TransformSetupAdaptor transformAdaptor(pTransformSetup);
00065 this->m_CurrentTransformParameters = transformAdaptor.GetCurrentTransformParameters();
00066 this->m_Registration->SetInitialTransformParameters(this->m_CurrentTransformParameters );
00067
00068
00069 if(this->fnOnProgress.IsNotNull())
00070 this->fnOnProgress->Execute(RSTInitProcessor, "Connecting images",this);
00071 this->m_Registration->SetFixedImage(this->m_smpFixedImage);
00072 this->m_Registration->SetMovingImage(this->m_smpMovingImage);
00073
00074
00075
00076
00077
00078
00079
00080
00081 std::string sParams = "";
00082 for ( int iIndex = 0; iIndex < transformAdaptor.GetCurrentTransformParameters().Size(); iIndex++)
00083 {
00084 sParams =sParams+"; "+FREE::Convert::ToStr( transformAdaptor.GetCurrentTransformParameters().GetElement(iIndex),5);
00085 }
00086 if(this->fnOnProgress.IsNotNull())
00087 this->fnOnProgress->Execute(RSTInitProcessor, "Initial transform parameters: "+sParams,this);
00088
00089
00090 this->m_IterationObserver->fnOnNotify = this->m_IterationNotificationEvent;
00091 optimizerObserverID = this->m_Optimizer->AddObserver(itk::IterationEvent(), this->m_IterationObserver);
00092
00093
00094
00095 }
00096 catchAllNPassMacro("Unknown Error while preparing the registration.");
00097
00098 try
00099 {
00100 if(this->fnOnProgress.IsNotNull())
00101 this->fnOnProgress->Execute(RSTProcessing,"",this);
00102
00103
00104
00105 if(this->fnOnNextLevel.IsNotNull())
00106 this->fnOnNextLevel->Execute(this->m_iResolutionLevel,this);
00107
00108 this->m_Registration->StartRegistration();
00109
00110 this->m_Optimizer->RemoveObserver(optimizerObserverID);
00111 }
00112 catchAllNPassMacro("Unknown Error while itk processing the registration.");
00113
00114
00115 TransformSetupAdaptor finalTransformAdaptor(this->GetComponentSetup()->Components().GetElement(cComp_MainTransform));
00116 finalTransformAdaptor.SetCurrentTransformParameters(this->m_Registration->GetLastTransformParameters());
00117
00118 this->m_CurrentTransformParameters = this->m_Registration->GetLastTransformParameters();
00119 };
00120
00121 template <class TRegistrationMethod, class TTransformBase, class TInterpolateBase,
00122 class TMetricBase, class TOptimizerBase, unsigned int VImageDimension>
00123 SingleRigidRegistrationProcessorBase<TRegistrationMethod, TTransformBase, TInterpolateBase, TMetricBase, TOptimizerBase, VImageDimension>::
00124 SingleRigidRegistrationProcessorBase():RigidRegistrationProcessorBase<TRegistrationMethod, TTransformBase, TInterpolateBase, TMetricBase, TOptimizerBase, VImageDimension>()
00125 {}
00126 ;
00127
00128 template <class TRegistrationMethod, class TTransformBase, class TInterpolateBase,
00129 class TMetricBase, class TOptimizerBase, unsigned int VImageDimension>
00130 SingleRigidRegistrationProcessorBase<TRegistrationMethod, TTransformBase, TInterpolateBase, TMetricBase, TOptimizerBase, VImageDimension>::
00131 ~SingleRigidRegistrationProcessorBase()
00132 {}
00133 ;
00134
00135
00139
00140
00141 template <unsigned int VImageDimension>
00142 RigidRegistrationProcessor<VImageDimension>::
00143 RigidRegistrationProcessor():SingleRigidRegistrationProcessorBase< RegistrationType, TransformType, InterpolatorType, MetricType, OptimizerType, VImageDimension>()
00144 {}
00145 ;
00146
00147 template <unsigned int VImageDimension>
00148 RigidRegistrationProcessor<VImageDimension>::
00149 ~RigidRegistrationProcessor()
00150 {}
00151 ;
00152
00156
00157
00158 template <unsigned int VImageDimension>
00159 const int
00160 MultiRigidRegistrationProcessor<VImageDimension>::
00161 GetResolutionLevelCount() const
00162 {
00163 int iLevelCount;
00164 if (this->GetComponentSetup())
00165 if (this->GetComponentSetup()->Parameters().GetParameterValue(cParam_MRRegLevel,iLevelCount))
00166 return iLevelCount;
00167 return 1;
00168 };
00169
00170 template <unsigned int VImageDimension>
00171 void
00172 MultiRigidRegistrationProcessor<VImageDimension>::
00173 ComputeRegistration()
00174 {
00175 long optimizerObserverID = 0;
00176 try
00177 {
00178 Superclass::ComputeRegistration();
00179
00180 m_bFirstResLevel = true;
00181 this->m_iResolutionLevel = 0;
00182
00183 int iLevelNumber;
00184 this->GetComponentSetup()->Parameters().GetParameterValue(cParam_MRRegLevel,iLevelNumber);
00185 this->m_Registration->SetNumberOfLevels(iLevelNumber);
00186
00187
00188 if(this->fnOnProgress.IsNotNull())
00189 this->fnOnProgress->Execute(RSTInitProcessor, "Setting initial transform parameters and setup values of components",this);
00190
00191 SessionBuilder::ActualizeComponent(this->m_pComponentCache, this->m_pSessionInfo);
00192 ComponentSetup* pTransformSetup = this->GetComponentSetup()->Components().GetElement(cComp_MainTransform);
00193
00194 if (!pTransformSetup)
00195 throwExceptionMacro("Component setup of MainTransform does not exist.");
00196
00197 TransformSetupAdaptor transformAdaptor(pTransformSetup);
00198 this->m_Registration->SetInitialTransformParameters(transformAdaptor.GetCurrentTransformParameters());
00199
00200
00201 bool bUseSchedule;
00202 this->GetComponentSetup()->Parameters().GetParameterValue(cParam_MRRegUseSchedule,bUseSchedule);
00203
00204 if (bUseSchedule)
00205 {
00206
00207 int iPixelSize;
00208 itk::Array2D<unsigned int> schedule(iLevelNumber,VImageDimension);
00209 for (int iLevel=0; iLevel<iLevelNumber; iLevel++)
00210 {
00211 for (int iDim=0; iDim<VImageDimension; iDim++)
00212 {
00213 this->GetComponentSetup()->Parameters().GetParameterValue(cParam_MRRegResolutionSchedule,iPixelSize,iDim,iLevel);
00214 schedule[iLevel][iDim] = iPixelSize;
00215 };
00216 };
00217 smpFixedImagePyramid->SetNumberOfLevels(iLevelNumber);
00218 smpMovingImagePyramid->SetNumberOfLevels(iLevelNumber);
00219 smpFixedImagePyramid->SetSchedule(schedule);
00220 smpMovingImagePyramid->SetSchedule(schedule);
00221 }
00222
00223 this->m_Registration->SetFixedImagePyramid( smpFixedImagePyramid );
00224 this->m_Registration->SetMovingImagePyramid( smpMovingImagePyramid );
00225
00226
00227 if(this->fnOnProgress.IsNotNull())
00228 this->fnOnProgress->Execute(RSTInitProcessor, "Connecting images",this);
00229
00230 this->m_Registration->SetFixedImage(this->m_smpFixedImage);
00231 this->m_Registration->SetMovingImage(this->m_smpMovingImage);
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241 this->m_Registration->SetFixedImageRegion(this->m_smpFixedImage->GetLargestPossibleRegion());
00242
00243
00244 std::string sParams = "";
00245 for ( int iIndex = 0; iIndex < transformAdaptor.GetCurrentTransformParameters().Size(); iIndex++)
00246 {
00247 sParams =sParams+"; "+FREE::Convert::ToStr( transformAdaptor.GetCurrentTransformParameters().GetElement(iIndex),5);
00248 }
00249 if(this->fnOnProgress.IsNotNull())
00250 this->fnOnProgress->Execute(RSTInitProcessor, "Initial transform parameters: "+sParams, this);
00251
00252
00253 this->m_IterationObserver->fnOnNotify = this->m_IterationNotificationEvent;
00254 this->m_LevelObserver->fnOnNotify = m_LevelNotificationEvent;
00255
00256 optimizerObserverID = this->m_Optimizer->AddObserver(itk::IterationEvent(), this->m_IterationObserver);
00257 this->m_Registration->AddObserver(itk::IterationEvent(), m_LevelObserver);
00258
00259
00260
00261 }
00262 catchAllNPassMacro("Unknown Error while preparing the registration.");
00263
00264 try
00265 {
00266 if(this->fnOnProgress.IsNotNull())
00267 this->fnOnProgress->Execute(RSTProcessing,"Starting registration",this);
00268 this->m_Registration->StartRegistration();
00269
00270 this->m_Optimizer->RemoveObserver(optimizerObserverID);
00271 }
00272 catchAllNPassMacro("Unknown Error while itk processing the registration.");
00273
00274
00275 TransformSetupAdaptor finalTransformAdaptor(this->GetComponentSetup()->Components().GetElement(cComp_MainTransform));
00276 finalTransformAdaptor.SetCurrentTransformParameters(this->m_Registration->GetLastTransformParameters());
00277
00278 this->m_CurrentTransformParameters = this->m_Registration->GetLastTransformParameters();
00279 };
00280
00281 template <unsigned int VImageDimension>
00282 MultiRigidRegistrationProcessor<VImageDimension>::
00283 MultiRigidRegistrationProcessor():RigidRegistrationProcessorBase< RegistrationType, TransformType, InterpolatorType, MetricType, OptimizerType, VImageDimension>()
00284 {
00285 smpFixedImagePyramid = PyramidType::New();
00286 smpMovingImagePyramid = PyramidType::New();
00287 m_LevelObserver = IterationObserver::New();
00288 m_LevelNotificationEvent = NotificationEvent<MultiRigidRegistrationProcessor>::New(this, &Self::OnNewLevel);
00289 };
00290
00291 template <unsigned int VImageDimension>
00292 MultiRigidRegistrationProcessor<VImageDimension>::
00293 ~MultiRigidRegistrationProcessor()
00294 {}
00295 ;
00296
00297 template <unsigned int VImageDimension>
00298 void
00299 MultiRigidRegistrationProcessor<VImageDimension>::
00300 OnNewLevel(void* pSender, long threadID)
00301 {
00302 if (m_bFirstResLevel)
00303 m_bFirstResLevel = false;
00304 else
00305 {
00306 this->m_iResolutionLevel++;
00307 try
00308 {
00309 if (this->m_iResolutionLevel>0)
00310 {
00311 SessionBuilder::ActualizeComponent(this->m_pComponentCache, this->m_pSessionInfo, this->m_iResolutionLevel);
00312
00313 }
00314 }
00315 catchAllNPassMacro("Unknown error, while actualizing the setup parameters during a level change.");
00316 }
00317
00318 this->m_lCurIteration = 0;
00319
00320 if (this->fnOnNextLevel)
00321 {
00322 try
00323 {
00324 if(this->fnOnNextLevel.IsNotNull())
00325 this->fnOnNextLevel->Execute(this->m_iResolutionLevel,this);
00326 }
00327 catchAllNPassMacro("Unknown error, while calling OnNextLevel event.");
00328 };
00329 };
00330
00331 }
00332
00333 #endif