Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkImageEqualTest.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #include "mitkImage.h"
18 #include "mitkImageGenerator.h"
19 #include "mitkImageSliceSelector.h"
20 #include "mitkTestFixture.h"
21 #include "mitkTestingMacros.h"
22 
23 class mitkImageEqualTestSuite : public mitk::TestFixture
24 {
25  CPPUNIT_TEST_SUITE(mitkImageEqualTestSuite);
26  MITK_TEST(Equal_CloneAndOriginal_ReturnsTrue);
27  MITK_TEST(Equal_DifferentImageGeometry_ReturnsFalse);
28  MITK_TEST(Equal_DifferentPixelTypes_ReturnsFalse);
29  MITK_TEST(Equal_DifferentDimensions_ReturnsFalse);
30  MITK_TEST(Equal_DifferentDimensionalities_ReturnsFalse);
31  MITK_TEST(Equal_DifferentPixelValues_ReturnsFalse);
32  CPPUNIT_TEST_SUITE_END();
33 
34 private:
36  mitk::Image::Pointer m_Image;
37  mitk::Image::Pointer m_AnotherImage;
38 
39 public:
44  void setUp() override
45  {
46  // generate a gradient test image
47  m_Image = mitk::ImageGenerator::GenerateGradientImage<unsigned char>(3u, 3u, 1u);
48  m_AnotherImage = m_Image->Clone();
49  }
50 
51  void tearDown() override
52  {
53  m_Image = nullptr;
54  m_AnotherImage = nullptr;
55  }
56 
57  void Equal_CloneAndOriginal_ReturnsTrue()
58  {
59  MITK_ASSERT_EQUAL(m_Image, m_Image->Clone(), "A clone should be equal to its original.");
60  }
61 
62  void Equal_DifferentImageGeometry_ReturnsFalse()
63  {
64  mitk::Point3D origin;
65  origin[0] = 0.0;
66  origin[1] = 0.0;
67  origin[2] = mitk::eps * 1.01;
68 
69  m_AnotherImage->GetGeometry()->SetOrigin(origin);
70 
71  MITK_ASSERT_NOT_EQUAL(m_Image, m_AnotherImage, "One origin was modified. Result should be false.");
72  }
73 
74  void Equal_DifferentPixelTypes_ReturnsFalse()
75  {
76  m_AnotherImage = mitk::ImageGenerator::GenerateGradientImage<float>(3u, 3u, 1u);
77 
79  m_Image, m_AnotherImage, "One pixel type is float, the other unsigned char. Result should be false.");
80  }
81 
82  void Equal_DifferentDimensions_ReturnsFalse()
83  {
84  m_AnotherImage = mitk::ImageGenerator::GenerateGradientImage<unsigned char>(5u, 7u, 3u);
85 
87  m_Image,
88  m_AnotherImage,
89  "Dimensions of first image are: (3, 3, 1). Dimensions of second image are: (5, 7, 3). Result should be false.");
90  }
91 
92  void Equal_DifferentDimensionalities_ReturnsFalse()
93  {
94  // Select the first slice of a 2D image and compare it to the 3D original
96  sliceSelector->SetInput(m_Image);
97  sliceSelector->SetSliceNr(0);
98  sliceSelector->Update();
99  m_AnotherImage = sliceSelector->GetOutput();
100 
101  MITK_ASSERT_NOT_EQUAL(m_Image, m_AnotherImage, "First image is 3D. Second image is 2D. Result should be false.");
102  }
103 
104  void Equal_DifferentPixelValues_ReturnsFalse()
105  {
106  // todo: Replace the random images via simpler images with fixed values.
107  m_Image = mitk::ImageGenerator::GenerateRandomImage<unsigned char>(3u, 3u);
108  m_AnotherImage = mitk::ImageGenerator::GenerateRandomImage<unsigned char>(3u, 3u);
109 
110  MITK_ASSERT_NOT_EQUAL(m_Image, m_AnotherImage, "We compare two random images. Result should be false.");
111  }
112 
113  void Equal_EpsilonDifference_ReturnsTrue()
114  {
115  m_Image = mitk::ImageGenerator::GenerateRandomImage<double>(10, 10);
116  m_AnotherImage = m_Image->Clone();
117 
118  CPPUNIT_ASSERT_MESSAGE("Epsilon = 0.0 --> double images should not be regarded as equal",
119  !mitk::Equal(*m_Image, *m_AnotherImage, 0, true));
120  CPPUNIT_ASSERT_MESSAGE("Epsilon = 0.001 --> double images should be regarded as equal",
121  mitk::Equal(*m_Image, *m_AnotherImage, 0.001, true));
122  }
123 };
124 
125 MITK_TEST_SUITE_REGISTRATION(mitkImageEqual)
MITK_TEST_SUITE_REGISTRATION(mitkImageToItk)
#define MITK_TEST(TESTMETHOD)
Adds a test to the current test suite.
static Pointer New()
#define MITK_ASSERT_NOT_EQUAL(OBJ1, OBJ2, MSG)
Testing macro to test if two objects are not equal.
Test fixture for parameterized tests.
#define MITK_ASSERT_EQUAL(EXPECTED, ACTUAL, MSG)
Testing macro to test if two objects are equal.
MITKNEWMODULE_EXPORT bool Equal(mitk::ExampleDataStructure *leftHandSide, mitk::ExampleDataStructure *rightHandSide, mitk::ScalarType eps, bool verbose)
Returns true if the example data structures are considered equal.
MITKCORE_EXPORT const ScalarType eps