[ITK-users] Husdorff Distance

stefano serviddio s.serviddio at gmail.com
Tue Sep 29 05:38:40 EDT 2015


Hi,
I'm trying to measure Husdorff distance between two dicom/png image for
evaluating the difference before and after registration, but I have a weird
result because, also I see that the images are exactly aligned the husdroff
distance increases instead decreases.
This is my code, probably I have done some mistakes.

#include <fstream>
#include <time.h>

#include "itkImageRegionIterator.h"
#include "itkImageFileReader.h"

#include "itkGDCMImageIO.h"
#include "itkPNGImageIO.h"

#include "itkDirectedHausdorffDistanceImageFilter.h"
#include "itkHausdorffDistanceImageFilter.h"



int main()
{
  typedef unsigned short PixelType1;
  typedef unsigned short PixelType2;
  const unsigned int Dimension=2;




      time_t     now = time(0);
      struct tm  tstruct;
      char       buf[80];
      tstruct = *localtime(&now);
      strftime(buf, sizeof(buf), "%Y-%m-%d %X", &tstruct);

     std::ofstream resultfile;
      char Risultati[] = "D:/Immagini/HusdorffResul.txt";

    resultfile.open(Risultati, std::ios::app);
    if (resultfile.is_open())
     {
        std::cout << "File Open exists\n";

     }

resultfile<<"Data :"<<buf<<std::endl;


  typedef itk::Image<PixelType1, Dimension> Image1Type;
  typedef itk::Image<PixelType2, Dimension> Image2Type;

  typedef itk::ImageFileReader<Image1Type> FileReaderType1;
  typedef itk::ImageFileReader<Image2Type> FileReaderType2;

  FileReaderType1::Pointer reader1=FileReaderType1::New();
  FileReaderType2::Pointer reader2=FileReaderType2::New();


  reader1->SetFileName("D:/Images/12.png");
  reader1->SetImageIO(itk::PNGImageIO::New());

  reader2->SetFileName("D:/Images/240.png");
  reader2->SetImageIO(itk::PNGImageIO::New());


  typedef itk::GDCMImageIO GDCMType;
  GDCMType::Pointer gdcm=GDCMType::New();

  /*reader1->SetImageIO(gdcm);
  reader2->SetImageIO(gdcm);*/
  reader1->Update();
  reader2->Update();

  std::cout<<"Image Loading Complete!!!"<<std::endl;

  Image1Type::Pointer image1 = reader1->GetOutput();
  Image2Type::Pointer image2 = reader2->GetOutput();


  image1->Allocate();
  image2->Allocate();

  Image1Type::PointType origin1;
  Image1Type::PointType origin2;
  Image1Type::SpacingType spacing;
  origin1.Fill(0.0);

  spacing.Fill(1.0);
  image1->SetOrigin(origin1);
  image1->SetSpacing(spacing);


  std::cout<<"Immagini allocate in memoria"<<std::endl;


  resultfile << "Dim Immagine Fix: " <<
image1->GetLargestPossibleRegion().GetSize() << std::endl;
  resultfile << "Dim Immagine Mov: " <<
image2->GetLargestPossibleRegion().GetSize() << std::endl;



  image2->SetOrigin(image1->GetOrigin());

  image2->SetSpacing(image1->GetSpacing());
  resultfile<<"Nuova Origine Mov:
"<<reader2->GetOutput()->GetOrigin()<<std::endl;

  resultfile<<"Nuovo Spacing Mov:
"<<reader2->GetOutput()->GetSpacing()<<std::endl;
  std::cout<<"Nuova Origine Mov:
"<<reader2->GetOutput()->GetOrigin()<<std::endl;

  std::cout<<"Nuovo Spacing Mov:
"<<reader2->GetOutput()->GetSpacing()<<std::endl;

  {
  typedef itk::DirectedHausdorffDistanceImageFilter<Image1Type,Image2Type>
DirectHausdType;

  DirectHausdType::Pointer filter1=DirectHausdType::New();

  filter1->SetInput1(image1);
  filter1->SetInput2(image2);

  filter1->SetUseImageSpacing(true);
    try
    {
    filter1->Update();
DirectHausdType::RealType distance=filter1->GetDirectedHausdorffDistance();
   resultfile<<"Distanza h(a,b) Immagini: "<<(double)distance<<" mm
"<<std::endl;
   resultfile<<"Distanza Media h(a,b) le due Immagini mov-fix:
"<<(double)filter1->GetAverageHausdorffDistance()<<" mm"<<std::endl;

   std::cout<<"Distanza h(a,b) Immagini: "<<(double)distance<<std::endl;
   std::cout<<"Distanza Media h(a,b) Immagini mov-fix:
"<<(double)filter1->GetAverageHausdorffDistance()<<" mm "<<std::endl;


    }
    catch( itk::ExceptionObject & err )
    {
    std::cerr << "ExceptionObject caught !" << std::endl;
    std::cerr << err << std::endl;
       return EXIT_FAILURE;
    }
  }


  {
  typedef itk::DirectedHausdorffDistanceImageFilter<Image2Type,Image1Type>
DirectHausdType2;
  DirectHausdType2::Pointer filter=DirectHausdType2::New();


  filter->SetInput1(image2);

  filter->SetInput2(image1);
   try
    {
filter->SetUseImageSpacing(true);
    filter->Update();
DirectHausdType2::RealType distance=filter->GetDirectedHausdorffDistance();
    resultfile<<"Distanza h(b,a) Immagini: "<<(double)distance<<" mm
"<<std::endl;
    resultfile<<"Distanza media h(b,a) Immagini fix-mov:
"<<(double)filter->GetAverageHausdorffDistance()<<" mm"<<std::endl;

std::cout<<"Distanza tra le due Immagini: "<<(double)distance<<std::endl;
    std::cout<<"Distanza media le due Immagini fix-mov:
"<<(double)filter->GetAverageHausdorffDistance()<<std::endl;
    }
    catch( itk::ExceptionObject & err )
    {
    std::cerr << "ExceptionObject caught !" << std::endl;
    std::cerr << err << std::endl;
       return EXIT_FAILURE;
    }

   }

  //  Hausdorff distance H(image1,image2)
  {
  typedef itk::HausdorffDistanceImageFilter<Image1Type,Image2Type>
FilterType;
  FilterType::Pointer filter = FilterType::New();

  filter->SetInput1( image1 );
  filter->SetInput2( image2 );
  try{

  filter->SetUseImageSpacing(true);
  filter->Update();
   FilterType::RealType distance = filter->GetHausdorffDistance();
  resultfile << " H(a,b) Distanza Hausdorff " << (double)distance <<" mm
"<< std::endl;
  resultfile << " H(a,b) Distanza Hausdorff Media " <<
filter->GetAverageHausdorffDistance() << " mm "<< std::endl;

  std::cout << " H(a,b) Distanza Hausdorff " << (double)distance <<
std::endl;
  std::cout << " H(a,b) Distanza Hausdorff Media " <<
filter->GetAverageHausdorffDistance() << std::endl;

  }catch( itk::ExceptionObject & err )
    {
    std::cerr << "ExceptionObject caught !" << std::endl;
    std::cerr << err << std::endl;
       return EXIT_FAILURE;
    }





  }
  //  Hausdorff distance H(image2,image1)
  {
  typedef itk::HausdorffDistanceImageFilter<Image2Type,Image1Type>
FilterType;
  FilterType::Pointer filter = FilterType::New();

  filter->SetInput1( image2 );
  filter->SetInput2( image1 );


  try{
  filter->SetUseImageSpacing(true);
  filter->Update();

  FilterType::RealType distance = filter->GetHausdorffDistance();
  resultfile << " H(b,a) Distanza Hausdorff " << (double)distance <<" mm
"<< std::endl;
  resultfile << " H(b,a) Distanza Hausdorff Media " <<
(double)filter->GetAverageHausdorffDistance() <<" mm "<< std::endl;

  std::cout << " H(b,a) Distanza Hausdorff " << (double)distance <<
std::endl;
  std::cout << " H(b,a) Distanza Hausdorff Media " << (double)
filter->GetAverageHausdorffDistance() << std::endl;

  }
  catch( itk::ExceptionObject & err )
  {
    std::cerr << "ExceptionObject caught !" << std::endl;
    std::cerr << err << std::endl;
       return EXIT_FAILURE;
  }




  }

  resultfile.close();
  return EXIT_SUCCESS;


}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/insight-users/attachments/20150929/bf6cf765/attachment.html>


More information about the Insight-users mailing list