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
|
/*=========================================================================
*
* 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.
*
*=========================================================================*/
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile: itkCenteredTransformInitializer2.txx,v $
Date: $Date: 2009-08-15 23:42:49 $
Version: $Revision: 1.22 $
Copyright (c) Insight Software Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#ifndef itkCenteredTransformInitializer2_hxx
#define itkCenteredTransformInitializer2_hxx
#include "itkCenteredTransformInitializer2.h"
#include "itkImageMaskSpatialObject.h"
namespace itk
{
template <class TTransform, class TFixedImage, class TMovingImage>
CenteredTransformInitializer2<TTransform, TFixedImage, TMovingImage>::CenteredTransformInitializer2()
{
m_FixedCalculator = FixedImageCalculatorType::New();
m_MovingCalculator = MovingImageCalculatorType::New();
m_UseMoments = false;
m_UseOrigins = false;
m_UseTop = false;
this->m_CenterOfGravityUsesLowerThreshold = false;
this->m_NumberOfSamplesForCenteredTransformInitialization = 10000;
this->m_LowerThresholdForCenterGravity = 500;
}
/** Initialize the transform using data from the images */
template <class TTransform, class TFixedImage, class TMovingImage>
void
CenteredTransformInitializer2<TTransform, TFixedImage, TMovingImage>::InitializeTransform()
{
// Sanity check
if (!m_FixedImage)
{
itkExceptionMacro("Fixed Image has not been set");
}
if (!m_MovingImage)
{
itkExceptionMacro("Moving Image has not been set");
}
if (!m_Transform)
{
itkExceptionMacro("Transform has not been set");
}
// If images come from filters, then update those filters.
m_FixedImage->UpdateSource();
m_MovingImage->UpdateSource();
InputPointType rotationCenter;
OutputVectorType translationVector;
using FixedMaskSpatialObjectType = ImageMaskSpatialObject<InputSpaceDimension>;
using MovingMaskSpatialObjectType = ImageMaskSpatialObject<OutputSpaceDimension>;
if (m_UseMoments)
{
// Convert the masks to spatial objects
typename FixedMaskSpatialObjectType::Pointer fixedMaskAsSpatialObject; // default-constructed (null)
if (this->m_FixedImageMask)
{
fixedMaskAsSpatialObject = FixedMaskSpatialObjectType::New();
fixedMaskAsSpatialObject->SetImage(this->m_FixedImageMask);
fixedMaskAsSpatialObject->Update();
}
typename MovingMaskSpatialObjectType::Pointer movingMaskAsSpatialObject; // default-constructed (null)
if (this->m_MovingImageMask)
{
movingMaskAsSpatialObject = MovingMaskSpatialObjectType::New();
movingMaskAsSpatialObject->SetImage(this->m_MovingImageMask);
movingMaskAsSpatialObject->Update();
}
// Moments
m_FixedCalculator->SetImage(m_FixedImage);
m_FixedCalculator->SetSpatialObjectMask(fixedMaskAsSpatialObject);
if (this->m_CenterOfGravityUsesLowerThreshold)
{
/** Set the lower threshold for center gravity calculation. */
m_FixedCalculator->SetCenterOfGravityUsesLowerThreshold(this->m_CenterOfGravityUsesLowerThreshold);
m_FixedCalculator->SetLowerThresholdForCenterGravity(this->m_LowerThresholdForCenterGravity);
}
m_FixedCalculator->SetNumberOfSamplesForCenteredTransformInitialization(
this->m_NumberOfSamplesForCenteredTransformInitialization);
m_FixedCalculator->Compute();
m_MovingCalculator->SetImage(m_MovingImage);
m_MovingCalculator->SetSpatialObjectMask(movingMaskAsSpatialObject);
if (this->m_CenterOfGravityUsesLowerThreshold)
{
/** Set the lower threshold for center gravity calculation. */
m_MovingCalculator->SetCenterOfGravityUsesLowerThreshold(this->m_CenterOfGravityUsesLowerThreshold);
m_MovingCalculator->SetLowerThresholdForCenterGravity(this->m_LowerThresholdForCenterGravity);
}
m_MovingCalculator->SetNumberOfSamplesForCenteredTransformInitialization(
this->m_NumberOfSamplesForCenteredTransformInitialization);
m_MovingCalculator->Compute();
typename FixedImageCalculatorType::VectorType fixedCenter = m_FixedCalculator->GetCenterOfGravity();
typename MovingImageCalculatorType::VectorType movingCenter = m_MovingCalculator->GetCenterOfGravity();
for (unsigned int i = 0; i < InputSpaceDimension; ++i)
{
rotationCenter[i] = fixedCenter[i];
translationVector[i] = movingCenter[i] - fixedCenter[i];
}
}
else if (m_UseOrigins)
{
// Origins
const typename MovingImageType::RegionType & movingRegion = m_MovingImage->GetLargestPossibleRegion();
const typename MovingImageType::IndexType & movingIndex = movingRegion.GetIndex();
const typename MovingImageType::SizeType & movingSize = movingRegion.GetSize();
using CoordRepType = typename InputPointType::ValueType;
using ContinuousIndexType = ContinuousIndex<CoordRepType, InputSpaceDimension>;
using ContinuousIndexValueType = typename ContinuousIndexType::ValueType;
InputPointType centerMovingPoint;
ContinuousIndexType centerMovingIndex;
for (unsigned int m = 0; m < InputSpaceDimension; ++m)
{
centerMovingIndex[m] = static_cast<ContinuousIndexValueType>(movingIndex[m]) +
static_cast<ContinuousIndexValueType>(movingSize[m] - 1) / 2.0;
}
m_MovingImage->TransformContinuousIndexToPhysicalPoint(centerMovingIndex, centerMovingPoint);
// Origins points
InputPointType originMovingPoint;
m_MovingImage->TransformIndexToPhysicalPoint(movingIndex, originMovingPoint);
const typename FixedImageType::RegionType & fixedRegion = m_FixedImage->GetLargestPossibleRegion();
const typename FixedImageType::IndexType & fixedIndex = fixedRegion.GetIndex();
InputPointType originFixedPoint;
m_FixedImage->TransformIndexToPhysicalPoint(fixedIndex, originFixedPoint);
for (unsigned int i = 0; i < InputSpaceDimension; ++i)
{
translationVector[i] = originMovingPoint[i] - originFixedPoint[i];
rotationCenter[i] = centerMovingPoint[i] - translationVector[i];
}
}
else if (m_UseTop)
{
// Align the geometrical tops (z-direction) of the fixed and moving image.
// When masks are used the geometrical tops (z-direction) of the bounding box
// of the masks are used. Center of rotation point is set to the center of the fixed image.
// Get fixed image (mask) information
using FixedRegionType = typename FixedImageType::RegionType;
FixedRegionType fixedRegion = this->m_FixedImage->GetLargestPossibleRegion();
if (this->m_FixedImageMask)
{
auto fixedMaskAsSpatialObject = FixedMaskSpatialObjectType::New();
fixedMaskAsSpatialObject->SetImage(this->m_FixedImageMask);
fixedRegion = fixedMaskAsSpatialObject->ComputeMyBoundingBoxInIndexSpace();
}
// Get moving image (mask) information
using MovingRegionType = typename MovingImageType::RegionType;
MovingRegionType movingRegion = this->m_MovingImage->GetLargestPossibleRegion();
if (this->m_MovingImageMask)
{
auto movingMaskAsSpatialObject = MovingMaskSpatialObjectType::New();
movingMaskAsSpatialObject->SetImage(this->m_MovingImageMask);
movingRegion = movingMaskAsSpatialObject->ComputeMyBoundingBoxInIndexSpace();
}
// Create eight corner points in voxel coordinates for both fixed and moving image.
std::vector<ContinuousIndex<double, InputSpaceDimension>> fixedCorners(8);
std::vector<ContinuousIndex<double, InputSpaceDimension>> movingCorners(8);
for (unsigned int z = 0, index = 0; z < 2; ++z)
{
for (unsigned int y = 0; y < 2; ++y)
{
for (unsigned int x = 0; x < 2; ++x, ++index)
{
fixedCorners[index][0] = fixedRegion.GetIndex()[0] + x * fixedRegion.GetSize()[0];
fixedCorners[index][1] = fixedRegion.GetIndex()[1] + y * fixedRegion.GetSize()[1];
if constexpr (InputSpaceDimension > 2)
{
fixedCorners[index][2] = fixedRegion.GetIndex()[2] + z * fixedRegion.GetSize()[2];
}
movingCorners[index][0] = movingRegion.GetIndex()[0] + x * movingRegion.GetSize()[0];
movingCorners[index][1] = movingRegion.GetIndex()[1] + y * movingRegion.GetSize()[1];
if constexpr (InputSpaceDimension > 2)
{
movingCorners[index][2] = movingRegion.GetIndex()[2] + z * movingRegion.GetSize()[2];
}
}
}
}
// Transform eight corner points to world coordinates and determine min and max coordinate values.
typename TransformType::InputPointType minWorldFixed, maxWorldFixed, minWorldMoving, maxWorldMoving;
for (std::size_t i = 0; i < fixedCorners.size(); ++i)
{
typename TransformType::InputPointType worldFixed, worldMoving;
this->m_FixedImage->TransformContinuousIndexToPhysicalPoint(fixedCorners[i], worldFixed);
this->m_MovingImage->TransformContinuousIndexToPhysicalPoint(movingCorners[i], worldMoving);
if (i == 0)
{
minWorldFixed = worldFixed;
maxWorldFixed = worldFixed;
minWorldMoving = worldMoving;
maxWorldMoving = worldMoving;
}
else
{
for (std::size_t k = 0; k < InputSpaceDimension; ++k)
{
if (worldFixed[k] < minWorldFixed[k])
{
minWorldFixed[k] = worldFixed[k];
}
if (worldFixed[k] > maxWorldFixed[k])
{
maxWorldFixed[k] = worldFixed[k];
}
if (worldMoving[k] < minWorldMoving[k])
{
minWorldMoving[k] = worldMoving[k];
}
if (worldMoving[k] > maxWorldMoving[k])
{
maxWorldMoving[k] = worldMoving[k];
}
}
}
}
// Calculate topCenterFixed coordinate.
typename TransformType::InputPointType topCenterFixed;
topCenterFixed[0] = (maxWorldFixed[0] + minWorldFixed[0]) / 2.0;
topCenterFixed[1] = (maxWorldFixed[1] + minWorldFixed[1]) / 2.0;
topCenterFixed[2] = maxWorldFixed[2];
// Calculate topCentermoving coordinate.
typename TransformType::InputPointType topCenterMoving;
topCenterMoving[0] = (maxWorldMoving[0] + minWorldMoving[0]) / 2.0;
topCenterMoving[1] = (maxWorldMoving[1] + minWorldMoving[1]) / 2.0;
topCenterMoving[2] = maxWorldMoving[2];
// Compute the difference between the centers
for (unsigned int i = 0; i < InputSpaceDimension; ++i)
{
rotationCenter[i] = (maxWorldFixed[i] + minWorldFixed[i]) / 2.0;
translationVector[i] = topCenterMoving[i] - topCenterFixed[i];
}
}
else
{
// Align the geometrical centers of the fixed and moving image.
// When masks are used the geometrical centers of the bounding box
// of the masks are used.
// Get fixed image (mask) information
using FixedRegionType = typename FixedImageType::RegionType;
FixedRegionType fixedRegion = this->m_FixedImage->GetLargestPossibleRegion();
if (this->m_FixedImageMask)
{
auto fixedMaskAsSpatialObject = FixedMaskSpatialObjectType::New();
fixedMaskAsSpatialObject->SetImage(this->m_FixedImageMask);
fixedRegion = fixedMaskAsSpatialObject->ComputeMyBoundingBoxInIndexSpace();
}
// Compute center of the fixed image (mask bounding box) in physical units
ContinuousIndex<double, InputSpaceDimension> fixedCenterCI;
for (unsigned int k = 0; k < InputSpaceDimension; ++k)
{
fixedCenterCI[k] = fixedRegion.GetIndex()[k] + (fixedRegion.GetSize()[k] - 1.0) / 2.0;
}
typename TransformType::InputPointType centerFixed;
this->m_FixedImage->TransformContinuousIndexToPhysicalPoint(fixedCenterCI, centerFixed);
// Get moving image (mask) information
using MovingRegionType = typename MovingImageType::RegionType;
MovingRegionType movingRegion = this->m_MovingImage->GetLargestPossibleRegion();
if (this->m_MovingImageMask)
{
auto movingMaskAsSpatialObject = MovingMaskSpatialObjectType::New();
movingMaskAsSpatialObject->SetImage(this->m_MovingImageMask);
movingRegion = movingMaskAsSpatialObject->ComputeMyBoundingBoxInIndexSpace();
}
// Compute center of the moving image (mask bounding box) in physical units
ContinuousIndex<double, InputSpaceDimension> movingCenterCI;
for (unsigned int k = 0; k < InputSpaceDimension; ++k)
{
movingCenterCI[k] = movingRegion.GetIndex()[k] + (movingRegion.GetSize()[k] - 1.0) / 2.0;
}
typename TransformType::InputPointType centerMoving;
this->m_MovingImage->TransformContinuousIndexToPhysicalPoint(movingCenterCI, centerMoving);
// Compute the difference between the centers
for (unsigned int i = 0; i < InputSpaceDimension; ++i)
{
rotationCenter[i] = centerFixed[i];
translationVector[i] = centerMoving[i] - centerFixed[i];
}
}
// Set the initialization
m_Transform->SetCenter(rotationCenter);
m_Transform->SetTranslation(translationVector);
}
template <class TTransform, class TFixedImage, class TMovingImage>
void
CenteredTransformInitializer2<TTransform, TFixedImage, TMovingImage>::PrintSelf(std::ostream & os, Indent indent) const
{
Superclass::PrintSelf(os, indent);
os << indent << "Transform = " << std::endl;
if (m_Transform)
{
os << indent << m_Transform << std::endl;
}
else
{
os << indent << "None" << std::endl;
}
os << indent << "FixedImage = " << std::endl;
if (m_FixedImage)
{
os << indent << m_FixedImage << std::endl;
}
else
{
os << indent << "None" << std::endl;
}
os << indent << "MovingImage = " << std::endl;
if (m_MovingImage)
{
os << indent << m_MovingImage << std::endl;
}
else
{
os << indent << "None" << std::endl;
}
os << indent << "MovingMomentCalculator = " << std::endl;
if (m_UseMoments && m_MovingCalculator)
{
os << indent << m_MovingCalculator << std::endl;
}
else if (m_UseOrigins && m_MovingCalculator)
{
os << indent << m_MovingCalculator << std::endl;
}
else
{
os << indent << "None" << std::endl;
}
os << indent << "FixedMomentCalculator = " << std::endl;
if (m_UseMoments && m_FixedCalculator)
{
os << indent << m_FixedCalculator << std::endl;
}
else if (m_UseOrigins && m_FixedCalculator)
{
os << indent << m_FixedCalculator << std::endl;
}
else
{
os << indent << "None" << std::endl;
}
}
} // namespace itk
#endif /* itkCenteredTransformInitializer2_hxx */
|