~ubuntu-branches/debian/sid/simpleitk/sid

« back to all changes in this revision

Viewing changes to Testing/Unit/sitkImageCompare.h

  • Committer: Package Import Robot
  • Author(s): Ghislain Antony Vaillant
  • Date: 2017-11-02 08:49:18 UTC
  • Revision ID: package-import@ubuntu.com-20171102084918-7hs09ih668xq87ej
Tags: upstream-1.0.1
ImportĀ upstreamĀ versionĀ 1.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*=========================================================================
 
2
*
 
3
*  Copyright Insight Software Consortium
 
4
*
 
5
*  Licensed under the Apache License, Version 2.0 (the "License");
 
6
*  you may not use this file except in compliance with the License.
 
7
*  You may obtain a copy of the License at
 
8
*
 
9
*         http://www.apache.org/licenses/LICENSE-2.0.txt
 
10
*
 
11
*  Unless required by applicable law or agreed to in writing, software
 
12
*  distributed under the License is distributed on an "AS IS" BASIS,
 
13
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
14
*  See the License for the specific language governing permissions and
 
15
*  limitations under the License.
 
16
*
 
17
*=========================================================================*/
 
18
#ifndef __ImageCompare_h
 
19
#define __ImageCompare_h
 
20
#include <SimpleITKTestHarness.h>
 
21
#include <sitkImage.h>
 
22
 
 
23
class ImageCompare {
 
24
public:
 
25
  typedef ImageCompare Self;
 
26
  ImageCompare();
 
27
 
 
28
  // Compare the image to the named baseline image slice by slice, display the difference in the center slice
 
29
  // If the baseline does not exist, fail, and write a baseline image in the output directory
 
30
  bool compare ( const itk::simple::Image& image, std::string testGroup, std::string tag );
 
31
 
 
32
  float testImages( const itk::simple::Image& testImage,
 
33
                    const itk::simple::Image& baselineImage,
 
34
                    bool retportErrors,
 
35
                    const std::string &baselineImageFilename );
 
36
 
 
37
 
 
38
  // Return the message from the previous image comparison.
 
39
  std::string getMessage() { return mMessage; }
 
40
  void setTolerance ( double t ) { mTolerance = t; }
 
41
  double getTolerance() { return mTolerance; }
 
42
 
 
43
  static void NormalizeAndSave ( const itk::simple::Image &image, const std::string &filename );
 
44
protected:
 
45
  std::string mMessage;
 
46
  double mTolerance;
 
47
};
 
48
 
 
49
#define IMAGECOMPARE( x, tag )                  \
 
50
  { \
 
51
ImageCompare imageCompare;  \
 
52
 EXPECT_TRUE( imageCompare.compare( x, "", tag ) ) << imageCompare.getMessage(); \
 
53
 }
 
54
#define IMAGECOMPAREWITHTOLERANCE( x, tag, tolerance )        \
 
55
  { \
 
56
  ImageCompare imageCompare;  \
 
57
  imageCompare.setTolerance(tolerance); \
 
58
  EXPECT_TRUE( imageCompare.compare( x, "", tag ) ) << imageCompare.getMessage(); \
 
59
  }
 
60
 
 
61
#define IMAGECOMPAREWITHHASH( hash, hashType, image, message )        \
 
62
  { \
 
63
  std::string actualHashValue = itk::simple::Hash( image, itk::simple::HashImageFilter::hashType ); \
 
64
  EXPECT_EQ ( hash, actualHashValue  ) << message;                      \
 
65
  if ( hash != actualHashValue )                                        \
 
66
    {                                                                   \
 
67
    std::string TestImageFilename = dataFinder.GetOutputFile ( actualHashValue + ".png"); \
 
68
    try {                                                               \
 
69
      ImageCompare::NormalizeAndSave( image, TestImageFilename );       \
 
70
                                                                        \
 
71
      std::cout << "<DartMeasurementFile name=\"TestImage\" type=\"image/png\">"; \
 
72
      std::cout << TestImageFilename << "</DartMeasurementFile>" << std::endl; \
 
73
    } catch (std::exception &e) {                                       \
 
74
      std::cerr << "Unexpected error while writing image:" << e.what() << std::endl; \
 
75
    } catch (...) {                                                     \
 
76
      std::cerr << "Unknow Error while writing image for measurement" << std::endl; \
 
77
    }                                                                   \
 
78
                                                                        \
 
79
    itk::simple::Image timage = image;                                  \
 
80
    if (timage.GetPixelIDValue() == itk::simple::sitkLabelUInt8) \
 
81
      {                                                                 \
 
82
      timage = itk::simple::Cast( timage, itk::simple::sitkUInt8); \
 
83
      }                                                                 \
 
84
    else if (timage.GetPixelIDValue() == itk::simple::sitkLabelUInt16) \
 
85
      {                                                                 \
 
86
      timage = itk::simple::Cast( timage, itk::simple::sitkUInt16); \
 
87
      }                                                                 \
 
88
    else if (timage.GetPixelIDValue() == itk::simple::sitkLabelUInt32) \
 
89
      {                                                                 \
 
90
      timage = itk::simple::Cast( timage, itk::simple::sitkUInt32); \
 
91
      }                                                                 \
 
92
    else if (timage.GetPixelIDValue() == itk::simple::sitkLabelUInt64) \
 
93
      {                                                                 \
 
94
      timage = itk::simple::Cast( timage, itk::simple::sitkUInt64); \
 
95
      }                                                                 \
 
96
    itk::simple::WriteImage ( timage, dataFinder.GetOutputFile ( actualHashValue + ".nrrd" ) ); \
 
97
    std::cout << "Saved actual image as " << dataFinder.GetOutputFile ( actualHashValue + ".nrrd" ) << std::endl; \
 
98
    }                                                                   \
 
99
  }
 
100
 
 
101
#define IMAGECOMPARE_WITH_TESTCASE( x, testCase, tag )  \
 
102
  { \
 
103
ImageCompare imageCompare;  \
 
104
 EXPECT_TRUE( imageCompare.compare( x, testCase, tag ) ) << imageCompare.getMessage(); \
 
105
 }
 
106
#define IMAGECOMPAREWITHTOLERANCE_WITH_TESTCASE( x, testCase, tag, tolerance ) \
 
107
  { \
 
108
  ImageCompare imageCompare;  \
 
109
  imageCompare.setTolerance(tolerance); \
 
110
  EXPECT_TRUE( imageCompare.compare( x, testCase, tag ) ) << imageCompare.getMessage(); \
 
111
  }
 
112
 
 
113
 
 
114
#endif