Medical Imaging Interaction Toolkit  2018.4.99-899caed2
Medical Imaging Interaction Toolkit
mitkGrabItkImageMemoryTest.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 "mitkTestingMacros.h"
15 
17 
18 #include <itkThresholdImageFilter.h>
19 
20 #include "mitkImageAccessByItk.h"
21 
32 template <typename TPixel, unsigned int VDimensions>
34 static void ItkThresholdFilter(const itk::Image<TPixel, VDimensions> *image,
35  mitk::Image::Pointer &output,
36  const double th[])
37 {
38  typedef itk::Image<TPixel, VDimensions> InputImageType;
39  typedef itk::ThresholdImageFilter<InputImageType> ThresholdFilterType;
40 
41  typename ThresholdFilterType::Pointer thresholder = ThresholdFilterType::New();
42  thresholder->SetInput(image);
43  thresholder->ThresholdOutside(th[0], th[1]);
44  thresholder->Update();
45 
46  try
47  {
48  output = mitk::GrabItkImageMemory(thresholder->GetOutput());
49  }
50  catch ( const itk::ExceptionObject & )
51  {
52  MITK_TEST_FAILED_MSG(<< "Thresholding computation failed");
53  }
54 }
56 
61 template <typename TPixel>
63 {
64  // data for 3x3x3 image
65  const unsigned int dimensions[3] = {3, 3, 3};
66  auto image_data = new TPixel[27];
67 
68  // ground truth for result check
69  auto ground_truth = new TPixel[27];
70  double threshold[2] = {90.0, 180.0};
71 
72  // fill image
73  for (unsigned int i = 0; i < 27; i++)
74  {
75  image_data[i] = static_cast<TPixel>(i * 10);
76 
77  ground_truth[i] = 0;
78  if (image_data[i] >= threshold[0] && image_data[i] <= threshold[1])
79  ground_truth[i] = static_cast<TPixel>(i * 10);
80  }
81 
83  input->Initialize(mitk::MakeScalarPixelType<TPixel>(), 3, dimensions);
84  input->SetImportVolume(image_data);
85 
88  AccessByItk_2(input, ItkThresholdFilter, output, threshold);
90 
91  mitk::ImagePixelReadAccessor<TPixel, 3> readAccessor(output);
92  const TPixel *output_data = readAccessor.GetData();
93 
94  bool equal = true;
95  for (unsigned int i = 0; i < 27; i++)
96  {
97  equal &= (ground_truth[i] == output_data[i]);
98  if (!equal)
99  {
100  MITK_INFO << " :: At position " << i << " : " << ground_truth[i] << " ? " << output_data[i] << "\n";
101  break;
102  }
103  }
104 
105  MITK_TEST_CONDITION(equal, " Imported output data equals the ground truth");
106 
107  return equal;
108 }
109 
110 int mitkGrabItkImageMemoryTest(int /*argc*/, char * /*argv*/ [])
111 {
112  MITK_TEST_BEGIN("mitkGrabItkImageMemoryTest")
113 
114  Assert_ItkImportWithinAccessByItkSucceded_ReturnsTrue<short>(); // "Import successful on 3D short");
115  Assert_ItkImportWithinAccessByItkSucceded_ReturnsTrue<float>(); // "Import succesfull on float");
116  Assert_ItkImportWithinAccessByItkSucceded_ReturnsTrue<unsigned char>(); // "Import succesfull on uchar");
117  Assert_ItkImportWithinAccessByItkSucceded_ReturnsTrue<int>(); // "Import succesfull on int");
118 
119  MITK_TEST_END()
120 }
Gives locked and index-based read access for a particular image part. The class provides several set-...
#define MITK_INFO
Definition: mitkLogMacros.h:18
section GeneralTestsDeprecatedOldTestingStyle Deprecated macros All tests with MITK_TEST_BEGIN()
bool Assert_ItkImportWithinAccessByItkSucceded_ReturnsTrue()
[ItkThresholdFilter]
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.
#define MITK_TEST_CONDITION(COND, MSG)
#define MITK_TEST_FAILED_MSG(MSG)
Fail and finish test with message MSG.
const TPixel * GetData() const
Gives const access to the data.
mitk::Image::Pointer image
static Pointer New()
static void ItkThresholdFilter(const itk::Image< TPixel, VDimensions > *image, mitk::Image::Pointer &output, const double th[])
[ItkThresholdFilter]
#define AccessByItk_2(mitkImage, itkImageTypeFunction, arg1, arg2)
and MITK_TEST_END()
int mitkGrabItkImageMemoryTest(int, char *[])