File: itkHausdorffDistanceImageFilterTest.cxx

package info (click to toggle)
insighttoolkit 3.20.1%2Bgit20120521-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 80,652 kB
  • sloc: cpp: 458,133; ansic: 196,223; fortran: 28,000; python: 3,839; tcl: 1,811; sh: 1,184; java: 583; makefile: 430; csh: 220; perl: 193; xml: 20
file content (216 lines) | stat: -rw-r--r-- 6,470 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*=========================================================================

  Program:   Insight Segmentation & Registration Toolkit
  Module:    itkHausdorffDistanceImageFilterTest.cxx
  Language:  C++
  Date:      $Date$
  Version:   $Revision$

  Copyright (c) Insight Software Consortium. All rights reserved.
  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even 
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/
#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif

#include "itkDirectedHausdorffDistanceImageFilter.h"
#include "itkHausdorffDistanceImageFilter.h"
#include "itkImageRegionIterator.h"
#include "itkFilterWatcher.h"

int itkHausdorffDistanceImageFilterTest(int, char* [] )
{

  typedef unsigned int Pixel1Type;
  typedef float Pixel2Type;
  enum { ImageDimension = 3 };

  typedef itk::Image<Pixel1Type,ImageDimension> Image1Type;
  typedef itk::Image<Pixel2Type,ImageDimension> Image2Type;

  Image1Type::Pointer image1 = Image1Type::New();
  Image2Type::Pointer image2 = Image2Type::New();

  Image1Type::SizeType size;
  size.Fill( 50 );
  
  image1->SetRegions( size );
  image2->SetRegions( size );

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

  image1->FillBuffer( itk::NumericTraits<Pixel1Type>::Zero );
  image2->FillBuffer( itk::NumericTraits<Pixel2Type>::Zero );

  typedef Image1Type::RegionType RegionType;
  RegionType region1;
  RegionType region2;

  typedef Image1Type::IndexType IndexType;
  IndexType index;

  size.Fill( 20 );
  index.Fill( 10 );
  region1.SetSize( size );
  region1.SetIndex( index );

  size.Fill( 15 );
  index.Fill( 20 );
  region2.SetSize( size );
  region2.SetIndex( index );

  itk::ImageRegionIterator<Image1Type> it1( image1, region1 );
  Pixel1Type count = itk::NumericTraits<Pixel1Type>::Zero;
  while ( !it1.IsAtEnd() )
    {
    it1.Set( ++count );
    ++it1;
    }

  itk::ImageRegionIterator<Image2Type> it2( image2, region2 );
  while ( !it2.IsAtEnd() )
    {
    it2.Set( 7.2 );
    ++it2;
    }


  // compute the directed Hausdorff distance h(image1,image2)
  {
  typedef itk::DirectedHausdorffDistanceImageFilter<Image1Type,Image2Type> FilterType;
  FilterType::Pointer filter = FilterType::New();
  FilterWatcher watcher(filter, "filter");

  filter->SetInput1( image1 );
  filter->SetInput2( image2 );
  filter->Update();
  filter->Print( std::cout );


  // check results
  
  FilterType::RealType trueDistance = 10 * 
    vcl_sqrt( static_cast<double>(ImageDimension) );
  FilterType::RealType distance = filter->GetDirectedHausdorffDistance();

  std::cout << " True distance: " << trueDistance << std::endl;
  std::cout << " Computed computed: " << distance << std::endl;
  std::cout << " Average distance: " << filter->GetAverageHausdorffDistance() << std::endl;

  if ( vnl_math_abs( trueDistance - distance ) > 0.1 )
    {
    std::cout << "Test failed. " << std::endl;
    return EXIT_FAILURE;
    }
  if ( vnl_math_abs( 6.5 - filter->GetAverageHausdorffDistance() ) > 0.1 )
    {
    std::cout << "Test failed, average distance too great. " << std::endl;
    return EXIT_FAILURE;
    }
  }

  // compute the directed Hausdorff distance h(image2,image1)
  {
  typedef itk::DirectedHausdorffDistanceImageFilter<Image2Type,Image1Type> FilterType;
  FilterType::Pointer filter = FilterType::New();

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


  // check results
  FilterType::RealType trueDistance = 5 *
    vcl_sqrt( static_cast<double>(ImageDimension) );
  FilterType::RealType distance = filter->GetDirectedHausdorffDistance();

  std::cout << " True distance: " << trueDistance << std::endl;
  std::cout << " Computed computed: " << distance << std::endl;
  std::cout << " Average distance: " << filter->GetAverageHausdorffDistance() << std::endl;

  if ( vnl_math_abs( trueDistance - distance ) > 0.1 )
    {
    std::cout << "Test failed. " << std::endl;
    return EXIT_FAILURE;
    }
  if ( vnl_math_abs( 2.5 - filter->GetAverageHausdorffDistance() ) > 0.1 )
    {
    std::cout << "Test failed, average distance too great. " << std::endl;
    return EXIT_FAILURE;
    }
  }


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

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


  // check results
  FilterType::RealType trueDistance = 10 * 
    vcl_sqrt( static_cast<double>(ImageDimension) );
  FilterType::RealType distance = filter->GetHausdorffDistance();

  std::cout << " True distance: " << trueDistance << std::endl;
  std::cout << " Computed computed: " << distance << std::endl;
  std::cout << " Average distance: " << filter->GetAverageHausdorffDistance() << std::endl;

  if ( vnl_math_abs( trueDistance - distance ) > 0.1 )
    {
    std::cout << "Test failed. " << std::endl;
    return EXIT_FAILURE;
    }
  if ( vnl_math_abs( 4.5 - filter->GetAverageHausdorffDistance() ) > 0.1 )
    {
    std::cout << "Test failed, average distance too great. " << std::endl;
    return EXIT_FAILURE;
    }
  }

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

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


  // check results
  FilterType::RealType trueDistance = 10 *
    vcl_sqrt( static_cast<double>(ImageDimension) );
  FilterType::RealType distance = filter->GetHausdorffDistance();

  std::cout << " True distance: " << trueDistance << std::endl;
  std::cout << " Computed computed: " << distance << std::endl;
  std::cout << " Average distance: " << filter->GetAverageHausdorffDistance() << std::endl;

  if ( vnl_math_abs( trueDistance - distance ) > 0.1 )
    {
    std::cout << "Test failed. " << std::endl;
    return EXIT_FAILURE;
    }
  if ( vnl_math_abs( 4.5 - filter->GetAverageHausdorffDistance() ) > 0.1 )
    {
    std::cout << "Test failed, average distance too great. " << std::endl;
    return EXIT_FAILURE;
    }
  }


  std::cout << "Test passed. " << std::endl;
  return EXIT_SUCCESS;
  
}