1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
|
/*=========================================================================
*
* Copyright UMC Utrecht and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#ifndef _itkMultiMetricMultiResolutionImageRegistrationMethod_hxx
#define _itkMultiMetricMultiResolutionImageRegistrationMethod_hxx
#include "itkMultiMetricMultiResolutionImageRegistrationMethod.h"
#include "itkContinuousIndex.h"
#include <vnl/vnl_math.h>
/** Macro that implements the set methods. */
#define itkImplementationSetMacro(_name, _type) \
template <typename TFixedImage, typename TMovingImage> \
void MultiMetricMultiResolutionImageRegistrationMethod<TFixedImage, TMovingImage>::Set##_name(_type _arg, \
unsigned int pos) \
{ \
if (pos == 0) \
{ \
this->Superclass::Set##_name(_arg); \
} \
if (pos >= this->GetNumberOf##_name##s()) \
{ \
this->SetNumberOf##_name##s(pos + 1); \
} \
if (this->m_##_name##s[pos] != _arg) \
{ \
this->m_##_name##s[pos] = _arg; \
this->Modified(); \
} \
} // comment to allow ; after calling macro
/** Macro that implements the get methods. */
#define itkImplementationGetMacro(_name, _type1, _type2) \
template <typename TFixedImage, typename TMovingImage> \
auto MultiMetricMultiResolutionImageRegistrationMethod<TFixedImage, TMovingImage>::Get##_name(unsigned int pos) \
const->_type1 _type2 \
{ \
if (pos >= this->GetNumberOf##_name##s()) \
{ \
return 0; \
} \
else \
{ \
return this->m_##_name##s[pos].GetPointer(); \
} \
} // comment to allow ; after calling macro
namespace itk
{
itkImplementationSetMacro(FixedImage, const FixedImageType *);
itkImplementationSetMacro(MovingImage, const MovingImageType *);
itkImplementationSetMacro(FixedImageRegion, const FixedImageRegionType);
itkImplementationSetMacro(Interpolator, InterpolatorType *);
itkImplementationSetMacro(FixedImagePyramid, FixedImagePyramidType *);
itkImplementationSetMacro(MovingImagePyramid, MovingImagePyramidType *);
itkImplementationGetMacro(FixedImage, const, FixedImageType *);
itkImplementationGetMacro(MovingImage, const, MovingImageType *);
itkImplementationGetMacro(Interpolator, , InterpolatorType *);
itkImplementationGetMacro(FixedImagePyramid, , FixedImagePyramidType *);
itkImplementationGetMacro(MovingImagePyramid, , MovingImagePyramidType *);
/**
* ****************** Constructor ******************
*/
template <typename TFixedImage, typename TMovingImage>
MultiMetricMultiResolutionImageRegistrationMethod<TFixedImage,
TMovingImage>::MultiMetricMultiResolutionImageRegistrationMethod()
{
this->SetMetric(CombinationMetricType::New());
this->m_Stop = false;
this->m_LastTransformParameters = ParametersType(1);
this->m_LastTransformParameters.Fill(0.0f);
} // end Constructor
/**
* **************** GetFixedImageRegion **********************************
*/
template <typename TFixedImage, typename TMovingImage>
auto
MultiMetricMultiResolutionImageRegistrationMethod<TFixedImage, TMovingImage>::GetFixedImageRegion(
unsigned int pos) const -> const FixedImageRegionType &
{
if (pos >= this->GetNumberOfFixedImageRegions())
{
/** Return a dummy fixed image region. */
return this->m_NullFixedImageRegion;
}
else
{
return this->m_FixedImageRegions[pos];
}
} // end GetFixedImageRegion()
/**
* ********************** SetMetric *******************************
*/
template <typename TFixedImage, typename TMovingImage>
void
MultiMetricMultiResolutionImageRegistrationMethod<TFixedImage, TMovingImage>::SetMetric(MetricType * _arg)
{
CombinationMetricType * testPtr = dynamic_cast<CombinationMetricType *>(_arg);
if (testPtr)
{
if (this->m_CombinationMetric != testPtr)
{
this->m_CombinationMetric = testPtr;
this->Superclass::SetMetric(this->m_CombinationMetric);
this->Modified();
}
}
else
{
itkExceptionMacro("The metric must of type CombinationImageToImageMetric!");
}
} // end SetMetric()
/**
* ****************** Initialize *******************************
*/
template <typename TFixedImage, typename TMovingImage>
void
MultiMetricMultiResolutionImageRegistrationMethod<TFixedImage, TMovingImage>::Initialize()
{
this->CheckOnInitialize();
/** Setup the metric. */
this->GetCombinationMetric()->SetTransform(this->GetModifiableTransform());
this->GetCombinationMetric()->SetFixedImage(this->GetFixedImagePyramid()->GetOutput(this->GetCurrentLevel()));
for (unsigned int i = 0; i < this->GetNumberOfFixedImagePyramids(); ++i)
{
this->GetCombinationMetric()->SetFixedImage(this->GetFixedImagePyramid(i)->GetOutput(this->GetCurrentLevel()), i);
}
this->GetCombinationMetric()->SetMovingImage(this->GetMovingImagePyramid()->GetOutput(this->GetCurrentLevel()));
for (unsigned int i = 0; i < this->GetNumberOfMovingImagePyramids(); ++i)
{
this->GetCombinationMetric()->SetMovingImage(this->GetMovingImagePyramid(i)->GetOutput(this->GetCurrentLevel()), i);
}
this->GetCombinationMetric()->SetInterpolator(this->GetInterpolator());
for (unsigned int i = 0; i < this->GetNumberOfInterpolators(); ++i)
{
this->GetCombinationMetric()->SetInterpolator(this->GetInterpolator(i), i);
}
this->GetCombinationMetric()->SetFixedImageRegion(this->m_FixedImageRegionPyramids[0][this->GetCurrentLevel()]);
for (unsigned int i = 0; i < this->m_FixedImageRegionPyramids.size(); ++i)
{
this->GetCombinationMetric()->SetFixedImageRegion(this->m_FixedImageRegionPyramids[i][this->GetCurrentLevel()], i);
}
// this->GetMetric()->Initialize();
this->GetCombinationMetric()->Initialize();
/** Setup the optimizer. */
this->GetModifiableOptimizer()->SetCostFunction(this->GetModifiableMetric());
this->GetModifiableOptimizer()->SetInitialPosition(this->GetInitialTransformParametersOfNextLevel());
/** Connect the transform to the Decorator. */
TransformOutputType * transformOutput = static_cast<TransformOutputType *>(this->ProcessObject::GetOutput(0));
transformOutput->Set(this->GetTransform());
} // end Initialize()
/**
* ****************** PrepareAllPyramids ******************
*/
template <typename TFixedImage, typename TMovingImage>
void
MultiMetricMultiResolutionImageRegistrationMethod<TFixedImage, TMovingImage>::PrepareAllPyramids()
{
this->CheckPyramids();
/** Set up the fixed image pyramids and the fixed image region pyramids. */
using SizeType = typename FixedImageRegionType::SizeType;
using IndexType = typename FixedImageRegionType::IndexType;
using ScheduleType = typename FixedImagePyramidType::ScheduleType;
this->m_FixedImageRegionPyramids.resize(this->GetNumberOfFixedImagePyramids());
for (unsigned int i = 0; i < this->GetNumberOfFixedImagePyramids(); ++i)
{
// Setup the fixed image pyramid
FixedImagePyramidPointer fixpyr = this->GetFixedImagePyramid(i);
if (fixpyr.IsNotNull())
{
fixpyr->SetNumberOfLevels(this->GetNumberOfLevels());
if (this->GetNumberOfFixedImages() > 1)
{
fixpyr->SetInput(this->GetFixedImage(i));
}
else
{
fixpyr->SetInput(this->GetFixedImage());
}
fixpyr->UpdateLargestPossibleRegion();
ScheduleType schedule = fixpyr->GetSchedule();
FixedImageRegionType fixedImageRegion;
if (this->GetNumberOfFixedImageRegions() > 1)
{
fixedImageRegion = this->GetFixedImageRegion(i);
}
else
{
fixedImageRegion = this->GetFixedImageRegion();
}
SizeType inputSize = fixedImageRegion.GetSize();
IndexType inputStart = fixedImageRegion.GetIndex();
IndexType inputEnd = inputStart;
for (unsigned int dim = 0; dim < TFixedImage::ImageDimension; ++dim)
{
inputEnd[dim] += (inputSize[dim] - 1);
}
this->m_FixedImageRegionPyramids[i].reserve(this->GetNumberOfLevels());
this->m_FixedImageRegionPyramids[i].resize(this->GetNumberOfLevels());
// Compute the FixedImageRegion corresponding to each level of the
// pyramid.
//
// In the ITK implementation this uses the same algorithm of the ShrinkImageFilter
// since the regions should be compatible. However, we inherited another
// Multiresolution pyramid, which does not use the same shrinking pattern.
// Instead of copying the shrinking code, we compute image regions from
// the result of the fixed image pyramid.
using PointType = typename FixedImageType::PointType;
using CoordRepType = typename PointType::CoordRepType;
PointType inputStartPoint;
PointType inputEndPoint;
fixpyr->GetInput()->TransformIndexToPhysicalPoint(inputStart, inputStartPoint);
fixpyr->GetInput()->TransformIndexToPhysicalPoint(inputEnd, inputEndPoint);
for (unsigned int level = 0; level < this->GetNumberOfLevels(); ++level)
{
SizeType size;
IndexType start;
FixedImageType * fixedImageAtLevel = fixpyr->GetOutput(level);
/** map the original fixed image region to the image resulting from the
* FixedImagePyramid at level l.
* To be on the safe side, the start point is ceiled, and the end point
* is floored. To see why, consider an image of 4 by 4, and its
* downsampled version of 2 by 2.
*/
const auto startcindex =
fixedImageAtLevel->template TransformPhysicalPointToContinuousIndex<CoordRepType>(inputStartPoint);
const auto endcindex =
fixedImageAtLevel->template TransformPhysicalPointToContinuousIndex<CoordRepType>(inputEndPoint);
for (unsigned int dim = 0; dim < TFixedImage::ImageDimension; ++dim)
{
start[dim] = static_cast<IndexValueType>(std::ceil(startcindex[dim]));
size[dim] = std::max(
NumericTraits<SizeValueType>::One,
static_cast<SizeValueType>(static_cast<SizeValueType>(std::floor(endcindex[dim])) - start[dim] + 1));
}
this->m_FixedImageRegionPyramids[i][level].SetSize(size);
this->m_FixedImageRegionPyramids[i][level].SetIndex(start);
} // end for loop over res levels
} // end if fixpyr!=0
} // end for loop over fixed pyramids
/** Setup the moving image pyramids. */
for (unsigned int i = 0; i < this->GetNumberOfMovingImagePyramids(); ++i)
{
MovingImagePyramidPointer movpyr = this->GetMovingImagePyramid(i);
if (movpyr.IsNotNull())
{
movpyr->SetNumberOfLevels(this->GetNumberOfLevels());
if (this->GetNumberOfMovingImages() > 1)
{
movpyr->SetInput(this->GetMovingImage(i));
}
else
{
movpyr->SetInput(this->GetMovingImage());
}
movpyr->UpdateLargestPossibleRegion();
}
}
} // end PrepareAllPyramids()
/**
* ****************** PrintSelf ******************
*/
template <typename TFixedImage, typename TMovingImage>
void
MultiMetricMultiResolutionImageRegistrationMethod<TFixedImage, TMovingImage>::PrintSelf(std::ostream & os,
Indent indent) const
{
Superclass::PrintSelf(os, indent);
} // end PrintSelf()
/**
* ********************* GenerateData ***********************
*/
template <typename TFixedImage, typename TMovingImage>
void
MultiMetricMultiResolutionImageRegistrationMethod<TFixedImage, TMovingImage>::GenerateData()
{
this->m_Stop = false;
/** Check the transform and set the initial parameters. */
if (this->GetTransform() == nullptr)
{
itkExceptionMacro("Transform is not present");
}
this->SetInitialTransformParametersOfNextLevel(this->GetInitialTransformParameters());
if (this->GetInitialTransformParametersOfNextLevel().Size() != this->GetTransform()->GetNumberOfParameters())
{
itkExceptionMacro("Size mismatch between initial parameter and transform");
}
/** Prepare the fixed and moving pyramids. */
this->PrepareAllPyramids();
/** Loop over the resolution levels. */
for (unsigned int currentLevel = 0; currentLevel < this->GetNumberOfLevels(); ++currentLevel)
{
this->SetCurrentLevel(currentLevel);
// Invoke an iteration event.
// This allows a UI to reset any of the components between
// resolution level.
this->InvokeEvent(IterationEvent());
// Check if there has been a stop request
if (this->m_Stop)
{
break;
}
try
{
// initialize the interconnects between components
this->Initialize();
}
catch (const ExceptionObject &)
{
this->m_LastTransformParameters = ParametersType(1);
this->m_LastTransformParameters.Fill(0.0f);
// pass exception to caller
throw;
}
try
{
// do the optimization
this->GetModifiableOptimizer()->StartOptimization();
}
catch (const ExceptionObject &)
{
// An error has occurred in the optimization.
// Update the parameters
this->m_LastTransformParameters = this->GetOptimizer()->GetCurrentPosition();
// Pass exception to caller
throw;
}
// get the results
this->m_LastTransformParameters = this->GetOptimizer()->GetCurrentPosition();
this->GetModifiableTransform()->SetParameters(this->m_LastTransformParameters);
// setup the initial parameters for next level
if (this->GetCurrentLevel() < this->GetNumberOfLevels() - 1)
{
this->SetInitialTransformParametersOfNextLevel(this->m_LastTransformParameters);
}
} // end for loop over res levels
} // end GenerateData()
/**
* ***************** GetMTime ******************
*/
template <typename TFixedImage, typename TMovingImage>
ModifiedTimeType
MultiMetricMultiResolutionImageRegistrationMethod<TFixedImage, TMovingImage>::GetMTime() const
{
ModifiedTimeType mtime = Superclass::GetMTime();
ModifiedTimeType m;
// Some of the following should be removed once ivars are put in the
// input and output lists
for (unsigned int i = 0; i < this->GetNumberOfInterpolators(); ++i)
{
InterpolatorPointer interpolator = this->GetInterpolator(i);
if (interpolator)
{
m = interpolator->GetMTime();
mtime = (m > mtime ? m : mtime);
}
}
for (unsigned int i = 0; i < this->GetNumberOfFixedImages(); ++i)
{
FixedImageConstPointer fixedImage = this->GetFixedImage(i);
if (fixedImage)
{
m = fixedImage->GetMTime();
mtime = (m > mtime ? m : mtime);
}
}
for (unsigned int i = 0; i < this->GetNumberOfMovingImages(); ++i)
{
MovingImageConstPointer movingImage = this->GetMovingImage(i);
if (movingImage)
{
m = movingImage->GetMTime();
mtime = (m > mtime ? m : mtime);
}
}
for (unsigned int i = 0; i < this->GetNumberOfFixedImagePyramids(); ++i)
{
FixedImagePyramidPointer fixedImagePyramid = this->GetFixedImagePyramid(i);
if (fixedImagePyramid)
{
m = fixedImagePyramid->GetMTime();
mtime = (m > mtime ? m : mtime);
}
}
for (unsigned int i = 0; i < this->GetNumberOfMovingImagePyramids(); ++i)
{
MovingImagePyramidPointer movingImagePyramid = this->GetMovingImagePyramid(i);
if (movingImagePyramid)
{
m = movingImagePyramid->GetMTime();
mtime = (m > mtime ? m : mtime);
}
}
return mtime;
} // end GetMTime()
/**
* ****************** CheckPyramids ******************
*/
template <typename TFixedImage, typename TMovingImage>
void
MultiMetricMultiResolutionImageRegistrationMethod<TFixedImage, TMovingImage>::CheckPyramids()
{
/** Check if at least one of the following are provided. */
if (this->GetFixedImage() == nullptr)
{
itkExceptionMacro("FixedImage is not present");
}
if (this->GetMovingImage() == nullptr)
{
itkExceptionMacro("MovingImage is not present");
}
if (this->GetFixedImagePyramid() == nullptr)
{
itkExceptionMacro("Fixed image pyramid is not present");
}
if (this->GetMovingImagePyramid() == nullptr)
{
itkExceptionMacro("Moving image pyramid is not present");
}
/** Check if the number if fixed/moving pyramids >= nr of fixed/moving images,
* and whether the number of fixed image regions == the number of fixed images.
*/
if (this->GetNumberOfFixedImagePyramids() < this->GetNumberOfFixedImages())
{
itkExceptionMacro("The number of fixed image pyramids should be >= the number of fixed images");
}
if (this->GetNumberOfMovingImagePyramids() < this->GetNumberOfMovingImages())
{
itkExceptionMacro("The number of moving image pyramids should be >= the number of moving images");
}
if (this->GetNumberOfFixedImageRegions() != this->GetNumberOfFixedImages())
{
itkExceptionMacro("The number of fixed image regions should equal the number of fixed images");
}
} // end CheckPyramids()
/**
* ****************** CheckOnInitialize ******************
*/
template <typename TFixedImage, typename TMovingImage>
void
MultiMetricMultiResolutionImageRegistrationMethod<TFixedImage, TMovingImage>::CheckOnInitialize()
{
/** Check if at least one of the following is present. */
if (this->GetMetric() == nullptr)
{
itkExceptionMacro("Metric is not present");
}
if (this->GetOptimizer() == nullptr)
{
itkExceptionMacro("Optimizer is not present");
}
if (this->GetTransform() == nullptr)
{
itkExceptionMacro("Transform is not present");
}
if (this->GetInterpolator() == nullptr)
{
itkExceptionMacro("Interpolator is not present");
}
/** nrofmetrics >= nrofinterpolators >= nrofpyramids >= nofimages */
unsigned int nrOfMetrics = this->GetCombinationMetric()->GetNumberOfMetrics();
if (this->GetNumberOfInterpolators() > nrOfMetrics)
{
itkExceptionMacro("NumberOfInterpolators can not exceed the NumberOfMetrics in the CombinationMetric!");
}
if (this->GetNumberOfFixedImagePyramids() > nrOfMetrics)
{
itkExceptionMacro("NumberOfFixedImagePyramids can not exceed the NumberOfMetrics in the CombinationMetric!");
}
if (this->GetNumberOfMovingImagePyramids() > nrOfMetrics)
{
itkExceptionMacro("NumberOfMovingImagePyramids can not exceed the NumberOfMetrics in the CombinationMetric!");
}
if (this->GetNumberOfMovingImagePyramids() > this->GetNumberOfInterpolators())
{
itkExceptionMacro("NumberOfMovingImagePyramids can not exceed the NumberOfInterpolators!");
}
/** For all components: ==nrofmetrics of ==1. */
if ((this->GetNumberOfInterpolators() != 1) && (this->GetNumberOfInterpolators() != nrOfMetrics))
{
itkExceptionMacro("The NumberOfInterpolators should equal 1 or equal the NumberOfMetrics");
}
if ((this->GetNumberOfFixedImagePyramids() != 1) && (this->GetNumberOfFixedImagePyramids() != nrOfMetrics))
{
itkExceptionMacro("The NumberOfFixedImagePyramids should equal 1 or equal the NumberOfMetrics");
}
if ((this->GetNumberOfMovingImagePyramids() != 1) && (this->GetNumberOfMovingImagePyramids() != nrOfMetrics))
{
itkExceptionMacro("The NumberOfMovingImagePyramids should equal 1 or equal the NumberOfMetrics");
}
if ((this->GetNumberOfFixedImages() != 1) && (this->GetNumberOfFixedImages() != nrOfMetrics))
{
itkExceptionMacro("The NumberOfFixedImages should equal 1 or equal the NumberOfMetrics");
}
if ((this->GetNumberOfMovingImages() != 1) && (this->GetNumberOfMovingImages() != nrOfMetrics))
{
itkExceptionMacro("The NumberOfMovingImages should equal 1 or equal the NumberOfMetrics");
}
} // end CheckOnInitialize()
} // end namespace itk
#undef itkImplementationSetMacro
#undef itkImplementationGetMacro
#endif
|