Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkImageGeneratorTest.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 // Testing
14 #include "mitkTestFixture.h"
15 #include <mitkTestingMacros.h>
16 // MITK includes
17 #include <mitkCoreServices.h>
18 #include "mitkImage.h"
19 #include "mitkImageGenerator.h"
20 #include "mitkImageReadAccessor.h"
22 
23 class mitkImageGeneratorTestSuite : public mitk::TestFixture
24 {
25  CPPUNIT_TEST_SUITE(mitkImageGeneratorTestSuite);
26  MITK_TEST(SetSpacingX2D_Success);
27  MITK_TEST(SetDefaultSpacingX2D_Success);
28  MITK_TEST(SetSpacingX3D_Success);
29  MITK_TEST(SetSpacingY2D_Success);
30  MITK_TEST(SetDefaultSpacingY2D_Success);
31  MITK_TEST(SetSpacingY3D_Success);
32  MITK_TEST(SetSpacingZ2D_Success);
33  MITK_TEST(SetDefaultSpacingZ2D_Success);
34  MITK_TEST(SetSpacingZ3D_Success);
35  MITK_TEST(SetDimension2D_Success);
36  MITK_TEST(SetDimension3D_Success);
37  MITK_TEST(SetDimension4D_Success);
38  MITK_TEST(SetDimensionX2D_Success);
39  MITK_TEST(SetDimensionY2D_Success);
40  MITK_TEST(SetDimensionZ3D_Success);
41  MITK_TEST(SetDimensionT4D_Success);
42  MITK_TEST(SetDimensions3Dc_Success);
43  MITK_TEST(SetDataTypeFloat2D_Success);
44  MITK_TEST(SetDataTypeUChar2D_Success);
45  MITK_TEST(SetDataTypeInt3D_Success);
46  MITK_TEST(SetDataTypeDouble3D_Success);
47  MITK_TEST(SetDataTypeFloat4D_Success);
48  MITK_TEST(SetDataTypeUChar4D_Success);
49  MITK_TEST(SetDataTypeUInt3D_Success);
50  MITK_TEST(SetPixelTypeFloat2D_Success);
51  MITK_TEST(SetPixelTypeUChar2D_Success);
52  MITK_TEST(SetPixelTypeInt3D_Success);
53  MITK_TEST(SetPixelTypeDouble3D_Success);
54  MITK_TEST(SetPixelTypeFloat4D_Success);
55  MITK_TEST(SetPixelTypeUChar4D_Success);
56  MITK_TEST(SetPixelTypeUInt3D_Success);
57  MITK_TEST(MaxValueHolds_Success);
58  MITK_TEST(MinValueHolds_Success);
59  MITK_TEST(DefaultMaxValueHolds_Success);
60  MITK_TEST(DefaultMinValueHolds_Success);
61  MITK_TEST(SetGradientImageValues_Success);
62  CPPUNIT_TEST_SUITE_END();
63 
64 private:
65  // create some images with arbitrary parameters (corner cases)
66  mitk::Image::Pointer m_Image2Da;
67  mitk::Image::Pointer m_Image2Db;
68  mitk::Image::Pointer m_Image3Da;
69  mitk::Image::Pointer m_Image3Db;
70  mitk::Image::Pointer m_Image4Da;
71  mitk::Image::Pointer m_Image4Db;
72  mitk::Image::Pointer m_Image3Dc;
73 
74 public:
75  void setUp()
76  {
77  m_Image2Da = mitk::ImageGenerator::GenerateRandomImage<float>(120, 205, 0, 0, 0.1, 0.2, 0.3, 577, 23);
78  m_Image2Db = mitk::ImageGenerator::GenerateRandomImage<unsigned char>(1, 1, 0, 0);
79  m_Image3Da = mitk::ImageGenerator::GenerateRandomImage<int>(512, 205, 1, 0);
80  m_Image3Db = mitk::ImageGenerator::GenerateRandomImage<double>(512, 532, 112, 0);
81  m_Image4Da = mitk::ImageGenerator::GenerateRandomImage<float>(120, 205, 78, 1);
82  m_Image4Db = mitk::ImageGenerator::GenerateRandomImage<unsigned char>(550, 33, 78, 150);
83  m_Image3Dc = mitk::ImageGenerator::GenerateGradientImage<unsigned int>(1, 2, 3, 4, 5, 6);
84  }
85  void tearDown()
86  {
87  m_Image2Da = nullptr;
88  m_Image2Db = nullptr;
89  m_Image3Da = nullptr;
90  m_Image3Db = nullptr;
91  m_Image4Da = nullptr;
92  m_Image4Db = nullptr;
93  m_Image3Dc = nullptr;
94  }
95 
96  void SetSpacingX2D_Success()
97  {
98  CPPUNIT_ASSERT_MESSAGE("Testing if spacing 2D x is set correctly.",
99  fabs(m_Image2Da->GetGeometry()->GetSpacing()[0] - 0.1) < 0.0001);
100  }
101 
102  void SetDefaultSpacingX2D_Success()
103  {
104  CPPUNIT_ASSERT_MESSAGE("Testing if default spacing 2D x is set correctly.",
105  fabs(m_Image2Db->GetGeometry()->GetSpacing()[0] - 1.0) < 0.0001);
106  }
107 
108  void SetSpacingX3D_Success()
109  {
110  CPPUNIT_ASSERT_MESSAGE("Testing if spacing 3D x is set correctly.",
111  fabs(m_Image3Dc->GetGeometry()->GetSpacing()[0] - 4) < 0.0001);
112  }
113 
114  void SetSpacingY2D_Success()
115  {
116  CPPUNIT_ASSERT_MESSAGE("Testing if spacing 2D y is set correctly.",
117  fabs(m_Image2Da->GetGeometry()->GetSpacing()[1] - 0.2) < 0.0001);
118  }
119 
120  void SetDefaultSpacingY2D_Success()
121  {
122  CPPUNIT_ASSERT_MESSAGE("Testing if default spacing 2D y is set correctly.",
123  fabs(m_Image2Db->GetGeometry()->GetSpacing()[1] - 1.0) < 0.0001);
124  }
125 
126  void SetSpacingY3D_Success()
127  {
128  CPPUNIT_ASSERT_MESSAGE("Testing if spacing 3D y is set correctly.",
129  fabs(m_Image3Dc->GetGeometry()->GetSpacing()[1] - 5) < 0.0001);
130  }
131 
132  void SetSpacingZ2D_Success()
133  {
134  CPPUNIT_ASSERT_MESSAGE("Testing if spacing 2D z is set correctly.",
135  fabs(m_Image2Da->GetGeometry()->GetSpacing()[2] - 0.3) < 0.0001);
136  }
137 
138  void SetDefaultSpacingZ2D_Success()
139  {
140  CPPUNIT_ASSERT_MESSAGE("Testing if default spacing 2D z is set correctly.",
141  fabs(m_Image2Db->GetGeometry()->GetSpacing()[2] - 1.0) < 0.0001);
142  }
143 
144  void SetSpacingZ3D_Success()
145  {
146  CPPUNIT_ASSERT_MESSAGE("Testing if spacing z is set correctly.",
147  fabs(m_Image3Dc->GetGeometry()->GetSpacing()[2] - 6) < 0.0001);
148  }
149 
150  void SetDimension2D_Success()
151  {
152  CPPUNIT_ASSERT_MESSAGE("Testing if the dimension 2D is set correctly.", m_Image2Da->GetDimension() == 2);
153  CPPUNIT_ASSERT_MESSAGE("Testing if the dimension 2D is set correctly.", m_Image2Db->GetDimension() == 2);
154  }
155 
156  void SetDimension3D_Success()
157  {
158  CPPUNIT_ASSERT_MESSAGE("Testing if the dimension 3D is set correctly.",m_Image3Da->GetDimension() == 2);
159  CPPUNIT_ASSERT_MESSAGE("Testing if the dimension 3D is set correctly.", m_Image3Db->GetDimension() == 3);
160  }
161 
162  void SetDimension4D_Success()
163  {
164  CPPUNIT_ASSERT_MESSAGE("Testing if the dimension 4D is set correctly.", m_Image4Da->GetDimension() == 3);
165  CPPUNIT_ASSERT_MESSAGE("Testing if the dimension 4D is set correctly.", m_Image4Db->GetDimension() == 4);
166  }
167 
168  void SetDimensionX2D_Success()
169  {
170  CPPUNIT_ASSERT_MESSAGE("Testing if the X dimension of the 2D image is set correctly.", m_Image2Da->GetDimension(0) == 120);
171  }
172 
173  void SetDimensionY2D_Success()
174  {
175  CPPUNIT_ASSERT_MESSAGE("Testing if the Y dimension of the 2D image is set correctly.", m_Image2Db->GetDimension(1) == 1);
176  }
177 
178  void SetDimensionZ3D_Success()
179  {
180  CPPUNIT_ASSERT_MESSAGE("Testing if the Z dimension of the 3D image is set correctly.", m_Image3Da->GetDimension(2) == 1);
181  CPPUNIT_ASSERT_MESSAGE("Testing if the Z dimension of the 3D image is set correctly.", m_Image3Db->GetDimension(2) == 112);
182  }
183 
184  void SetDimensionT4D_Success()
185  {
186  CPPUNIT_ASSERT_MESSAGE("Testing if the T dimension of the 4D image is set correctly.", m_Image4Da->GetDimension(3) == 1);
187  CPPUNIT_ASSERT_MESSAGE("Testing if the T dimension of the 4D image is set correctly.", m_Image4Db->GetDimension(3) == 150);
188  }
189 
190  void SetDimensions3Dc_Success()
191  {
192  CPPUNIT_ASSERT_MESSAGE("Testing if image3Dc dimension x is set correctly.", m_Image3Dc->GetDimension(0) == 1);
193  CPPUNIT_ASSERT_MESSAGE("Testing if image3Dc dimension y is set correctly.", m_Image3Dc->GetDimension(1) == 2);
194  CPPUNIT_ASSERT_MESSAGE("Testing if image3Dc dimension z is set correctly.", m_Image3Dc->GetDimension(2) == 3);
195  }
196 
197  void SetDataTypeFloat2D_Success()
198  {
199  CPPUNIT_ASSERT_MESSAGE("Testing if the data type for a float 3D image is set correctly.",
200  m_Image2Da->GetPixelType().GetComponentType() == itk::ImageIOBase::FLOAT);
201  }
202 
203  void SetDataTypeUChar2D_Success()
204  {
205  CPPUNIT_ASSERT_MESSAGE("Testing if the data type for a UChar 2D image is set correctly.",
206  m_Image2Db->GetPixelType().GetComponentType() == itk::ImageIOBase::UCHAR);
207  }
208 
209  void SetDataTypeInt3D_Success()
210  {
211  CPPUNIT_ASSERT_MESSAGE("Testing if the data type for a Int 3D image is set correctly.",
212  m_Image3Da->GetPixelType().GetComponentType() == itk::ImageIOBase::INT);
213  }
214 
215  void SetDataTypeDouble3D_Success()
216  {
217  CPPUNIT_ASSERT_MESSAGE("Testing if the data type for a Double 3D image is set correctly.",
218  m_Image3Db->GetPixelType().GetComponentType() == itk::ImageIOBase::DOUBLE);
219  }
220 
221  void SetDataTypeFloat4D_Success()
222  {
223  CPPUNIT_ASSERT_MESSAGE("Testing if the data type for a Float 4D image is set correctly.",
224  m_Image4Da->GetPixelType().GetComponentType() == itk::ImageIOBase::FLOAT);
225  }
226 
227  void SetDataTypeUChar4D_Success()
228  {
229  CPPUNIT_ASSERT_MESSAGE("Testing if the data type for a UChar 4D image is set correctly.",
230  m_Image4Db->GetPixelType().GetComponentType() == itk::ImageIOBase::UCHAR);
231  }
232 
233  void SetDataTypeUInt3D_Success()
234  {
235  CPPUNIT_ASSERT_MESSAGE("Testing if the data type for a UInt 3D image is set correctly.",
236  m_Image3Dc->GetPixelType().GetComponentType() == itk::ImageIOBase::UINT);
237  }
238 
239  void SetPixelTypeFloat2D_Success()
240  {
241  itk::ImageIOBase::IOPixelType scalarType = itk::ImageIOBase::SCALAR;
242  CPPUNIT_ASSERT_MESSAGE("Testing if the pixel type for a Float 2D image is set correctly.",
243  m_Image2Da->GetPixelType().GetPixelType() == scalarType);
244  }
245 
246  void SetPixelTypeUChar2D_Success()
247  {
248  itk::ImageIOBase::IOPixelType scalarType = itk::ImageIOBase::SCALAR;
249  CPPUNIT_ASSERT_MESSAGE("Testing if the pixel type for a UChar 2D image is set correctly.",
250  m_Image2Db->GetPixelType().GetPixelType() == scalarType);
251  }
252 
253  void SetPixelTypeInt3D_Success()
254  {
255  itk::ImageIOBase::IOPixelType scalarType = itk::ImageIOBase::SCALAR;
256  CPPUNIT_ASSERT_MESSAGE("Testing if the pixel type for a Int 3D image is set correctly.",
257  m_Image3Da->GetPixelType().GetPixelType() == scalarType);
258  }
259 
260  void SetPixelTypeDouble3D_Success()
261  {
262  itk::ImageIOBase::IOPixelType scalarType = itk::ImageIOBase::SCALAR;
263  CPPUNIT_ASSERT_MESSAGE("Testing if the pixel type for a Double 3D image is set correctly.",
264  m_Image3Db->GetPixelType().GetPixelType() == scalarType);
265  }
266 
267  void SetPixelTypeFloat4D_Success()
268  {
269  itk::ImageIOBase::IOPixelType scalarType = itk::ImageIOBase::SCALAR;
270  CPPUNIT_ASSERT_MESSAGE("Testing if the pixel type for a Float 4D image is set correctly.",
271  m_Image4Da->GetPixelType().GetPixelType() == scalarType);
272  }
273 
274  void SetPixelTypeUChar4D_Success()
275  {
276  itk::ImageIOBase::IOPixelType scalarType = itk::ImageIOBase::SCALAR;
277  CPPUNIT_ASSERT_MESSAGE("Testing if the pixel type for a UChar 4D image is set correctly.",
278  m_Image4Db->GetPixelType().GetPixelType() == scalarType);
279  }
280 
281  void SetPixelTypeUInt3D_Success()
282  {
283  itk::ImageIOBase::IOPixelType scalarType = itk::ImageIOBase::SCALAR;
284  CPPUNIT_ASSERT_MESSAGE("Testing if the pixel type for a UInt 3D image is set correctly.",
285  m_Image3Dc->GetPixelType().GetPixelType() == scalarType);
286  }
287 
288  void MaxValueHolds_Success()
289  {
290  CPPUNIT_ASSERT_MESSAGE("Testing if max value holds", m_Image2Da->GetStatistics()->GetScalarValueMax() <= 577);
291  }
292 
293  void MinValueHolds_Success()
294  {
295  CPPUNIT_ASSERT_MESSAGE("Testing if min value holds", m_Image2Da->GetStatistics()->GetScalarValueMin() >= 23);
296  }
297 
298  void DefaultMaxValueHolds_Success()
299  {
300  CPPUNIT_ASSERT_MESSAGE("Testing if default max value holds", m_Image3Da->GetStatistics()->GetScalarValueMax() <= 1000);
301  }
302 
303  void DefaultMinValueHolds_Success()
304  {
305  CPPUNIT_ASSERT_MESSAGE("Testing if default min value holds", m_Image3Da->GetStatistics()->GetScalarValueMin() >= 0);
306  }
307 
308  void SetGradientImageValues_Success()
309  {
310  const unsigned int *image3DcBuffer = nullptr;
311  try
312  {
313  mitk::ImageReadAccessor readAccess(m_Image3Dc);
314  image3DcBuffer = static_cast<const unsigned int *>(readAccess.GetData());
315  }
316  catch (...)
317  {
318  MITK_ERROR << "Read access not granted on mitk::Image.";
319  }
320  for (unsigned int i = 0; i < 2 * 3; i++)
321  {
322  CPPUNIT_ASSERT_MESSAGE("Testing if gradient image values are set correctly", image3DcBuffer[i] == i);
323  }
324  }
325 };
326 MITK_TEST_SUITE_REGISTRATION(mitkImageGenerator)
327 
MITK_TEST_SUITE_REGISTRATION(mitkImageToItk)
#define MITK_ERROR
Definition: mitkLogMacros.h:20
#define MITK_TEST(TESTMETHOD)
Adds a test to the current test suite.
Test fixture for parameterized tests.
ImageReadAccessor class to get locked read access for a particular image part.