/*========================================================================= Program: Insight Segmentation & Registration Toolkit Module: $RCSfile: ImageCompare.cxx,v $ Language: C++ Date: $Date: 2007/08/20 12:21:34 $ Version: $Revision: 1.7 $ 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. =========================================================================*/ #include "itkNumericTraits.h" #include "itkImage.h" #include "itkImageFileReader.h" #include "itkImageFileWriter.h" #include "itkRescaleIntensityImageFilter.h" #include "itkExtractImageFilter.h" #include "itkDifferenceImageFilter.h" #include #include #ifdef __BORLANDC__ #define ITK_TEST_DIMENSION_MAX 5 #else #define ITK_TEST_DIMENSION_MAX 6 #endif int RegressionTestImage (const char *, const char *, int, bool); int main(int argc, char **argv) { if(argc < 3) { std::cerr << "Usage:" << std::endl; std::cerr << "testImage, baselineImage1, [baselineImage2, baselineImage3, ...]" << std::endl; std::cerr << "Note that if you supply more than one baselineImage, this test will pass if any" << std::endl; std::cerr << "of them match the testImage" << std::endl; return -1; } int bestBaselineStatus = 2001; int bestBaseline = 2; try { if(argc == 3) { bestBaselineStatus = RegressionTestImage(argv[1], argv[2], 0, false); } else { for(int i=2;i ImageType; typedef itk::Image OutputType; typedef itk::Image DiffOutputType; typedef itk::ImageFileReader ReaderType; // Read the baseline file ReaderType::Pointer baselineReader = ReaderType::New(); baselineReader->SetFileName(baselineImageFilename); try { baselineReader->UpdateLargestPossibleRegion(); } catch (itk::ExceptionObject& e) { std::cerr << "Exception detected while reading " << baselineImageFilename << " : " << e.GetDescription(); return 1000; } // Read the file generated by the test ReaderType::Pointer testReader = ReaderType::New(); testReader->SetFileName(testImageFilename); try { testReader->UpdateLargestPossibleRegion(); } catch (itk::ExceptionObject& e) { std::cerr << "Exception detected while reading " << testImageFilename << " : " << e.GetDescription() << std::endl; return 1000; } // The sizes of the baseline and test image must match ImageType::SizeType baselineSize; baselineSize = baselineReader->GetOutput()->GetLargestPossibleRegion().GetSize(); ImageType::SizeType testSize; testSize = testReader->GetOutput()->GetLargestPossibleRegion().GetSize(); if (baselineSize != testSize) { std::cerr << "The size of the Baseline image and Test image do not match!" << std::endl; std::cerr << "Baseline image: " << baselineImageFilename << " has size " << baselineSize << std::endl; std::cerr << "Test image: " << testImageFilename << " has size " << testSize << std::endl; return 1; } // Now compare the two images typedef itk::DifferenceImageFilter DiffType; DiffType::Pointer diff = DiffType::New(); diff->SetValidInput(baselineReader->GetOutput()); diff->SetTestInput(testReader->GetOutput()); diff->SetDifferenceThreshold(2.0); diff->UpdateLargestPossibleRegion(); double status = diff->GetTotalDifference(); if (reportErrors) { typedef itk::RescaleIntensityImageFilter RescaleType; typedef itk::ExtractImageFilter ExtractType; typedef itk::ImageFileWriter WriterType; typedef itk::ImageRegion RegionType; OutputType::IndexType index; index.Fill(0); OutputType::SizeType size; size.Fill(0); RescaleType::Pointer rescale = RescaleType::New(); rescale->SetOutputMinimum(itk::NumericTraits::NonpositiveMin()); rescale->SetOutputMaximum(itk::NumericTraits::max()); rescale->SetInput(diff->GetOutput()); rescale->UpdateLargestPossibleRegion(); RegionType region; region.SetIndex(index); size = rescale->GetOutput()->GetLargestPossibleRegion().GetSize(); for (unsigned int i = 2; i < ITK_TEST_DIMENSION_MAX; i++) { size[i] = 0; } region.SetSize(size); ExtractType::Pointer extract = ExtractType::New(); extract->SetInput(rescale->GetOutput()); extract->SetExtractionRegion(region); WriterType::Pointer writer = WriterType::New(); writer->SetInput(extract->GetOutput()); if(differences) { // if there are discrepencies, create an diff image std::cout << ""; std::cout << status; std::cout << "" << std::endl; itksys_ios::ostringstream diffName; diffName << testImageFilename << ".diff.png"; try { rescale->SetInput(diff->GetOutput()); rescale->Update(); } catch (...) { std::cerr << "Error during rescale of " << diffName.str() << std::endl; } writer->SetFileName(diffName.str().c_str()); try { writer->Update(); } catch (...) { std::cerr << "Error during write of " << diffName.str() << std::endl; } std::cout << ""; std::cout << diffName.str(); std::cout << "" << std::endl; } itksys_ios::ostringstream baseName; baseName << testImageFilename << ".base.png"; try { rescale->SetInput(baselineReader->GetOutput()); rescale->Update(); } catch (...) { std::cerr << "Error during rescale of " << baseName.str() << std::endl; } try { writer->SetFileName(baseName.str().c_str()); writer->Update(); } catch (...) { std::cerr << "Error during write of " << baseName.str() << std::endl; } std::cout << ""; std::cout << baseName.str(); std::cout << "" << std::endl; itksys_ios::ostringstream testName; testName << testImageFilename << ".test.png"; try { rescale->SetInput(testReader->GetOutput()); rescale->Update(); } catch (...) { std::cerr << "Error during rescale of " << testName.str() << std::endl; } try { writer->SetFileName(testName.str().c_str()); writer->Update(); } catch (...) { std::cerr << "Error during write of " << testName.str() << std::endl; } std::cout << ""; std::cout << testName.str(); std::cout << "" << std::endl; } return (status != 0) ? 1 : 0; }