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