Medical Imaging Interaction Toolkit  2018.4.99-389bf124
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 (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #include "mitkImage.h"
14 #include "mitkImageGenerator.h"
15 #include "mitkImageSliceSelector.h"
16 #include "mitkTestFixture.h"
17 #include "mitkTestingMacros.h"
18 
19 class mitkImageEqualTestSuite : public mitk::TestFixture
20 {
21  CPPUNIT_TEST_SUITE(mitkImageEqualTestSuite);
22  MITK_TEST(Equal_CloneAndOriginal_ReturnsTrue);
23  MITK_TEST(Equal_DifferentImageGeometry_ReturnsFalse);
24  MITK_TEST(Equal_DifferentPixelTypes_ReturnsFalse);
25  MITK_TEST(Equal_DifferentDimensions_ReturnsFalse);
26  MITK_TEST(Equal_DifferentDimensionalities_ReturnsFalse);
27  MITK_TEST(Equal_DifferentPixelValues_ReturnsFalse);
28  CPPUNIT_TEST_SUITE_END();
29 
30 private:
32  mitk::Image::Pointer m_Image;
33  mitk::Image::Pointer m_AnotherImage;
34 
35 public:
40  void setUp() override
41  {
42  // generate a gradient test image
43  m_Image = mitk::ImageGenerator::GenerateGradientImage<unsigned char>(3u, 3u, 1u);
44  m_AnotherImage = m_Image->Clone();
45  }
46 
47  void tearDown() override
48  {
49  m_Image = nullptr;
50  m_AnotherImage = nullptr;
51  }
52 
53  void Equal_CloneAndOriginal_ReturnsTrue()
54  {
55  MITK_ASSERT_EQUAL(m_Image, m_Image->Clone(), "A clone should be equal to its original.");
56  }
57 
58  void Equal_DifferentImageGeometry_ReturnsFalse()
59  {
60  mitk::Point3D origin;
61  origin[0] = 0.0;
62  origin[1] = 0.0;
63  origin[2] = mitk::eps * 1.01;
64 
65  m_AnotherImage->GetGeometry()->SetOrigin(origin);
66 
67  MITK_ASSERT_NOT_EQUAL(m_Image, m_AnotherImage, "One origin was modified. Result should be false.");
68  }
69 
70  void Equal_DifferentPixelTypes_ReturnsFalse()
71  {
72  m_AnotherImage = mitk::ImageGenerator::GenerateGradientImage<float>(3u, 3u, 1u);
73 
75  m_Image, m_AnotherImage, "One pixel type is float, the other unsigned char. Result should be false.");
76  }
77 
78  void Equal_DifferentDimensions_ReturnsFalse()
79  {
80  m_AnotherImage = mitk::ImageGenerator::GenerateGradientImage<unsigned char>(5u, 7u, 3u);
81 
83  m_Image,
84  m_AnotherImage,
85  "Dimensions of first image are: (3, 3, 1). Dimensions of second image are: (5, 7, 3). Result should be false.");
86  }
87 
88  void Equal_DifferentDimensionalities_ReturnsFalse()
89  {
90  // Select the first slice of a 2D image and compare it to the 3D original
92  sliceSelector->SetInput(m_Image);
93  sliceSelector->SetSliceNr(0);
94  sliceSelector->Update();
95  m_AnotherImage = sliceSelector->GetOutput();
96 
97  MITK_ASSERT_NOT_EQUAL(m_Image, m_AnotherImage, "First image is 3D. Second image is 2D. Result should be false.");
98  }
99 
100  void Equal_DifferentPixelValues_ReturnsFalse()
101  {
102  // todo: Replace the random images via simpler images with fixed values.
103  m_Image = mitk::ImageGenerator::GenerateRandomImage<unsigned char>(3u, 3u);
104  m_AnotherImage = mitk::ImageGenerator::GenerateRandomImage<unsigned char>(3u, 3u);
105 
106  MITK_ASSERT_NOT_EQUAL(m_Image, m_AnotherImage, "We compare two random images. Result should be false.");
107  }
108 
109  void Equal_EpsilonDifference_ReturnsTrue()
110  {
111  m_Image = mitk::ImageGenerator::GenerateRandomImage<double>(10, 10);
112  m_AnotherImage = m_Image->Clone();
113 
114  CPPUNIT_ASSERT_MESSAGE("Epsilon = 0.0 --> double images should not be regarded as equal",
115  !mitk::Equal(*m_Image, *m_AnotherImage, 0, true));
116  CPPUNIT_ASSERT_MESSAGE("Epsilon = 0.001 --> double images should be regarded as equal",
117  mitk::Equal(*m_Image, *m_AnotherImage, 0.001, true));
118  }
119 };
120 
121 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