Medical Imaging Interaction Toolkit  2018.4.99-3e3f1a6e
Medical Imaging Interaction Toolkit
mitkImportItkImageTest.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 "mitkITKImageImport.h"
14 #include "mitkImageCast.h"
15 #include "mitkTestingMacros.h"
16 
18 
19 #include <itkImageRegionConstIteratorWithIndex.h>
20 #include <itkRandomImageSource.h>
21 
27 template <typename TPixel, unsigned int VDimension>
28 typename itk::Image<TPixel, VDimension>::Pointer CreateTestImageRandom(short int size)
29 {
30  typedef typename itk::Image<TPixel, VDimension> ImageType;
31 
32  itk::Size<VDimension> regionSize;
33  regionSize.Fill(size);
34 
35  typename itk::RandomImageSource<ImageType>::Pointer randomImageSource = itk::RandomImageSource<ImageType>::New();
36  randomImageSource->SetNumberOfThreads(1); // to produce non-random results
37  randomImageSource->SetSize(regionSize);
38  randomImageSource->Update();
39 
40  return randomImageSource->GetOutput();
41 }
42 
50 template <typename TPixel, unsigned int VDimension>
51 typename itk::VectorImage<TPixel, VDimension>::Pointer CreateTestVectorImageFixedValue(
52  size_t size, const itk::VariableLengthVector<TPixel> &value)
53 {
54  typedef typename itk::VectorImage<TPixel, VDimension> ImageType;
55  typedef typename ImageType::Pointer ImagePointer;
56 
57  typename ImageType::RegionType imageRegion;
58  typename ImageType::RegionType::SizeType regionSize;
59  regionSize.Fill(size);
60 
61  typename ImageType::RegionType::IndexType regionIndex;
62  regionIndex.Fill(0);
63  imageRegion.SetSize(regionSize);
64  imageRegion.SetIndex(regionIndex);
65 
66  typename ImageType::SpacingType imageSpacing;
67  imageSpacing.Fill(1.0f);
68 
69  typename ImageType::PointType imageOrigin;
70  imageOrigin.Fill(0.0f);
71 
72  ImagePointer itkImage = ImageType::New();
73 
74  itkImage->SetVectorLength(value.GetNumberOfElements());
75  itkImage->SetRegions(imageRegion);
76  itkImage->SetOrigin(imageOrigin);
77  itkImage->SetSpacing(imageSpacing);
78  itkImage->Allocate();
79 
80  itkImage->FillBuffer(value);
81 
82  return itkImage;
83 }
84 
91 template <typename TPixel, unsigned int VDimension>
92 typename itk::Image<TPixel, VDimension>::Pointer CreateTestImageFixedValue(size_t size, TPixel value)
93 {
94  typedef typename itk::Image<TPixel, VDimension> ImageType;
95  typedef typename ImageType::Pointer ImagePointer;
96 
97  typename ImageType::RegionType imageRegion;
98  typename ImageType::RegionType::SizeType regionSize;
99  regionSize.Fill(size);
100 
101  typename ImageType::RegionType::IndexType regionIndex;
102  regionIndex.Fill(0);
103  imageRegion.SetSize(regionSize);
104  imageRegion.SetIndex(regionIndex);
105 
106  typename ImageType::SpacingType imageSpacing;
107  imageSpacing.Fill(1.0f);
108 
109  typename ImageType::PointType imageOrigin;
110  imageOrigin.Fill(0.0f);
111 
112  ImagePointer itkImage = ImageType::New();
113 
114  itkImage->SetRegions(imageRegion);
115  itkImage->SetOrigin(imageOrigin);
116  itkImage->SetSpacing(imageSpacing);
117  itkImage->Allocate();
118 
119  itkImage->FillBuffer(value);
120 
121  return itkImage;
122 }
123 
127 template <typename ImageType>
128 bool Assert_ImageMetaData_AreEqual(typename ImageType::Pointer itkImage, mitk::Image::Pointer mitkImage)
129 {
130  bool return_value = true;
131 
132  typename ImageType::RegionType itkRegion = itkImage->GetLargestPossibleRegion();
133  typename ImageType::SizeType itkImageSize = itkRegion.GetSize();
134 
135  // check dimension
136  for (unsigned int idx = 0; idx < mitkImage->GetDimension(); idx++)
137  {
138  return_value &= (itkImageSize[idx] == mitkImage->GetDimension(idx));
139  }
140  MITK_TEST_CONDITION(return_value, " - Dimensions equal!")
141 
142  // check pixel type
143  bool ptype_compare = (mitkImage->GetPixelType() == mitk::MakePixelType<ImageType>());
144  return_value &= ptype_compare;
145  MITK_TEST_CONDITION(ptype_compare, " - Pixel types equal!")
146 
147  mitk::BaseGeometry *imageGeometry = mitkImage->GetGeometry();
148  const mitk::Point3D origin = imageGeometry->GetOrigin();
149 
150  bool origin_compare = true;
151  for (unsigned int idx = 0; idx < 3; idx++)
152  {
153  origin_compare &= (itkImage->GetOrigin()[idx] == origin[idx]);
154  }
155  return_value &= origin_compare;
156  MITK_TEST_CONDITION(origin_compare, " - Origin equals!")
157 
158  return return_value;
159 }
160 
165 template <typename TPixel, unsigned int VDimension>
167 {
168  std::stringstream msg;
169  msg << "Current type: (Random Image, " << VDimension << "D):" << typeid(TPixel).name() << "\n";
170  std::cout << msg.str();
171 
172  bool assert_value = true;
173 
174  typedef typename itk::Image<TPixel, VDimension> ImageType;
175  typedef typename ImageType::Pointer ImagePointer;
176 
177  ImagePointer itkImage = CreateTestImageRandom<TPixel, VDimension>(5);
178 
179  mitk::Image::Pointer output_import = mitk::ImportItkImage(itkImage);
180 
181  itk::ImageRegionConstIteratorWithIndex<ImageType> iter(itkImage, itkImage->GetLargestPossibleRegion());
182  iter.GoToBegin();
183 
184  mitk::ImagePixelReadAccessor<TPixel, VDimension> readAccessor(output_import);
185 
186  bool difference = false;
187  while (!iter.IsAtEnd())
188  {
189  TPixel ref = iter.Get();
190  TPixel val = readAccessor.GetPixelByIndex(iter.GetIndex());
191 
192  difference |= (ref != val);
193  if (difference)
194  {
195  std::cout << iter.GetIndex() << ":" << ref << " ? " << val << "\n";
196  }
197  ++iter;
198  }
199 
200  assert_value = Assert_ImageMetaData_AreEqual<ImageType>(itkImage, output_import);
201 
202  MITK_TEST_CONDITION(assert_value && (!difference), "Pixel values are same in voxel-wise comparison.");
203 }
204 
209 template <typename TPixel, unsigned int VDimension>
211 {
212  std::stringstream msg;
213  msg << "Current type: " << VDimension << "D):" << typeid(TPixel).name() << "\n";
214  std::cout << msg.str();
215 
216  bool assert_value = true;
217 
218  typedef typename itk::Image<TPixel, VDimension> ImageType;
219  typedef typename ImageType::Pointer ImagePointer;
220 
221  ImagePointer itkImage = CreateTestImageFixedValue<TPixel, VDimension>(5, itk::NumericTraits<TPixel>::min());
222 
223  mitk::Image::Pointer output_import = mitk::ImportItkImage(itkImage);
224 
225  itk::ImageRegionConstIteratorWithIndex<ImageType> iter(itkImage, itkImage->GetLargestPossibleRegion());
226  iter.GoToBegin();
227 
228  mitk::ImagePixelReadAccessor<TPixel, VDimension> readAccessor(output_import);
229 
230  bool difference = false;
231  while (!iter.IsAtEnd())
232  {
233  TPixel ref = iter.Get();
234  TPixel val = readAccessor.GetPixelByIndex(iter.GetIndex());
235 
236  difference |= (ref != val);
237  if (difference)
238  {
239  std::cout << iter.GetIndex() << ":" << ref << " ? " << val << "\n";
240  }
241 
242  ++iter;
243  }
244 
245  assert_value = Assert_ImageMetaData_AreEqual<ImageType>(itkImage, output_import);
246 
247  MITK_TEST_CONDITION(assert_value && (!difference), "Pixel values are same in voxel-wise comparison.");
248 }
249 
251 {
252  typedef itk::VectorImage<short, 3> ImageType;
253  ImageType::PixelType value;
254  value.SetSize(2);
255  value.SetElement(0, 1);
256  value.SetElement(0, 2);
257  ImageType::Pointer itkImage = CreateTestVectorImageFixedValue<short, 3>(5, value);
258 
259  mitk::Image::Pointer mitkImage = mitk::ImportItkImage(itkImage);
260  mitk::PixelType pixelType = mitkImage->GetPixelType();
261  MITK_TEST_CONDITION(pixelType.GetPixelType() == itk::ImageIOBase::VECTOR, "Vector image pixel type")
262  MITK_TEST_CONDITION(pixelType.GetComponentType() == itk::ImageIOBase::SHORT, "Vector image component type")
263 
264  mitk::Image::Pointer mitkImage2;
265  mitk::CastToMitkImage(itkImage, mitkImage2);
266  mitk::PixelType pixelType2 = mitkImage2->GetPixelType();
267  MITK_TEST_CONDITION(pixelType == pixelType2, "ImportItkImage and CastToMitkImage produce same pixel types")
268 
269  ImageType::Pointer itkImageOut;
270  mitk::CastToItkImage(mitkImage, itkImageOut);
271 
272  MITK_TEST_CONDITION(pixelType == mitk::MakePixelType<ImageType>(2), "MITK pixel type equals ITK pixel type")
273 
274  typedef itk::VectorImage<int, 3> IntImageType;
275  IntImageType::Pointer itkIntImageOut;
276  mitk::CastToItkImage(mitkImage, itkIntImageOut);
277  MITK_TEST_CONDITION(!(pixelType == mitk::MakePixelType<IntImageType>(2)), "MITK pixel type != ITK pixel type")
278 
279  mitk::Image::Pointer mitkImage3;
280  mitk::CastToMitkImage(itkImageOut, mitkImage3);
281  MITK_ASSERT_EQUAL(mitkImage, mitkImage3, "Equality for vector images");
282 }
283 
284 int mitkImportItkImageTest(int /*argc*/, char * /*argv*/ [])
285 {
286  MITK_TEST_BEGIN("mitkImportItkImageTest")
287 
288  Assert_ItkImageImportSucceded_ReturnsTrue<short, 3>(); // "Import succesfull on 3D short");
289  Assert_ItkImageImportSucceded_ReturnsTrue<float, 3>(); // "Import succesfull on float");
290  Assert_ItkImageImportSucceded_ReturnsTrue<unsigned char, 3>(); // "Import succesfull on uchar");
291  Assert_ItkImageImportSucceded_ReturnsTrue<int, 3>(); // "Import succesfull on int");
292 
293  Assert_ItkImageImportRandomValuesSucceded_ReturnsTrue<short, 3>(); // "Import succesfull on 3D short");
294  Assert_ItkImageImportRandomValuesSucceded_ReturnsTrue<float, 3>(); // "Import succesfull on float");
295  Assert_ItkImageImportRandomValuesSucceded_ReturnsTrue<unsigned char, 3>(); // "Import succesfull on uchar");
296  Assert_ItkImageImportRandomValuesSucceded_ReturnsTrue<int, 3>(); // "Import succesfull on int");
297 
298  Assert_ItkImageImportRandomValuesSucceded_ReturnsTrue<short, 4>(); // "Import succesfull on 3D short");
299  Assert_ItkImageImportRandomValuesSucceded_ReturnsTrue<float, 4>(); // "Import succesfull on float");
300  Assert_ItkImageImportRandomValuesSucceded_ReturnsTrue<unsigned char, 4>(); // "Import succesfull on uchar");
301  Assert_ItkImageImportRandomValuesSucceded_ReturnsTrue<int, 4>(); // "Import succesfull on int");
302 
304 
305  MITK_TEST_END()
306 }
Gives locked and index-based read access for a particular image part. The class provides several set-...
itk::VectorImage< TPixel, VDimension >::Pointer CreateTestVectorImageFixedValue(vcl_size_t size, const itk::VariableLengthVector< TPixel > &value)
itk::Image< unsigned char, 3 > ImageType
const TPixel & GetPixelByIndex(const itk::Index< VDimension > &idx) const
section GeneralTestsDeprecatedOldTestingStyle Deprecated macros All tests with MITK_TEST_BEGIN()
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...
void Assert_ItkImageImportSucceded_ReturnsTrue()
int GetComponentType() const
Get the component type (the scalar (!) type). Each element may contain m_NumberOfComponents (more tha...
itk::Image< TPixel, VDimension >::Pointer CreateTestImageRandom(short int size)
int mitkImportItkImageTest(int, char *[])
#define MITK_TEST_CONDITION(COND, MSG)
itk::Image< TPixel, VDimension >::Pointer CreateTestImageFixedValue(vcl_size_t size, TPixel value)
#define MITK_ASSERT_EQUAL(EXPECTED, ACTUAL, MSG)
Testing macro to test if two objects are equal.
itk::ImageIOBase::IOPixelType GetPixelType() const
const Point3D GetOrigin() const
Get the origin, e.g. the upper-left corner of the plane.
static T min(T x, T y)
Definition: svm.cpp:53
void CastToMitkImage(const itk::SmartPointer< ItkOutputImageType > &itkimage, itk::SmartPointer< mitk::Image > &mitkoutputimage)
Cast an itk::Image (with a specific type) to an mitk::Image.
Definition: mitkImageCast.h:74
void MITKCORE_EXPORT CastToItkImage(const mitk::Image *mitkImage, itk::SmartPointer< ItkOutputImageType > &itkOutputImage)
Cast an mitk::Image to an itk::Image with a specific type.
and MITK_TEST_END()
bool Assert_ImageMetaData_AreEqual(typename ImageType::Pointer itkImage, mitk::Image::Pointer mitkImage)
void Assert_ItkVectorImageImportAndCast_ReturnsTrue()
BaseGeometry Describes the geometry of a data object.
Class for defining the data type of pixels.
Definition: mitkPixelType.h:51
void Assert_ItkImageImportRandomValuesSucceded_ReturnsTrue()