Medical Imaging Interaction Toolkit  2018.4.99-1bab67a2
Medical Imaging Interaction Toolkit
mitkCompareImageDataFilter.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 // mitk includes
15 #include "mitkITKImageImport.h"
16 #include "mitkImageAccessByItk.h"
17 #include "mitkImageCaster.h"
19 
20 // itk includes
21 #include <itkTestingComparisonImageFilter.h>
22 
24 {
25  this->SetNumberOfRequiredInputs(2);
26 
28 }
29 
31 {
39 }
40 
42 {
43  // check inputs
44 
45  const mitk::Image *input1 = this->GetInput(0);
46  const mitk::Image *input2 = this->GetInput(1);
47 
48  // Generally this filter is part of the mitk::Image::Equal() method and only checks the equality of the image data
49  // so no further image type comparison is performed!
50  // CAVE: If the images differ in a parameter other then the image data, the filter may fail!!
51 
52  // check what number of components the inputs have
53  if (input1->GetPixelType().GetNumberOfComponents() == 1 && input2->GetPixelType().GetNumberOfComponents() == 1)
54  {
55  AccessByItk_1(input1, EstimateValueDifference, input2);
56  }
57  else if (input1->GetPixelType().GetNumberOfComponents() > 1 && input2->GetPixelType().GetNumberOfComponents() > 1)
58  {
60 
62  mcComparator->SetTestImage(input1);
63  mcComparator->SetValidImage(input2);
64  mcComparator->SetCompareFilterResult(&m_CompareDetails);
65  mcComparator->SetTolerance(m_Tolerance);
66  mcComparator->Update();
67 
68  m_CompareResult = mcComparator->GetResult();
69  }
70 }
71 
73 {
74  if (!m_CompareResult)
75  {
76  return false;
77  }
78 
80  {
81  return false;
82  }
83 
84  return true;
85 }
86 
87 template <typename TPixel, unsigned int VImageDimension>
88 void mitk::CompareImageDataFilter::EstimateValueDifference(const itk::Image<TPixel, VImageDimension> *itkImage1,
89  const mitk::Image *referenceImage)
90 {
91  typedef itk::Image<TPixel, VImageDimension> InputImageType;
92  typedef itk::Image<double, VImageDimension> OutputImageType;
93 
94  typename InputImageType::Pointer itk_reference = InputImageType::New();
95 
96  mitk::CastToItkImage(referenceImage, itk_reference);
97 
98  typedef itk::Testing::ComparisonImageFilter<InputImageType, OutputImageType> CompareFilterType;
99  typename CompareFilterType::Pointer compare_filter = CompareFilterType::New();
100  compare_filter->SetTestInput(itkImage1);
101  compare_filter->SetValidInput(itk_reference);
102  compare_filter->SetDifferenceThreshold(m_Tolerance);
103 
104  try
105  {
106  compare_filter->Update();
107  }
108  catch (const itk::ExceptionObject &e)
109  {
112 
113  MITK_WARN << e.what();
114 
115  m_CompareResult = false;
116  return;
117  }
118 
119  // the filter has completed the calculation
120  m_CompareResult = true;
122 
123  m_CompareDetails.m_MaximumDifference = compare_filter->GetMaximumDifference();
124  m_CompareDetails.m_MinimumDifference = compare_filter->GetMinimumDifference();
125  m_CompareDetails.m_MeanDifference = compare_filter->GetMeanDifference();
126  m_CompareDetails.m_TotalDifference = compare_filter->GetTotalDifference();
127  m_CompareDetails.m_PixelsWithDifference = compare_filter->GetNumberOfPixelsWithDifferences();
128 
129  mitk::Image::Pointer output = mitk::GrabItkImageMemory(compare_filter->GetOutput());
130  this->SetOutput(MakeNameFromOutputIndex(0), output.GetPointer());
131 }
bool GetResult(vcl_size_t threshold=0)
Get the result of the comparison.
const mitk::PixelType GetPixelType(int n=0) const
Returns the PixelType of channel n.
Definition: mitkImage.cpp:101
vcl_size_t GetNumberOfComponents() const
Get the number of components of which each element consists.
void EstimateValueDifference(const itk::Image< TPixel, VImageDimension > *itkImage1, const mitk::Image *referenceImage)
#define AccessByItk_1(mitkImage, itkImageTypeFunction, arg1)
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_WARN
Definition: mitkLogMacros.h:19
static Pointer New()
Image class for storing images.
Definition: mitkImage.h:72
mitk::Image OutputImageType
Some convenient typedefs.
static T max(T x, T y)
Definition: svm.cpp:56
static Pointer New()
InputImageType * GetInput(void)
void MITKCORE_EXPORT CastToItkImage(const mitk::Image *mitkImage, itk::SmartPointer< ItkOutputImageType > &itkOutputImage)
Cast an mitk::Image to an itk::Image with a specific type.
void ResetCompareResultsToInitial()
Method resets the compare detail memeber struct to its initial state.