]> Creatis software - FrontAlgorithms.git/blob - appli/examples/example_BinaryDistanceMap.cxx
...
[FrontAlgorithms.git] / appli / examples / example_BinaryDistanceMap.cxx
1 #include <ctime>
2 #include <iostream>
3 #include <string>
4
5 #include <itkImage.h>
6 #include <itkImageFileReader.h>
7 #include <itkImageFileWriter.h>
8 #include <itkInvertIntensityImageFilter.h>
9 #include <itkMinimumMaximumImageCalculator.h>
10 #include <itkDanielssonDistanceMapImageFilter.h>
11
12 // -------------------------------------------------------------------------
13 const unsigned int Dim = 3;
14 typedef unsigned char                        TPixel;
15 typedef float                                TScalar;
16 typedef itk::Image< TPixel, Dim >            TImage;
17 typedef itk::Image< TScalar, Dim >           TScalarImage;
18 typedef itk::ImageFileReader< TImage >       TImageReader;
19 typedef itk::ImageFileWriter< TScalarImage > TImageWriter;
20
21 // -------------------------------------------------------------------------
22 int main( int argc, char* argv[] )
23 {
24   if( argc < 3 )
25   {
26     std::cerr
27       << "Usage: " << argv[ 0 ]
28       << " input_image output_image"
29       << std::endl;
30     return( 1 );
31
32   } // fi
33   std::string input_image_fn = argv[ 1 ];
34   std::string output_image_fn = argv[ 2 ];
35
36   // Read image
37   TImageReader::Pointer input_image_reader = TImageReader::New( );
38   input_image_reader->SetFileName( input_image_fn );
39   try
40   {
41     input_image_reader->Update( );
42   }
43   catch( itk::ExceptionObject& err )
44   {
45     std::cerr << "Error caught: " << err << std::endl;
46     return( 1 );
47
48   } // yrt
49
50   // Invert intensity
51   typedef itk::MinimumMaximumImageCalculator< TImage > TMinMax;
52   TMinMax::Pointer minmax = TMinMax::New( );
53   minmax->SetImage( input_image_reader->GetOutput( ) );
54   minmax->Compute( );
55
56   typedef itk::InvertIntensityImageFilter< TImage > TInvert;
57   TInvert::Pointer invert = TInvert::New( );
58   invert->SetInput( input_image_reader->GetOutput( ) );
59   invert->SetMaximum( minmax->GetMaximum( ) );
60
61   typedef itk::DanielssonDistanceMapImageFilter< TImage, TScalarImage > TDistance;
62   TDistance::Pointer distance = TDistance::New( );
63   distance->SetInput( invert->GetOutput( ) );
64   distance->InputIsBinaryOn( );
65   distance->SquaredDistanceOn( );
66   std::clock_t start = std::clock( );
67   distance->Update( );
68   std::clock_t end = std::clock( );
69   double seconds = double( end - start ) / double( CLOCKS_PER_SEC );
70   std::cout << "Distance map time = " << seconds << std::endl;
71
72   TImageWriter::Pointer writer = TImageWriter::New( );
73   writer->SetInput( distance->GetOutput( ) );
74   writer->SetFileName( output_image_fn );
75   writer->Update( );
76
77   return( 0 );
78 }
79
80 // eof - $RCSfile$