00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef __frePrecisionSOMetric_txx
00023 #define __frePrecisionSOMetric_txx
00024
00025 #include "frePrecisionSOMetric.h"
00026 #include "freGenericSetupToImageAdaptor.h"
00027 #include "freVectorFieldSetVarianceCalculator.h"
00028 #include "freVectorToNormImageFilter.h"
00029 #include "freImageSampleCharacteristicsCalculator.h"
00030 #include "freSessionProcessor.h"
00031
00032 #include "itkNumericTraits.h"
00033 #include "itkAddImageFilter.h"
00034
00035 namespace FREE
00036 {
00037
00038 template <unsigned int VImageDimension>
00039 void
00040 PrecisionSOMetric<VImageDimension>::
00041 SetResultFieldPath(const IDPath& path)
00042 {
00043 itkDebugMacro("setting ResultFieldPath to " << path.ToStr() );
00044 this->m_ResultFieldPath = path;
00045 this->Modified(); \
00046 };
00047
00048 template <unsigned int VImageDimension>
00049 void
00050 PrecisionSOMetric<VImageDimension>::
00051 SetInterimFieldPath(const std::string& path)
00052 {
00053 itkDebugMacro("setting InterimFieldPath to " << path );
00054 this->m_InterimFieldPath = path;
00055 this->Modified();
00056 };
00057
00058 template <unsigned int VImageDimension>
00059 typename PrecisionSOMetric<VImageDimension>::DecomposedMeasureType
00060 PrecisionSOMetric<VImageDimension>::
00061 ComputeDecomposedValue( const ParametersType & parameters ) const
00062 {
00063 if (!this->m_pAdaptations) throwExceptionMacro("Error. Cannot calculate metric value, adaptiation list is null.");
00064 if (this->m_Transform.IsNull()) throwExceptionMacro("Error. Cannot calculate metric value, no setup transform set.");
00065
00066 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());
00067
00068 m_dMinError = itk::NumericTraits< double >::max();
00069 m_dMaxError = 0.0;
00070 m_lFailedRegistrations = 0;
00071 m_lActAdaptation = 0;
00072 m_dErrorMean = 0.0;
00073 m_dErrorVar = 0.0;
00074
00075 m_Transform->SetParameters(parameters);
00076 m_smpSetup = m_Transform->GenerateTransformedSetup();
00077
00078 typedef FREE::VectorFieldSetVarianceCalculator<VImageDimension> EvaluatorType;
00079 EvaluatorType evaluator;
00080
00081 evaluator.SetSaveFieldsOnDisk(m_SaveInterimOnDisc);
00082 evaluator.SetFieldsPath(m_InterimFieldPath);
00083
00084 GenericSetupToImageAdaptor adaptor;
00085 adaptor.SetTemplateSetup(m_smpSetup);
00086 adaptor.AddAdaptations(*m_pAdaptations);
00087
00088 while (!adaptor.EndOfAdaptation())
00089 {
00090 typename Setup::Pointer pSetup = adaptor.AdaptNextSetup();
00091
00092 if (this->fnOnNextAdaptation.IsNotNull()) this->fnOnNextAdaptation->Execute(adaptor.GetCurrentAdaptationID(),"",(void*)this);
00093 this->InvokeEvent( NextAdaptationObserverEvent() );
00094
00095 m_lActAdaptation = adaptor.GetCurrentAdaptationID();
00096
00097 for (unsigned int iIndex = 0; iIndex<m_SampleSize; iIndex++)
00098 {
00099 SessionProcessor processor;
00100
00101 processor.SetSetup(pSetup);
00102
00103 processor.DefineOutput("resultField", this->m_ResultFieldPath);
00104
00105 try
00106 {
00107 processor.InitializeSession();
00108
00109 typename TransformationFieldType::Pointer smpResult = processor.GetCastedOutput<TransformationFieldType>("resultField");
00110
00111 evaluator.AddVectorField(smpResult);
00112 }
00113 catch( FREE::ExceptionBase & err )
00114 {
00115 if (this->fnOnEvaluationFailed.IsNotNull()) this->fnOnEvaluationFailed->Execute(m_lActAdaptation,std::string(err.what()),(void*)this);
00116 m_lFailedRegistrations++;
00117 }
00118 catch(...)
00119 {
00120 if (this->fnOnEvaluationFailed.IsNotNull()) this->fnOnEvaluationFailed->Execute(m_lActAdaptation,"unkown error",(void*)this);
00121 m_lFailedRegistrations++;
00122 }
00123 }
00124
00125 if (fnOnEvaluationDone.IsNotNull()) fnOnEvaluationDone->Execute(adaptor.GetCurrentAdaptationID(),"",(void*)this);
00126 this->InvokeEvent( EvaluationDoneObserverEvent() );
00127 };
00128
00129 evaluator.Compute();
00130 typename EvaluatorType::VarianceFieldType::Pointer smpVarField = evaluator.GetVarianceField();
00131
00132 FREE::ImageSampleCharacteristicsCalculator<typename EvaluatorType::VarianceFieldType> varCalculator;
00133 varCalculator.SetImage(smpVarField);
00134 varCalculator.Compute();
00135
00136 m_dErrorMean = varCalculator.GetMean();
00137 m_dErrorVar = varCalculator.GetVariance();
00138
00139
00140 typedef itk::ImageRegionConstIterator<typename EvaluatorType::VarianceFieldType > NormIterator;
00141 NormIterator it(smpVarField,smpVarField->GetBufferedRegion());
00142 for ( it.GoToBegin(); !it.IsAtEnd(); ++it )
00143 {
00144 if (it.Get()<m_dMinError) m_dMinError = it.Get();
00145 if (it.Get()>m_dMaxError) m_dMaxError = it.Get();
00146 }
00147
00148
00149 DecomposedMeasureType results(5);
00150 results.SetElement(0,m_dErrorMean);
00151 results.SetElement(1,std::sqrt(m_dErrorVar));
00152 results.SetElement(2,m_dMinError);
00153 results.SetElement(3,m_dMaxError);
00154 results.SetElement(4,m_lFailedRegistrations);
00155
00156 return results;
00157 };
00158
00159 template <unsigned int VImageDimension>
00160 PrecisionSOMetric<VImageDimension>::
00161 PrecisionSOMetric()
00162 {
00163 m_IterationEvent = FREE::ProgressEvent<Self>::New(this,&PrecisionSOMetric<VImageDimension>::OnEvaluationProgress);
00164
00165 m_SaveInterimOnDisc = true;
00166 m_InterimFieldPath = "";
00167 };
00168
00169 template <unsigned int VImageDimension>
00170 void
00171 PrecisionSOMetric<VImageDimension>::
00172 OnEvaluationProgress(const long status, const std::string& sComment, void* pSender, long threadID)
00173 {
00174 if (fnOnEvaluationProgress.IsNotNull()) fnOnEvaluationProgress->Execute(status, "", pSender, threadID);
00175 this->InvokeEvent( itk::IterationEvent() );
00176 }
00177
00178 }
00179
00180 #endif