Medical Imaging Interaction Toolkit  2018.4.99-12ad79a3
Medical Imaging Interaction Toolkit
mitkImageToItkTest.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 "itkDiffusionTensor3D.h"
14 #include "mitkITKImageImport.h"
15 #include "mitkImage.h"
17 #include "mitkTestFixture.h"
18 #include "mitkTestingMacros.h"
19 #include <fstream>
20 #include <mitkIOUtil.h>
21 #include <mitkImageAccessByItk.h>
22 #include <mitkImageCast.h>
23 
25 {
26  // create empty image
27  mitk::Image::Pointer imgMem;
28  imgMem = mitk::Image::New();
29 
30  // create geometry information for image
31  mitk::Point3D origin;
32  mitk::Vector3D right, bottom;
33  mitk::Vector3D spacing;
34  mitk::FillVector3D(origin, 17.0, 19.92, 7.83);
35  mitk::FillVector3D(right, 1.0, 2.0, 3.0);
36  mitk::FillVector3D(bottom, 0.0, -3.0, 2.0);
37  mitk::FillVector3D(spacing, 0.78, 0.91, 2.23);
39  planegeometry->InitializeStandardPlane(100, 100, right, bottom, &spacing);
40  planegeometry->SetOrigin(origin);
41 
42  // initialize image
43  imgMem->Initialize(pt, 40, *planegeometry);
44 
45  return imgMem;
46 }
47 
48 class mitkImageToItkTestSuite : public mitk::TestFixture
49 {
50  CPPUNIT_TEST_SUITE(mitkImageToItkTestSuite);
51  MITK_TEST(ImageCastIntToFloat_EmptyImage);
52  MITK_TEST(ImageCastDoubleToFloat_EmptyImage);
53  MITK_TEST(ImageCastFloatToFloatTensor_EmptyImage_ThrowsException);
54  MITK_TEST(ImageCastFloatTensorToFloatTensor_EmptyImage);
55  MITK_TEST(ImageCastDoubleToTensorDouble_EmptyImage_ThrowsException);
56  MITK_TEST(ImageCastToItkAndBack_SamePointer_Success);
57  MITK_TEST(ImageCastToItk_TestImage_Success);
58  CPPUNIT_TEST_SUITE_END();
59 
60 private:
61  mitk::Image::Pointer m_TestImage;
62 
63 public:
64  void setUp() override
65  {
66  // empty on purpose
67  }
68 
69  void tearDown() override { m_TestImage = nullptr; }
70  void ImageCastIntToFloat_EmptyImage()
71  {
72  mitk::Image::Pointer m_TestImage = GetEmptyTestImageWithGeometry(mitk::MakeScalarPixelType<int>());
73  itk::Image<float, 3>::Pointer itkImage;
74 
75  mitk::CastToItkImage(m_TestImage, itkImage);
76  mitk::Image::Pointer mitkImageAfterCast = mitk::ImportItkImage(itkImage);
77 
78  CPPUNIT_ASSERT_MESSAGE("Cast output is not null", mitkImageAfterCast.IsNotNull());
79  }
80 
81  void ImageCastDoubleToFloat_EmptyImage()
82  {
83  mitk::Image::Pointer m_TestImage = GetEmptyTestImageWithGeometry(mitk::MakeScalarPixelType<double>());
84  itk::Image<float, 3>::Pointer diffImage;
85 
86  mitk::CastToItkImage(m_TestImage, diffImage);
87 
88  CPPUNIT_ASSERT_MESSAGE("Casting scalar double (MITK) image to scalar float tensor (ITK). Result shouldn't be nullptr.",
89  diffImage.IsNotNull());
90  }
91 
92  void ImageCastFloatToFloatTensor_EmptyImage_ThrowsException()
93  {
94  mitk::Image::Pointer m_TestImage = GetEmptyTestImageWithGeometry(mitk::MakeScalarPixelType<float>());
95  itk::Image<itk::DiffusionTensor3D<float>, 3>::Pointer diffImage;
96 
97  CPPUNIT_ASSERT_THROW_MESSAGE("Casting scalar float (MITK) image to scalar float (ITK) throws exception.",
98  mitk::CastToItkImage(m_TestImage, diffImage),
100  }
101 
102  void ImageCastFloatTensorToFloatTensor_EmptyImage()
103  {
104  typedef itk::Image<itk::DiffusionTensor3D<float>, 3> ItkTensorImageType;
105  mitk::Image::Pointer m_TestImage = GetEmptyTestImageWithGeometry(mitk::MakePixelType<ItkTensorImageType>());
106  itk::Image<itk::DiffusionTensor3D<float>, 3>::Pointer diffImage;
107 
108  mitk::CastToItkImage(m_TestImage, diffImage);
109  MITK_TEST_CONDITION_REQUIRED(diffImage.IsNotNull(),
110  "Casting float tensor (MITK) image to float tensor (ITK). Result shouldn't be nullptr");
111 
112  mitk::Image::Pointer mitkImageAfterCast = mitk::ImportItkImage(diffImage);
113  MITK_ASSERT_EQUAL(mitkImageAfterCast, m_TestImage, "Same type, images shoul be equal.");
114  }
115 
116  void ImageCastDoubleToTensorDouble_EmptyImage_ThrowsException()
117  {
118  mitk::Image::Pointer m_TestImage = GetEmptyTestImageWithGeometry(mitk::MakeScalarPixelType<double>());
119  itk::Image<itk::DiffusionTensor3D<double>, 3>::Pointer diffImage;
120 
121  CPPUNIT_ASSERT_THROW(mitk::CastToItkImage(m_TestImage, diffImage), mitk::AccessByItkException);
122  }
123 
124  void ImageCastToItkAndBack_SamePointer_Success()
125  {
126  typedef itk::Image<short, 3> ItkImageType;
127  ItkImageType::Pointer itkImage = ItkImageType::New();
128 
129  std::string m_ImagePath = GetTestDataFilePath("Pic3D.nrrd");
130  mitk::Image::Pointer testDataImage = mitk::IOUtil::Load<mitk::Image>(m_ImagePath);
131 
132  // modify ITK image
133  itk::Matrix<double, 3, 3> dir = itkImage->GetDirection();
134  dir *= 2;
135  itkImage->SetDirection(dir);
136 
137  CPPUNIT_ASSERT_THROW_MESSAGE("No exception thrown for casting back the same memory",
138  testDataImage = mitk::GrabItkImageMemory(itkImage, testDataImage),
139  itk::ExceptionObject);
140  CPPUNIT_ASSERT(testDataImage.IsNotNull());
141  }
142 
143  void ImageCastToItk_TestImage_Success()
144  {
145  itk::Image<short, 3>::Pointer itkImage;
146  std::string m_ImagePath = GetTestDataFilePath("Pic3D.nrrd");
147  mitk::Image::Pointer testDataImage = mitk::IOUtil::Load<mitk::Image>(m_ImagePath);
148 
149  mitk::CastToItkImage(testDataImage, itkImage);
150  mitk::Image::Pointer mitkImageAfterCast = mitk::ImportItkImage(itkImage);
151 
152  // dereference itk image
153  itkImage = nullptr;
154 
155  MITK_ASSERT_EQUAL(mitkImageAfterCast, testDataImage, "Cast with test data followed by import produces same images");
156  }
157 
158 }; // END TEST SUITE CLASS DECL
159 MITK_TEST_SUITE_REGISTRATION(mitkImageToItk);
static mitk::Image::Pointer GetEmptyTestImageWithGeometry(mitk::PixelType pt)
MITK_TEST_SUITE_REGISTRATION(mitkImageToItk)
#define MITK_TEST_CONDITION_REQUIRED(COND, MSG)
#define MITK_TEST(TESTMETHOD)
Adds a test to the current test suite.
static std::string GetTestDataFilePath(const std::string &testData)
Get the absolute path for test data.
void FillVector3D(Tout &out, mitk::ScalarType x, mitk::ScalarType y, mitk::ScalarType z)
Definition: mitkArray.h:106
Image::Pointer ImportItkImage(const itk::SmartPointer< ItkOutputImageType > &itkimage, const BaseGeometry *geometry=nullptr, bool update=true)
Imports an itk::Image (with a specific type) as an mitk::Image.Instantiates instance of ITKImageImpor...
Image::Pointer GrabItkImageMemory(itk::SmartPointer< ItkOutputImageType > &itkimage, mitk::Image *mitkImage=nullptr, const BaseGeometry *geometry=nullptr, bool update=true)
Grabs the memory of an itk::Image (with a specific type) and puts it into an mitk::Image.The memory is managed by the mitk::Image after calling this function. The itk::Image remains valid until the mitk::Image decides to free the memory.
Test fixture for parameterized tests.
#define MITK_ASSERT_EQUAL(EXPECTED, ACTUAL, MSG)
Testing macro to test if two objects are equal.
static Pointer New()
Exception class thrown in AccessByItk macros.
void MITKCORE_EXPORT CastToItkImage(const mitk::Image *mitkImage, itk::SmartPointer< ItkOutputImageType > &itkOutputImage)
Cast an mitk::Image to an itk::Image with a specific type.
static Pointer New()
Class for defining the data type of pixels.
Definition: mitkPixelType.h:51