55 #ifndef _BASIS_TESTDRIVER_ITK_HXX 56 #define _BASIS_TESTDRIVER_ITK_HXX 61 #ifdef ITK_IO_FACTORY_REGISTER_MANAGER 62 # undef ITK_IO_FACTORY_REGISTER_MANAGER 68 #include <itkImageFileReader.h> 69 #include <itkImageFileWriter.h> 70 #include <itkRescaleIntensityImageFilter.h> 71 #include <itkExtractImageFilter.h> 72 #if defined(ITK_VERSION_MAJOR) && ITK_VERSION_MAJOR < 4 73 # include <itkDifferenceImageFilter.h> 75 # include <itkTestingComparisonImageFilter.h> 77 #include <itkOrientImageFilter.h> 79 #include <itkGDCMImageIOFactory.h> 80 #include <itkMetaImageIOFactory.h> 81 #include <itkJPEGImageIOFactory.h> 82 #include <itkPNGImageIOFactory.h> 83 #include <itkTIFFImageIOFactory.h> 84 #include <itkBMPImageIOFactory.h> 85 #include <itkVTKImageIOFactory.h> 86 #include <itkNrrdImageIOFactory.h> 87 #include <itkGiplImageIOFactory.h> 88 #include <itkNiftiImageIOFactory.h> 89 #include <itkObjectFactoryBase.h> 93 #define ITK_TEST_DIMENSION_MAX BASIS_MAX_TEST_IMAGE_DIMENSION 99 itk::ObjectFactoryBase::RegisterFactory(itk::MetaImageIOFactory::New());
100 itk::ObjectFactoryBase::RegisterFactory(itk::GDCMImageIOFactory::New());
101 itk::ObjectFactoryBase::RegisterFactory(itk::JPEGImageIOFactory::New());
102 itk::ObjectFactoryBase::RegisterFactory(itk::VTKImageIOFactory::New());
103 itk::ObjectFactoryBase::RegisterFactory(itk::PNGImageIOFactory::New());
104 itk::ObjectFactoryBase::RegisterFactory(itk::TIFFImageIOFactory::New());
105 itk::ObjectFactoryBase::RegisterFactory(itk::BMPImageIOFactory::New());
106 itk::ObjectFactoryBase::RegisterFactory(itk::NrrdImageIOFactory::New());
107 itk::ObjectFactoryBase::RegisterFactory(itk::GiplImageIOFactory::New());
108 itk::ObjectFactoryBase::RegisterFactory(itk::NiftiImageIOFactory::New());
123 const char* baselineImageFilename,
125 double intensityTolerance,
126 unsigned int numberOfPixelsTolerance,
127 unsigned int radiusTolerance,
128 bool orientationInsensitive)
131 typedef itk::Image<double, ITK_TEST_DIMENSION_MAX> ImageType;
132 typedef itk::Image<unsigned char, ITK_TEST_DIMENSION_MAX> OutputType;
133 typedef itk::Image<unsigned char, 2> DiffOutputType;
134 typedef itk::ImageFileReader<ImageType> ReaderType;
137 ReaderType::Pointer baselineReader = ReaderType::New();
138 baselineReader->SetFileName(baselineImageFilename);
141 baselineReader->UpdateLargestPossibleRegion();
143 catch (itk::ExceptionObject&
e)
145 std::cerr <<
"Exception detected while reading " << baselineImageFilename <<
" : " <<
e;
150 ReaderType::Pointer testReader = ReaderType::New();
151 testReader->SetFileName(testImageFilename);
154 testReader->UpdateLargestPossibleRegion();
156 catch (itk::ExceptionObject& e)
158 std::cerr <<
"Exception detected while reading " << testImageFilename <<
" : " << e << std::endl;
162 ImageType::Pointer baselineImage = baselineReader->GetOutput();
163 ImageType::Pointer testImage = testReader ->GetOutput();
165 testImage ->DisconnectPipeline();
166 baselineImage->DisconnectPipeline();
168 ImageType::SizeType baselineSize = baselineImage->GetLargestPossibleRegion().GetSize();
169 ImageType::SizeType testSize = testImage ->GetLargestPossibleRegion().GetSize();
171 if (orientationInsensitive) {
172 const unsigned int OrientImageDimension = 3;
173 typedef itk::Image<double, OrientImageDimension> OrienterImageType;
174 typedef itk::ExtractImageFilter<ImageType, OrienterImageType> ExtractorType;
175 typedef itk::CastImageFilter<OrienterImageType, ImageType> UpCasterType;
176 typedef itk::OrientImageFilter<OrienterImageType, OrienterImageType> OrienterType;
178 ExtractorType::Pointer extractor = ExtractorType::New();
179 OrienterType ::Pointer orienter = OrienterType ::New();
180 UpCasterType ::Pointer caster = UpCasterType ::New();
182 ImageType::SizeType extract_size;
183 ImageType::IndexType extract_index;
184 ImageType::RegionType extract_region;
185 extract_index.Fill(0);
186 extract_size .Fill(0);
189 if (baselineSize[i] > 1) extract_size[i] = baselineSize[i];
192 extract_region.SetIndex(extract_index);
193 extract_region.SetSize (extract_size);
194 extractor->SetExtractionRegion(extract_region);
195 #if !defined(ITK_VERSION_MAJOR) || ITK_VERSION_MAJOR > 3 196 extractor->SetDirectionCollapseToSubmatrix();
199 orienter->UseImageDirectionOn();
200 orienter->SetDesiredCoordinateOrientation(itk::SpatialOrientation::ITK_COORDINATE_ORIENTATION_RPI);
203 extractor->SetInput(baselineImage);
204 orienter ->SetInput(extractor->GetOutput());
205 caster ->SetInput(orienter ->GetOutput());
211 catch (itk::ExceptionObject& e)
213 std::cerr <<
"Failed to change orientation of baseline image to RPI : " << e << std::endl;
217 baselineImage = caster->GetOutput();
218 baselineSize = baselineImage->GetLargestPossibleRegion().GetSize();
219 baselineImage->DisconnectPipeline();
222 extractor->SetInput(testImage);
223 orienter ->SetInput(extractor->GetOutput());
224 caster ->SetInput(orienter ->GetOutput());
230 catch (itk::ExceptionObject& e)
232 std::cerr <<
"Failed to change orientation of test image to RPI : " << e << std::endl;
236 testImage = caster->GetOutput();
237 testSize = testImage->GetLargestPossibleRegion().GetSize();
238 testImage->DisconnectPipeline();
242 if (baselineSize != testSize)
244 std::cerr <<
"The size of the Baseline image and Test image do not match!" << std::endl;
245 std::cerr <<
"Baseline image: " << baselineImageFilename
246 <<
" has size " << baselineSize << std::endl;
247 std::cerr <<
"Test image: " << testImageFilename
248 <<
" has size " << testSize << std::endl;
253 #if defined(ITK_VERSION_MAJOR) && ITK_VERSION_MAJOR < 4 254 typedef itk::DifferenceImageFilter<ImageType,ImageType> DiffType;
256 typedef itk::Testing::ComparisonImageFilter<ImageType,ImageType> DiffType;
258 DiffType::Pointer
diff = DiffType::New();
259 diff->SetValidInput(baselineImage);
260 diff->SetTestInput(testImage);
262 diff->SetDifferenceThreshold( intensityTolerance );
263 diff->SetToleranceRadius( radiusTolerance );
265 diff->UpdateLargestPossibleRegion();
267 bool differenceFailed =
false;
269 double averageIntensityDifference = diff->GetTotalDifference();
271 unsigned long numberOfPixelsWithDifferences = diff->GetNumberOfPixelsWithDifferences();
276 std::cout <<
"<DartMeasurement name=\"ImageError\" type=\"numeric/double\">";
277 std::cout << numberOfPixelsWithDifferences;
278 std::cout <<
"</DartMeasurement>" << std::endl;
281 if( averageIntensityDifference > 0.0 ) {
282 if( numberOfPixelsWithDifferences > numberOfPixelsTolerance ) {
283 differenceFailed =
true;
285 differenceFailed =
false;
288 differenceFailed =
false;
291 if (differenceFailed && reportErrors) {
292 typedef itk::RescaleIntensityImageFilter<ImageType,OutputType> RescaleType;
293 typedef itk::ExtractImageFilter<OutputType,DiffOutputType> ExtractType;
294 typedef itk::ImageFileWriter<DiffOutputType> WriterType;
295 typedef itk::ImageRegion<ITK_TEST_DIMENSION_MAX> RegionType;
297 OutputType::IndexType index; index.Fill(0);
298 OutputType::SizeType size; size.Fill(0);
300 RescaleType::Pointer rescale = RescaleType::New();
302 rescale->SetOutputMinimum(itk::NumericTraits<unsigned char>::NonpositiveMin());
303 rescale->SetOutputMaximum(itk::NumericTraits<unsigned char>::max());
304 rescale->SetInput(diff->GetOutput());
305 rescale->UpdateLargestPossibleRegion();
312 size = rescale->GetOutput()->GetLargestPossibleRegion().GetSize();
314 index[i] = size[i] / 2;
320 region.SetIndex(index);
321 region.SetSize(size);
323 ExtractType::Pointer extract = ExtractType::New();
325 extract->SetInput(rescale->GetOutput());
326 extract->SetExtractionRegion(region);
327 #if !defined(ITK_VERSION_MAJOR) || ITK_VERSION_MAJOR > 3 328 extract->SetDirectionCollapseToIdentity();
331 WriterType::Pointer writer = WriterType::New();
332 writer->SetInput(extract->GetOutput());
334 std::ostringstream diffName;
335 diffName << testImageFilename <<
".diff.png";
337 rescale->SetInput(diff->GetOutput());
339 }
catch(
const std::exception& e) {
340 std::cerr <<
"Error during rescale of " << diffName.str() << std::endl;
341 std::cerr << e.what() <<
"\n";
345 std::cerr <<
"Error during rescale of " << diffName.str() << std::endl;
347 writer->SetFileName(diffName.str().c_str());
352 catch(
const std::exception& e)
354 std::cerr <<
"Error during write of " << diffName.str() << std::endl;
355 std::cerr << e.what() <<
"\n";
359 std::cerr <<
"Error during write of " << diffName.str() << std::endl;
362 std::cout <<
"<DartMeasurementFile name=\"DifferenceImage\" type=\"image/png\">";
363 std::cout << diffName.str();
364 std::cout <<
"</DartMeasurementFile>" << std::endl;
366 std::ostringstream baseName;
367 baseName << testImageFilename <<
".base.png";
370 rescale->SetInput(baselineImage);
373 catch(
const std::exception& e)
375 std::cerr <<
"Error during rescale of " << baseName.str() << std::endl;
376 std::cerr << e.what() <<
"\n";
380 std::cerr <<
"Error during rescale of " << baseName.str() << std::endl;
384 writer->SetFileName(baseName.str().c_str());
387 catch(
const std::exception& e)
389 std::cerr <<
"Error during write of " << baseName.str() << std::endl;
390 std::cerr << e.what() <<
"\n";
394 std::cerr <<
"Error during write of " << baseName.str() << std::endl;
397 std::cout <<
"<DartMeasurementFile name=\"BaselineImage\" type=\"image/png\">";
398 std::cout << baseName.str();
399 std::cout <<
"</DartMeasurementFile>" << std::endl;
401 std::ostringstream testName;
402 testName << testImageFilename <<
".test.png";
405 rescale->SetInput(testImage);
408 catch(
const std::exception& e)
410 std::cerr <<
"Error during rescale of " << testName.str() << std::endl;
411 std::cerr << e.what() <<
"\n";
415 std::cerr <<
"Error during rescale of " << testName.str() << std::endl;
419 writer->SetFileName(testName.str().c_str());
422 catch(
const std::exception& e)
424 std::cerr <<
"Error during write of " << testName.str() << std::endl;
425 std::cerr << e.what() <<
"\n";
429 std::cerr <<
"Error during write of " << testName.str() << std::endl;
432 std::cout <<
"<DartMeasurementFile name=\"TestImage\" type=\"image/png\">";
433 std::cout << testName.str();
434 std::cout <<
"</DartMeasurementFile>" << std::endl;
437 return differenceFailed;
441 #endif // _BASIS_TESTDRIVER_ITK_HXX #define ITK_TEST_DIMENSION_MAX
MultiStringArg diff("", "diff", "Compare the <test> file to the <baseline> file byte by byte." " Can by used to compare any files including text files." " For images, the --compare option should be used instead.", false, "<test> <baseline>", 2, false, &diff_visitor)
int RegressionTestImage(const char *testImageFilename, const char *baselineImageFilename, int reportErrors, double intensityTolerance, unsigned int numberOfPixelsTolerance, unsigned int radiusTolerance, bool orientationInsensitive)
void RegisterRequiredFactories()