#include <iomanip>
#include <stdio.h>
template< class TInput >
class RescaleDynamicRangeFunctor
{
public:
typedef unsigned char OutputPixelType;
RescaleDynamicRangeFunctor() {};
~RescaleDynamicRangeFunctor() {};
inline OutputPixelType operator()( const TInput &A )
{
if( (A > 0.0) )
{
if( -(30.0 * std::log(A)) > 255 )
{
return static_cast<OutputPixelType>( 255 );
}
else
{
return itk::Math::Round<OutputPixelType>( -(30.0 * std::log(A)) );
}
}
else
{
return static_cast<OutputPixelType>(255);
}
}
};
namespace
{
class HistogramWriter
{
public:
typedef float InternalPixelType;
itkStaticConstMacro(
Dimension,
unsigned int, 2);
InternalImageType,
InternalImageType > MetricType;
typedef MetricType::Pointer MetricPointer;
typedef MetricType::HistogramType HistogramType;
HistogramToEntropyImageFilterType;
typedef HistogramToEntropyImageFilterType::Pointer
HistogramToImageFilterPointer;
typedef HistogramToEntropyImageFilterType::OutputImageType OutputImageType;
typedef HistogramFileWriterType::Pointer HistogramFileWriterPointer;
typedef HistogramToEntropyImageFilterType::OutputPixelType OutputPixelType;
HistogramWriter():
m_Metric(0)
{
this->m_Filter = HistogramToEntropyImageFilterType::New();
this->m_HistogramFileWriter = HistogramFileWriterType::New();
this->m_HistogramFileWriter->SetInput( this->m_Filter->GetOutput() );
}
~HistogramWriter() { };
void SetMetric( MetricPointer metric )
{
this->m_Metric = metric;
}
MetricPointer GetMetric() const
{
return this->m_Metric;
}
void WriteHistogramFile( unsigned int iterationNumber )
{
std::string outputFileBase = "JointHistogram";
std::ostringstream outputFilename;
outputFilename << outputFileBase
<< "."
<< std::setfill('0') << std::setw(3) << iterationNumber
<< "."
<< "mhd";
m_HistogramFileWriter->SetFileName( outputFilename.str() );
this->m_Filter->SetInput( this->GetMetric()->GetHistogram() );
this->m_Filter->Modified();
try
{
m_Filter->Update();
}
{
std::cerr << "ERROR: ExceptionObject caught !" << std::endl;
std::cerr << err << std::endl;
}
try
{
m_HistogramFileWriter->Update();
}
{
std::cerr << "Exception thrown " << excp << std::endl;
}
std::cout << "Joint Histogram file: ";
std::cout << outputFilename.str() << " written" << std::endl;
}
void WriteHistogramFile( std::string &outputFilename )
{
this->m_Filter->SetInput( this->GetMetric()->GetHistogram() );
this->m_Filter->Modified();
typedef RescaleDynamicRangeFunctor<
OutputPixelType
> RescaleDynamicRangeFunctorType;
OutputImageType,
RescaledOutputImageType,
RescaleDynamicRangeFunctorType
> RescaleDynamicRangeFilterType;
RescaleDynamicRangeFilterType::Pointer rescaler =
RescaleDynamicRangeFilterType::New();
rescaler->SetInput( m_Filter->GetOutput() );
RescaledWriterType::Pointer rescaledWriter =
RescaledWriterType::New();
rescaledWriter->SetInput( rescaler->GetOutput() );
rescaledWriter->SetFileName( outputFilename );
try
{
m_Filter->Update();
}
{
std::cerr << "ERROR: ExceptionObject caught !" << std::endl;
std::cerr << err << std::endl;
}
try
{
rescaledWriter->Update();
}
{
std::cerr << "Exception thrown " << excp << std::endl;
}
std::cout << "Joint Histogram file: " << outputFilename <<
" written" << std::endl;
}
private:
MetricPointer m_Metric;
HistogramToImageFilterPointer m_Filter;
HistogramFileWriterPointer m_HistogramFileWriter;
std::string m_OutputFile;
};
{
public:
typedef CommandIterationUpdate
Self;
itkSimpleNewMacro( Self );
protected:
CommandIterationUpdate()
{
m_WriteHistogramsAfterEveryIteration = false;
}
public:
typedef const OptimizerType * OptimizerPointer;
{
}
{
OptimizerPointer optimizer = static_cast< OptimizerPointer >( object );
if( ! itk::IterationEvent().CheckEvent( &event ) || optimizer == ITK_NULLPTR )
{
return;
}
std::cout << optimizer->GetCurrentIteration() << " ";
std::cout << optimizer->GetValue() << " ";
std::cout << optimizer->GetCurrentPosition() << std::endl;
if( optimizer->GetCurrentIteration() == 0 )
{
m_JointHistogramWriter.WriteHistogramFile( m_InitialHistogramFile );
}
if( m_WriteHistogramsAfterEveryIteration )
{
m_JointHistogramWriter.WriteHistogramFile(
optimizer->GetCurrentIteration() );
}
}
void SetWriteHistogramsAfterEveryIteration( bool value )
{
m_WriteHistogramsAfterEveryIteration = value;
}
void SetInitialHistogramFile( const char * filename )
{
m_InitialHistogramFile = filename;
}
HistogramWriter m_JointHistogramWriter;
private:
bool m_WriteHistogramsAfterEveryIteration;
std::string m_InitialHistogramFile;
};
}
int main( int argc, char *argv[] )
{
if( argc < 8 )
{
std::cerr << "Missing Parameters " << std::endl;
std::cerr << "Usage: " << argv[0];
std::cerr << " fixedImageFile movingImageFile ";
std::cerr << "outputImagefile WriteJointHistogramsAfterEveryIteration ";
std::cerr << "JointHistogramPriorToRegistrationFile ";
std::cerr << "JointHistogramAfterRegistrationFile ";
std::cerr << "NumberOfHistogramBinsForWritingTheMutualInformationHistogramMetric";
std::cerr << std::endl;
return EXIT_FAILURE;
}
typedef unsigned char PixelType;
typedef float InternalPixelType;
InternalImageType,
double > InterpolatorType;
InternalImageType,
InternalImageType > RegistrationType;
InternalImageType,
InternalImageType > MetricType;
TransformType::Pointer transform = TransformType::New();
OptimizerType::Pointer optimizer = OptimizerType::New();
InterpolatorType::Pointer interpolator = InterpolatorType::New();
RegistrationType::Pointer registration = RegistrationType::New();
MetricType::Pointer metric = MetricType::New();
registration->SetOptimizer( optimizer );
registration->SetTransform( transform );
registration->SetInterpolator( interpolator );
unsigned int numberOfHistogramBins = atoi( argv[7] );
histogramSize[0] = numberOfHistogramBins;
histogramSize[1] = numberOfHistogramBins;
metric->SetHistogramSize( histogramSize );
const unsigned int numberOfParameters = transform->GetNumberOfParameters();
typedef MetricType::ScalesType ScalesType;
ScalesType scales( numberOfParameters );
scales.Fill( 1.0 );
metric->SetDerivativeStepLengthScales(scales);
CommandIterationUpdate::Pointer observer = CommandIterationUpdate::New();
observer->m_JointHistogramWriter.SetMetric( metric );
registration->SetMetric( metric );
FixedImageReaderType::Pointer fixedImageReader = FixedImageReaderType::New();
MovingImageReaderType::Pointer movingImageReader = MovingImageReaderType::New();
fixedImageReader->SetFileName( argv[1] );
movingImageReader->SetFileName( argv[2] );
FixedImageType,
InternalImageType
> FixedNormalizeFilterType;
MovingImageType,
InternalImageType
> MovingNormalizeFilterType;
FixedNormalizeFilterType::Pointer fixedNormalizer =
FixedNormalizeFilterType::New();
MovingNormalizeFilterType::Pointer movingNormalizer =
MovingNormalizeFilterType::New();
InternalImageType,
InternalImageType
> GaussianFilterType;
GaussianFilterType::Pointer fixedSmoother = GaussianFilterType::New();
GaussianFilterType::Pointer movingSmoother = GaussianFilterType::New();
fixedSmoother->SetVariance( 2.0 );
movingSmoother->SetVariance( 2.0 );
fixedNormalizer->SetInput( fixedImageReader->GetOutput() );
movingNormalizer->SetInput( movingImageReader->GetOutput() );
fixedSmoother->SetInput( fixedNormalizer->GetOutput() );
movingSmoother->SetInput( movingNormalizer->GetOutput() );
registration->SetFixedImage( fixedSmoother->GetOutput() );
registration->SetMovingImage( movingSmoother->GetOutput() );
try
{
fixedNormalizer->Update();
}
{
std::cout << "ExceptionObject caught !" << std::endl;
std::cout << err << std::endl;
return EXIT_FAILURE;
}
registration->SetFixedImageRegion(
fixedNormalizer->GetOutput()->GetBufferedRegion() );
typedef RegistrationType::ParametersType ParametersType;
ParametersType initialParameters( transform->GetNumberOfParameters() );
initialParameters[0] = 0.0;
initialParameters[1] = 0.0;
registration->SetInitialTransformParameters( initialParameters );
optimizer->SetMaximumStepLength( 4.00 );
optimizer->SetMinimumStepLength( 0.01 );
optimizer->SetRelaxationFactor( 0.90 );
optimizer->SetNumberOfIterations( 200 );
optimizer->MaximizeOn();
optimizer->AddObserver( itk::IterationEvent(), observer );
observer->SetInitialHistogramFile( argv[5] );
if( atoi(argv[4]) )
{
observer->SetWriteHistogramsAfterEveryIteration( true );
}
try
{
registration->Update();
std::cout << "Optimizer stop condition: "
<< registration->GetOptimizer()->GetStopConditionDescription()
<< std::endl;
}
{
std::cout << "ExceptionObject caught !" << std::endl;
std::cout << err << std::endl;
return EXIT_FAILURE;
}
ParametersType finalParameters = registration->GetLastTransformParameters();
double TranslationAlongX = finalParameters[0];
double TranslationAlongY = finalParameters[1];
unsigned int numberOfIterations = optimizer->GetCurrentIteration();
double bestValue = optimizer->GetValue();
std::cout << "Result = " << std::endl;
std::cout << " Translation X = " << TranslationAlongX << std::endl;
std::cout << " Translation Y = " << TranslationAlongY << std::endl;
std::cout << " Iterations = " << numberOfIterations << std::endl;
std::cout << " Metric value = " << bestValue << std::endl;
std::string histogramAfter(argv[6]);
try
{
observer->m_JointHistogramWriter.WriteHistogramFile( histogramAfter );
}
{
std::cerr << "ERROR: ExceptionObject caught !" << std::endl;
std::cerr << err << std::endl;
return EXIT_FAILURE;
}
MovingImageType,
FixedImageType > ResampleFilterType;
TransformType::Pointer finalTransform = TransformType::New();
finalTransform->SetParameters( finalParameters );
finalTransform->SetFixedParameters( transform->GetFixedParameters() );
ResampleFilterType::Pointer resample = ResampleFilterType::New();
resample->SetTransform( finalTransform );
resample->SetInput( movingImageReader->GetOutput() );
FixedImageType::Pointer fixedImage = fixedImageReader->GetOutput();
resample->SetSize( fixedImage->GetLargestPossibleRegion().GetSize() );
resample->SetOutputOrigin( fixedImage->GetOrigin() );
resample->SetOutputSpacing( fixedImage->GetSpacing() );
resample->SetOutputDirection( fixedImage->GetDirection() );
resample->SetDefaultPixelValue( 100 );
typedef unsigned char OutputPixelType;
FixedImageType,
OutputImageType > CastFilterType;
WriterType::Pointer writer = WriterType::New();
CastFilterType::Pointer caster = CastFilterType::New();
writer->SetFileName( argv[3] );
caster->SetInput( resample->GetOutput() );
writer->SetInput( caster->GetOutput() );
try
{
writer->Update();
}
{
std::cerr << "ERROR: ExceptionObject caught !" << std::endl;
std::cerr << err << std::endl;
}
return EXIT_SUCCESS;
}