#include "itkRecursiveGaussianImageFilter.h"
#include "itkWarpImageFilter.h"
#include "itkUnaryFunctorImageFilter.h"
#include "itkRescaleIntensityImageFilter.h"
#include "itkCastImageFilter.h"
#include <iostream>
int main(
int argc,
char** argv)
{
if (argc != 9)
{
std::cerr << "Usage: " << argv[0];
std::cerr << " fixedFileName movingFileName fieldOutNameHorizontal fieldOutNameVertical imageOutName ";
std::cerr << "explorationSize bluringSigma nbIterations ";
return EXIT_FAILURE;
}
const unsigned int ImageDimension = 2;
using PixelType = double;
using DisplacementPixelType = itk::Vector<double, ImageDimension>;
using OutputPixelType = unsigned char;
FixedReaderType::Pointer fReader = FixedReaderType::New();
fReader->SetFileName(argv[1]);
MovingReaderType::Pointer mReader = MovingReaderType::New();
mReader->SetFileName(argv[2]);
using FixedBlurType = itk::RecursiveGaussianImageFilter<FixedImageType, FixedImageType>;
FixedBlurType::Pointer fBlur = FixedBlurType::New();
fBlur->SetInput(fReader->GetOutput());
fBlur->SetSigma(std::stof(argv[7]));
using MovingBlurType = itk::RecursiveGaussianImageFilter<MovingImageType, MovingImageType>;
MovingBlurType::Pointer mBlur = MovingBlurType::New();
mBlur->SetInput(mReader->GetOutput());
mBlur->SetSigma(std::stof(argv[7]));
RegistrationFilterType::Pointer registrator = RegistrationFilterType::New();
registrator->SetMovingImage(mBlur->GetOutput());
registrator->SetFixedImage(fBlur->GetOutput());
using RadiusType = RegistrationFilterType::RadiusType;
RadiusType radius;
radius[0] = std::stoi(argv[6]);
radius[1] = std::stoi(argv[6]);
registrator->SetNCCRadius(radius);
std::cout << "NCC radius " << registrator->GetNCCRadius() << std::endl;
registrator->SetNumberOfIterations(std::stoi(argv[8]));
ChannelExtractionFilterType::Pointer channelExtractor = ChannelExtractionFilterType::New();
channelExtractor->SetInput(registrator->GetOutput());
channelExtractor->SetChannel(1);
using RescalerType = itk::RescaleIntensityImageFilter<MovingImageType, OutputImageType>;
RescalerType::Pointer fieldRescaler = RescalerType::New();
fieldRescaler->SetInput(channelExtractor->GetOutput());
fieldRescaler->SetOutputMaximum(255);
fieldRescaler->SetOutputMinimum(0);
DFWriterType::Pointer dfWriter = DFWriterType::New();
dfWriter->SetFileName(argv[3]);
dfWriter->SetInput(fieldRescaler->GetOutput());
dfWriter->Update();
channelExtractor->SetChannel(2);
dfWriter->SetFileName(argv[4]);
dfWriter->Update();
using WarperType = itk::WarpImageFilter<MovingImageType, MovingImageType, DisplacementFieldType>;
WarperType::Pointer warper = WarperType::New();
MovingImageType::PixelType padValue = 4.0;
warper->SetInput(mReader->GetOutput());
warper->SetDisplacementField(registrator->GetOutput());
warper->SetEdgePaddingValue(padValue);
using CastFilterType = itk::CastImageFilter<MovingImageType, OutputImageType>;
CastFilterType::Pointer caster = CastFilterType::New();
caster->SetInput(warper->GetOutput());
WriterType::Pointer writer = WriterType::New();
writer->SetFileName(argv[5]);
writer->SetInput(caster->GetOutput());
writer->Update();
return EXIT_SUCCESS;
}