Medical Imaging Interaction Toolkit  2016.11.0
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,
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 "itkDiffusionTensor3D.h"
18 #include "mitkITKImageImport.h"
19 #include "mitkImage.h"
21 #include "mitkTestFixture.h"
22 #include "mitkTestingMacros.h"
23 #include <fstream>
24 #include <mitkIOUtil.h>
25 #include <mitkImageAccessByItk.h>
26 #include <mitkImageCast.h>
27 
29 {
30  // create empty image
31  mitk::Image::Pointer imgMem;
32  imgMem = mitk::Image::New();
33 
34  // create geometry information for image
35  mitk::Point3D origin;
36  mitk::Vector3D right, bottom;
37  mitk::Vector3D spacing;
38  mitk::FillVector3D(origin, 17.0, 19.92, 7.83);
39  mitk::FillVector3D(right, 1.0, 2.0, 3.0);
40  mitk::FillVector3D(bottom, 0.0, -3.0, 2.0);
41  mitk::FillVector3D(spacing, 0.78, 0.91, 2.23);
43  planegeometry->InitializeStandardPlane(100, 100, right, bottom, &spacing);
44  planegeometry->SetOrigin(origin);
45 
46  // initialize image
47  imgMem->Initialize(pt, 40, *planegeometry);
48 
49  return imgMem;
50 }
51 
52 class mitkImageToItkTestSuite : public mitk::TestFixture
53 {
54  CPPUNIT_TEST_SUITE(mitkImageToItkTestSuite);
55  MITK_TEST(ImageCastIntToFloat_EmptyImage);
56  MITK_TEST(ImageCastDoubleToFloat_EmptyImage);
57  MITK_TEST(ImageCastFloatToFloatTensor_EmptyImage_ThrowsException);
58  MITK_TEST(ImageCastFloatTensorToFloatTensor_EmptyImage);
59  MITK_TEST(ImageCastDoubleToTensorDouble_EmptyImage_ThrowsException);
60  MITK_TEST(ImageCastToItkAndBack_SamePointer_Success);
61  MITK_TEST(ImageCastToItk_TestImage_Success);
62  CPPUNIT_TEST_SUITE_END();
63 
64 private:
65  mitk::Image::Pointer m_TestImage;
66 
67 public:
68  void setUp() override
69  {
70  // empty on purpose
71  }
72 
73  void tearDown() override { m_TestImage = nullptr; }
74  void ImageCastIntToFloat_EmptyImage()
75  {
76  mitk::Image::Pointer m_TestImage = GetEmptyTestImageWithGeometry(mitk::MakeScalarPixelType<int>());
78 
79  mitk::CastToItkImage(m_TestImage, itkImage);
80  mitk::Image::Pointer mitkImageAfterCast = mitk::ImportItkImage(itkImage);
81 
82  CPPUNIT_ASSERT_MESSAGE("Cast output is not null", mitkImageAfterCast.IsNotNull());
83  }
84 
85  void ImageCastDoubleToFloat_EmptyImage()
86  {
87  mitk::Image::Pointer m_TestImage = GetEmptyTestImageWithGeometry(mitk::MakeScalarPixelType<double>());
89 
90  mitk::CastToItkImage(m_TestImage, diffImage);
91 
92  CPPUNIT_ASSERT_MESSAGE("Casting scalar double (MITK) image to scalar float tensor (ITK). Result shouldn't be NULL.",
93  diffImage.IsNotNull());
94  }
95 
96  void ImageCastFloatToFloatTensor_EmptyImage_ThrowsException()
97  {
98  mitk::Image::Pointer m_TestImage = GetEmptyTestImageWithGeometry(mitk::MakeScalarPixelType<float>());
99  itk::Image<itk::DiffusionTensor3D<float>, 3>::Pointer diffImage;
100 
101  CPPUNIT_ASSERT_THROW_MESSAGE("Casting scalar float (MITK) image to scalar float (ITK) throws exception.",
102  mitk::CastToItkImage(m_TestImage, diffImage),
104  }
105 
106  void ImageCastFloatTensorToFloatTensor_EmptyImage()
107  {
108  typedef itk::Image<itk::DiffusionTensor3D<float>, 3> ItkTensorImageType;
109  mitk::Image::Pointer m_TestImage = GetEmptyTestImageWithGeometry(mitk::MakePixelType<ItkTensorImageType>());
110  itk::Image<itk::DiffusionTensor3D<float>, 3>::Pointer diffImage;
111 
112  mitk::CastToItkImage(m_TestImage, diffImage);
113  MITK_TEST_CONDITION_REQUIRED(diffImage.IsNotNull(),
114  "Casting float tensor (MITK) image to float tensor (ITK). Result shouldn't be NULL");
115 
116  mitk::Image::Pointer mitkImageAfterCast = mitk::ImportItkImage(diffImage);
117  MITK_ASSERT_EQUAL(mitkImageAfterCast, m_TestImage, "Same type, images shoul be equal.");
118  }
119 
120  void ImageCastDoubleToTensorDouble_EmptyImage_ThrowsException()
121  {
122  mitk::Image::Pointer m_TestImage = GetEmptyTestImageWithGeometry(mitk::MakeScalarPixelType<double>());
123  itk::Image<itk::DiffusionTensor3D<double>, 3>::Pointer diffImage;
124 
125  CPPUNIT_ASSERT_THROW(mitk::CastToItkImage(m_TestImage, diffImage), mitk::AccessByItkException);
126  }
127 
128  void ImageCastToItkAndBack_SamePointer_Success()
129  {
130  typedef itk::Image<short, 3> ItkImageType;
132 
133  std::string m_ImagePath = GetTestDataFilePath("Pic3D.nrrd");
134  mitk::Image::Pointer testDataImage = mitk::IOUtil::LoadImage(m_ImagePath);
135 
136  // modify ITK image
137  itk::Matrix<double, 3, 3> dir = itkImage->GetDirection();
138  dir *= 2;
139  itkImage->SetDirection(dir);
140 
141  CPPUNIT_ASSERT_THROW_MESSAGE("No exception thrown for casting back the same memory",
142  testDataImage = mitk::GrabItkImageMemory(itkImage, testDataImage),
143  itk::ExceptionObject);
144  CPPUNIT_ASSERT(testDataImage.IsNotNull());
145  }
146 
147  void ImageCastToItk_TestImage_Success()
148  {
150  std::string m_ImagePath = GetTestDataFilePath("Pic3D.nrrd");
151  mitk::Image::Pointer testDataImage = mitk::IOUtil::LoadImage(m_ImagePath);
152 
153  mitk::CastToItkImage(testDataImage, itkImage);
154  mitk::Image::Pointer mitkImageAfterCast = mitk::ImportItkImage(itkImage);
155 
156  // dereference itk image
157  itkImage = 0;
158 
159  MITK_ASSERT_EQUAL(mitkImageAfterCast, testDataImage, "Cast with test data followed by import produces same images");
160  }
161 
162 }; // END TEST SUITE CLASS DECL
163 MITK_TEST_SUITE_REGISTRATION(mitkImageToItk);
itk::SmartPointer< Self > Pointer
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:110
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()
static mitk::Image::Pointer LoadImage(const std::string &path)
LoadImage Convenience method to load an arbitrary mitkImage.
Definition: mitkIOUtil.cpp:597
Class for defining the data type of pixels.
Definition: mitkPixelType.h:55
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.