Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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,
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 "mitkTestingMacros.h"
19 
21 
22 #include <itkThresholdImageFilter.h>
23 
24 #include "mitkImageAccessByItk.h"
25 
36 template <typename TPixel, unsigned int VDimensions>
38 static void ItkThresholdFilter(const itk::Image<TPixel, VDimensions> *image,
39  mitk::Image::Pointer &output,
40  const double th[])
41 {
42  typedef itk::Image<TPixel, VDimensions> InputImageType;
43  typedef itk::Image<TPixel, VDimensions> OutputImageType;
44  typedef itk::ThresholdImageFilter<InputImageType> ThresholdFilterType;
45 
47  thresholder->SetInput(image);
48  thresholder->ThresholdOutside(th[0], th[1]);
49  thresholder->Update();
50 
51  try
52  {
53  output = mitk::GrabItkImageMemory(thresholder->GetOutput());
54  }
55  catch (itk::ExceptionObject &)
56  {
57  MITK_TEST_FAILED_MSG(<< "Thresholding computation failed");
58  }
59 }
61 
66 template <typename TPixel>
68 {
69  // data for 3x3x3 image
70  const unsigned int dimensions[3] = {3, 3, 3};
71  auto image_data = new TPixel[27];
72 
73  // ground truth for result check
74  auto ground_truth = new TPixel[27];
75  double threshold[2] = {90.0, 180.0};
76 
77  // fill image
78  for (unsigned int i = 0; i < 27; i++)
79  {
80  image_data[i] = static_cast<TPixel>(i * 10);
81 
82  ground_truth[i] = 0;
83  if (image_data[i] >= threshold[0] && image_data[i] <= threshold[1])
84  ground_truth[i] = static_cast<TPixel>(i * 10);
85  }
86 
88  input->Initialize(mitk::MakeScalarPixelType<TPixel>(), 3, dimensions);
89  input->SetImportVolume(image_data);
90 
93  AccessByItk_2(input, ItkThresholdFilter, output, threshold);
95 
96  mitk::ImagePixelReadAccessor<TPixel, 3> readAccessor(output);
97  const TPixel *output_data = readAccessor.GetData();
98 
99  bool equal = true;
100  for (unsigned int i = 0; i < 27; i++)
101  {
102  equal &= (ground_truth[i] == output_data[i]);
103  if (!equal)
104  {
105  MITK_INFO << " :: At position " << i << " : " << ground_truth[i] << " ? " << output_data[i] << "\n";
106  break;
107  }
108  }
109 
110  MITK_TEST_CONDITION(equal, " Imported output data equals the ground truth");
111 
112  return equal;
113 }
114 
115 int mitkGrabItkImageMemoryTest(int /*argc*/, char * /*argv*/ [])
116 {
117  MITK_TEST_BEGIN("mitkGrabItkImageMemoryTest")
118 
119  Assert_ItkImportWithinAccessByItkSucceded_ReturnsTrue<short>(); // "Import successful on 3D short");
120  Assert_ItkImportWithinAccessByItkSucceded_ReturnsTrue<float>(); // "Import succesfull on float");
121  Assert_ItkImportWithinAccessByItkSucceded_ReturnsTrue<unsigned char>(); // "Import succesfull on uchar");
122  Assert_ItkImportWithinAccessByItkSucceded_ReturnsTrue<int>(); // "Import succesfull on int");
123 
124  MITK_TEST_END()
125 }
itk::SmartPointer< Self > Pointer
Gives locked and index-based read access for a particular image part. The class provides several set-...
#define MITK_INFO
Definition: mitkLogMacros.h:22
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.
itk::Image< double, 3 > InputImageType
#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.
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 *[])
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.