Main Page   Groups   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Concepts

itkTestMain.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Insight Segmentation & Registration Toolkit
00004   Module:    $RCSfile: itkTestMain.h,v $
00005   Language:  C++
00006   Date:      $Date: 2008-03-17 19:28:02 $
00007   Version:   $Revision: 1.27 $
00008 
00009   Copyright (c) Insight Software Consortium. All rights reserved.
00010   See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
00011 
00012   Portions of this code are covered under the VTK copyright.
00013   See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.htm for details.
00014 
00015      This software is distributed WITHOUT ANY WARRANTY; without even 
00016      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
00017      PURPOSE.  See the above copyright notices for more information.
00018 
00019 =========================================================================*/
00020 
00021 // This file is used to create TestDriver executables
00022 // These executables are able to register a function pointer to a string name
00023 // in a lookup table.   By including this file, it creates a main function
00024 // that calls RegisterTests() then looks up the function pointer for the test
00025 // specified on the command line.
00026 #include "itkWin32Header.h"
00027 #include <map>
00028 #include <string>
00029 #include <iostream>
00030 #include <fstream>
00031 #include "itkNumericTraits.h"
00032 #include "itkMultiThreader.h"
00033 #include "itkImage.h"
00034 #include "itkImageFileReader.h"
00035 #include "itkImageFileWriter.h"
00036 #include "itkImageRegionConstIterator.h"
00037 #include "itkSubtractImageFilter.h"
00038 #include "itkRescaleIntensityImageFilter.h"
00039 #include "itkExtractImageFilter.h"
00040 #include "itkDifferenceImageFilter.h"
00041 #include "itkImageRegion.h"
00042 #include "itksys/SystemTools.hxx"
00043 
00044 #define ITK_TEST_DIMENSION_MAX 6
00045 
00046 typedef int (*MainFuncPointer)(int , char* [] );
00047 std::map<std::string, MainFuncPointer> StringToTestFunctionMap;
00048 
00049 #define REGISTER_TEST(test) \
00050 extern int test(int, char* [] ); \
00051 StringToTestFunctionMap[#test] = test
00052 
00053 int RegressionTestImage (const char *testImageFilename, 
00054                          const char *baselineImageFilename, 
00055                          int reportErrors,
00056                          double intensityTolerance = 2.0,
00057                          unsigned int numberOfPixelsTolerance = 0,
00058                          unsigned int radiusTolerance = 0);
00059 
00060 std::map<std::string,int> RegressionTestBaselines (char *);
00061 
00062 void RegisterTests();
00063 void PrintAvailableTests()
00064 {
00065   std::cout << "Available tests:\n";
00066   std::map<std::string, MainFuncPointer>::iterator j = StringToTestFunctionMap.begin();
00067   int i = 0;
00068   while(j != StringToTestFunctionMap.end())
00069     {
00070     std::cout << i << ". " << j->first << "\n";
00071     ++i;
00072     ++j;
00073     }
00074 }
00075 
00076 int main(int ac, char* av[] )
00077 {
00078   char *baselineFilename = NULL;
00079   char *testFilename = NULL;
00080   double intensityTolerance  = 2.0;
00081   unsigned int numberOfPixelsTolerance = 0;
00082   unsigned int radiusTolerance = 0;
00083 
00084   RegisterTests();
00085   std::string testToRun;
00086   if(ac < 2)
00087     {
00088     PrintAvailableTests();
00089     std::cout << "To run a test, enter the test number: ";
00090     int testNum = 0;
00091     std::cin >> testNum;
00092     std::map<std::string, MainFuncPointer>::iterator j = StringToTestFunctionMap.begin();
00093     int i = 0;
00094     while(j != StringToTestFunctionMap.end() && i < testNum)
00095       {
00096       ++i;
00097       ++j;
00098       }
00099     if(j == StringToTestFunctionMap.end())
00100       {
00101       std::cerr << testNum << " is an invalid test number\n";
00102       return -1;
00103       }
00104     testToRun = j->first;
00105     }
00106   else
00107     {
00108     if (strcmp(av[1], "--with-threads") == 0)
00109       {
00110       int numThreads = atoi(av[2]);
00111       itk::MultiThreader::SetGlobalDefaultNumberOfThreads(numThreads);
00112       av += 2;
00113       ac -= 2;
00114       }
00115     else if (strcmp(av[1], "--without-threads") == 0)
00116       {
00117       itk::MultiThreader::SetGlobalDefaultNumberOfThreads(1);
00118       av += 1;
00119       ac -= 1;
00120       }
00121     if (ac > 3 && strcmp(av[1], "--compare") == 0)
00122       {
00123       baselineFilename = av[2];
00124       testFilename = av[3];
00125       av += 3;
00126       ac -= 3;
00127       // Advanced options for image comparison (The options must appear in 
00128       // this order in the command line because this parser code is not that smart)
00129       if (ac > 2 && strcmp(av[1], "--compareNumberOfPixelsTolerance") == 0)
00130         {
00131         numberOfPixelsTolerance = atoi( av[2] );
00132         av += 2;
00133         ac -= 2;
00134         }
00135       if (ac > 2 && strcmp(av[1], "--compareRadiusTolerance") == 0)
00136         {
00137         radiusTolerance = atoi( av[2] );
00138         av += 2;
00139         ac -= 2;
00140         }
00141       if (ac > 2 && strcmp(av[1], "--compareIntensityTolerance") == 0)
00142         {
00143         intensityTolerance = atof( av[2] );
00144         av += 2;
00145         ac -= 2;
00146         }
00147       }
00148     testToRun = av[1];
00149     }
00150   std::map<std::string, MainFuncPointer>::iterator j = StringToTestFunctionMap.find(testToRun);
00151   if(j != StringToTestFunctionMap.end())
00152     {
00153     MainFuncPointer f = j->second;
00154     int result;
00155     try
00156       {
00157       // Invoke the test's "main" function.
00158       result = (*f)(ac-1, av+1);
00159 
00160       // Make a list of possible baselines
00161       if (baselineFilename && testFilename)
00162         {
00163         std::map<std::string,int> baselines = RegressionTestBaselines(baselineFilename);
00164         std::map<std::string,int>::iterator baseline = baselines.begin();
00165         std::string bestBaseline;
00166         int bestBaselineStatus = itk::NumericTraits<int>::max();
00167         while (baseline != baselines.end())
00168           {
00169           baseline->second = RegressionTestImage(testFilename,
00170                                                  (baseline->first).c_str(),
00171                                                  0, 
00172                                                  intensityTolerance, 
00173                                                  numberOfPixelsTolerance, 
00174                                                  radiusTolerance );
00175           if (baseline->second < bestBaselineStatus)
00176             {
00177             bestBaseline = baseline->first;
00178             bestBaselineStatus = baseline->second;
00179             }
00180           if (baseline->second == 0)
00181             {
00182             break;
00183             }
00184           ++baseline;
00185           }
00186         // if the best we can do still has errors, generate the error images
00187          if (bestBaselineStatus)
00188           {
00189           RegressionTestImage(testFilename,
00190                               bestBaseline.c_str(),
00191                               1, 
00192                               intensityTolerance,
00193                               numberOfPixelsTolerance, 
00194                               radiusTolerance );
00195           }
00196 
00197         // output the matching baseline
00198         std::cout << "<DartMeasurement name=\"BaselineImageName\" type=\"text/string\">";
00199         std::cout << itksys::SystemTools::GetFilenameName(bestBaseline);
00200         std::cout << "</DartMeasurement>" << std::endl;
00201         
00202         result += bestBaselineStatus;
00203         }
00204       }
00205     catch(const itk::ExceptionObject& e)
00206       {
00207       std::cerr << "ITK test driver caught an ITK exception:\n";
00208       std::cerr << e.GetFile() << ":" << e.GetLine() << ":\n"
00209                 << e.GetDescription() << "\n";
00210       result = -1;
00211       }
00212     catch(const std::exception& e)
00213       {
00214       std::cerr << "ITK test driver caught an exception:\n";
00215       std::cerr << e.what() << "\n";
00216       result = -1;
00217       }
00218     catch(...)
00219       {
00220       std::cerr << "ITK test driver caught an unknown exception!!!\n";
00221       result = -1;
00222       }
00223     return result;
00224     }
00225   PrintAvailableTests();
00226   std::cerr << "Failed: " << testToRun << ": No test registered with name " << testToRun << "\n";
00227   return -1;
00228 }
00229 
00230 // Regression Testing Code
00231 
00232 int RegressionTestImage (const char *testImageFilename, 
00233                          const char *baselineImageFilename, 
00234                          int reportErrors,
00235                          double intensityTolerance,
00236                          unsigned int numberOfPixelsTolerance, 
00237                          unsigned int radiusTolerance )
00238 {
00239   // Use the factory mechanism to read the test and baseline files and convert them to double
00240   typedef itk::Image<double,ITK_TEST_DIMENSION_MAX> ImageType;
00241   typedef itk::Image<unsigned char,ITK_TEST_DIMENSION_MAX> OutputType;
00242   typedef itk::Image<unsigned char,2> DiffOutputType;
00243   typedef itk::ImageFileReader<ImageType> ReaderType;
00244 
00245   // Read the baseline file
00246   ReaderType::Pointer baselineReader = ReaderType::New();
00247     baselineReader->SetFileName(baselineImageFilename);
00248   try
00249     {
00250     baselineReader->UpdateLargestPossibleRegion();
00251     }
00252   catch (itk::ExceptionObject& e)
00253     {
00254     std::cerr << "Exception detected while reading " << baselineImageFilename << " : "  << e.GetDescription();
00255     return 1000;
00256     }
00257 
00258   // Read the file generated by the test
00259   ReaderType::Pointer testReader = ReaderType::New();
00260     testReader->SetFileName(testImageFilename);
00261   try
00262     {
00263     testReader->UpdateLargestPossibleRegion();
00264     }
00265   catch (itk::ExceptionObject& e)
00266     {
00267     std::cerr << "Exception detected while reading " << testImageFilename << " : "  << e.GetDescription() << std::endl;
00268     return 1000;
00269     }
00270 
00271   // The sizes of the baseline and test image must match
00272   ImageType::SizeType baselineSize;
00273     baselineSize = baselineReader->GetOutput()->GetLargestPossibleRegion().GetSize();
00274   ImageType::SizeType testSize;
00275     testSize = testReader->GetOutput()->GetLargestPossibleRegion().GetSize();
00276   
00277   if (baselineSize != testSize)
00278     {
00279     std::cerr << "The size of the Baseline image and Test image do not match!" << std::endl;
00280     std::cerr << "Baseline image: " << baselineImageFilename
00281               << " has size " << baselineSize << std::endl;
00282     std::cerr << "Test image:     " << testImageFilename
00283               << " has size " << testSize << std::endl;
00284     return 1;
00285     }
00286 
00287   // Now compare the two images
00288   typedef itk::DifferenceImageFilter<ImageType,ImageType> DiffType;
00289   DiffType::Pointer diff = DiffType::New();
00290     diff->SetValidInput(baselineReader->GetOutput());
00291     diff->SetTestInput(testReader->GetOutput());
00292     diff->SetDifferenceThreshold( intensityTolerance );
00293     diff->SetToleranceRadius( radiusTolerance );
00294     diff->UpdateLargestPossibleRegion();
00295 
00296   double status = diff->GetNumberOfPixelsWithDifferences();
00297 
00298   // if there are discrepencies, create an diff image
00299   if ( (status > numberOfPixelsTolerance) && reportErrors )
00300     {
00301     typedef itk::RescaleIntensityImageFilter<ImageType,OutputType> RescaleType;
00302     typedef itk::ExtractImageFilter<OutputType,DiffOutputType> ExtractType;
00303     typedef itk::ImageFileWriter<DiffOutputType> WriterType;
00304     typedef itk::ImageRegion<ITK_TEST_DIMENSION_MAX> RegionType;
00305     OutputType::IndexType index; index.Fill(0);
00306     OutputType::SizeType size; size.Fill(0);
00307 
00308     RescaleType::Pointer rescale = RescaleType::New();
00309       rescale->SetOutputMinimum(itk::NumericTraits<unsigned char>::NonpositiveMin());
00310       rescale->SetOutputMaximum(itk::NumericTraits<unsigned char>::max());
00311       rescale->SetInput(diff->GetOutput());
00312       rescale->UpdateLargestPossibleRegion();
00313 
00314     RegionType region;
00315     region.SetIndex(index);
00316     
00317     size = rescale->GetOutput()->GetLargestPossibleRegion().GetSize();
00318     for (unsigned int i = 2; i < ITK_TEST_DIMENSION_MAX; i++)
00319       {
00320       size[i] = 0;
00321       }
00322     region.SetSize(size);
00323 
00324     ExtractType::Pointer extract = ExtractType::New();
00325       extract->SetInput(rescale->GetOutput());
00326       extract->SetExtractionRegion(region);
00327 
00328     WriterType::Pointer writer = WriterType::New();
00329       writer->SetInput(extract->GetOutput());
00330 
00331     std::cout << "<DartMeasurement name=\"ImageError\" type=\"numeric/double\">";
00332     std::cout << status;
00333     std::cout <<  "</DartMeasurement>" << std::endl;
00334 
00335     ::itk::OStringStream diffName;
00336       diffName << testImageFilename << ".diff.png";
00337     try
00338       {
00339       rescale->SetInput(diff->GetOutput());
00340       rescale->Update();
00341       }
00342     catch (...)
00343       {
00344       std::cerr << "Error during rescale of " << diffName.str() << std::endl;
00345       }
00346     writer->SetFileName(diffName.str().c_str());
00347     try
00348       {
00349       writer->Update();
00350       }
00351     catch (...)
00352       {
00353       std::cerr << "Error during write of " << diffName.str() << std::endl;
00354       }
00355 
00356     std::cout << "<DartMeasurementFile name=\"DifferenceImage\" type=\"image/png\">";
00357     std::cout << diffName.str();
00358     std::cout << "</DartMeasurementFile>" << std::endl;
00359 
00360     ::itk::OStringStream baseName;
00361     baseName << testImageFilename << ".base.png";
00362     try
00363       {
00364       rescale->SetInput(baselineReader->GetOutput());
00365       rescale->Update();
00366       }
00367     catch (...)
00368       {
00369       std::cerr << "Error during rescale of " << baseName.str() << std::endl;
00370       }
00371     try
00372       {
00373       writer->SetFileName(baseName.str().c_str());
00374       writer->Update();
00375       }
00376     catch (...)
00377       {
00378       std::cerr << "Error during write of " << baseName.str() << std::endl;
00379       }
00380 
00381     std::cout << "<DartMeasurementFile name=\"BaselineImage\" type=\"image/png\">";
00382     std::cout << baseName.str();
00383     std::cout << "</DartMeasurementFile>" << std::endl;
00384 
00385     ::itk::OStringStream testName;
00386     testName << testImageFilename << ".test.png";
00387     try
00388       {
00389       rescale->SetInput(testReader->GetOutput());
00390       rescale->Update();
00391       }
00392     catch (...)
00393       {
00394       std::cerr << "Error during rescale of " << testName.str()
00395                 << std::endl;
00396       }
00397     try
00398       {
00399       writer->SetFileName(testName.str().c_str());
00400       writer->Update();
00401       }
00402     catch (...)
00403       {
00404       std::cerr << "Error during write of " << testName.str() << std::endl;
00405       }
00406 
00407     std::cout << "<DartMeasurementFile name=\"TestImage\" type=\"image/png\">";
00408     std::cout << testName.str();
00409     std::cout << "</DartMeasurementFile>" << std::endl;
00410 
00411 
00412     }
00413   return (status > numberOfPixelsTolerance) ? 1 : 0;
00414 }
00415 
00416 //
00417 // Generate all of the possible baselines
00418 // The possible baselines are generated fromn the baselineFilename using the following algorithm:
00419 // 1) strip the suffix
00420 // 2) append a digit .x
00421 // 3) append the original suffix.
00422 // It the file exists, increment x and continue
00423 //
00424 std::map<std::string,int> RegressionTestBaselines (char *baselineFilename)
00425 {
00426   std::map<std::string,int> baselines;
00427   baselines[std::string(baselineFilename)] = 0;
00428 
00429   std::string originalBaseline(baselineFilename);
00430 
00431   int x = 0;
00432   std::string::size_type suffixPos = originalBaseline.rfind(".");
00433   std::string suffix;
00434   if (suffixPos != std::string::npos)
00435     {
00436     suffix = originalBaseline.substr(suffixPos,originalBaseline.length());
00437     originalBaseline.erase(suffixPos,originalBaseline.length());
00438     }
00439   while (++x)
00440     {
00441     ::itk::OStringStream filename;
00442     filename << originalBaseline << "." << x << suffix;
00443     std::ifstream filestream(filename.str().c_str());
00444     if (!filestream)
00445       {
00446         break;
00447       }
00448     baselines[filename.str()] = 0;
00449     filestream.close();
00450     }
00451   return baselines;
00452 }
00453 
00454 // Needed for explicit instantiation
00455 #include "itkDifferenceImageFilter.txx"
00456 

Generated at Mon Apr 14 14:35:37 2008 for ITK by doxygen 1.5.1 written by Dimitri van Heesch, © 1997-2000