#include "itkUnaryFunctorImageFilter.h"
#include "itkRecursiveGaussianImageFilter.h"
#include "itkWarpImageFilter.h"
#include "itkMeanReciprocalSquareDifferenceImageToImageMetric.h"
#include "itkRescaleIntensityImageFilter.h"
#include "itkCastImageFilter.h"
#include <iostream>
int main(
int argc,
char** argv)
{
if (argc < 11)
{
std::cerr << "Usage: " << argv[0];
std::cerr << " fixedFileName movingFileName fieldOutNameHorizontal fieldOutNameVertical fieldMetric warped ";
std::cerr << "smoothingSigma metricRadius explorationRadius subpixelPrecision";
return EXIT_FAILURE;
}
const unsigned int ImageDimension = 2;
using PixelType = double;
using DisplacementPixelType = itk::Vector<double, ImageDimension>;
using OutputPixelType = unsigned char;
InputReaderType::Pointer fReader = InputReaderType::New();
fReader->SetFileName(argv[1]);
InputReaderType::Pointer mReader = InputReaderType::New();
mReader->SetFileName(argv[2]);
using InputBlurType = itk::RecursiveGaussianImageFilter<InputImageType, InputImageType>;
InputBlurType::Pointer fBlur = InputBlurType::New();
fBlur->SetInput(fReader->GetOutput());
fBlur->SetSigma(atof(argv[7]));
InputBlurType::Pointer mBlur = InputBlurType::New();
mBlur->SetInput(mReader->GetOutput());
mBlur->SetSigma(atof(argv[7]));
RegistrationFilterType::Pointer registrator = RegistrationFilterType::New();
registrator->SetMovingInput(mBlur->GetOutput());
registrator->SetFixedInput(fBlur->GetOutput());
using RadiusType = RegistrationFilterType::SizeType;
RadiusType searchRadius;
searchRadius[0] = atoi(argv[8]);
searchRadius[1] = atoi(argv[8]);
registrator->SetSearchRadius(searchRadius);
std::cout << "Exploration radius " << registrator->GetSearchRadius() << std::endl;
RadiusType metricRadius;
metricRadius[0] = atoi(argv[9]);
metricRadius[1] = atoi(argv[9]);
registrator->SetRadius(metricRadius);
std::cout << "Metric radius " << registrator->GetRadius() << std::endl;
registrator->SetConvergenceAccuracy(atof(argv[10]));
if (argc > 11)
{
using MRSDMetricType = itk::MeanReciprocalSquareDifferenceImageToImageMetric<InputImageType, InputImageType>;
MRSDMetricType::Pointer mrsdMetric = MRSDMetricType::New();
registrator->SetMetric(mrsdMetric);
registrator->MinimizeOff();
}
ChannelExtractionFilterType::Pointer channelExtractor = ChannelExtractionFilterType::New();
channelExtractor->SetInput(registrator->GetOutputDisplacementField());
channelExtractor->SetChannel(1);
using RescalerType = itk::RescaleIntensityImageFilter<InputImageType, 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<InputImageType, InputImageType, DisplacementFieldType>;
WarperType::Pointer warper = WarperType::New();
InputImageType::PixelType padValue = 4.0;
warper->SetInput(mReader->GetOutput());
warper->SetDisplacementField(registrator->GetOutputDisplacementField());
warper->SetEdgePaddingValue(padValue);
using MetricRescalerType = itk::RescaleIntensityImageFilter<MetricImageType, OutputImageType>;
MetricRescalerType::Pointer metricRescaler = MetricRescalerType::New();
metricRescaler->SetInput(registrator->GetOutput());
metricRescaler->SetOutputMinimum(0);
metricRescaler->SetOutputMaximum(255);
WriterType::Pointer writer1 = WriterType::New();
writer1->SetInput(metricRescaler->GetOutput());
writer1->SetFileName(argv[5]);
writer1->Update();
using CastFilterType = itk::CastImageFilter<InputImageType, OutputImageType>;
CastFilterType::Pointer caster = CastFilterType::New();
caster->SetInput(warper->GetOutput());
WriterType::Pointer writer2 = WriterType::New();
writer2->SetFileName(argv[6]);
writer2->SetInput(caster->GetOutput());
writer2->Update();
return EXIT_SUCCESS;
}