18 #ifndef __itkPSMEntropyModelFilter_hxx 19 #define __itkPSMEntropyModelFilter_hxx 20 #include "itkPSMEntropyModelFilter.h" 22 #include "itkZeroCrossingImageFilter.h" 23 #include "itkImageRegionConstIteratorWithIndex.h" 28 template <
class TImage,
class TShapeMatrix>
29 PSMEntropyModelFilter<TImage, TShapeMatrix>::PSMEntropyModelFilter()
33 this->SetNumberOfScales(1);
35 m_Initializing =
false;
37 m_ParticleSystem = PSMParticleSystem<Dimension>::New();
41 m_ShapeMatrix = ShapeMatrixType::New();
42 m_ShapeMatrix->SetDomainsPerShape(1);
46 m_ParticleEntropyFunction
47 = PSMParticleEntropyFunction<typename ImageType::PixelType, Dimension>::New();
53 m_ShapeEntropyFunction = PSMShapeEntropyFunction<Dimension>::New();
54 m_ShapeEntropyFunction->SetShapeMatrix(m_ShapeMatrix);
57 m_Optimizer = OptimizerType::New();
58 m_IterateCallback = itk::MemberCommand<PSMEntropyModelFilter<TImage, TShapeMatrix> >::New();
60 m_Optimizer->AddObserver(itk::IterationEvent(), m_IterateCallback);
64 m_CostFunction = PSMTwoCostFunction<Dimension>::New();
65 m_CostFunction->SetFunctionA(m_ParticleEntropyFunction);
66 m_CostFunction->SetFunctionB(m_ShapeEntropyFunction);
68 m_Initialized =
false;
74 m_ParticleSystem->RegisterAttribute(m_ShapeMatrix);
78 template<
class TImage,
class TShapeMatrix>
86 m_ParticleSystem->RegisterAttribute(m_SigmaCache);
87 m_ParticleEntropyFunction->SetSpatialSigmaCache(m_SigmaCache);
93 template <
class TImage,
class TShapeMatrix>
97 m_WorkingImages.resize(this->GetNumberOfInputs());
98 for (
unsigned int i = 0; i < this->GetNumberOfInputs(); i++)
100 m_WorkingImages[i] =
const_cast<TImage *
>(this->GetInput(i));
104 template <
class TImage,
class TShapeMatrix>
112 for (
unsigned int i = 0; i < this->GetNumberOfInputs(); i++)
115 ImageType::PixelType, Dimension>::New() );
117 m_DomainList[i]->SetSigma(m_WorkingImages[i]->GetSpacing()[0] * 2.0);
118 m_DomainList[i]->SetImage(m_WorkingImages[i]);
120 if (m_CuttingPlanes.size() > i)
122 m_DomainList[i]->SetCuttingPlane(m_CuttingPlanes[i].x,
123 m_CuttingPlanes[i].y,
124 m_CuttingPlanes[i].z);
127 m_ParticleSystem->AddDomain(m_DomainList[i]);
128 m_ParticleSystem->SetNeighborhood(i, m_NeighborhoodList[i]);
133 template <
class TImage,
class TShapeMatrix>
137 const vnl_vector_fixed<double,3> &y,
138 const vnl_vector_fixed<double,3> &z,
141 if (m_CuttingPlanes.size() < domain+1)
143 m_CuttingPlanes.resize(domain+1);
150 m_CuttingPlanes[domain] = p;
153 template <
class TImage,
class TShapeMatrix>
158 if (m_DomainListNames.size() < (i+1))
160 m_DomainListNames.resize(i+1);
162 m_DomainListNames[i] = s;
165 template <
class TImage,
class TShapeMatrix>
174 for (
unsigned int i = 0; i < m_DomainListNames.size(); i++)
176 if (m_DomainListNames[i] == name)
182 itkExceptionMacro(
"The domain name " + name +
" was not found. ");
186 template <
class TImage,
class TShapeMatrix>
194 if (m_InputCorrespondencePoints.size() > 0)
196 if (this->GetNumberOfInputs() != m_InputCorrespondencePoints.size() )
198 itkExceptionMacro(
"The number of inputs does not match the number of correspondence point lists.");
202 for (
unsigned int i = 0; i < m_InputCorrespondencePoints.size(); i++)
204 this->GetParticleSystem()->AddPositionList(m_InputCorrespondencePoints[i],i);
210 this->CreateSingleCorrespondence();
215 this->GetParticleSystem()->SynchronizePositions();
218 template <
class TImage,
class TShapeMatrix>
224 this->SetInPlace(
false);
227 if (this->GetInitialized() ==
false)
229 this->AllocateWorkingImages();
230 this->AllocateDataCaches();
231 this->GetOptimizer()->SetCostFunction(m_CostFunction);
233 this->AllocateDomainsAndNeighborhoods();
236 this->GetOptimizer()->SetParticleSystem(this->GetParticleSystem());
238 this->InitializeCorrespondences();
239 this->InitializeOptimizationFunctions();
241 this->SetInitialized(
true);
247 if (this->GetInitializing() ==
true)
return;
251 for (
unsigned int scale = m_CurrentScale; scale < m_NumberOfScales; scale++)
253 m_CurrentScale = scale;
258 if (this->GetAbortGenerateData() ==
false)
261 m_Optimizer->SetTolerance(m_Tolerances[scale]);
262 m_Optimizer->SetMaximumNumberOfIterations(m_MaximumNumberOfIterations[scale]);
268 if (m_RegularizationDecaySpan[scale] >= 1.0f)
270 m_ShapeEntropyFunction->SetMinimumVarianceDecay(m_RegularizationInitial[scale],
271 m_RegularizationFinal[scale],
272 m_RegularizationDecaySpan[scale]);
276 m_ShapeEntropyFunction->SetMinimumVariance(m_RegularizationInitial[scale]);
277 m_ShapeEntropyFunction->SetHoldMinimumVariance(
true);
281 this->SetAbortGenerateData(
false);
286 this->GetOptimizer()->StartOptimization();
291 if ((m_NumberOfScales > 1) && (scale != m_NumberOfScales-1) )
293 m_ParticleSystem->SplitAllParticles(this->GetInput()->GetSpacing()[0] * 1.0);
299 template <
class TImage,
class TShapeMatrix>
305 unsigned int maxdim = 0;
306 double maxradius = 0.0;
307 double spacing = this->GetInput()->GetSpacing()[0];
308 for (
unsigned int i = 0; i < TImage::ImageDimension; i++)
310 if (this->GetInput()->GetRequestedRegion().GetSize()[i] > maxdim)
312 maxdim = this->GetInput()->GetRequestedRegion().GetSize()[i];
313 maxradius = maxdim * this->GetInput()->GetSpacing()[i];
319 m_ParticleEntropyFunction->SetMinimumNeighborhoodRadius(spacing * 5.0);
320 m_ParticleEntropyFunction->SetMaximumNeighborhoodRadius(maxradius);
322 m_ShapeMatrix->Initialize();
325 template <
class TImage,
class TShapeMatrix>
335 this->InvokeEvent(itk::IterationEvent());
338 if (this->GetAbortGenerateData() ==
true)
340 m_Optimizer->StopOptimization();
344 template <
class TImage,
class TShapeMatrix>
350 for (
unsigned int i = 0; i < m_ParticleSystem->GetNumberOfDomains(); i++)
352 typename itk::ZeroCrossingImageFilter<ImageType, ImageType>::Pointer zc =
353 itk::ZeroCrossingImageFilter<ImageType, ImageType>::New();
356 m_ParticleSystem->GetDomain(i))->GetImage());
358 itk::ImageRegionConstIteratorWithIndex<ImageType> it(zc->GetOutput(),
359 zc->GetOutput()->GetRequestedRegion());
362 for (it.GoToReverseBegin(); !it.IsAtReverseEnd() && done ==
false; --it)
368 m_ParticleSystem->GetDomain(i))->GetImage()->TransformIndexToPhysicalPoint(it.GetIndex(), pos);
371 m_ParticleSystem->AddPosition(pos, i);
374 catch(itk::ExceptionObject &)
unsigned int GetDomainIndexByName(const std::string &name)
PSMSurfaceNeighborhood is a general purpose neighborhood object that computes neighborhoods based on ...
virtual void AllocateWorkingImages()
void OptimizerIterateCallback(Object *, const EventObject &)
bool CreateSingleCorrespondence()
ImageType::PointType PointType
void SetDomainName(const std::string &s, unsigned int i)
virtual void AllocateDomainsAndNeighborhoods()
virtual void AllocateDataCaches()
This the most basic of all PSM model optimization filters. This filter assembles all of the necessary...
void InitializeCorrespondences()
void AddCuttingPlane(const vnl_vector_fixed< double, 3 > &x, const vnl_vector_fixed< double, 3 > &y, const vnl_vector_fixed< double, 3 > &z, unsigned int domain)