9 #include <itkTimeProbe.h> 14 template <
unsigned int NDimensions>
15 DenseSVFTransformAgregator <NDimensions>::
28 template <
unsigned int NDimensions>
41 itk::TimeProbe tmpTime;
47 this->estimateSVFFromTranslations();
52 this->estimateSVFFromRigidTransforms();
58 this->estimateSVFFromAffineTransforms();
65 std::cout <<
"Agregation performed in " << tmpTime.GetTotal() << std::endl;
70 template <
unsigned int NDimensions>
73 estimateSVFFromTranslations()
75 const unsigned int NDegreesFreedom = NDimensions;
78 velocityField->Initialize();
84 velocityField->Allocate();
88 itk::ImageRegionIterator < VelocityFieldType > svfIterator(velocityField,
m_LargestRegion);
89 while (!svfIterator.IsAtEnd())
91 svfIterator.Set(zeroDisp);
96 weights->Initialize();
103 weights->FillBuffer(0);
109 for (
unsigned int i = 0;i < nbPts;++i)
116 for (
unsigned int j = 0;j < NDimensions;++j)
117 tmpParams[j] = tmpTr->GetOffset()[j];
121 weights->TransformPhysicalPointToIndex(this->
GetInputOrigin(i),posIndex);
122 for (
unsigned int j = 0;j < NDimensions;++j)
123 curDisp[j] = tmpParams[j];
125 velocityField->SetPixel(posIndex,curDisp);
126 weights->SetPixel(posIndex,tmpWeight);
130 typename SVFMEstimateType::Pointer fieldSmoother = SVFMEstimateType::New();
132 fieldSmoother->SetInput(velocityField);
133 fieldSmoother->SetWeightImage(weights);
139 fieldSmoother->SetMaxNumIterations(100);
143 fieldSmoother->Update();
145 velocityField = fieldSmoother->GetOutput();
146 velocityField->DisconnectPipeline();
149 typename BaseOutputTransformType::Pointer resultTransform = BaseOutputTransformType::New();
150 resultTransform->SetIdentity();
151 resultTransform->SetParametersAsVectorField(velocityField.GetPointer());
156 template <
unsigned int NDimensions>
159 estimateSVFFromRigidTransforms()
161 const unsigned int NDegreesFreedom = NDimensions * (NDimensions + 1) / 2;
162 typedef itk::Image < itk::Vector <ScalarType, NDegreesFreedom>, NDimensions > RigidFieldType;
163 typedef typename RigidFieldType::Pointer RigidFieldPointer;
164 typedef itk::Vector <ScalarType, NDegreesFreedom> RigidVectorType;
167 RigidFieldPointer rigidField = RigidFieldType::New();
168 rigidField->Initialize();
174 rigidField->Allocate();
176 RigidVectorType zeroDisp;
178 itk::ImageRegionIterator < RigidFieldType > rigidIterator(rigidField,
m_LargestRegion);
179 while (!rigidIterator.IsAtEnd())
181 rigidIterator.Set(zeroDisp);
186 weights->Initialize();
193 weights->FillBuffer(0);
195 RigidVectorType curLog;
198 vnl_matrix <InternalScalarType> tmpMatrix(NDimensions+1,NDimensions+1,0);
199 tmpMatrix(NDimensions,NDimensions) = 1;
203 for (
unsigned int i = 0;i < nbPts;++i)
206 weights->TransformPhysicalPointToIndex(this->
GetInputOrigin(i),posIndex);
208 LogRigidTransformType *tmpTrsf = (LogRigidTransformType *)this->
GetInputTransform(i);
209 rigidField->SetPixel(posIndex,tmpTrsf->GetLogVector());
210 weights->SetPixel(posIndex,tmpWeight);
214 typename SVFMEstimateType::Pointer fieldSmoother = SVFMEstimateType::New();
216 fieldSmoother->SetInput(rigidField);
217 fieldSmoother->SetWeightImage(weights);
223 fieldSmoother->SetMaxNumIterations(100);
227 fieldSmoother->Update();
229 rigidField = fieldSmoother->GetOutput();
230 rigidField->DisconnectPipeline();
233 velocityField->Initialize();
237 velocityField->SetOrigin (
m_Origin);
239 velocityField->Allocate();
241 itk::ImageRegionIteratorWithIndex < VelocityFieldType > svfIterator(velocityField,
m_LargestRegion);
247 rigidIterator = itk::ImageRegionIterator < RigidFieldType > (rigidField,
m_LargestRegion);
248 while (!svfIterator.IsAtEnd())
250 curLog = rigidIterator.Get();
252 unsigned int pos = 0;
253 for (
unsigned int j = 0;j < NDimensions;++j)
254 for (
unsigned int k = j+1;k < NDimensions;++k)
256 tmpMatrix(j,k) = curLog[pos];
257 tmpMatrix(k,j) = - curLog[pos];
261 for (
unsigned int j = 0;j < NDimensions;++j)
263 tmpMatrix(j,NDimensions) = curLog[pos];
267 curIndex = svfIterator.GetIndex();
268 velocityField->TransformIndexToPhysicalPoint(curIndex,curPoint);
269 for (
unsigned int i = 0;i < NDimensions;++i)
271 curDisp[i] = tmpMatrix(i,NDimensions);
272 for (
unsigned int j = 0;j < NDimensions;++j)
273 curDisp[i] += tmpMatrix(i,j) * curPoint[j];
276 svfIterator.Set(curDisp);
283 typename BaseOutputTransformType::Pointer resultTransform = BaseOutputTransformType::New();
284 resultTransform->SetIdentity();
285 resultTransform->SetParametersAsVectorField(velocityField.GetPointer());
290 template <
unsigned int NDimensions>
293 estimateSVFFromAffineTransforms()
295 const unsigned int NDegreesFreedom = NDimensions * (NDimensions + 1);
296 typedef itk::Image < itk::Vector <ScalarType, NDegreesFreedom>, NDimensions > AffineFieldType;
297 typedef typename AffineFieldType::Pointer AffineFieldPointer;
298 typedef itk::Vector <ScalarType, NDegreesFreedom> AffineVectorType;
301 AffineFieldPointer affineField = AffineFieldType::New();
302 affineField->Initialize();
308 affineField->Allocate();
310 AffineVectorType zeroDisp;
312 itk::ImageRegionIterator < AffineFieldType > affineIterator(affineField,
m_LargestRegion);
313 while (!affineIterator.IsAtEnd())
315 affineIterator.Set(zeroDisp);
320 weights->Initialize();
327 weights->FillBuffer(0);
329 AffineVectorType curLog;
332 vnl_matrix <InternalScalarType> tmpMatrix(NDimensions+1,NDimensions+1,0);
333 tmpMatrix(NDimensions,NDimensions) = 1;
334 std::vector < AffineVectorType > logVectors(nbPts);
340 MatrixLoggerFilterType *logFilter =
new MatrixLoggerFilterType;
343 logFilter->SetUseRigidTransforms(
false);
346 logVectors = logFilter->GetOutput();
353 DistoTrsfType *tmpTrsf;
354 for (
unsigned int i = 0;i < nbPts;++i)
357 logVectors[i] = tmpTrsf->GetLogVector();
361 for (
unsigned int i = 0;i < nbPts;++i)
364 weights->TransformPhysicalPointToIndex(this->
GetInputOrigin(i),posIndex);
366 if (std::isnan(logVectors[i][0]))
369 logVectors[i].Fill(0);
372 affineField->SetPixel(posIndex,logVectors[i]);
373 weights->SetPixel(posIndex,tmpWeight);
377 typename SVFMEstimateType::Pointer fieldSmoother = SVFMEstimateType::New();
379 fieldSmoother->SetInput(affineField);
380 fieldSmoother->SetWeightImage(weights);
386 fieldSmoother->SetMaxNumIterations(100);
390 fieldSmoother->Update();
392 affineField = fieldSmoother->GetOutput();
393 affineField->DisconnectPipeline();
396 velocityField->Initialize();
400 velocityField->SetOrigin (
m_Origin);
402 velocityField->Allocate();
404 itk::ImageRegionIteratorWithIndex < VelocityFieldType > svfIterator(velocityField,
m_LargestRegion);
410 affineIterator = itk::ImageRegionIterator < AffineFieldType > (affineField,
m_LargestRegion);
411 while (!svfIterator.IsAtEnd())
413 curLog = affineIterator.Get();
415 unsigned int pos = 0;
416 for (
unsigned int j = 0;j < NDimensions;++j)
417 for (
unsigned int k = 0;k <= NDimensions;++k)
419 tmpMatrix(j,k) = curLog[pos];
423 curIndex = svfIterator.GetIndex();
424 velocityField->TransformIndexToPhysicalPoint(curIndex,curPoint);
425 for (
unsigned int i = 0;i < NDimensions;++i)
427 curDisp[i] = tmpMatrix(i,NDimensions);
428 for (
unsigned int j = 0;j < NDimensions;++j)
429 curDisp[i] += tmpMatrix(i,j) * curPoint[j];
432 svfIterator.Set(curDisp);
439 typename BaseOutputTransformType::Pointer resultTransform = BaseOutputTransformType::New();
440 resultTransform->SetIdentity();
441 resultTransform->SetParametersAsVectorField(velocityField.GetPointer());
void SetInput(InputType &input)
Class to compute many log-vectors in a multi-threaded way.