[Insight-users] MeanSquaresImageToImageMetric problem

indian rock indianrock at gmail.com
Wed Jun 15 14:33:20 EDT 2005


Hello All,

I have two stacks of images. I'm trying to compute the corresponding
closest match between the two stacks without any translation/rotation
performed on any of the images. The code is as attached below,
however, the matching is not perfect. The image metric value produces
an incorrect mapping.

=====================================================

#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkMeanSquaresImageToImageMetric.h"
#include "itkIdentityTransform.h"
#include "itkNearestNeighborInterpolateImageFunction.h"
#include "itkLinearInterpolateImageFunction.h"


int main( int argc, char * argv[] )
{
 if( argc < 3 )
   {
   std::cerr << "Usage: " << std::endl;
   std::cerr << argv[0] << "  fixedImage  movingImage" << std::endl;
   return 1;
   }

 const     unsigned int   Dimension = 2;
 typedef   unsigned char  PixelType;
 FILE *fp;

 typedef itk::Image< PixelType, Dimension >   ImageType;
 typedef itk::Image< PixelType, Dimension >   ImageType;


 typedef itk::ImageFileReader< ImageType >  ReaderType;

 ReaderType::Pointer fixedReader  = ReaderType::New();
 ReaderType::Pointer movingReader = ReaderType::New();

 fixedReader->SetFileName(  argv[ 1 ] );
 movingReader->SetFileName( argv[ 2 ] );

 try
   {
   fixedReader->Update();
   movingReader->Update();
   }
 catch( itk::ExceptionObject & excep )
   {
   std::cerr << "Exception catched !" << std::endl;
   std::cerr << excep << std::endl;
   }


 typedef itk::MeanSquaresImageToImageMetric< ImageType, ImageType >  MetricType;

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


 typedef itk::IdentityTransform< double, Dimension >  TransformType;

 TransformType::Pointer transform = TransformType::New();


 typedef itk::NearestNeighborInterpolateImageFunction<
                                ImageType, double >  InterpolatorType;

 InterpolatorType::Pointer interpolator = InterpolatorType::New();


 metric->SetInterpolator( interpolator );
 metric->SetTransform( transform );


 transform->SetIdentity();

 ImageType::ConstPointer fixedImage  = fixedReader->GetOutput();
 ImageType::ConstPointer movingImage = movingReader->GetOutput();

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

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

 try
   {
   metric->Initialize();
   }
 catch( itk::ExceptionObject & excep )
   {
   std::cerr << "Exception catched !" << std::endl;
   std::cerr << excep << std::endl;
   return -1;
   }


 MetricType::TransformParametersType displacement( Dimension );

 int rangex = 30;
 int rangey = 30;

   fp = fopen("linking_log.txt", "a");
       fprintf(fp, "%s \t%s\t", argv[1], argv[2]);
       //fprintf(fp, "\n");

 for( int dx = -rangex; dx <= rangex; dx++ )
   {
   for( int dy = -rangey; dy <= rangey; dy++ )
     {
     displacement[0] = dx;
     displacement[1] = dy;
     const double value = metric->GetValue( displacement );

     std::cout << dx << "   "  << dy << "   " << value << std::endl;

        fprintf(fp, "%f", value);

     }
       }
       fprintf(fp, "\n");
       fclose(fp);
 std::cout << std::endl;


 return 0;
}

// Software Guide : EndCodeSnippet

===============================================

The metric value produces incorrect results

Results:

Stack 1         Stack 2
16.img            18.img        (This is a close match and it seems right)
8.img              17.img        (This is not even close. It's definitely wrong)
9.img              19.img        (This is not even close. It's definitely wrong)

Am i doing something wrong?

Thanks,
IR.


More information about the Insight-users mailing list