[Insight-users] Similarity measure

Luis Ibanez luis.ibanez@kitware.com
Fri May 14 15:32:19 EDT 2004


This is a multi-part message in MIME format.
--------------050909020004040107010204
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 8bit


Hi Josiane,


Many small details:


A) You are missing to connect the transform to the metric, like

       metric->SetTransform( transform );

B) you are passing the transform as argument to the GetValue()
    method of the metric.  That shouldn't even compile...   :-/
    GetValue() expects an array of doubles, those are the
    parameters for the transform.

C) You are missing to connect an interpolator to the metric,
    probably you want to use the NearestNeighborhood interpolator.

D) You are not calling Initialize() in the metric before invoking
    GetValue();


Please find attached the code that will do the Metric computation.


You will find useful to read the Registration chapter from the
SoftwareGuide.


Note also that if you have images that are already registered,
there are much faster and simpler ways to do this computation.



   Regards,



     Luis



----------------------------------------------------------------------
Josiane Yankam Njiwa--DEA Clarysse--Fin 11/04 wrote:

> Hello
> 
> 
> I have two images and i want to calculate the square difference métric
> beetween the two images. I have the code below which is not work and
> return zero when i run the programm. Can you help me to solve my problem?
> Thanks for your help
> 
> Josiane
> 
> 
> --------------------------------------------------------------------------
> typedef itk::MeanSquaresImageToImageMetric<
>                                     FixedImageType,
>                                     MovingImageType >    MetricType;
> typedef itk::AffineTransform< PixelType, Dimension >  TransformType;
> TransformType::Pointer transform = TransformType::New();
> MetricType::Pointer         metric        = MetricType::New();
> TransformType::OutputVectorType translation;
>   translation[0] = 0;  // X translation in millimeters
>   translation[1] = 1;  // Y translation in millimeters
>   transform->Translate( translation );
>   transform->Rotate2D(0,false);
> metric->SetFixedImage(    fixedImageReader->GetOutput()    );
> metric->SetMovingImage(   caster->GetOutput()   );
> metric->SetFixedImageRegion(
> fixedImageReader->GetOutput()->GetBufferedRegion() );
> const double bestValue = metric->GetValue(transform);
> std::cout << " Metric value  = " << bestValue          << std::endl;
> ---------------------------------------------------------------------------
> _______________________________________________
> Insight-users mailing list
> Insight-users@itk.org
> http://www.itk.org/mailman/listinfo/insight-users
> 
-------------------------------------------------------------------


--------------050909020004040107010204
Content-Type: text/plain;
 name="meanSquaredDifferences.cxx"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="meanSquaredDifferences.cxx"


#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkMeanSquaresImageToImageMetric.h"
#include "itkTranslationTransform.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;

  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::TranslationTransform< 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;
    }


  const double value = metric->GetValue( displacement );

  std::cout << "Mean Squares difference  " << value << std::endl;

  return 0;
}


--------------050909020004040107010204--





More information about the Insight-users mailing list