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
|
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile: BSplineWarping2.cxx,v $
Language: C++
Date: $Date: 2010-03-16 22:50:12 $
Version: $Revision: 1.7 $
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.
=========================================================================*/
#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif
// Software Guide : BeginLatex
//
// This example illustrates how to deform a 3D image using a BSplineTransform.
//
// \index{BSplineDeformableTransform}
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkImage.h"
#include "itkResampleImageFilter.h"
#include "itkLinearInterpolateImageFunction.h"
#include "itkBSplineDeformableTransform.h"
#include "itkTransformFileWriter.h"
// Software Guide : EndCodeSnippet
#include <fstream>
// The following section of code implements a Command observer
// used to monitor the evolution of the registration process.
//
#include "itkCommand.h"
class CommandProgressUpdate : public itk::Command
{
public:
typedef CommandProgressUpdate Self;
typedef itk::Command Superclass;
typedef itk::SmartPointer<Self> Pointer;
itkNewMacro( Self );
protected:
CommandProgressUpdate() {};
public:
void Execute(itk::Object *caller, const itk::EventObject & event)
{
Execute( (const itk::Object *)caller, event);
}
void Execute(const itk::Object * object, const itk::EventObject & event)
{
const itk::ProcessObject * filter =
dynamic_cast< const itk::ProcessObject * >( object );
if( ! itk::ProgressEvent().CheckEvent( &event ) )
{
return;
}
std::cout << filter->GetProgress() << std::endl;
}
};
int main( int argc, char * argv[] )
{
if( argc < 5 )
{
std::cerr << "Missing Parameters " << std::endl;
std::cerr << "Usage: " << argv[0];
std::cerr << " coefficientsFile fixedImage ";
std::cerr << "movingImage deformedMovingImage" << std::endl;
std::cerr << "[deformationField]" << std::endl;
return EXIT_FAILURE;
}
// Software Guide : BeginCodeSnippet
const unsigned int ImageDimension = 3;
typedef unsigned char PixelType;
typedef itk::Image< PixelType, ImageDimension > FixedImageType;
typedef itk::Image< PixelType, ImageDimension > MovingImageType;
typedef itk::ImageFileReader< FixedImageType > FixedReaderType;
typedef itk::ImageFileReader< MovingImageType > MovingReaderType;
typedef itk::ImageFileWriter< MovingImageType > MovingWriterType;
FixedReaderType::Pointer fixedReader = FixedReaderType::New();
fixedReader->SetFileName( argv[2] );
try
{
fixedReader->Update();
}
catch( itk::ExceptionObject & excp )
{
std::cerr << "Exception thrown " << std::endl;
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}
MovingReaderType::Pointer movingReader = MovingReaderType::New();
MovingWriterType::Pointer movingWriter = MovingWriterType::New();
movingReader->SetFileName( argv[3] );
movingWriter->SetFileName( argv[4] );
FixedImageType::ConstPointer fixedImage = fixedReader->GetOutput();
typedef itk::ResampleImageFilter< MovingImageType,
FixedImageType > FilterType;
FilterType::Pointer resampler = FilterType::New();
typedef itk::LinearInterpolateImageFunction<
MovingImageType, double > InterpolatorType;
InterpolatorType::Pointer interpolator = InterpolatorType::New();
resampler->SetInterpolator( interpolator );
FixedImageType::SpacingType fixedSpacing = fixedImage->GetSpacing();
FixedImageType::PointType fixedOrigin = fixedImage->GetOrigin();
FixedImageType::DirectionType fixedDirection = fixedImage->GetDirection();
resampler->SetOutputSpacing( fixedSpacing );
resampler->SetOutputOrigin( fixedOrigin );
resampler->SetOutputDirection( fixedDirection );
FixedImageType::RegionType fixedRegion = fixedImage->GetBufferedRegion();
FixedImageType::SizeType fixedSize = fixedRegion.GetSize();
resampler->SetSize( fixedSize );
resampler->SetOutputStartIndex( fixedRegion.GetIndex() );
resampler->SetInput( movingReader->GetOutput() );
movingWriter->SetInput( resampler->GetOutput() );
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// We instantiate now the type of the \code{BSplineDeformableTransform} using
// as template parameters the type for coordinates representation, the
// dimension of the space, and the order of the B-spline.
//
// \index{BSplineDeformableTransform!New}
// \index{BSplineDeformableTransform!Instantiation}
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
const unsigned int SpaceDimension = ImageDimension;
const unsigned int SplineOrder = 3;
typedef double CoordinateRepType;
typedef itk::BSplineDeformableTransform<
CoordinateRepType,
SpaceDimension,
SplineOrder > TransformType;
TransformType::Pointer bsplineTransform = TransformType::New();
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// Since we are using a B-spline of order 3, the coverage of the BSpling grid
// should exceed by one the spatial extent of the image on the lower region of
// image indices, and by two grid points on the upper region of image indices.
// We choose here to use a $8 \times 8$ B-spline grid, from which only a $5
// \times 5$ sub-grid will be covering the input image. If we use an input
// image of size $500 \times 500$ pixels, and pixel spacing $2.0 \times 2.0$
// then we need the $5 \times 5$ B-spline grid to cover a physical extent of $1000
// \times 1000$ mm. This can be achieved by setting the pixel spacing of the
// B-spline grid to $250.0 \times 250.0$ mm. The origin of the B-spline grid
// must be set at one grid position away from the origin of the desired output
// image. All this is done with the following lines of code.
//
// \index{BSplineDeformableTransform}
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef TransformType::RegionType RegionType;
RegionType bsplineRegion;
RegionType::SizeType size;
const unsigned int numberOfGridNodesOutsideTheImageSupport = 3;
const unsigned int numberOfGridNodesInsideTheImageSupport = 5;
const unsigned int numberOfGridNodes =
numberOfGridNodesInsideTheImageSupport +
numberOfGridNodesOutsideTheImageSupport;
const unsigned int numberOfGridCells =
numberOfGridNodesInsideTheImageSupport - 1;
size.Fill( numberOfGridNodes );
bsplineRegion.SetSize( size );
typedef TransformType::SpacingType SpacingType;
SpacingType spacing;
typedef TransformType::OriginType OriginType;
OriginType origin;
for( unsigned int i=0; i< SpaceDimension; i++ )
{
spacing[i] = fixedSpacing[i] * (fixedSize[i] - 1) / numberOfGridCells;
}
origin = fixedOrigin - fixedDirection * spacing;
bsplineTransform->SetGridSpacing( spacing );
bsplineTransform->SetGridOrigin( origin );
bsplineTransform->SetGridRegion( bsplineRegion );
bsplineTransform->SetGridDirection( fixedDirection );
typedef TransformType::ParametersType ParametersType;
const unsigned int numberOfParameters =
bsplineTransform->GetNumberOfParameters();
const unsigned int numberOfNodes = numberOfParameters / SpaceDimension;
ParametersType parameters( numberOfParameters );
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The B-spline grid should now be fed with coeficients at each node. Since
// this is a two dimensional grid, each node should receive two coefficients.
// Each coefficient pair is representing a displacement vector at this node.
// The coefficients can be passed to the B-spline in the form of an array where
// the first set of elements are the first component of the displacements for
// all the nodes, and the second set of elemets is formed by the second
// component of the displacements for all the nodes.
//
// In this example we read such displacements from a file, but for convinience
// we have written this file using the pairs of $(x,y)$ displacement for every
// node. The elements read from the file should therefore be reorganized when
// assigned to the elements of the array. We do this by storing all the odd
// elements from the file in the first block of the array, and all the even
// elements from the file in the second block of the array. Finally the array
// is passed to the B-spline transform using the \code{SetParameters()}.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
std::ifstream infile;
infile.open( argv[1] );
for( unsigned int n=0; n < numberOfNodes; n++ )
{
infile >> parameters[n]; // X coordinate
infile >> parameters[n+numberOfNodes]; // Y coordinate
infile >> parameters[n+numberOfNodes*2]; // Z coordinate
}
infile.close();
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// Finally the array is passed to the B-spline transform using the
// \code{SetParameters()}.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
bsplineTransform->SetParameters( parameters );
// Software Guide : EndCodeSnippet
CommandProgressUpdate::Pointer observer = CommandProgressUpdate::New();
resampler->AddObserver( itk::ProgressEvent(), observer );
// Software Guide : BeginLatex
//
// At this point we are ready to use the transform as part of the resample
// filter. We trigger the execution of the pipeline by invoking
// \code{Update()} on the last filter of the pipeline, in this case writer.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
resampler->SetTransform( bsplineTransform );
try
{
movingWriter->Update();
}
catch( itk::ExceptionObject & excp )
{
std::cerr << "Exception thrown " << std::endl;
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}
// Software Guide : EndCodeSnippet
typedef itk::Point< float, ImageDimension > PointType;
typedef itk::Vector< float, ImageDimension > VectorType;
typedef itk::Image< VectorType, ImageDimension > DeformationFieldType;
DeformationFieldType::Pointer field = DeformationFieldType::New();
field->SetRegions( fixedRegion );
field->SetOrigin( fixedOrigin );
field->SetSpacing( fixedSpacing );
field->SetDirection( fixedDirection );
field->Allocate();
typedef itk::ImageRegionIterator< DeformationFieldType > FieldIterator;
FieldIterator fi( field, fixedRegion );
fi.GoToBegin();
TransformType::InputPointType fixedPoint;
TransformType::OutputPointType movingPoint;
DeformationFieldType::IndexType index;
VectorType displacement;
while( ! fi.IsAtEnd() )
{
index = fi.GetIndex();
field->TransformIndexToPhysicalPoint( index, fixedPoint );
movingPoint = bsplineTransform->TransformPoint( fixedPoint );
displacement = movingPoint - fixedPoint;
fi.Set( displacement );
++fi;
}
typedef itk::ImageFileWriter< DeformationFieldType > FieldWriterType;
FieldWriterType::Pointer fieldWriter = FieldWriterType::New();
fieldWriter->SetInput( field );
if( argc >= 6 )
{
fieldWriter->SetFileName( argv[5] );
try
{
fieldWriter->Update();
}
catch( itk::ExceptionObject & excp )
{
std::cerr << "Exception thrown " << std::endl;
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}
}
if( argc >= 7 )
{
fieldWriter->SetFileName( argv[6] );
try
{
typedef itk::TransformFileWriter TransformWriterType;
TransformWriterType::Pointer transformWriter = TransformWriterType::New();
transformWriter->AddTransform( bsplineTransform );
transformWriter->SetFileName( argv[6] );
transformWriter->Update();
}
catch( itk::ExceptionObject & excp )
{
std::cerr << "Exception thrown " << std::endl;
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}
|