Medical Imaging Interaction Toolkit  2018.4.99-6aa36ba9
Medical Imaging Interaction Toolkit
mitkCropImageFilterTest.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 <mitkTestFixture.h>
14 #include <mitkTestingMacros.h>
15 #include <mitkImage.h>
16 #include <mitkImageReadAccessor.h>
17 #include <mitkCropImageFilter.h>
18 #include <random>
19 
20 class mitkCropImageFilterTestSuite : public mitk::TestFixture
21 {
22  CPPUNIT_TEST_SUITE(mitkCropImageFilterTestSuite);
23  MITK_TEST(testCropImage);
24  CPPUNIT_TEST_SUITE_END();
25 
26 private:
27 
28  mitk::CropImageFilter::Pointer m_CropImageFilter;
29  const unsigned int NUM_ITERATIONS = 5;
30  const unsigned int DATA_DIM = 25;
31 
32 public:
33 
34  void setUp() override
35  {
36  m_CropImageFilter = mitk::CropImageFilter::New();
37  }
38 
39  void test()
40  {
41  std::random_device r;
42  std::default_random_engine randGen(r());
43  std::uniform_int_distribution<int> randDistr(0, (DATA_DIM / 2) - 1);
44 
45  float* data = new float[DATA_DIM*DATA_DIM*DATA_DIM];
46 
47  for (unsigned int i = 0; i < DATA_DIM*DATA_DIM*DATA_DIM; ++i)
48  {
49  data[i] = (float)i;
50  }
51 
53  unsigned int dimension[3]{ DATA_DIM, DATA_DIM, DATA_DIM };
54  inputImage->Initialize(mitk::MakeScalarPixelType<float>(), 3, dimension);
55  inputImage->SetImportVolume(data, 0, 0, mitk::Image::ImportMemoryManagementType::CopyMemory);
56 
57  for (unsigned int iteration = 0; iteration < NUM_ITERATIONS; ++iteration)
58  {
59  unsigned int XPixelsCropStart = randDistr(randGen);
60  unsigned int YPixelsCropStart = randDistr(randGen);
61  unsigned int ZPixelsCropStart = randDistr(randGen);
62  unsigned int XPixelsCropEnd = randDistr(randGen);
63  unsigned int YPixelsCropEnd = randDistr(randGen);
64  unsigned int ZPixelsCropEnd = randDistr(randGen);
65 
66  unsigned int newXDim = DATA_DIM - XPixelsCropStart - XPixelsCropEnd;
67  unsigned int newYDim = DATA_DIM - YPixelsCropStart - YPixelsCropEnd;
68  unsigned int newZDim = DATA_DIM - ZPixelsCropStart - ZPixelsCropEnd;
69 
70  m_CropImageFilter->SetInput(inputImage);
71 
72  m_CropImageFilter->SetXPixelsCropStart(XPixelsCropStart);
73  m_CropImageFilter->SetYPixelsCropStart(YPixelsCropStart);
74  m_CropImageFilter->SetZPixelsCropStart(ZPixelsCropStart);
75  m_CropImageFilter->SetXPixelsCropEnd(XPixelsCropEnd);
76  m_CropImageFilter->SetYPixelsCropEnd(YPixelsCropEnd);
77  m_CropImageFilter->SetZPixelsCropEnd(ZPixelsCropEnd);
78 
79  m_CropImageFilter->Update();
80  mitk::Image::Pointer outputImage = m_CropImageFilter->GetOutput();
81 
82  mitk::ImageReadAccessor readAccess(outputImage);
83  const float* outputData = (const float*)readAccess.GetData();
84 
85  CPPUNIT_ASSERT_MESSAGE(std::string("expected x size to be " + std::to_string(newXDim) + " but was " + std::to_string(outputImage->GetDimension(0))), newXDim == outputImage->GetDimension(0));
86  CPPUNIT_ASSERT_MESSAGE(std::string("expected y size to be " + std::to_string(newYDim) + " but was " + std::to_string(outputImage->GetDimension(1))), newYDim == outputImage->GetDimension(1));
87  CPPUNIT_ASSERT_MESSAGE(std::string("expected z size to be " + std::to_string(newZDim) + " but was " + std::to_string(outputImage->GetDimension(2))), newZDim == outputImage->GetDimension(2));
88 
89  for (unsigned int z = 0; z < newZDim; ++z)
90  {
91  for (unsigned int y = 0; y < newYDim; ++y)
92  {
93  for (unsigned int x = 0; x < newXDim; ++x)
94  {
95  unsigned int origPos = (x + XPixelsCropStart) + (y + YPixelsCropStart) * DATA_DIM + (z + ZPixelsCropStart) * DATA_DIM * DATA_DIM;
96  unsigned int outPos = x + y * newXDim + z * newXDim * newYDim;
97  CPPUNIT_ASSERT_MESSAGE(std::string("expected " + std::to_string(data[origPos])
98  + " but was " + std::to_string(outputData[outPos])),
99  std::abs((float)outputData[outPos] - (float)data[origPos]) < mitk::eps);
100  }
101  }
102  }
103  }
104 
105  delete[] data;
106  }
107 
108  void testCropImage()
109  {
110  for (int repetition = 0; repetition < 20; ++repetition)
111  {
112  MITK_INFO << "[" << (repetition + 1) << "/20]";
113  test();
114  }
115  }
116 
117  void tearDown() override
118  {
119  m_CropImageFilter = nullptr;
120  }
121 };
122 
123 MITK_TEST_SUITE_REGISTRATION(mitkCropImageFilter)
MITK_TEST_SUITE_REGISTRATION(mitkImageToItk)
#define MITK_INFO
Definition: mitkLogMacros.h:18
Follow Up Storage - Class to facilitate loading/accessing structured follow-up data.
Definition: testcase.h:28
#define MITK_TEST(TESTMETHOD)
Adds a test to the current test suite.
static Pointer New()
Test fixture for parameterized tests.
static Pointer New()
MITKCORE_EXPORT const ScalarType eps
ImageReadAccessor class to get locked read access for a particular image part.