Skip to content

Commit

Permalink
STYLE: Use itk::ReadImage and itk::WriteImage in src/ Reg., Seg., Num.
Browse files Browse the repository at this point in the history
  • Loading branch information
josetascon authored and dzenanz committed Jul 22, 2021
1 parent 6fa7e3c commit 7ab88a6
Show file tree
Hide file tree
Showing 29 changed files with 79 additions and 330 deletions.
13 changes: 2 additions & 11 deletions src/Numerics/Optimizers/ExhaustiveOptimizer/Code.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,12 @@ main(int argc, char * argv[])
}
using FixedImageType = itk::Image<double, 2>;
using MovingImageType = itk::Image<double, 2>;
using FixedImageReaderType = itk::ImageFileReader<FixedImageType>;
using MovingImageReaderType = itk::ImageFileReader<MovingImageType>;
using TransformType = itk::Euler2DTransform<double>;
using OptimizerType = itk::ExhaustiveOptimizerv4<double>;
using MetricType = itk::MeanSquaresImageToImageMetricv4<FixedImageType, MovingImageType>;
using TransformInitializerType = itk::CenteredTransformInitializer<TransformType, FixedImageType, MovingImageType>;
using RegistrationType = itk::ImageRegistrationMethodv4<FixedImageType, MovingImageType, TransformType>;

FixedImageReaderType::Pointer fixedImageReader = FixedImageReaderType::New();
MovingImageReaderType::Pointer movingImageReader = MovingImageReaderType::New();
FixedImageType::Pointer fixedImage = FixedImageType::New();
MovingImageType::Pointer movingImage = MovingImageType::New();
TransformType::Pointer transform = TransformType::New();
Expand All @@ -92,13 +88,8 @@ main(int argc, char * argv[])
RegistrationType::Pointer registration = RegistrationType::New();
TransformInitializerType::Pointer initializer = TransformInitializerType::New();

fixedImageReader->SetFileName(argv[1]);
fixedImageReader->Update();
fixedImage = fixedImageReader->GetOutput();

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

// Create the Command observer and register it with the optimizer.
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,7 @@ main(int argc, char * argv[])
constexpr unsigned int MeasurementVectorSize = 1; // Grayscale
const auto binsPerDimension = static_cast<unsigned int>(std::stoi(argv[2]));

using ReaderType = itk::ImageFileReader<ImageType>;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(argv[1]);

ImageType::Pointer image = reader->GetOutput();
ImageType::Pointer image = itk::ReadImage<ImageType>(argv[1]);

using ImageToHistogramFilterType = itk::Statistics::ImageToHistogramFilter<ImageType>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,5 @@ void CreateHalfMask(itk::ImageRegion<2> region, UnsignedCharImageType::Pointer m
rescaleFilter->SetInput(mask);
rescaleFilter->Update();

using WriterType = itk::ImageFileWriter<UnsignedCharImageType>;
WriterType::Pointer writer = WriterType::New();
writer->SetFileName("mask.png");
writer->SetInput(rescaleFilter->GetOutput());
writer->Update();
itk::WriteImage(rescaleFilter->GetOutput(), "mask.png");
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include "itkImageToListSampleFilter.h"
#include "itkCovariantVector.h"
#include "itkImageRegionIterator.h"
#include "itkImageFileReader.h"
#include "itkSimpleFilterWatcher.h"

using PixelType = itk::CovariantVector<unsigned char, 3>;
Expand Down
12 changes: 2 additions & 10 deletions src/Registration/Common/ComputeMeanSquareBetweenTwoImages/Code.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,15 @@ int
main(int argc, char * argv[])
{
using ImageType = itk::Image<double, 2>;
using ReaderType = itk::ImageFileReader<ImageType>;

if (argc < 3)
{
std::cout << "Usage: " << argv[0] << " imageFile1 imageFile2" << std::endl;
return EXIT_FAILURE;
}
ReaderType::Pointer fixedReader = ReaderType::New();
fixedReader->SetFileName(argv[1]);
fixedReader->Update();

ReaderType::Pointer movingReader = ReaderType::New();
movingReader->SetFileName(argv[2]);
movingReader->Update();

ImageType::Pointer fixedImage = fixedReader->GetOutput();
ImageType::Pointer movingImage = movingReader->GetOutput();
ImageType::Pointer fixedImage = itk::ReadImage<ImageType>(argv[1]);
ImageType::Pointer movingImage = itk::ReadImage<ImageType>(argv[2]);

using MetricType = itk::MeanSquaresImageToImageMetric<ImageType, ImageType>;
using InterpolatorType = itk::LinearInterpolateImageFunction<ImageType, double>;
Expand Down
21 changes: 4 additions & 17 deletions src/Registration/Common/GlobalRegistrationOfTwoImages/Code.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include "itkImage.h"
#include "itkImageRegistrationMethod.h"
#include "itkLinearInterpolateImageFunction.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkMeanSquaresImageToImageMetric.h"
#include "itkRegularStepGradientDescentOptimizer.h"
Expand Down Expand Up @@ -84,17 +83,8 @@ main(int, char *[])
CreateEllipseImage(movingImage);

// Write the two synthetic inputs
using WriterType = itk::ImageFileWriter<ImageType>;

WriterType::Pointer fixedWriter = WriterType::New();
fixedWriter->SetFileName("fixed.png");
fixedWriter->SetInput(fixedImage);
fixedWriter->Update();

WriterType::Pointer movingWriter = WriterType::New();
movingWriter->SetFileName("moving.png");
movingWriter->SetInput(movingImage);
movingWriter->Update();
itk::WriteImage(fixedImage, "fixed.png");
itk::WriteImage(movingImage, "moving.png");

// Set the registration inputs
registration->SetFixedImage(fixedImage);
Expand Down Expand Up @@ -211,13 +201,10 @@ main(int, char *[])

using CastFilterType = itk::CastImageFilter<ImageType, ImageType>;

WriterType::Pointer writer = WriterType::New();
CastFilterType::Pointer caster = CastFilterType::New();
writer->SetFileName("output.png");

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

itk::WriteImage(caster->GetOutput(), "output.png");

/*
// The fixed image and the transformed moving image can easily be compared
Expand Down
7 changes: 0 additions & 7 deletions src/Registration/Common/MatchFeaturePoints/Code.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*=========================================================================*/
#include "itkBlockMatchingImageFilter.h"
#include "itkImage.h"
#include "itkImageFileWriter.h"
#include "itkPoint.h"
#include "itkPointSet.h"

Expand All @@ -36,12 +35,6 @@ main(int /*argc*/, char * /*argv*/[])
ImageType::Pointer movingImage = ImageType::New();
CreateImage(movingImage, 50);

// using WriterType = itk::ImageFileWriter<ImageType>;
// WriterType::Pointer writer = WriterType::New();
// writer->SetFileName("input.png");
// writer->SetInput(input);
// writer->Update();

// using BlockMatchingImageFilterType = itk::BlockMatchingImageFilter<ImageType, ImageType, PointSetType>;
using BlockMatchingImageFilterType = itk::BlockMatchingImageFilter<ImageType>;
BlockMatchingImageFilterType::Pointer blockMatchingImageFilter = BlockMatchingImageFilterType::New();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,10 @@ main(int, char *[])
rescaleFilter->SetOutputMaximum(255);
rescaleFilter->Update();

using FileWriterType = itk::ImageFileWriter<UnsignedCharImageType>;
FileWriterType::Pointer writer = FileWriterType::New();
std::stringstream ss;
std::stringstream ss;
ss << "output_" << i << ".png";
std::cout << "Writing " << ss.str() << std::endl;
writer->SetFileName(ss.str());
writer->SetInput(rescaleFilter->GetOutput());
writer->Update();
itk::WriteImage(rescaleFilter->GetOutput(), ss.str());
}

return EXIT_SUCCESS;
Expand Down
20 changes: 3 additions & 17 deletions src/Registration/Common/MutualInformation/Code.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,14 @@ using ImageType = itk::Image<PixelType, Dimension>;
int
main(int argc, char * argv[])
{
using ReaderType = itk::ImageFileReader<ImageType>;

if (argc < 4)
{
std::cout << "Usage: " << argv[0] << " imageFile1 imageFile2 outputFile" << std::endl;
return EXIT_FAILURE;
}
ReaderType::Pointer fixedReader = ReaderType::New();
fixedReader->SetFileName(argv[1]);
fixedReader->Update();
ImageType::Pointer fixedImage = fixedReader->GetOutput();

ReaderType::Pointer movingReader = ReaderType::New();
movingReader->SetFileName(argv[2]);
movingReader->Update();
ImageType::Pointer movingImage = movingReader->GetOutput();
ImageType::Pointer fixedImage = itk::ReadImage<ImageType>(argv[1]);
ImageType::Pointer movingImage = itk::ReadImage<ImageType>(argv[2]);

// We use floats internally
using InternalImageType = itk::Image<float, 2>;
Expand Down Expand Up @@ -240,13 +232,7 @@ main(int argc, char * argv[])
resample->SetOutputDirection(fixedImage->GetDirection());
resample->SetDefaultPixelValue(100);

// Write transformed moving image
using WriterType = itk::ImageFileWriter<ImageType>;

WriterType::Pointer writer = WriterType::New();
writer->SetFileName(argv[3]);
writer->SetInput(resample->GetOutput());
writer->Update();
itk::WriteImage(resample->GetOutput(), argv[3]);

return EXIT_SUCCESS;
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,8 @@ main(int argc, char * argv[])
using FixedImageType = itk::Image<PixelType, Dimension>;
using MovingImageType = itk::Image<PixelType, Dimension>;


using FixedImageReaderType = itk::ImageFileReader<FixedImageType>;
auto fixedImageReader = FixedImageReaderType::New();
fixedImageReader->SetFileName(fixedImageFile);

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


using TransformType = itk::TranslationTransform<double, Dimension>;
Expand All @@ -83,8 +77,8 @@ main(int argc, char * argv[])
registration->SetInitialTransform(initialTransform);
registration->SetMetric(metric);
registration->SetOptimizer(optimizer);
registration->SetFixedImage(fixedImageReader->GetOutput());
registration->SetMovingImage(movingImageReader->GetOutput());
registration->SetFixedImage(fixedImage);
registration->SetMovingImage(movingImage);

auto movingInitialTransform = TransformType::New();
TransformType::ParametersType initialParameters(movingInitialTransform->GetNumberOfParameters());
Expand Down Expand Up @@ -145,9 +139,8 @@ main(int argc, char * argv[])

using ResampleFilterType = itk::ResampleImageFilter<MovingImageType, FixedImageType>;
auto resampler = ResampleFilterType::New();
resampler->SetInput(movingImageReader->GetOutput());
resampler->SetInput(movingImage);
resampler->SetTransform(outputCompositeTransform);
auto fixedImage = fixedImageReader->GetOutput();
resampler->SetUseReferenceImage(true);
resampler->SetReferenceImage(fixedImage);
resampler->SetDefaultPixelValue(100);
Expand All @@ -159,15 +152,11 @@ main(int argc, char * argv[])
auto caster = CastFilterType::New();
caster->SetInput(resampler->GetOutput());

using WriterType = itk::ImageFileWriter<OutputImageType>;
auto writer = WriterType::New();
writer->SetFileName(outputImageFile);
writer->SetInput(caster->GetOutput());
writer->Update();
itk::WriteImage(caster->GetOutput(), outputImageFile);

using DifferenceFilterType = itk::SubtractImageFilter<FixedImageType, FixedImageType, FixedImageType>;
auto difference = DifferenceFilterType::New();
difference->SetInput1(fixedImageReader->GetOutput());
difference->SetInput1(fixedImage);
difference->SetInput2(resampler->GetOutput());

using RescalerType = itk::RescaleIntensityImageFilter<FixedImageType, OutputImageType>;
Expand All @@ -177,14 +166,10 @@ main(int argc, char * argv[])
intensityRescaler->SetOutputMaximum(itk::NumericTraits<OutputPixelType>::max());

resampler->SetDefaultPixelValue(1);

writer->SetInput(intensityRescaler->GetOutput());
writer->SetFileName(differenceImageAfterFile);
writer->Update();
itk::WriteImage(intensityRescaler->GetOutput(), differenceImageAfterFile);

resampler->SetTransform(identityTransform);
writer->SetFileName(differenceImageBeforeFile);
writer->Update();
itk::WriteImage(intensityRescaler->GetOutput(), differenceImageBeforeFile);

return EXIT_SUCCESS;
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,8 @@ main(int argc, char * argv[])
metric->SetFixedImageStandardDeviation(0.4);
metric->SetMovingImageStandardDeviation(0.4);

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

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

fixedImageReader->SetFileName(fixedImageFile);
movingImageReader->SetFileName(movingImageFile);
const auto fixedImage = itk::ReadImage<FixedImageType>(fixedImageFile);
const auto movingImage = itk::ReadImage<MovingImageType>(movingImageFile);

using FixedNormalizeFilterType = itk::NormalizeImageFilter<FixedImageType, InternalImageType>;

Expand All @@ -181,8 +175,8 @@ main(int argc, char * argv[])
fixedSmoother->SetVariance(2.0);
movingSmoother->SetVariance(2.0);

fixedNormalizer->SetInput(fixedImageReader->GetOutput());
movingNormalizer->SetInput(movingImageReader->GetOutput());
fixedNormalizer->SetInput(fixedImage);
movingNormalizer->SetInput(movingImage);

fixedSmoother->SetInput(fixedNormalizer->GetOutput());
movingSmoother->SetInput(movingNormalizer->GetOutput());
Expand Down Expand Up @@ -310,10 +304,7 @@ main(int argc, char * argv[])
ResampleFilterType::Pointer resample = ResampleFilterType::New();

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

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

resample->SetInput(movingImage);
resample->SetSize(fixedImage->GetLargestPossibleRegion().GetSize());
resample->SetOutputOrigin(fixedImage->GetOrigin());
resample->SetOutputSpacing(fixedImage->GetSpacing());
Expand All @@ -327,27 +318,19 @@ main(int argc, char * argv[])

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

using WriterType = itk::ImageFileWriter<OutputImageType>;

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

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

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

// Generate checkerboards before and after registration
using CheckerBoardFilterType = itk::CheckerBoardImageFilter<FixedImageType>;

CheckerBoardFilterType::Pointer checker = CheckerBoardFilterType::New();

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

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

// Before registration
TransformType::Pointer identityTransform = TransformType::New();
Expand All @@ -356,24 +339,14 @@ main(int argc, char * argv[])

if (argc > 4)
{
writer->SetFileName(checkerBoardBefore);
itk::WriteImage(caster->GetOutput(), checkerBoardBefore);
}

// After registration
resample->SetTransform(finalTransform);
if (argc > 5)
{
writer->SetFileName(checkerBoardAfter);
}

try
{
writer->Update();
}
catch (itk::ExceptionObject & error)
{
std::cerr << "Error: " << error << std::endl;
return EXIT_FAILURE;
itk::WriteImage(caster->GetOutput(), checkerBoardAfter);
}

return EXIT_SUCCESS;
Expand Down
Loading

1 comment on commit 7ab88a6

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.