[ITK] problem with calculating Normalized Mutual Information

Constantinus Spanakis c.spanakis83 at gmail.com
Tue Nov 22 02:52:10 EST 2016


Hello. I want to caculate mutual information between 2 images. The problem
is that the outcome changes only when the angle of rotation changes. What
could be the problem?

#include "itkMutualInformationImageToImageMetric.h"
#include "itkRandomImageSource.h"
#include "itkTranslationTransform.h"
#include"itkCenteredRigid2DTransform.h"
#include "itkCenteredTransformInitializer.h"
#include "itkLinearInterpolateImageFunction.h"
#include"itkImageFileReader.h"
#include"itkNormalizedMutualInformationHistogramImageToImageMetric.h"


typedef itk::Image< unsigned char, 2>  ImageType;
//typedef itk::Image< float, 2> ImageType;

int main( int argc, char *argv[] )
{

const    unsigned int    ImageDimension = 2;
typedef  unsigned char    PixelType;
typedef itk::Image<PixelType, ImageDimension> FixedImage;
typedef itk::Image<PixelType, ImageDimension> MovingImage;

typedef itk::ImageFileReader<FixedImage> FixedImageTypeReader;
typedef itk::ImageFileReader<MovingImage> MovingImageTypeReader;

FixedImageTypeReader::Pointer fixedreader = FixedImageTypeReader::New();
MovingImageTypeReader::Pointer movingreader = MovingImageTypeReader::New();

fixedreader->SetFileName(argv[1]);
movingreader->SetFileName(argv[2]);

fixedreader->Update();
movingreader->Update();
FixedImage::Pointer fixedImage = fixedreader->GetOutput();
MovingImage::Pointer movingImage = movingreader->GetOutput();


  typedef itk::TranslationTransform<double, 2> TranslationTransformType; //
This cannot be float for some reason?
  TranslationTransformType::Pointer transform =
TranslationTransformType::New();

  typedef itk::CenteredRigid2DTransform<double> RigidTransformType;
  typedef itk::CenteredTransformInitializer < RigidTransformType,
FixedImage, MovingImage> RigidTransformInitializerType;


  RigidTransformType::Pointer rigidtransform = RigidTransformType::New();
  RigidTransformInitializerType::Pointer initializer =
RigidTransformInitializerType::New();

  initializer->SetTransform(rigidtransform);
  initializer->SetFixedImage(fixedreader->GetOutput());
  initializer->SetMovingImage(movingreader->GetOutput());
  initializer->GeometryOn();
  initializer->InitializeTransform();

  rigidtransform->SetAngleInDegrees(atof(argv[3]));

  RigidTransformType::OutputVectorType translation =
rigidtransform->GetTranslation();
  translation[0] = atof(argv[4]);
  translation[1] = atof(argv[5]);



  /*typedef itk::MutualInformationImageToImageMetric<FixedImage,
MovingImage >    MetricType;*/

  typedef
itk::NormalizedMutualInformationHistogramImageToImageMetric<FixedImage,
MovingImage> MetricType;


  MetricType::Pointer metric = MetricType::New();

  metric->SetTransform(rigidtransform);


  metric->SetFixedImageRegion(fixedImage->GetLargestPossibleRegion());
  MetricType::MeasurementVectorType lower, upper;
  lower.SetSize(2);
  lower.Fill(0);

  upper.SetSize(2);
  upper.Fill(255);

  metric->SetLowerBound(lower);
  metric->SetUpperBound(upper);

  MetricType::HistogramType::SizeType histogramSize;
  histogramSize.SetSize(2);
  histogramSize[0] = 128;
  histogramSize[1] = 128;

  metric->SetHistogramSize(histogramSize);

  const unsigned int numberOfParameters =
rigidtransform->GetNumberOfParameters();
  typedef MetricType::ScalesType ScalesType;
  ScalesType scales(numberOfParameters);

  scales.Fill(1.0);

  metric->SetDerivativeStepLengthScales(scales);

  metric->SetFixedImage(fixedImage);
  metric->SetMovingImage(movingImage);




  metric->SetFixedImageRegion(fixedImage->GetLargestPossibleRegion());


  itk::LinearInterpolateImageFunction<MovingImage, double>::Pointer
interpolator = itk::LinearInterpolateImageFunction<MovingImage,
double>::New();
  interpolator->SetInputImage(fixedImage);
  metric->SetInterpolator(interpolator);



  std::cout <<
fixedreader->GetOutput()->GetLargestPossibleRegion().GetSize();
  RigidTransformType::InputPointType m = rigidtransform->GetCenter();
  m[0] = 300;
  m[1] = 300;
  rigidtransform->SetCenter(m);
  std::cout << "parameters: " << rigidtransform->GetParameters() <<
std::endl;
  std::cout << "pcenter: " << m << std::endl;

  metric->SetTransformParameters(rigidtransform->GetParameters());
  MetricType::MeasureType value =
metric->GetValue(rigidtransform->GetParameters());

  std::cout << "Value: " << value << std::endl;

  return EXIT_SUCCESS;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/community/attachments/20161122/224ad52d/attachment.html>


More information about the Community mailing list