Skip to content

Commit

Permalink
STYLE: Use itk::ReadImage/itk::WriteImage in Examples/Registration
Browse files Browse the repository at this point in the history
  • Loading branch information
sadhana-r committed Jan 24, 2025
1 parent 1fa5632 commit 7b17773
Show file tree
Hide file tree
Showing 35 changed files with 266 additions and 384 deletions.
40 changes: 9 additions & 31 deletions Examples/RegistrationITKv4/MultiResImageRegistration1.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -377,17 +377,11 @@ main(int argc, const char * argv[])
registration->SetOptimizer(optimizer);
registration->SetMetric(metric);

using FixedImageReaderType = itk::ImageFileReader<FixedImageType>;
using MovingImageReaderType = itk::ImageFileReader<MovingImageType>;
const auto fixedImage = itk::ReadImage<FixedImageType>(fixedImageFile);
const auto movingImage = itk::ReadImage<MovingImageType>(movingImageFile);

auto fixedImageReader = FixedImageReaderType::New();
auto movingImageReader = MovingImageReaderType::New();

fixedImageReader->SetFileName(fixedImageFile);
movingImageReader->SetFileName(movingImageFile);

registration->SetFixedImage(fixedImageReader->GetOutput());
registration->SetMovingImage(movingImageReader->GetOutput());
registration->SetFixedImage(fixedImage);
registration->SetMovingImage(movingImage);


using ParametersType = OptimizerType::ParametersType;
Expand Down Expand Up @@ -568,10 +562,7 @@ main(int argc, const char * argv[])
auto resample = ResampleFilterType::New();

resample->SetTransform(transform);
resample->SetInput(movingImageReader->GetOutput());

const FixedImageType::Pointer fixedImage = fixedImageReader->GetOutput();

resample->SetInput(movingImage);

resample->SetSize(fixedImage->GetLargestPossibleRegion().GetSize());
resample->SetOutputOrigin(fixedImage->GetOrigin());
Expand All @@ -586,20 +577,10 @@ main(int argc, const char * argv[])

using CastFilterType =
itk::CastImageFilter<FixedImageType, OutputImageType>;

using WriterType = itk::ImageFileWriter<OutputImageType>;


auto writer = WriterType::New();
auto caster = CastFilterType::New();


writer->SetFileName(outImagefile);


caster->SetInput(resample->GetOutput());
writer->SetInput(caster->GetOutput());
writer->Update();

itk::WriteImage(caster->GetOutput(), outImagefile);

//
// Generate checkerboards before and after registration
Expand All @@ -612,7 +593,6 @@ main(int argc, const char * argv[])
checker->SetInput2(resample->GetOutput());

caster->SetInput(checker->GetOutput());
writer->SetInput(caster->GetOutput());

resample->SetDefaultPixelValue(0);

Expand All @@ -627,17 +607,15 @@ main(int argc, const char * argv[])
}
if (checkerBoardBefore != std::string(""))
{
writer->SetFileName(checkerBoardBefore);
writer->Update();
itk::WriteImage(caster->GetOutput(), checkerBoardBefore);
}


// After registration
resample->SetTransform(transform);
if (checkerBoardAfter != std::string(""))
{
writer->SetFileName(checkerBoardAfter);
writer->Update();
itk::WriteImage(caster->GetOutput(), checkerBoardAfter);
}

// Software Guide : BeginLatex
Expand Down
36 changes: 10 additions & 26 deletions Examples/RegistrationITKv4/MultiResImageRegistration2.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -244,14 +244,8 @@ main(int argc, char * argv[])
registration->SetTransform(transform);
// Software Guide : EndCodeSnippet

using FixedImageReaderType = itk::ImageFileReader<FixedImageType>;
using MovingImageReaderType = itk::ImageFileReader<MovingImageType>;

auto fixedImageReader = FixedImageReaderType::New();
auto movingImageReader = MovingImageReaderType::New();

fixedImageReader->SetFileName(argv[1]);
movingImageReader->SetFileName(argv[2]);
const auto fixedImage = itk::ReadImage<FixedImageType>(argv[1]);
const auto movingImage = itk::ReadImage<MovingImageType>(argv[2]);

using FixedCastFilterType =
itk::CastImageFilter<FixedImageType, InternalImageType>;
Expand All @@ -260,8 +254,8 @@ main(int argc, char * argv[])
auto fixedCaster = FixedCastFilterType::New();
auto movingCaster = MovingCastFilterType::New();

fixedCaster->SetInput(fixedImageReader->GetOutput());
movingCaster->SetInput(movingImageReader->GetOutput());
fixedCaster->SetInput(fixedImage);
movingCaster->SetInput(movingImage);

registration->SetFixedImage(fixedCaster->GetOutput());
registration->SetMovingImage(movingCaster->GetOutput());
Expand Down Expand Up @@ -289,8 +283,8 @@ main(int argc, char * argv[])
MovingImageType>;
auto initializer = TransformInitializerType::New();
initializer->SetTransform(transform);
initializer->SetFixedImage(fixedImageReader->GetOutput());
initializer->SetMovingImage(movingImageReader->GetOutput());
initializer->SetFixedImage(fixedImage);
initializer->SetMovingImage(movingImage);
initializer->MomentsOn();
initializer->InitializeTransform();
registration->SetInitialTransformParameters(transform->GetParameters());
Expand Down Expand Up @@ -533,9 +527,8 @@ main(int argc, char * argv[])
auto resample = ResampleFilterType::New();

resample->SetTransform(finalTransform);
resample->SetInput(movingImageReader->GetOutput());
resample->SetInput(movingImage);

const FixedImageType::Pointer fixedImage = fixedImageReader->GetOutput();

PixelType backgroundGrayLevel = 100;
if (argc > 4)
Expand All @@ -553,16 +546,10 @@ main(int argc, char * argv[])
using OutputImageType = itk::Image<OutputPixelType, Dimension>;
using CastFilterType =
itk::CastImageFilter<FixedImageType, OutputImageType>;
using WriterType = itk::ImageFileWriter<OutputImageType>;

auto writer = WriterType::New();
auto caster = CastFilterType::New();

writer->SetFileName(argv[3]);

caster->SetInput(resample->GetOutput());
writer->SetInput(caster->GetOutput());
writer->Update();
itk::WriteImage(caster->GetOutput(), argv[3]);

// Software Guide : BeginLatex
//
Expand Down Expand Up @@ -613,7 +600,6 @@ main(int argc, char * argv[])
checker->SetInput2(resample->GetOutput());

caster->SetInput(checker->GetOutput());
writer->SetInput(caster->GetOutput());

resample->SetDefaultPixelValue(0);

Expand All @@ -625,16 +611,14 @@ main(int argc, char * argv[])

if (argc > 5)
{
writer->SetFileName(argv[5]);
writer->Update();
itk::WriteImage(caster->GetOutput(), argv[5]);
}

// After registration
resample->SetTransform(finalTransform);
if (argc > 6)
{
writer->SetFileName(argv[6]);
writer->Update();
itk::WriteImage(caster->GetOutput(), argv[6]);
}

return EXIT_SUCCESS;
Expand Down
42 changes: 10 additions & 32 deletions Examples/RegistrationITKv4/MultiResImageRegistration3.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -197,16 +197,10 @@ main(int argc, char * argv[])
registration->SetFixedImagePyramid(fixedImagePyramid);
registration->SetMovingImagePyramid(movingImagePyramid);


using FixedImageReaderType = itk::ImageFileReader<FixedImageType>;
using MovingImageReaderType = itk::ImageFileReader<MovingImageType>;

auto fixedImageReader = FixedImageReaderType::New();
auto movingImageReader = MovingImageReaderType::New();

fixedImageReader->SetFileName(argv[1]);
movingImageReader->SetFileName(argv[2]);

const FixedImageType::Pointer fixedImage =
itk::ReadImage<FixedImageType>(argv[1]);
const MovingImageType::Pointer movingImage =
itk::ReadImage<MovingImageType>(argv[2]);

using FixedCastFilterType =
itk::CastImageFilter<FixedImageType, InternalImageType>;
Expand All @@ -216,8 +210,8 @@ main(int argc, char * argv[])
auto fixedCaster = FixedCastFilterType::New();
auto movingCaster = MovingCastFilterType::New();

fixedCaster->SetInput(fixedImageReader->GetOutput());
movingCaster->SetInput(movingImageReader->GetOutput());
fixedCaster->SetInput(fixedImage);
movingCaster->SetInput(movingImage);

registration->SetFixedImage(fixedCaster->GetOutput());
registration->SetMovingImage(movingCaster->GetOutput());
Expand Down Expand Up @@ -329,9 +323,7 @@ main(int argc, char * argv[])
auto resample = ResampleFilterType::New();

resample->SetTransform(finalTransform);
resample->SetInput(movingImageReader->GetOutput());

const FixedImageType::Pointer fixedImage = fixedImageReader->GetOutput();
resample->SetInput(movingImage);

PixelType backgroundGrayLevel = 100;
if (argc > 4)
Expand All @@ -352,20 +344,9 @@ main(int argc, char * argv[])

using CastFilterType =
itk::CastImageFilter<FixedImageType, OutputImageType>;

using WriterType = itk::ImageFileWriter<OutputImageType>;


auto writer = WriterType::New();
auto caster = CastFilterType::New();


writer->SetFileName(argv[3]);


caster->SetInput(resample->GetOutput());
writer->SetInput(caster->GetOutput());
writer->Update();
itk::WriteImage(caster->GetOutput(), argv[3]);

//
// Generate checkerboards before and after registration
Expand All @@ -378,7 +359,6 @@ main(int argc, char * argv[])
checker->SetInput2(resample->GetOutput());

caster->SetInput(checker->GetOutput());
writer->SetInput(caster->GetOutput());

resample->SetDefaultPixelValue(0);

Expand All @@ -389,17 +369,15 @@ main(int argc, char * argv[])

if (argc > 5)
{
writer->SetFileName(argv[5]);
writer->Update();
itk::WriteImage(caster->GetOutput(), argv[5]);
}


// After registration
resample->SetTransform(finalTransform);
if (argc > 6)
{
writer->SetFileName(argv[6]);
writer->Update();
itk::WriteImage(caster->GetOutput(), argv[6]);
}

return EXIT_SUCCESS;
Expand Down
38 changes: 10 additions & 28 deletions Examples/RegistrationITKv4/MultiStageImageRegistration1.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -314,17 +314,11 @@ main(int argc, char * argv[])
compositeTransform->AddTransform(movingInitTx);
// Software Guide : EndCodeSnippet

using FixedImageReaderType = itk::ImageFileReader<FixedImageType>;
using MovingImageReaderType = itk::ImageFileReader<MovingImageType>;
const auto fixedImage = itk::ReadImage<FixedImageType>(argv[1]);
const auto movingImage = itk::ReadImage<MovingImageType>(argv[2]);

auto fixedImageReader = FixedImageReaderType::New();
auto movingImageReader = MovingImageReaderType::New();

fixedImageReader->SetFileName(argv[1]);
movingImageReader->SetFileName(argv[2]);

transRegistration->SetFixedImage(fixedImageReader->GetOutput());
transRegistration->SetMovingImage(movingImageReader->GetOutput());
transRegistration->SetFixedImage(fixedImage);
transRegistration->SetMovingImage(movingImage);
transRegistration->SetObjectName("TranslationRegistration");

// Software Guide : BeginLatex
Expand Down Expand Up @@ -478,8 +472,8 @@ main(int argc, char * argv[])
affineRegistration->SetMovingInitialTransform(compositeTransform);
// Software Guide : EndCodeSnippet

affineRegistration->SetFixedImage(fixedImageReader->GetOutput());
affineRegistration->SetMovingImage(movingImageReader->GetOutput());
affineRegistration->SetFixedImage(fixedImage);
affineRegistration->SetMovingImage(movingImage);
affineRegistration->SetObjectName("AffineRegistration");

affineMetric->SetNumberOfHistogramBins(24);
Expand Down Expand Up @@ -533,8 +527,6 @@ main(int argc, char * argv[])
using RegionType = FixedImageType::RegionType;
using SizeType = FixedImageType::SizeType;

const FixedImageType::Pointer fixedImage = fixedImageReader->GetOutput();

const SpacingType fixedSpacing = fixedImage->GetSpacing();
const OriginType fixedOrigin = fixedImage->GetOrigin();
const RegionType fixedRegion = fixedImage->GetLargestPossibleRegion();
Expand Down Expand Up @@ -881,7 +873,7 @@ main(int argc, char * argv[])
auto resample = ResampleFilterType::New();

resample->SetTransform(compositeTransform);
resample->SetInput(movingImageReader->GetOutput());
resample->SetInput(movingImage);

PixelType backgroundGrayLevel = 100;
if (argc > 4)
Expand All @@ -899,16 +891,10 @@ main(int argc, char * argv[])
using OutputImageType = itk::Image<OutputPixelType, Dimension>;
using CastFilterType =
itk::CastImageFilter<FixedImageType, OutputImageType>;
using WriterType = itk::ImageFileWriter<OutputImageType>;

auto writer = WriterType::New();
auto caster = CastFilterType::New();

writer->SetFileName(argv[3]);

caster->SetInput(resample->GetOutput());
writer->SetInput(caster->GetOutput());
writer->Update();
itk::WriteImage(caster->GetOutput(), argv[3]);

// Software Guide : BeginLatex
//
Expand Down Expand Up @@ -937,9 +923,7 @@ main(int argc, char * argv[])

checker->SetInput1(fixedImage);
checker->SetInput2(resample->GetOutput());

caster->SetInput(checker->GetOutput());
writer->SetInput(caster->GetOutput());

resample->SetDefaultPixelValue(0);

Expand All @@ -961,16 +945,14 @@ main(int argc, char * argv[])

if (argc > 5)
{
writer->SetFileName(argv[5]);
writer->Update();
itk::WriteImage(caster->GetOutput(), argv[5]);
}

// After registration
resample->SetTransform(compositeTransform);
if (argc > 6)
{
writer->SetFileName(argv[6]);
writer->Update();
itk::WriteImage(caster->GetOutput(), argv[6]);
}

return EXIT_SUCCESS;
Expand Down
Loading

0 comments on commit 7b17773

Please sign in to comment.